Containers definition
[u/mrichter/AliRoot.git] / CONTAINERS / AliMemArray.h
CommitLineData
08edbb90 1#ifndef ALIMEMARRAY_H
2#define ALIMEMARRAY_H
3/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * See cxx source for full Copyright notice */
5
6/* $Id$ */
7///////////////////////////////////////////////////////////////////////////////
8// //
9// AliMemArray //
10// (Pseudo)Container optimised for fast random access operator[ ]. //
11// Operator [] time doesnÂ’t depend on the number of elements in container O(1) -
12// like in standard array or like in STL vector
13///////////////////////////////////////////////////////////////////////////////
14#include "TObject.h"
15
16class AliMemArray: public TObject {
17public:
18 AliMemArray();
19 AliMemArray(Int_t objectSize, Int_t buffersize=0);
20 AliMemArray(const AliMemArray & arr);
21 AliMemArray & operator = (const AliMemArray &arr);
22 virtual ~AliMemArray();
23 void Swap(AliMemArray & arr);
24 //
25 inline void * UncheckedAt(UInt_t index) const; //
26 inline void * At(UInt_t i) const ; //controled return pointer to object
27 inline void * Unchecked1DAt(UInt_t i) const; //return pointer to the object
28 inline void * Unchecked2DAt(UInt_t i) const ; //return pointer to the object
29 inline void * Unchecked2DAt(UInt_t i, UInt_t j) const ; //return pointer to the object
30
31 Bool_t Is1D(){return (fBufferSize==0);} //true if 1D array
32 Bool_t Is2D(){return (fBufferSize!=0);} //true if 2D array
33 //
34 const void * GetArray()const {return fCont;} //return pointer to the buffer
35 void * GetRow(UInt_t row)const {return fBufferSize? ((void**)fCont)[row]: fCont;}
36
37 // return pointer to the object
38 //
39 const UInt_t GetCapacity() const {return fCapacity;} //return number of stored objects
40 const UInt_t GetSize() const {return fSize;} //return number of stored objects
41 //
42 void Delete(Option_t *option="");
43 //delete memory space occupated by the array
44 void Clear(Option_t *option="");
45 //clear memory space occupied by the array - doesn't call destructor
46 void Resize(UInt_t n);
47 void Reserve(UInt_t n);
48 //
49 const UInt_t GetBufferSize() const {return fBufferSize;}
50 const UInt_t GetObjectSize() const {return fObjectSize;}
51 const UInt_t GetNBuffers() const {return fBufferSize? fCapacity/fBufferSize :1;}
52 void SetBufferSize(UInt_t bufsize);
53protected :
54 void SetObjectSize(UInt_t size);
55 void Delete2D(); //delete memory space occupated by the array
56 void Clear2D(); //clear memory space occupated by the array
57 void Resize2D(UInt_t n);
58 void Reserve2D(UInt_t n);
59 //
60protected:
61 virtual void CTORBuffer(void *buffer, UInt_t size){;} //array placement constructor
62 virtual void DTORBuffer(void *buffer, UInt_t size){;} //array placement destructor
63 virtual void CopyBuffer(void *src, void *dest, UInt_t size); //array placement copy constructor
64 UInt_t fSize; //total number of valid objects
65 UInt_t fCapacity; //capacity of array
66 UInt_t fObjectSize; // object size
67 UInt_t fBufferSize; //number of object in one full buffer (0 means array in one buffer)
68 //
69private:
70 void * fCont; //!data buffer
71 ClassDef(AliMemArray,0)
72};
73
74void * AliMemArray::Unchecked1DAt(UInt_t i) const
75{
76 return &(((char*)fCont)[fObjectSize*i]);
77}
78
79void * AliMemArray::Unchecked2DAt(UInt_t i, UInt_t j) const
80{
81 return &( ((char**)fCont)[i] [j*fObjectSize]);
82}
83
84//********************************************************************
85void * AliMemArray::Unchecked2DAt(UInt_t i) const
86{
87 //
88 // called in GePointer if we have more then one buffer
89 return &( ((char**)fCont)[i/fBufferSize] [(i%fBufferSize)*fObjectSize]) ;
90}
91
92void * AliMemArray::UncheckedAt(UInt_t i) const
93{
94
95 return (!fBufferSize) ?Unchecked1DAt(i): Unchecked2DAt(i);
96}
97
98void * AliMemArray::At(UInt_t i) const
99{
100 return (i<fSize) ? UncheckedAt(i):0; //controled return pointer to object
101}
102
103#endif