59 mSize = mAllocSize = 0;
73 "Array::Array( T*, int) : you cannot create a not-owning memory array "
74 "without specifying a valid data pointer. ");
75 mSize = mAllocSize = size;
83 mSize = mAllocSize = mStep = 0;
84 *
this = originalArray;
90 mAllocSize=mSize=mStep=0;
96 bool Empty()
const {
return mSize==0; }
109 InitializeDataBlock(mSize,size);
111 UninitializeDataBlock(size,mSize);
123 "Array::Resize(): You cannot invoke this method on an array that "
124 "does not own any memory" );
126 "Array::Resize(): You are trying to allocate a negative amount of "
127 "space, which is a weird thing to do, isn't it?");
132 ResizeDataBuffer(newAllocSize);
142 mAllocSize = newAllocSize;
144 if (mAllocSize<mSize)
150 "Array::Resize() : Memory Allocation failed!" );
153 const T*
GetPtr(
void)
const {
return mpData; }
159 "Array::SetPtr() : You are not allowed to invoke SetPtr() on"
160 " an Array that owns memory or is not empty" );
162 mSize = mAllocSize = size;
165 if (ptr == 0 && size == 0)
192 " but this array does not own its memory!");
193 if (mSize>=mAllocSize)
195 new(&mpData[mSize]) T(elem);
201 " but this array does not own its memory!");
203 if (mSize>=mAllocSize)
205 InsertElemInDataBuffer(where);
206 new(&mpData[where]) T(elem);
213 mpData[where] = elem;
218 " but this array does not own its memory!");
221 DeleteElemInDataBuffer(where);
223 if (mSize<mAllocSize-mStep)
242 "Array::RegionWrite() : source size exceeds the Region bounds" );
244 "Array::operator= : if you want to create a not memory owning array "
245 "from one that does own memory, use instead Array::SetPtr() method");
249 CopyDataBlock(0,tocopy,src.
GetPtr());
250 InitializeCopyDataBlock(tocopy,src.
Size(),src.
GetPtr());
263 InitializeCopyDataBlock(start,end,0,src.mpData);
270 for (i=0; i<mSize; i++)
271 (*
this)[i] = f( (*
this)[i] );
274 void Apply( T (*f)(T,
int),
int parameter )
277 for (i=0; i<mSize; i++)
278 (*
this)[i] = f( (*
this)[i], parameter );
298 inline void ResizeDataBuffer(
int new_size);
299 inline void DestroyDataBuffer(
void);
300 inline void InsertElemInDataBuffer(
int position);
301 inline void DeleteElemInDataBuffer(
int position);
302 inline void InitializeElement(
int position);
303 inline void InitializeDataBlock(
int first,
int last);
304 inline void UninitializeDataBlock(
int first,
int last);
305 inline void CopyDataBlock(
int first,
int last,
const T* src);
306 inline void InitializeCopyDataBlock(
int first,
int last,
const T* src);
307 inline void InitializeCopyDataBlock(
int first,
int last,
int src_first,
const T* src);
311 if (mSize<=0)
return;
312 const char* className = mpData[0].GetClassName();
313 const char* label = className? className :
"Element";
314 for (
int i=0; i<mSize; i++)
317 storage.
Store(adapter);
320 void StoreBufferOn(
StaticTrue* asLeave,
const void * polymorphicSelector, Storage & storage)
const
322 XMLAdapter<unsigned> sizeAdapter(
Size(),
"size");
323 storage.Store(sizeAdapter);
324 XMLArrayAdapter<T> adapter(mpData,mSize);
325 storage.Store(adapter);
327 void StoreBufferOn(
StaticFalse* asLeave,
const void * polymorphicSelector, Storage & storage)
const
330 "Trying to Store an object that is not neither a streamable nor a Component");
332 void LoadBufferFrom(
StaticFalse* asLeave, Component * polymorphicSelector, Storage & storage)
334 const char* label = 0;
340 label = elem.GetClassName();
344 XMLComponentAdapter adapter(elem, label,
true);
345 if (!storage.Load(adapter))
return;
349 void LoadBufferFrom(
StaticTrue* asLeave,
void * polymorphicSelector, Storage & storage)
352 XMLAdapter<unsigned> sizeAdapter(size,
"size");
353 if (storage.Load(sizeAdapter))
357 XMLArrayAdapter<T> adapter(mpData,mSize);
358 storage.Load(adapter);
365 XMLAdapter<T> adapter(elem);
366 if ( ! storage.Load(adapter))
return;
370 void LoadBufferFrom(
StaticFalse* asLeave,
void * polymorphicSelector, Storage & storage)
373 "Trying to Store an object that is not neither a streamable nor a Component");
391 bool LoadMemberFrom(
StaticTrue* asLeave,
void * item, Storage & storage) {
392 XMLAdapter<T> adapter(*(T*)item);
393 return storage.Load(adapter);
395 bool LoadMemberFrom(
StaticFalse* asLeave, Component * item, Storage & storage) {
396 const char* className = (item->GetClassName());
397 const char* label = className? className :
"Element";
398 XMLComponentAdapter adapter(*item, label,
true);
399 return storage.Load(adapter);
401 bool LoadMemberFrom(
StaticFalse* asLeave,
void * item, Storage & storage) {
402 CLAM_ASSERT(
false,
"Trying to Load an object that is not neither a streamable nor a Component");
415 "Array::GiveChunk(): Chunk out of bounds.");
416 a.
SetPtr(&mpData[pos],size);
424 "Array::CopyChunk(): Chunk out of bounds.");
426 "Array::CopyChunk(): destination array does not have enough memory");
427 for (
int i=pos;i<last;i++)
428 a.mpData[i-pos]=mpData[i];
434 new (&mpData[i]) T();
438 void Array<T>::InitializeDataBlock(
int first,
int last)
441 for (i = first; i < last; i++)
442 InitializeElement(i);
446 void Array<T>::UninitializeDataBlock(
int first,
int last)
449 for (i = first; i < last; i++)
454 void Array<T>::CopyDataBlock(
int first,
int last,
const T* src)
457 for (i=first; i<last ;i++)
462 void Array<T>::InitializeCopyDataBlock(
int first,
int last,
const T* src)
465 for (i=first; i<last; i++)
466 new(&mpData[i]) T(src[i]);
470 void Array<T>::InitializeCopyDataBlock(
int first,
int last,
int src_first,
const T* src)
472 int i, j = src_first;
473 for (i=first; i<last; i++)
474 new (&mpData[i]) T(src[j++]);
479 void Array<T>::DestroyDataBuffer()
483 UninitializeDataBlock(0,mSize);
491 void Array<T>::ResizeDataBuffer(
int new_size)
493 if (new_size == mAllocSize)
495 T* old_data = mpData;
496 mpData = (T*) malloc(new_size*
sizeof(T));
497 if (!old_data)
return;
498 int elems = new_size;
501 InitializeCopyDataBlock(0,elems,old_data);
502 for (
int i=0; i<mSize; i++)
503 (&old_data[i])->~T();
515 void Array<T>::InsertElemInDataBuffer(
int position)
518 new(&mpData[mSize]) T(mpData[mSize-1]);
519 for (
int i=mSize-1; i>position; i--)
520 mpData[i] = mpData[i-1];
521 (&mpData[position])->~T();
531 void Array<T>::DeleteElemInDataBuffer(
int position)
533 for (
int i=position; i<mSize-1; i++)
534 mpData[i] = mpData[i+1];
535 (&mpData[mSize-1])->~T();
550 if (a.
Size()!=b.
Size())
return false;
551 for (
int i=0;i<a.
Size();i++)
553 if (a[i]!=b[i])
return false;
564 "Array::SetSize(): Argument larger than allocated size\n"
565 "You can probably fix this calling Resize() befor SetSize().";
569 "Array::operator[]: Index out of range\n"
570 "This may happen if you forgot to call SetSize(...) in your code.\n"
571 "This is now needed. Just calling Resize() is not enough any more.";
575 "Array::InsertElem: Index out of range";
579 "Array::DeleteElem: Index out of range";