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 Revision 1.1 2000/11/01 16:01:26 kowal2
19 Classes for handling the new hits structures
22 ///////////////////////////////////////////////////////////////////////////////
25 // (Pseudo)Container optimised for fast random access operator[ ].
26 // Operator [] time doesn
\92t depend on the number of elements in container O(1) -
27 // like in standard array or like in STL vector
28 // To achieve maximally-fast indexing and iteration in one buffer mode
29 // the vector maintains its storage as a single contiguous array of objects (one buffer mode)
31 // When a vector runs out of pre-allocated storage, in order to maintain it
32 // contiguous array it must allocate a whole new (larger) chunk of storage
33 // elsewhere and copy the objects to the new storage.
34 // void * AliMemArray::Unchecked1DAt(UInt_t i) const
35 // return &(((char*)fCont)[fObjectSize*i]);
37 // In multi buffer mode (like two dimensional array) when a vector runs out of
38 // pre-allocated storage we don
\92t need to copy whole array only small buffer but operator [] is slower
39 // void * AliMemArray::Unchecked2DAt(UInt_t i, UInt_t j) const
40 // return &( ((char**)fCont)[i] [j*fObjectSize]);
41 // void * AliMemArray::Unchecked2DAt(UInt_t i) const
42 // return &( ((char**)fCont)[i/fBufferSize] [(i%fBufferSize)*fObjectSize]) ;
46 //<img src="../gif/AliMemArray.gif">
50 // Streamer CTORBuffer and DTORBuffer are virtual - should be implemented in derived
51 // classes. For example AliObjectArray derived from AliMemArray is general array
52 // for objects with defined AliClassInfo information.
55 // Origin: Marian Ivanov, Uni. of Bratislava, ivanov@fmph.uniba.sk //
56 ///////////////////////////////////////////////////////////////////////////////
60 #include "AliMemArray.h"
69 AliMemArray::AliMemArray()
81 AliMemArray::AliMemArray(Int_t objectSize, Int_t buffersize)
84 // AliMemArray constructor
88 fObjectSize =objectSize;
89 fBufferSize =buffersize;
92 AliMemArray::AliMemArray(const AliMemArray & arr)
98 fCapacity = arr.fCapacity;
99 fObjectSize = arr.fObjectSize;
100 fBufferSize =arr.fBufferSize;
102 if (arr.fBufferSize==0) {
103 fCont = new char[fCapacity*fObjectSize];
104 CopyBuffer(fCont, arr.fCont, fSize);
107 Int_t buffers = fCapacity/fBufferSize;
108 if (fCapacity%fBufferSize) buffers++;
109 void ** table = (void**) new char*[buffers];
110 for (Int_t i=0; i<buffers;i++){
111 table[i] = new char[fBufferSize*fObjectSize];
112 Int_t size = fSize - i*fBufferSize;
113 if (size > (Int_t)fBufferSize) size = fBufferSize;
114 if (size >0) CopyBuffer(table[i], ((void**)arr.fCont)[i],size);
116 fCont = (void*)table;
121 void AliMemArray::Swap( AliMemArray &arr)
123 //swap contents of array
124 UInt_t size = arr.fSize;
127 UInt_t capacity = arr.fCapacity;
128 arr.fCapacity = fCapacity;
129 fCapacity = capacity;
130 UInt_t objectSize = arr.fObjectSize;
131 arr.fObjectSize = fObjectSize;
132 fObjectSize = objectSize;
133 UInt_t bufferSize = arr.fBufferSize;
134 arr.fBufferSize = fBufferSize;
135 fBufferSize = bufferSize;
136 void * cont = arr.fCont;
141 void AliMemArray::CopyBuffer(void *dest, void *src, UInt_t size)
144 //array placement copy constructor
145 memcpy(dest, src,size*fObjectSize);
148 AliMemArray::~AliMemArray()
156 void AliMemArray::SetObjectSize(UInt_t bufsize)
159 //set memory size for one object - it can be changed only when the array is empty
161 ::Error("AliMemArray::SetObjectSize", "forbidden to resize just allocated objects");
167 AliMemArray & AliMemArray::operator = (const AliMemArray &arr)
171 AliMemArray tmparr(arr);
176 void AliMemArray::SetBufferSize(UInt_t bufsize)
179 //set buffer size - it can be changed only when the array is empty
181 fBufferSize = bufsize;
184 if (fBufferSize == bufsize) return;
187 char *table = new char[fObjectSize*fCapacity];
189 for (UInt_t i=0; i<fSize; i++,p+=fObjectSize)
190 memcpy(p, At(i), fObjectSize);
191 //delete [](char*)fCont;
194 fBufferSize = bufsize;
197 Int_t buffers = fCapacity/bufsize;
198 if (fCapacity%bufsize) buffers++;
199 char ** table = new char*[buffers];
200 for (Int_t ibuf=0; ibuf<buffers;ibuf++){
201 table[ibuf] = new char[bufsize*fObjectSize];
202 Int_t size = fSize - ibuf*bufsize;
203 if (size > (Int_t)bufsize) size = bufsize;
204 if (size >0) for ( Int_t ip=0;ip<size;ip++)
205 memcpy(&table[ibuf][ip*fObjectSize], At(ibuf*bufsize+ip), fObjectSize);
207 // delete [](char**)fCont;
209 fCont = (void*)table;
210 fBufferSize = bufsize;
217 void AliMemArray::Delete(Option_t *)
220 //delete memory space occupied by the array -
221 //Use this routine when your objects allocate
222 //memory (e.g. objects inheriting from TNamed or containing TStrings
223 //allocate memory). If not you better use Clear() since if is faster.
231 DTORBuffer(Unchecked1DAt(0),fSize);
232 delete [] (char*)fCont;
239 void AliMemArray::Clear(Option_t *)
243 // Only use this routine when your objects don't
244 // allocate memory since it will not call the object dtors.
250 // memset(fCont, 0, fSize*fObjectSize);
251 //MI change - to have the same as in TClonesArray
256 void AliMemArray::Reserve(UInt_t n)
259 //reserve arrays space
261 if (fObjectSize<=0) {
262 cout<<"Object length not defined\n";
265 if (n==fCapacity) return;
268 Reserve2D(n); //if 2D buffer
274 DTORBuffer(Unchecked1DAt(n),fSize-n);
275 memset(&((char*)fCont)[n*fObjectSize], 0, (fSize-n)*fObjectSize);
278 fCont = (char*)TStorage::ReAlloc(fCont, n*fObjectSize,fCapacity*fObjectSize);
280 else fCont = new char[n*fObjectSize];
282 if (!fCont) fCapacity = 0;
287 void AliMemArray::Resize(UInt_t n)
292 if (fObjectSize<=0) {
293 cout<<"Object length not defined\n";
297 Resize2D(n); //if 2D buffer
301 if (n>fCapacity) Reserve(n); //reserve automaticaly space if sie >capacity
303 DTORBuffer(Unchecked1DAt(n),fSize-n);
304 //memset(&((char*)fCont)[n*fObjectSize], 0, (fSize-n)*fObjectSize);
305 //MI change - to have the same as in TClonesArray
307 if (fSize<n) CTORBuffer(Unchecked1DAt(fSize),n-fSize);
312 void AliMemArray::Delete2D()
315 //delete memory space occupied by the array
316 if (!fBufferSize) return;
318 Int_t nbuff = (fCapacity/fBufferSize);
319 if ( (fCapacity%fBufferSize)!=0) nbuff++;
320 for (Int_t i=0;i<nbuff;i++) {
321 Int_t size = fSize-i*fBufferSize;
323 DTORBuffer(GetRow(i),UInt_t(size)<fBufferSize? size:fBufferSize);
324 delete [] (char*)GetRow(i);
326 delete [] (void**)fCont;
332 void AliMemArray::Clear2D()
335 //clear memory space occupied by the array - doesn't call DTOR
336 Int_t nbuff = (fCapacity/fBufferSize);
337 if ( (fCapacity%fBufferSize)!=0) nbuff++;
338 // for (Int_t i=0;i<nbuff;i++) memset(GetRow(i), 0, fSize*fObjectSize);
339 //MI change - to have the same as in TClonesArray
346 void AliMemArray::Reserve2D(UInt_t n)
350 Int_t buffers = n/fBufferSize;
351 if (n%fBufferSize) buffers++;
352 UInt_t nobjects=buffers*fBufferSize;
353 Int_t oldbuffers = GetNBuffers() ;
354 if (buffers==oldbuffers) return;
356 void ** table = (void**) new char*[buffers];
358 Int_t max = buffers>oldbuffers ? buffers: oldbuffers;
359 for (Int_t i = 0;i<max;i++) {
360 if ( (i<oldbuffers) && (i<buffers)) table[i] = GetRow(i);
361 if ( (i<oldbuffers)&&(i>=buffers) ){
362 Int_t dsize = TMath::Min(Int_t(fSize) - i*Int_t(fBufferSize),Int_t(fBufferSize));
363 if (dsize>0) DTORBuffer(GetRow(i),dsize);
364 delete [] (char*)GetRow(i);
367 table[i] = new char[fBufferSize*fObjectSize];
369 if (fSize>nobjects) fSize=nobjects;
371 fCapacity = nobjects ;
372 delete [] (void**)fCont;
373 fCont = (void*)table;
378 void AliMemArray::Resize2D(UInt_t n)
382 if (n>fCapacity) Reserve2D(n); //reserve automaticaly space
383 Int_t buffers = n/fBufferSize;
384 if (n%fBufferSize) buffers++;
386 if (fSize>n){ //call destructor if we decrease the size of array
387 Int_t oldbuffer = fSize/fBufferSize;
389 for (Int_t i=buffers;i<oldbuffer; i++){
390 Int_t iold= fSize-i*fBufferSize;
391 if (iold>(Int_t)fBufferSize) iold= fBufferSize;
392 Int_t inew= n -i*fBufferSize;
394 DTORBuffer(Unchecked2DAt(i,inew),iold-inew);
397 if (fSize<n){ //call constructor if we increase the size of array
398 Int_t oldbuffer = fSize/fBufferSize;
399 for (Int_t i=oldbuffer;i<buffers; i++){
400 Int_t iold = fSize-i*fBufferSize;
401 if (iold<0) iold = 0;
402 Int_t inew = n -i*fBufferSize;
403 if (inew>(Int_t)fBufferSize) inew = fBufferSize;
404 CTORBuffer(Unchecked2DAt(i,iold),inew-iold);