1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
21 // (Pseudo)Container optimised for fast random access operator[ ].
22 // Operator [] time doesn
\92t depend on the number of elements in container O(1) -
23 // like in standard array or like in STL vector
24 // To achieve maximally-fast indexing and iteration in one buffer mode
25 // the vector maintains its storage as a single contiguous array of objects (one buffer mode)
27 // When a vector runs out of pre-allocated storage, in order to maintain it
28 // contiguous array it must allocate a whole new (larger) chunk of storage
29 // elsewhere and copy the objects to the new storage.
30 // void * AliMemArray::Unchecked1DAt(UInt_t i) const
31 // return &(((char*)fCont)[fObjectSize*i]);
33 // In multi buffer mode (like two dimensional array) when a vector runs out of
34 // pre-allocated storage we don
\92t need to copy whole array only small buffer but operator [] is slower
35 // void * AliMemArray::Unchecked2DAt(UInt_t i, UInt_t j) const
36 // return &( ((char**)fCont)[i] [j*fObjectSize]);
37 // void * AliMemArray::Unchecked2DAt(UInt_t i) const
38 // return &( ((char**)fCont)[i/fBufferSize] [(i%fBufferSize)*fObjectSize]) ;
42 //<img src="../gif/AliMemArray.gif">
46 // Streamer CTORBuffer and DTORBuffer are virtual - should be implemented in derived
47 // classes. For example AliObjectArray derived from AliMemArray is general array
48 // for objects with defined AliClassInfo information.
51 // Origin: Marian Ivanov, Uni. of Bratislava, ivanov@fmph.uniba.sk //
52 ///////////////////////////////////////////////////////////////////////////////
56 #include "AliMemArray.h"
57 #include "Riostream.h"
65 AliMemArray::AliMemArray()
77 AliMemArray::AliMemArray(Int_t objectSize, Int_t buffersize)
80 // AliMemArray constructor
84 fObjectSize =objectSize;
85 fBufferSize =buffersize;
88 AliMemArray::AliMemArray(const AliMemArray & arr):
95 fCapacity = arr.fCapacity;
96 fObjectSize = arr.fObjectSize;
97 fBufferSize =arr.fBufferSize;
99 if (arr.fBufferSize==0) {
100 fCont = new char[fCapacity*fObjectSize];
101 CopyBuffer(fCont, arr.fCont, fSize);
104 Int_t buffers = fCapacity/fBufferSize;
105 if (fCapacity%fBufferSize) buffers++;
106 void ** table = (void**) new char*[buffers];
107 for (Int_t i=0; i<buffers;i++){
108 table[i] = new char[fBufferSize*fObjectSize];
109 Int_t size = fSize - i*fBufferSize;
110 if (size > (Int_t)fBufferSize) size = fBufferSize;
111 if (size >0) CopyBuffer(table[i], ((void**)arr.fCont)[i],size);
113 fCont = (void*)table;
118 void AliMemArray::Swap( AliMemArray &arr)
120 //swap contents of array
121 UInt_t size = arr.fSize;
124 UInt_t capacity = arr.fCapacity;
125 arr.fCapacity = fCapacity;
126 fCapacity = capacity;
127 UInt_t objectSize = arr.fObjectSize;
128 arr.fObjectSize = fObjectSize;
129 fObjectSize = objectSize;
130 UInt_t bufferSize = arr.fBufferSize;
131 arr.fBufferSize = fBufferSize;
132 fBufferSize = bufferSize;
133 void * cont = arr.fCont;
138 void AliMemArray::CopyBuffer(void *dest, void *src, UInt_t size)
141 //array placement copy constructor
142 memcpy(dest, src,size*fObjectSize);
145 AliMemArray::~AliMemArray()
153 void AliMemArray::SetObjectSize(UInt_t bufsize)
156 //set memory size for one object - it can be changed only when the array is empty
158 ::Error("AliMemArray::SetObjectSize", "forbidden to resize just allocated objects");
164 AliMemArray & AliMemArray::operator = (const AliMemArray &arr)
168 AliMemArray tmparr(arr);
173 void AliMemArray::SetBufferSize(UInt_t bufsize)
176 //set buffer size - it can be changed only when the array is empty
178 fBufferSize = bufsize;
181 if (fBufferSize == bufsize) return;
184 char *table = new char[fObjectSize*fCapacity];
186 for (UInt_t i=0; i<fSize; i++,p+=fObjectSize)
187 memcpy(p, At(i), fObjectSize);
188 //delete [](char*)fCont;
191 fBufferSize = bufsize;
194 Int_t buffers = fCapacity/bufsize;
195 if (fCapacity%bufsize) buffers++;
196 char ** table = new char*[buffers];
197 for (Int_t ibuf=0; ibuf<buffers;ibuf++){
198 table[ibuf] = new char[bufsize*fObjectSize];
199 Int_t size = fSize - ibuf*bufsize;
200 if (size > (Int_t)bufsize) size = bufsize;
201 if (size >0) for ( Int_t ip=0;ip<size;ip++)
202 memcpy(&table[ibuf][ip*fObjectSize], At(ibuf*bufsize+ip), fObjectSize);
204 // delete [](char**)fCont;
206 fCont = (void*)table;
207 fBufferSize = bufsize;
214 void AliMemArray::Delete(Option_t *)
217 //delete memory space occupied by the array -
218 //Use this routine when your objects allocate
219 //memory (e.g. objects inheriting from TNamed or containing TStrings
220 //allocate memory). If not you better use Clear() since if is faster.
228 DTORBuffer(Unchecked1DAt(0),fSize);
229 delete [] (char*)fCont;
236 void AliMemArray::Clear(Option_t *)
240 // Only use this routine when your objects don't
241 // allocate memory since it will not call the object dtors.
247 // memset(fCont, 0, fSize*fObjectSize);
248 //MI change - to have the same as in TClonesArray
253 void AliMemArray::Reserve(UInt_t n)
256 //reserve arrays space
258 if (fObjectSize<=0) {
259 cout<<"Object length not defined\n";
262 if (n==fCapacity) return;
265 Reserve2D(n); //if 2D buffer
271 DTORBuffer(Unchecked1DAt(n),fSize-n);
272 memset(&((char*)fCont)[n*fObjectSize], 0, (fSize-n)*fObjectSize);
275 fCont = (char*)TStorage::ReAlloc(fCont, n*fObjectSize,fCapacity*fObjectSize);
277 else fCont = new char[n*fObjectSize];
279 if (!fCont) fCapacity = 0;
284 void AliMemArray::Resize(UInt_t n)
289 if (fObjectSize<=0) {
290 cout<<"Object length not defined\n";
294 Resize2D(n); //if 2D buffer
298 if (n>fCapacity) Reserve(2*n); //reserve automaticaly space if sie >capacity
300 DTORBuffer(Unchecked1DAt(n),fSize-n);
301 //memset(&((char*)fCont)[n*fObjectSize], 0, (fSize-n)*fObjectSize);
302 //MI change - to have the same as in TClonesArray
304 if (fSize<n) CTORBuffer(Unchecked1DAt(fSize),n-fSize);
309 void AliMemArray::Delete2D()
312 //delete memory space occupied by the array
313 if (!fBufferSize) return;
315 Int_t nbuff = (fCapacity/fBufferSize);
316 if ( (fCapacity%fBufferSize)!=0) nbuff++;
317 for (Int_t i=0;i<nbuff;i++) {
318 Int_t size = fSize-i*fBufferSize;
320 DTORBuffer(GetRow(i),UInt_t(size)<fBufferSize? size:fBufferSize);
321 delete [] (char*)GetRow(i);
323 delete [] (void**)fCont;
329 void AliMemArray::Clear2D()
332 //clear memory space occupied by the array - doesn't call DTOR
333 Int_t nbuff = (fCapacity/fBufferSize);
334 if ( (fCapacity%fBufferSize)!=0) nbuff++;
335 // for (Int_t i=0;i<nbuff;i++) memset(GetRow(i), 0, fSize*fObjectSize);
336 //MI change - to have the same as in TClonesArray
343 void AliMemArray::Reserve2D(UInt_t n)
347 Int_t buffers = n/fBufferSize;
348 if (n%fBufferSize) buffers++;
349 UInt_t nobjects=buffers*fBufferSize;
350 Int_t oldbuffers = GetNBuffers() ;
351 if (buffers==oldbuffers) return;
353 void ** table = (void**) new char*[buffers];
355 Int_t max = buffers>oldbuffers ? buffers: oldbuffers;
356 for (Int_t i = 0;i<max;i++) {
357 if ( (i<oldbuffers) && (i<buffers)) table[i] = GetRow(i);
358 if ( (i<oldbuffers)&&(i>=buffers) ){
359 Int_t dsize = TMath::Min(Int_t(fSize) - i*Int_t(fBufferSize),Int_t(fBufferSize));
360 if (dsize>0) DTORBuffer(GetRow(i),dsize);
361 delete [] (char*)GetRow(i);
364 table[i] = new char[fBufferSize*fObjectSize];
366 if (fSize>nobjects) fSize=nobjects;
368 fCapacity = nobjects ;
369 delete [] (void**)fCont;
370 fCont = (void*)table;
375 void AliMemArray::Resize2D(UInt_t n)
379 if (n>fCapacity) Reserve2D(n); //reserve automaticaly space
380 Int_t buffers = n/fBufferSize;
381 if (n%fBufferSize) buffers++;
383 if (fSize>n){ //call destructor if we decrease the size of array
384 Int_t oldbuffer = fSize/fBufferSize;
386 for (Int_t i=buffers;i<oldbuffer; i++){
387 Int_t iold= fSize-i*fBufferSize;
388 if (iold>(Int_t)fBufferSize) iold= fBufferSize;
389 Int_t inew= n -i*fBufferSize;
391 DTORBuffer(Unchecked2DAt(i,inew),iold-inew);
394 if (fSize<n){ //call constructor if we increase the size of array
395 Int_t oldbuffer = fSize/fBufferSize;
396 for (Int_t i=oldbuffer;i<buffers; i++){
397 Int_t iold = fSize-i*fBufferSize;
398 if (iold<0) iold = 0;
399 Int_t inew = n -i*fBufferSize;
400 if (inew>(Int_t)fBufferSize) inew = fBufferSize;
401 CTORBuffer(Unchecked2DAt(i,iold),inew-iold);