/usr/include/CLAM/Array.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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | /*
* Copyright (c) 2001-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 _Array_
#define _Array_
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <new>
#include "DataTypes.hxx"
#include "Err.hxx"
#include "Assert.hxx"
#include "ErrOutOfMemory.hxx"
#include "Storage.hxx"
#include "Component.hxx"
#include "TypeInfo.hxx"
// @todo Remove this include. See Bug#111
#include "DynamicType.hxx"
#include "XMLAdapter.hxx"
#include "XMLArrayAdapter.hxx"
#include "XMLComponentAdapter.hxx"
namespace CLAM {
template <class T> class Array:public Component
{
private:
T *mpData;
TSize mAllocSize;
TSize mSize;
int mStep;
public:
Array(TSize size = 0,TSize step = 1)
{
mSize = mAllocSize = 0;
mStep = step;
mpData = NULL;
Resize(size);
SetSize(size);
}
void Init(){
Resize(0);
SetSize(0);}
Array(T* ptr,int size = 0)
{
CLAM_ASSERT( ptr!=NULL,
"Array::Array( T*, int) : you cannot create a not-owning memory array "
"without specifying a valid data pointer. ");
mSize = mAllocSize = size;
mStep = -1;
mpData = ptr;
}
Array(const Array<T> &originalArray)
{
mpData = NULL;
mSize = mAllocSize = mStep = 0;
*this = originalArray;
}
~Array()
{
DestroyDataBuffer();
mAllocSize=mSize=mStep=0;
}
const char * GetClassName() const {return NULL;}
bool OwnsMemory() const {return mStep>=0; }
bool Empty() const { return mSize==0; }
TSize Size(void) const { return mSize; }
TSize SizeInBytes(void) const { return mSize*sizeof(T); }
TSize AllocatedSize(void) const { return mAllocSize; }
TSize AllocatedSizeInBytes(void) const { return mAllocSize*sizeof(T); }
void SetSize(TSize size)
{
CLAM_ASSERT(size <= AllocatedSize() || !OwnsMemory(), msgSetSizeOutOfRange);
if (OwnsMemory())
{
if (size > mSize)
InitializeDataBlock(mSize,size);
if (size < mSize)
UninitializeDataBlock(size,mSize);
}
mSize = size;
}
void SetStep(TSize step) { mStep = step;}
TSize GetStep() const {return mStep;}
void Resize(TSize newAllocSize)
{
CLAM_ASSERT(OwnsMemory(),
"Array::Resize(): You cannot invoke this method on an array that "
"does not own any memory" );
CLAM_ASSERT( newAllocSize >= 0,
"Array::Resize(): You are trying to allocate a negative amount of "
"space, which is a weird thing to do, isn't it?");
/* calculate the amount of bytes to allocate */
/* effectively resize the array by allocating more memory */
if(newAllocSize>0)
ResizeDataBuffer(newAllocSize);
else
{
if(mpData)
{
DestroyDataBuffer();
mpData=NULL;
}
}
mAllocSize = newAllocSize;
if (mAllocSize<mSize)
mSize = mAllocSize;
/* if the pointer to the end of the array is over then you're out of memory */
/* and an error message will be sent to the console */
CLAM_ASSERT( AllocatedSize()==0 || mpData!=NULL,
"Array::Resize() : Memory Allocation failed!" );
}
const T* GetPtr(void) const { return mpData; }
T* GetPtr(void) { return mpData; }
void SetPtr(T* ptr, int size = 0)
{
CLAM_ASSERT( !OwnsMemory() || mAllocSize == 0,
"Array::SetPtr() : You are not allowed to invoke SetPtr() on"
" an Array that owns memory or is not empty" );
mSize = mAllocSize = size;
mpData = ptr;
if (ptr == 0 && size == 0)
mStep = 1; // Sets the array to empty state.
else
mStep = -1; // the array gets not owner of the new data.
}
inline void GiveChunk(int pos, int size, Array<T>&) const;
inline void CopyChunk(int pos, int size, Array<T>&) const;
const T& operator [](const int& i) const
{
CLAM_DEBUG_ASSERT(i>=0,msgIndexOutOfRange);
CLAM_DEBUG_ASSERT(i<mSize,msgIndexOutOfRange);
return mpData[i];
}
T& operator [](const int& i)
{
CLAM_DEBUG_ASSERT(i>=0,msgIndexOutOfRange);
CLAM_DEBUG_ASSERT(i<mSize,msgIndexOutOfRange);
return mpData[i];
}
void AddElem(const T& elem)
{
CLAM_ASSERT(OwnsMemory(),"Array::AddElem(): Resize requiered,"
" but this array does not own its memory!");
if (mSize>=mAllocSize)
Resize(mAllocSize+mStep);
new(&mpData[mSize]) T(elem);
mSize++;
}
void InsertElem(int where,const T& elem)
{
CLAM_ASSERT(OwnsMemory(),"Array::InsertElem(): Resize requiered,"
" but this array does not own its memory!");
CLAM_ASSERT( (where>=0) && (where<mSize) ,msgInsertOutOfRange);
if (mSize>=mAllocSize)
Resize(mAllocSize+mStep);
InsertElemInDataBuffer(where);
new(&mpData[where]) T(elem);
mSize++;
}
void SetElem(int where,const T& elem)
{
CLAM_DEBUG_ASSERT(where>=0,msgIndexOutOfRange);
CLAM_DEBUG_ASSERT(where<mSize,msgIndexOutOfRange);
mpData[where] = elem;
}
void DeleteElem(int where)
{
CLAM_ASSERT(OwnsMemory(),"Array::DeleteElem(): Resize requiered,"
" but this array does not own its memory!");
CLAM_ASSERT(where>-1 ,msgDeleteOutOfRange);
CLAM_ASSERT(where<mSize,msgDeleteOutOfRange);
DeleteElemInDataBuffer(where);
mSize--;
if (mSize<mAllocSize-mStep)
Resize(mSize);
}
Array<T>& operator = (const Array<T>& src)
{
if ( OwnsMemory() )
{
if ( Size() != src.Size() )
Resize( src.Size() );
if ( src.OwnsMemory() )
mStep = src.mStep;
else
mStep = 1;
}
else
{
CLAM_ASSERT( AllocatedSize() >= src.Size(),
"Array::RegionWrite() : source size exceeds the Region bounds" );
CLAM_ASSERT( GetPtr() != NULL,
"Array::operator= : if you want to create a not memory owning array "
"from one that does own memory, use instead Array::SetPtr() method");
}
int tocopy = (src.Size()<Size())?src.Size():Size();
CopyDataBlock(0,tocopy,src.GetPtr());
InitializeCopyDataBlock(tocopy,src.Size(),src.GetPtr());
mSize=src.Size();
return *this;
}
Array<T>& operator += (const Array<T>& src)
{
int start = Size();
Resize(Size()+src.Size());
mSize+=src.Size();
int end = Size();
InitializeCopyDataBlock(start,end,0,src.mpData);
return *this;
}
void Apply( T (*f)(T) )
{
int i;
for (i=0; i<mSize; i++)
(*this)[i] = f( (*this)[i] );
}
void Apply( T (*f)(T,int),int parameter )
{
int i;
for (i=0; i<mSize; i++)
(*this)[i] = f( (*this)[i], parameter );
}
void StoreOn(Storage & storage) const
{
StoreBufferOn((typename TypeInfo<T>::StorableAsLeaf *)NULL, mpData, storage);
}
void LoadFrom(Storage & storage)
{
LoadBufferFrom((typename TypeInfo<T>::StorableAsLeaf *)NULL, mpData, storage);
}
// Error messages, to ease tests a little while we decide
// about error codes.
static const char *msgSetSizeOutOfRange;
static const char *msgIndexOutOfRange;
static const char *msgInsertOutOfRange;
static const char *msgDeleteOutOfRange;
private:
inline void ResizeDataBuffer(int new_size);
inline void DestroyDataBuffer(void);
inline void InsertElemInDataBuffer(int position);
inline void DeleteElemInDataBuffer(int position);
inline void InitializeElement(int position);
inline void InitializeDataBlock(int first, int last);
inline void UninitializeDataBlock(int first, int last);
inline void CopyDataBlock(int first, int last, const T* src);
inline void InitializeCopyDataBlock(int first, int last, const T* src);
inline void InitializeCopyDataBlock(int first, int last, int src_first, const T* src);
void StoreBufferOn(StaticFalse* asLeave, const Component * polymorphicSelector, Storage & storage) const
{
if (mSize<=0) return;
const char* className = mpData[0].GetClassName();
const char* label = className? className : "Element";
for (int i=0; i<mSize; i++)
{
XMLComponentAdapter adapter(mpData[i], label, true);
storage.Store(adapter);
}
}
void StoreBufferOn(StaticTrue* asLeave, const void * polymorphicSelector, Storage & storage) const
{
XMLAdapter<unsigned> sizeAdapter(Size(),"size");
storage.Store(sizeAdapter);
XMLArrayAdapter<T> adapter(mpData,mSize);
storage.Store(adapter);
}
void StoreBufferOn(StaticFalse* asLeave, const void * polymorphicSelector, Storage & storage) const
{
CLAM_ASSERT(false,
"Trying to Store an object that is not neither a streamable nor a Component");
}
void LoadBufferFrom(StaticFalse* asLeave, Component * polymorphicSelector, Storage & storage)
{
const char* label = 0;
while (true)
{
T elem;
if (!label)
{
label = elem.GetClassName();
if (!label)
label = "Element";
}
XMLComponentAdapter adapter(elem, label, true);
if (!storage.Load(adapter)) return;
AddElem(elem);
}
}
void LoadBufferFrom(StaticTrue* asLeave, void * polymorphicSelector, Storage & storage)
{
unsigned size;
XMLAdapter<unsigned> sizeAdapter(size,"size");
if (storage.Load(sizeAdapter))
{
Resize(size);
SetSize(size);
XMLArrayAdapter<T> adapter(mpData,mSize);
storage.Load(adapter);
// TODO: if false, then insert an error on the storage
return;
}
while (true) {
T elem;
XMLAdapter<T> adapter(elem);
if ( ! storage.Load(adapter)) return;
AddElem(elem);
}
}
void LoadBufferFrom(StaticFalse* asLeave, void * polymorphicSelector, Storage & storage)
{
CLAM_ASSERT(false,
"Trying to Store an object that is not neither a streamable nor a Component");
}
/*
void StoreMemberOn(StaticTrue* asLeave, void * item, Storage & storage) const {
XMLAdapter<T> adapter(*(T*)item);
storage.Store(adapter);
}
void StoreMemberOn(StaticFalse* asLeave, Component * item, Storage & storage) const {
const char* className = item->GetClassName();
const char* label = className? className : "Element";
XMLComponentAdapter adapter(*item, label, true);
storage.Store(adapter);
}
bool StoreMemberOn(StaticFalse* asLeave, void * item, Storage & storage) {
CLAM_ASSERT(false, "Trying to Store an object that is not neither a streamable nor a Component");
return false;
}
*/
bool LoadMemberFrom(StaticTrue* asLeave, void * item, Storage & storage) {
XMLAdapter<T> adapter(*(T*)item);
return storage.Load(adapter);
}
bool LoadMemberFrom(StaticFalse* asLeave, Component * item, Storage & storage) {
const char* className = (item->GetClassName());
const char* label = className? className : "Element";
XMLComponentAdapter adapter(*item, label, true);
return storage.Load(adapter);
}
bool LoadMemberFrom(StaticFalse* asLeave, void * item, Storage & storage) {
CLAM_ASSERT(false, "Trying to Load an object that is not neither a streamable nor a Component");
return false;
}
};
// Method implementations
template<class T>
void Array<T>::GiveChunk(int pos, int size, Array<T>& a) const
{
CLAM_ASSERT(pos + size <= mSize,
"Array::GiveChunk(): Chunk out of bounds.");
a.SetPtr(&mpData[pos],size);
}
template<class T>
void Array<T>::CopyChunk(int pos, int size, Array<T>& a) const
{
int last=pos+size;
CLAM_ASSERT(last <= mSize,
"Array::CopyChunk(): Chunk out of bounds.");
CLAM_ASSERT(size <= a.mSize,
"Array::CopyChunk(): destination array does not have enough memory");
for (int i=pos;i<last;i++)
a.mpData[i-pos]=mpData[i];
}
template<class T>
void Array<T>::InitializeElement(int i)
{
new (&mpData[i]) T();
}
template<class T>
void Array<T>::InitializeDataBlock(int first, int last)
{
int i;
for (i = first; i < last; i++)
InitializeElement(i);
}
template<class T>
void Array<T>::UninitializeDataBlock(int first, int last)
{
int i;
for (i = first; i < last; i++)
(&mpData[i])->~T();
}
template<class T>
void Array<T>::CopyDataBlock(int first, int last, const T* src)
{
int i;
for (i=first; i<last ;i++)
mpData[i]=src[i];
}
template<class T>
void Array<T>::InitializeCopyDataBlock(int first, int last, const T* src)
{
int i;
for (i=first; i<last; i++)
new(&mpData[i]) T(src[i]);
}
template<class T>
void Array<T>::InitializeCopyDataBlock(int first, int last, int src_first, const T* src)
{
int i, j = src_first;
for (i=first; i<last; i++)
new (&mpData[i]) T(src[j++]);
}
template<class T>
void Array<T>::DestroyDataBuffer()
{
if (OwnsMemory())
{
UninitializeDataBlock(0,mSize);
free(mpData);
}
mpData=NULL;
}
/** Unoptimised and safe default resizing code */
template<class T>
void Array<T>::ResizeDataBuffer(int new_size)
{
if (new_size == mAllocSize)
return;
T* old_data = mpData;
mpData = (T*) malloc(new_size*sizeof(T));
if (!old_data) return;
int elems = new_size;
if (mSize < elems)
elems = mSize;
InitializeCopyDataBlock(0,elems,old_data);
for (int i=0; i<mSize; i++)
(&old_data[i])->~T();
free(old_data);
}
/** Unoptimised and safe default element insertion code.
* <p>
* Warning: this does only the "memmove" operation.
* <p>
* mSize must hold the size BEFORE adding the element.
* The allocated size must be enough to hold a new element.
*/
template<class T>
void Array<T>::InsertElemInDataBuffer(int position)
{
if (mSize>0)
new(&mpData[mSize]) T(mpData[mSize-1]);
for (int i=mSize-1; i>position; i--)
mpData[i] = mpData[i-1];
(&mpData[position])->~T();
}
/** Unoptimised and safe default element removal code.
* <p>
* Warning: this does only the "memmove" operation.
* <p>
* mSize must hold the size BEFORE removing the element.
*/
template<class T>
void Array<T>::DeleteElemInDataBuffer(int position)
{
for (int i=position; i<mSize-1; i++)
mpData[i] = mpData[i+1];
(&mpData[mSize-1])->~T();
}
template <class T> inline Array<T> operator + (
const Array<T>& a,const Array<T>& b)
{
Array<T> ret = a;
ret += b;
return ret;
}
template <class T> inline bool operator == (
const Array<T>& a,const Array<T>& b)
{
if (a.Size()!=b.Size()) return false;
for (int i=0;i<a.Size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
typedef Array<TData> DataArray;
template<class T>
const char* Array<T>::msgSetSizeOutOfRange =
"Array::SetSize(): Argument larger than allocated size\n"
"You can probably fix this calling Resize() befor SetSize().";
template<class T>
const char* Array<T>::msgIndexOutOfRange =
"Array::operator[]: Index out of range\n"
"This may happen if you forgot to call SetSize(...) in your code.\n"
"This is now needed. Just calling Resize() is not enough any more.";
template<class T>
const char* Array<T>::msgInsertOutOfRange =
"Array::InsertElem: Index out of range";
template<class T>
const char* Array<T>::msgDeleteOutOfRange =
"Array::DeleteElem: Index out of range";
}
#endif//_Array_
|