Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TEveChunkManager.h
Go to the documentation of this file.
1 // @(#)root/eve:$Id$
2 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TEveChunkManager
13 #define ROOT_TEveChunkManager
14 
15 #include "TEveUtil.h"
16 
17 #include "TObject.h"
18 #include "TArrayC.h"
19 
20 #include <vector>
21 
22 
23 /******************************************************************************/
24 // TEveChunkManager
25 /******************************************************************************/
26 
27 class TEveChunkManager
28 {
29 private:
30  TEveChunkManager(const TEveChunkManager&); // Not implemented
31  TEveChunkManager& operator=(const TEveChunkManager&); // Not implemented
32 
33 protected:
34  Int_t fS; // Size of atom
35  Int_t fN; // Number of atoms in a chunk
36 
37  Int_t fSize; // Size of container, number of atoms
38  Int_t fVecSize; // Number of allocated chunks
39  Int_t fCapacity; // Available capacity within the chunks
40 
41  std::vector<TArrayC*> fChunks; // Memory blocks
42 
43  void ReleaseChunks();
44 
45 public:
46  TEveChunkManager();
47  TEveChunkManager(Int_t atom_size, Int_t chunk_size);
48  virtual ~TEveChunkManager();
49 
50  void Reset(Int_t atom_size, Int_t chunk_size);
51  void Refit();
52 
53  Int_t S() const { return fS; }
54  Int_t N() const { return fN; }
55 
56  Int_t Size() const { return fSize; }
57  Int_t VecSize() const { return fVecSize; }
58  Int_t Capacity() const { return fCapacity; }
59 
60  Char_t* Atom(Int_t idx) const { return fChunks[idx/fN]->fArray + idx%fN*fS; }
61  Char_t* Chunk(Int_t chk) const { return fChunks[chk]->fArray; }
62  Int_t NAtoms(Int_t chk) const { return (chk < fVecSize-1) ? fN : (fSize-1)%fN + 1; }
63 
64  Char_t* NewAtom();
65  Char_t* NewChunk();
66 
67 
68  // Iterator
69 
70  struct iterator
71  {
72  TEveChunkManager *fPlex;
73  Char_t *fCurrent;
74  Int_t fAtomIndex;
75  Int_t fNextChunk;
76  Int_t fAtomsToGo;
77 
78  const std::set<Int_t> *fSelection;
79  std::set<Int_t>::const_iterator fSelectionIterator;
80 
81  iterator(TEveChunkManager* p) :
82  fPlex(p), fCurrent(0), fAtomIndex(-1),
83  fNextChunk(0), fAtomsToGo(0), fSelection(0), fSelectionIterator() {}
84  iterator(TEveChunkManager& p) :
85  fPlex(&p), fCurrent(0), fAtomIndex(-1),
86  fNextChunk(0), fAtomsToGo(0), fSelection(0), fSelectionIterator() {}
87  iterator(const iterator& i) :
88  fPlex(i.fPlex), fCurrent(i.fCurrent), fAtomIndex(i.fAtomIndex),
89  fNextChunk(i.fNextChunk), fAtomsToGo(i.fAtomsToGo),
90  fSelection(i.fSelection), fSelectionIterator(i.fSelectionIterator) {}
91 
92  iterator& operator=(const iterator& i) {
93  fPlex = i.fPlex; fCurrent = i.fCurrent; fAtomIndex = i.fAtomIndex;
94  fNextChunk = i.fNextChunk; fAtomsToGo = i.fAtomsToGo;
95  fSelection = i.fSelection; fSelectionIterator = i.fSelectionIterator;
96  return *this;
97  }
98 
99  Bool_t next();
100  void reset() { fCurrent = 0; fAtomIndex = -1; fNextChunk = fAtomsToGo = 0; }
101 
102  Char_t* operator()() { return fCurrent; }
103  Char_t* operator*() { return fCurrent; }
104  Int_t index() { return fAtomIndex; }
105  };
106 
107  ClassDef(TEveChunkManager, 1); // Vector-like container with chunked memory allocation.
108 };
109 
110 
111 //______________________________________________________________________________
112 inline Char_t* TEveChunkManager::NewAtom()
113 {
114  Char_t *a = (fSize >= fCapacity) ? NewChunk() : Atom(fSize);
115  ++fSize;
116  return a;
117 }
118 
119 
120 /******************************************************************************/
121 // Templated some-class TEveChunkVector
122 /******************************************************************************/
123 
124 template<class T>
125 class TEveChunkVector : public TEveChunkManager
126 {
127 private:
128  TEveChunkVector(const TEveChunkVector&); // Not implemented
129  TEveChunkVector& operator=(const TEveChunkVector&); // Not implemented
130 
131 public:
132  TEveChunkVector() : TEveChunkManager() {}
133  TEveChunkVector(Int_t chunk_size) : TEveChunkManager(sizeof(T), chunk_size) {}
134  virtual ~TEveChunkVector() {}
135 
136  void Reset(Int_t chunk_size) { Reset(sizeof(T), chunk_size); }
137 
138  T* At(Int_t idx) { return reinterpret_cast<T*>(Atom(idx)); }
139  T& Ref(Int_t idx) { return *At(idx); }
140 
141  ClassDef(TEveChunkVector, 1); // Templated class for specific atom classes (given as template argument).
142 };
143 
144 #endif