Loading [MathJax]/extensions/tex2jax.js
ROOT  6.06/09
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
RooHashTable.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 #include "RooFit.h"
18 
19 #include "TMath.h"
20 #include "TMath.h"
21 #include "TCollection.h"
22 #include "RooHashTable.h"
23 #include "RooLinkedList.h"
24 #include "RooAbsArg.h"
25 #include "RooSetPair.h"
26 
27 using namespace std;
28 
30 ;
31 
32 //////////////////////////////////////////////////////////////////////////////
33 //
34 // BEGIN_HTML
35 // RooHashTable implements a hash table for TObjects. The hashing can be
36 // done on the object addresses, object names, or using the objects
37 // internal hash method. This is a utility class for RooLinkedList
38 // that uses RooHashTable to speed up direct access to large collections.
39 // END_HTML
40 //
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// Construct a hash table with given capacity and hash method
44 
46  _hashMethod(hashMethod)
47 {
48  if (capacity <= 0) {
50  }
52  _arr = new RooLinkedList* [_size] ;
53  memset(_arr, 0, _size*sizeof(RooLinkedList*));
54 
55  _usedSlots = 0 ;
56  _entries = 0 ;
57 }
58 
59 
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Copy constructor
63 
65  TObject(other),
66  _hashMethod(other._hashMethod),
67  _usedSlots(other._usedSlots),
68  _entries(other._entries),
69  _size(other._size)
70 {
71  _arr = new RooLinkedList* [_size] ;
72  memset(_arr, 0, _size*sizeof(RooLinkedList*));
73  Int_t i ;
74  for (i=0 ; i<_size ; i++) {
75  if (other._arr[i]) {
76  _arr[i] = new RooLinkedList(*other._arr[i]) ;
77  }
78  }
79 }
80 
81 
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Add given object to table. If hashArg is given, hash will be calculation
85 /// on that rather than on 'arg'
86 
87 void RooHashTable::add(TObject* arg, TObject* hashArg)
88 {
89  Int_t slot = hash(hashArg?hashArg:arg) % _size ;
90  if (!_arr[slot]) {
91  _arr[slot] = new RooLinkedList(0) ;
92  _arr[slot]->useNptr(kFALSE) ;
93  _usedSlots++ ;
94  }
95  _arr[slot]->Add(arg);
96  _entries++;
97 }
98 
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Remove given object from table. If hashArg is given, hash will be calculation
103 /// on that rather than on 'arg'
104 
106 {
107  Int_t slot = hash(hashArg?hashArg:arg) % _size ;
108  if (_arr[slot]) {
109  if (_arr[slot]->Remove(arg)) {
110  _entries-- ;
111  if (_arr[slot]->GetSize()==0) {
112  delete _arr[slot] ;
113  _arr[slot] = 0 ;
114  _usedSlots-- ;
115  }
116  return kTRUE ;
117  }
118  }
119 
120  if (_hashMethod != Name) return kFALSE;
121 
122  // If we didn't find it by name, see if it might have been renamed
123  RooAbsArg* p = dynamic_cast<RooAbsArg*>(arg);
124  //cout << "RooHashTable::remove possibly renamed '" << arg->GetName() << "', kRenamedArg=" << (p&&p->namePtr()->TestBit(RooNameReg::kRenamedArg)) << endl;
125  if (p && !p->namePtr()->TestBit(RooNameReg::kRenamedArg)) return kFALSE;
126 
127  // If so, check the whole list
128  Int_t i;
129  for (i=0 ; i<_size ; i++) {
130  if (i != slot && _arr[i] && _arr[i]->Remove(arg)) {
131  _entries-- ;
132  if (_arr[i]->GetSize()==0) {
133  delete _arr[i] ;
134  _arr[i] = 0 ;
135  _usedSlots-- ;
136  }
137  return kTRUE ;
138  }
139  }
140 
141  return kFALSE ;
142 }
143 
144 
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Calculate the average number of collisions (table slots with >1 filled entry)
148 
150 {
151  Int_t i,h[20] ;
152  for (i=0 ; i<20 ; i++) h[i]=0 ;
153 
154  for (i=0 ; i<_size ; i++) {
155  if (_arr[i]) {
156  Int_t count = _arr[i]->GetSize() ;
157  if (count<20) {
158  h[count]++ ;
159  } else {
160  h[19]++ ;
161  }
162  } else {
163  h[0]++ ;
164  }
165  }
166 
167  return 0 ;
168 }
169 
170 
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Replace oldArg with newArg in the table. If oldHashArg is given, use that to calculate
174 /// the hash associated with oldArg
175 
176 Bool_t RooHashTable::replace(const TObject* oldArg, const TObject* newArg, const TObject* oldHashArg)
177 {
178  Int_t slot = hash(oldHashArg?oldHashArg:oldArg) % _size ;
179  if (_arr[slot]) {
180  Int_t newSlot = hash(newArg) % _size ;
181  if (newSlot == slot) {
182  return _arr[slot]->Replace(oldArg,newArg) ;
183  }
184  }
185 
186  // We didn't find the oldArg or they have different slots.
187  if (remove((TObject*)oldArg,(TObject*)oldHashArg)) {
188  add((TObject*)newArg);
189  return kTRUE;
190  }
191  return kFALSE ;
192 }
193 
194 
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Return the object with given name from the table.
198 
199 TObject* RooHashTable::find(const char* name) const
200 {
201  if (_hashMethod != Name) assert(0) ;
202 
203  Int_t slot = TMath::Hash(name) % _size ;
204  if (_arr[slot]) return _arr[slot]->find(name) ;
205  return 0;
206 }
207 
208 
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 
213 {
214  if (_hashMethod != Name) assert(0) ;
215 
216  Int_t slot = TMath::Hash(arg->GetName()) % _size ;
217  if (_arr[slot]) return _arr[slot]->findArg(arg) ;
218  return 0;
219 }
220 
221 
222 
223 ////////////////////////////////////////////////////////////////////////////////
224 /// Return object with the given pointer from the table
225 
226 TObject* RooHashTable::find(const TObject* hashArg) const
227 {
228  RooLinkedListElem* elem = findLinkTo(hashArg) ;
229  return elem ? elem->_arg : 0 ;
230 }
231 
232 
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 /// Return RooLinkedList element link to object 'hashArg'
236 
238 {
239  if (_hashMethod != Pointer) assert(0) ;
240 
241  Int_t slot = hash(hashArg) % _size ;
242  RooLinkedList* lst = _arr[slot];
243  if (lst) {
244  RooFIter it = lst->fwdIterator() ;
245  TObject* obj;
246  while ((obj=it.next())) {
247  RooLinkedListElem* elem = (RooLinkedListElem*)obj ;
248  if (elem->_arg == hashArg) return elem ;
249  }
250  }
251  return 0;
252 }
253 
254 
255 
256 ////////////////////////////////////////////////////////////////////////////////
257 /// Return RooSetPair with given pointers in table
258 
259 RooSetPair* RooHashTable::findSetPair(const RooArgSet* set1, const RooArgSet* set2) const
260 {
261  if (_hashMethod != Intrinsic) assert(0) ;
262 
263  Int_t slot = RooSetPair(set1,set2).Hash() % _size ;
264  if (_arr[slot]) {
265  Int_t i ;
266  for (i=0 ; i<_arr[slot]->GetSize() ; i++) {
267  RooSetPair* pair = (RooSetPair*)_arr[slot]->At(i) ;
268  if (pair->_set1==set1 && pair->_set2==set2) {
269  return pair ;
270  }
271  }
272  }
273 
274  return 0 ;
275 }
276 
277 
278 
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// Destructor
282 
284 {
285  Int_t i ;
286  for (i=0 ; i<_size ; i++) {
287  if (_arr[i]) delete _arr[i] ;
288  }
289  delete[] _arr ;
290 }
Double_t avgCollisions() const
Calculate the average number of collisions (table slots with >1 filled entry)
TObject * find(const char *name) const
Return the object with given name from the table.
Int_t _entries
Definition: RooHashTable.h:65
#define assert(cond)
Definition: unittest.h:542
TH1 * h
Definition: legend2.C:5
RooAbsArg * findArg(const RooAbsArg *) const
Return pointer to object with given name in collection.
RooHashTable(Int_t initSize=17, HashMethod hashMethod=Name)
Construct a hash table with given capacity and hash method.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Bool_t replace(const TObject *oldArg, const TObject *newArg, const TObject *oldHashArg=0)
Replace oldArg with newArg in the table.
STL namespace.
Int_t _usedSlots
Definition: RooHashTable.h:64
Bool_t Replace(const TObject *oldArg, const TObject *newArg)
Replace object 'oldArg' in collection with new object 'newArg'.
ClassImp(RooHashTable)
RooArgSet * _set1
Definition: RooSetPair.h:38
const TNamed * namePtr() const
Definition: RooAbsArg.h:459
virtual ULong_t Hash() const
Return hash value for this object.
Definition: RooSetPair.h:41
void useNptr(Bool_t flag)
Definition: RooLinkedList.h:90
RooArgSet * _set2
Definition: RooSetPair.h:39
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
void add(TObject *arg, TObject *hashArg=0)
Add given object to table.
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
HashMethod _hashMethod
Definition: RooHashTable.h:63
Bool_t remove(TObject *arg, TObject *hashArg=0)
Remove given object from table.
ULong_t Hash(const void *txt, Int_t ntxt)
Calculates hash index from any char string.
Definition: TMath.cxx:1371
virtual ~RooHashTable()
Destructor.
RooAbsArg * next()
Long_t NextPrime(Long_t x)
TMath Base functions.
Definition: TMathBase.cxx:29
double Double_t
Definition: RtypesCore.h:55
RooFIter fwdIterator() const
TObject * find(const char *name) const
Return pointer to object with given name in collection.
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
RooLinkedList ** _arr
Definition: RooHashTable.h:67
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
Int_t GetSize() const
Definition: RooLinkedList.h:60
RooAbsArg * findArg(const RooAbsArg *arg) const
RooSetPair * findSetPair(const RooArgSet *set1, const RooArgSet *set2) const
Return RooSetPair with given pointers in table.
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
RooLinkedListElem * findLinkTo(const TObject *arg) const
Return RooLinkedList element link to object 'hashArg'.
ULong_t hash(const TObject *arg) const
Definition: RooHashTable.h:53