Fork me on GitHub

source: svn/trunk/Utilities/ExRootAnalysis/interface/TSimpleArray.h@ 296

Last change on this file since 296 was 3, checked in by Xavier Rouby, 16 years ago

first commit

File size: 5.1 KB
Line 
1#ifndef TSimpleArray_h
2#define TSimpleArray_h
3
4#include "TObjArray.h"
5#include "TRefArray.h"
6
7#include <map>
8
9class TCompare;
10
11class TBaseArray
12{
13 public:
14 TBaseArray(Int_t size = 100) :
15 fIsOwner(kTRUE), fIsRefArray(kFALSE), fArray(0), fIter(0)
16 {
17 fArray = new TObjArray(size);
18 fIter = fArray->MakeIterator();
19 }
20
21 TBaseArray(TObjArray *array) :
22 fIsOwner(kFALSE), fIsRefArray(kFALSE), fArray(array), fIter(0)
23 {
24 fIter = fArray->MakeIterator();
25 }
26
27 TBaseArray(TRefArray &array) :
28 fIsOwner(kFALSE), fIsRefArray(kTRUE), fArray(&array), fIter(0)
29 {
30 fIter = fArray->MakeIterator();
31 }
32
33 virtual ~TBaseArray()
34 {
35 delete fIter;
36 if(fIsOwner) delete fArray;
37 }
38
39 Int_t GetEntries() const
40 {
41 if(fIsRefArray)
42 {
43 return static_cast<TRefArray*>(fArray)->GetEntriesFast();
44 }
45 else
46 {
47 return static_cast<TObjArray*>(fArray)->GetEntriesFast();
48 }
49 }
50
51 void Clear()
52 {
53 fArray->Clear();
54 Reset();
55 ClearFilter();
56 }
57
58 void Reset() { fIter->Reset(); }
59
60 virtual void ClearFilter(const void *filter = 0) = 0;
61
62private:
63
64 Bool_t fIsOwner;
65
66protected:
67
68 Bool_t fIsRefArray;
69 TSeqCollection *fArray;
70 TIterator *fIter;
71};
72
73//------------------------------------------------------------------------------
74
75template<typename T>
76class TSimpleArray: public TBaseArray
77{
78public:
79
80 class FilterExeption{};
81
82 TSimpleArray(Int_t size = 100) :
83 TBaseArray(size), fMap() {}
84
85 TSimpleArray(TObjArray *array) :
86 TBaseArray(array), fMap() {}
87
88 TSimpleArray(TRefArray &array) :
89 TBaseArray(array), fMap() {}
90
91 ~TSimpleArray()
92 {
93 TFilterMapConstIter it_map;
94 TCategoryMapConstIter it_submap;
95 for(it_map = fMap.begin(); it_map != fMap.end(); ++it_map)
96 {
97 for(it_submap = it_map->second.second.begin();
98 it_submap != it_map->second.second.end(); ++it_submap)
99 {
100 delete (it_submap->second);
101 }
102 }
103 }
104
105 void ClearFilter(const void *filter = 0)
106 {
107 TFilterMapIter it_map;
108 TCategoryMapIter it_submap;
109 if(filter)
110 {
111 it_map = fMap.find(filter);
112 if(it_map != fMap.end())
113 {
114 it_map->second.first = kTRUE;
115 for(it_submap = it_map->second.second.begin();
116 it_submap != it_map->second.second.end(); ++it_submap)
117 {
118 it_submap->second->Clear();
119 }
120 }
121 }
122 else
123 {
124 for(it_map = fMap.begin(); it_map != fMap.end(); ++it_map)
125 {
126 it_map->second.first = kTRUE;
127 for(it_submap = it_map->second.second.begin();
128 it_submap != it_map->second.second.end(); ++it_submap)
129 {
130 it_submap->second->Clear();
131 }
132 }
133 }
134 }
135
136 void Add(T *obj) { fArray->Add(obj); }
137
138 T *At(Int_t i) const
139 {
140 if(fIsRefArray)
141 {
142 return static_cast<T*>((*static_cast<TRefArray*>(fArray))[i]);
143 }
144 else
145 {
146 return static_cast<T*>((*static_cast<TObjArray*>(fArray))[i]);
147 }
148 }
149 T *operator[](Int_t i) const { return At(i); }
150
151 void Sort(TCompare *comp = 0)
152 {
153 TCompare *temp = T::fgCompare;
154 if(comp)
155 {
156 T::fgCompare = comp;
157 }
158 if(fIsRefArray)
159 {
160 static_cast<TRefArray*>(fArray)->Sort();
161 }
162 else
163 {
164 static_cast<TObjArray*>(fArray)->Sort();
165 }
166 if(comp)
167 {
168 T::fgCompare = temp;
169 }
170 }
171
172 template <typename F>
173 TSimpleArray<T> *GetSubArray(const F *filter, Int_t category = 1)
174 {
175 T *element;
176 Int_t result;
177 TCategoryMapIter it_submap;
178 std::pair<TCategoryMapIter, bool> pair_submap;
179 std::pair<TFilterMapIter, bool> pair_map;
180
181 TFilterMapIter it_map = fMap.find(filter);
182 if(it_map == fMap.end())
183 {
184 pair_map = fMap.insert(make_pair(filter, make_pair(kTRUE, TCategoryMap())));
185 if(!pair_map.second) throw FilterExeption();
186
187 it_map = pair_map.first;
188 }
189
190 if(it_map->second.first)
191 {
192 it_map->second.first = kFALSE;
193 Reset();
194 while(element = Next())
195 {
196 result = (*filter)(element);
197 if(result < 0) continue;
198 it_submap = it_map->second.second.find(result);
199 if(it_submap == it_map->second.second.end())
200 {
201 pair_submap = it_map->second.second.insert(make_pair(result,
202 new TSimpleArray<T>(fArray->GetSize())));
203 if(!pair_submap.second) throw FilterExeption();
204
205 pair_submap.first->second->Add(element);
206 }
207 else
208 {
209 it_submap->second->Add(element);
210 }
211 }
212 }
213
214 it_submap = it_map->second.second.find(category);
215 return (it_submap != it_map->second.second.end()) ? it_submap->second : 0;
216 }
217
218 T *Next() { return static_cast<T*>(fIter->Next()); }
219
220private:
221 typedef std::map<Int_t, TSimpleArray<T>*> TCategoryMap;
222 typedef std::map< const void*, std::pair<Bool_t, TCategoryMap> > TFilterMap;
223
224 typedef typename TFilterMap::iterator TFilterMapIter;
225 typedef typename TCategoryMap::iterator TCategoryMapIter;
226 typedef typename TFilterMap::const_iterator TFilterMapConstIter;
227 typedef typename TCategoryMap::const_iterator TCategoryMapConstIter;
228
229 TFilterMap fMap;
230};
231
232#endif /* TSimpleArray */
233
Note: See TracBrowser for help on using the repository browser.