/usr/include/CLAM/WritingRegion.hxx is in libclam-dev 1.4.0-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | /*
* Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
* UNIVERSITAT POMPEU FABRA
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __WritingRegion_hxx__
#define __WritingRegion_hxx__
#include "ReadingRegion.hxx"
#include "StreamImpl.hxx"
#include <iostream> // TODO: remove
namespace CLAM
{
template< typename Token, template <class> class DataStructure = PhantomBuffer>
class WritingRegion : public Region
{
public:
typedef StreamImpl< Token, DataStructure > ProperStream;
typedef Token ProperToken;
typedef ReadingRegion< WritingRegion<Token,DataStructure> > ProperReadingRegion;
WritingRegion();
virtual ~WritingRegion();
Region::ReadingRegionsIterator BeginReaders();
Region::ReadingRegionsIterator EndReaders();
void LinkRegions( ProperReadingRegion & reader);
void RemoveRegion( Region & region );
/**
* As a precondition all the regions must be even (in order to center them correctly).
* Otherways the method gives an assertion
*/
void CenterEvenRegions();
/**
returns the underlying stream. Useful for testing.
*/
ProperStream& Stream();
/** when no linked reading region is found it returns the writing pos thus eviting overlap condition */
int RearmostReadingPos();
/**
Checks that after moving the writing-region no overlap occur with
reading regions. This could happen since this is a circular buffer.
Returns true if no overlapping occur so it can produce.
*/
bool CanProduce();
void Produce();
Token& operator[](int offset); // TODO decide if operator[0] is the best option to get the data chunk
bool FulfilsInvariant();
int LogicalStreamSize();
Region* ProducerRegion();
/**
* Initializes all the data contained inside the region ( to the Token's default constructor)
*/
void ClearData();
/*
* Synchronizes Follower Region and returns last written Tokens
*/
Token & GetLastWrittenData( int offset = 0);
private:
/** Don't allow copies. Thus copy contructor made private */
WritingRegion(const WritingRegion<Token>& original){}
void SizeChanged(const int & newSize);
int GetGreatestReaderRegionSize();
/*
* This method checks that all the regions have even size and are located at the correct position (the beginning of the buffer )
*/
void CheckRegionsAreEven();
void CenterReadingRegions( int centralIndex );
/*
* Synchronizes the follower region with the last zone written, with the same size and hop than the writer.
*/
void SyncFollowerRegion();
ReadingRegionsList mReadingRegions;
ProperStream mStream;
ProperReadingRegion mFollowerRegion;
};
/////// Implementation ////////
template< typename Token, template <class> class DataStructure>
WritingRegion< Token, DataStructure >::WritingRegion()
{
SizeChanged( Size() );
}
template< typename Token, template <class> class DataStructure>
WritingRegion< Token, DataStructure >::~WritingRegion()
{
ReadingRegionsIterator it;
for(it=BeginReaders(); it!=EndReaders(); it++)
(*it)->RemoveProducer();
mReadingRegions.clear();
}
template< typename Token, template <class> class DataStructure>
Region::ReadingRegionsIterator WritingRegion< Token, DataStructure >::BeginReaders()
{
return mReadingRegions.begin();
}
template< typename Token, template <class> class DataStructure>
Region::ReadingRegionsIterator WritingRegion< Token, DataStructure >::EndReaders()
{
return mReadingRegions.end();
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::LinkRegions( ProperReadingRegion & reader)
{
mReadingRegions.push_back(&reader);
reader.LinkProducerRegion(*this);
reader.LinkAndNotifySizeToStream( Stream() );
}
template< typename Token, template <class> class DataStructure>
int WritingRegion< Token, DataStructure >::GetGreatestReaderRegionSize()
{
int maxSize = 0;
ReadingRegionsIterator it;
for(it=BeginReaders(); it!=EndReaders(); it++)
{
if(((*it)->Size())>maxSize)
maxSize = (*it)->Size();
}
return maxSize;
}
template< typename Token, template <class> class DataStructure>
void WritingRegion<Token, DataStructure >::CheckRegionsAreEven()
{
//TODO: writer must be even?
CLAM_ASSERT( Size()%2==0, "WritingRegion::CheckRegionsAreEven - Writer's size must be even for centering." );
ReadingRegionsIterator it;
for(it=BeginReaders(); it!=EndReaders(); it++)
{
CLAM_ASSERT( (*it)->Size()%2==0, "WritingRegion::CheckRegionsAreEven - Reader's size must be even for centering.");
}
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::ClearData()
{
for( int i=0; i<Size(); i++ )
operator[](i) = Token();
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::CenterReadingRegions( int centralIndex )
{
ReadingRegionsIterator it;
for(it=BeginReaders(); it!=EndReaders(); it++)
{
int hopToMove = centralIndex - (*it)->Size()/2;
(*it)->Pos( hopToMove );
(*it)->BeginDistance( hopToMove );
}
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::CenterEvenRegions()
{
CheckRegionsAreEven();
int centralIndex = GetGreatestReaderRegionSize()/2;
Pos( 0 );
BeginDistance( 0);
int currentHop = Hop();
int currentSize = Size();
Size( centralIndex);
Hop( centralIndex );
ClearData();
CenterReadingRegions( centralIndex );
Produce();
Size( currentSize );
Hop( currentHop );
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::RemoveRegion( Region & region )
{
CLAM_ASSERT( region.ProducerRegion()==this, "Region::RemoveRegion() - Trying to remove an unlinked region");
mReadingRegions.remove( ®ion );
region.RemoveProducer();
}
template< typename Token, template <class> class DataStructure>
typename WritingRegion< Token, DataStructure>::ProperStream& WritingRegion< Token, DataStructure >::Stream()
{
return mStream;
}
template< typename Token, template <class> class DataStructure>
int WritingRegion< Token, DataStructure >::RearmostReadingPos()
{
int result = Pos();
ReadingRegionsIterator it;
for(it=mReadingRegions.begin(); it!=mReadingRegions.end(); it++)
if( (*it)->Pos() < result) result = (*it)->Pos();
return result;
}
template< typename Token, template <class> class DataStructure>
bool WritingRegion< Token, DataStructure >::CanProduce()
{
CLAM_DEBUG_ASSERT( FulfilsInvariant(), "WritingRegion::canProduce() - fulfils invariant checking failed" );
return !mStream.ExistsCircularOverlap( RearmostReadingPos(), Pos()+Size() );
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::Produce()
{
CLAM_DEBUG_ASSERT( CanProduce(), "WritingRegion::produce() - WritingRegion can't produce" );
Pos( Pos() + Hop());
// reserve stream tokens for next position
mStream.WriterHasAdvanced( *this );
}
template< typename Token, template <class> class DataStructure>
Token& WritingRegion< Token, DataStructure >::operator[](int offset)
{
CLAM_DEBUG_ASSERT(0 <= offset && offset < Size(), "WritingRegion::operator [] - Index out of bounds" );
int physicalIndex = BeginDistance() + offset;
if (offset==0)
return mStream.Read(physicalIndex, Size());
return mStream[physicalIndex];
}
template< typename Token, template <class> class DataStructure>
bool WritingRegion< Token, DataStructure >::FulfilsInvariant()
{
ReadingRegionsIterator it;
for(it=mReadingRegions.begin(); it!=mReadingRegions.end(); it++)
if( (*it)->Pos() >= Pos()+Size() )
return false;
return true;
}
template< typename Token, template <class> class DataStructure>
int WritingRegion< Token, DataStructure >::LogicalStreamSize()
{
return mStream.LogicalSize();
}
template< typename Token, template <class> class DataStructure>
Region* WritingRegion< Token, DataStructure >::ProducerRegion()
{
return 0;
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::SizeChanged(const int & newSize)
{
mStream.NewWritingRegionSize( *this );
}
template< typename Token, template <class> class DataStructure>
void WritingRegion< Token, DataStructure >::SyncFollowerRegion()
{
mFollowerRegion.LinkProducerRegion(*this);
mFollowerRegion.Size( Size() );
mFollowerRegion.Hop( Hop() );
mFollowerRegion.LinkAndNotifySizeToStream( Stream() );
mFollowerRegion.Pos( Pos() - Hop() );
mFollowerRegion.BeginDistance( BeginDistance() - Hop() );
if(mFollowerRegion.BeginDistance() < 0 )
mFollowerRegion.BeginDistance( mFollowerRegion.BeginDistance() + mStream.LogicalSize() );
}
template< typename Token, template <class> class DataStructure>
Token & WritingRegion< Token, DataStructure >::GetLastWrittenData( int offset)
{
SyncFollowerRegion();
int physicalIndex = mFollowerRegion.BeginDistance() + offset;
// CLAM_ASSERT( physicalIndex < mStream.LogicalSize(), "WritingRegion GetLastWrittenData - Out of bounds access" );
return mStream.operator[]( physicalIndex );
}
} // namespace CLAM
#endif // __WritingRegion_hxx__
|