ROOT  6.06/09
Reference Guide
THashList.cxx
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 10/08/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 /** \class THashList
13 THashList implements a hybrid collection class consisting of a
14 hash table and a list to store TObject's. The hash table is used for
15 quick access and lookup of objects while the list allows the objects
16 to be ordered. The hash value is calculated using the value returned
17 by the TObject's Hash() function. Each class inheriting from TObject
18 can override Hash() as it sees fit.
19 */
20 
21 #include "THashList.h"
22 #include "THashTable.h"
23 
24 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Create a THashList object. Capacity is the initial hashtable capacity
29 /// (i.e. number of slots), by default kInitHashTableCapacity = 17, and
30 /// rehash is the value at which a rehash will be triggered. I.e. when the
31 /// average size of the linked lists at a slot becomes longer than rehash
32 /// then the hashtable will be resized and refilled to reduce the collision
33 /// rate to about 1. The higher the collision rate, i.e. the longer the
34 /// linked lists, the longer lookup will take. If rehash=0 the table will
35 /// NOT automatically be rehashed. Use Rehash() for manual rehashing.
36 ///
37 /// WARNING !!!
38 /// If the name of an object in the HashList is modified, The hashlist
39 /// must be Rehashed
40 
41 THashList::THashList(Int_t capacity, Int_t rehash)
42 {
43  fTable = new THashTable(capacity, rehash);
44 }
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// For backward compatibility only. Use other ctor.
48 
50 {
51  fTable = new THashTable(capacity, rehash);
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Delete a hashlist. Objects are not deleted unless the THashList is the
56 /// owner (set via SetOwner()).
57 
59 {
60  Clear();
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Add object at the beginning of the list.
66 
68 {
69  TList::AddFirst(obj);
70  fTable->Add(obj);
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Add object at the beginning of the list and also store option.
75 /// Storing an option is useful when one wants to change the behaviour
76 /// of an object a little without having to create a complete new
77 /// copy of the object. This feature is used, for example, by the Draw()
78 /// method. It allows the same object to be drawn in different ways.
79 
81 {
82  TList::AddFirst(obj, opt);
83  fTable->Add(obj);
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Add object at the end of the list.
88 
90 {
91  TList::AddLast(obj);
92  fTable->Add(obj);
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Add object at the end of the list and also store option.
97 /// Storing an option is useful when one wants to change the behaviour
98 /// of an object a little without having to create a complete new
99 /// copy of the object. This feature is used, for example, by the Draw()
100 /// method. It allows the same object to be drawn in different ways.
101 
103 {
104  TList::AddLast(obj, opt);
105  fTable->Add(obj);
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Insert object before object before in the list.
110 
112 {
113  TList::AddBefore(before, obj);
114  fTable->AddBefore(before, obj);
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// Insert object before object before in the list.
119 
121 {
122  TList::AddBefore(before, obj);
123  fTable->AddBefore(before->GetObject(), obj);
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Insert object after object after in the list.
128 
130 {
131  TList::AddAfter(after, obj);
132  fTable->Add(obj);
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Insert object after object after in the list.
137 
139 {
140  TList::AddAfter(after, obj);
141  fTable->Add(obj);
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Insert object at location idx in the list.
146 
148 {
149  TList::AddAt(obj, idx);
150  fTable->Add(obj);
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Return the average collision rate. The higher the number the longer
155 /// the linked lists in the hashtable, the slower the lookup. If the number
156 /// is high, or lookup noticeably too slow, perform a Rehash().
157 
159 {
160  return fTable->AverageCollisions();
161 }
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Remove all objects from the list. Does not delete the objects unless
165 /// the THashList is the owner (set via SetOwner()).
166 
168 {
169  fTable->Clear("nodelete"); // clear table so not more lookups
170  if (IsOwner())
171  TList::Delete(option);
172  else
173  TList::Clear(option);
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Remove all objects from the list AND delete all heap based objects.
178 /// If option="slow" then keep list consistent during delete. This allows
179 /// recursive list operations during the delete (e.g. during the dtor
180 /// of an object in this list one can still access the list to search for
181 /// other not yet deleted objects).
182 
184 {
185  Bool_t slow = option ? (!strcmp(option, "slow") ? kTRUE : kFALSE) : kFALSE;
186 
187  if (!slow) {
188  fTable->Clear("nodelete"); // clear table so no more lookups
189  TList::Delete(option); // this deletes the objects
190  } else {
191  while (fFirst) {
192  TObjLink *tlk = fFirst;
193  fFirst = fFirst->Next();
194  fSize--;
195  // remove object from table
196  fTable->Remove(tlk->GetObject());
197  // delete only heap objects
198  if (tlk->GetObject() && tlk->GetObject()->IsOnHeap())
200 
201  delete tlk;
202  }
203  fFirst = fLast = fCache = 0;
204  fSize = 0;
205  }
206 }
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Find object using its name. Uses the hash value returned by the
210 /// TString::Hash() after converting name to a TString.
211 
212 TObject *THashList::FindObject(const char *name) const
213 {
214  return fTable->FindObject(name);
215 }
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// Find object using its hash value (returned by its Hash() member).
219 
221 {
222  return fTable->FindObject(obj);
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Return the THashTable's list (bucket) in which obj can be found based on
227 /// its hash; see THashTable::GetListForObject().
228 
229 const TList *THashList::GetListForObject(const char *name) const
230 {
231  return fTable->GetListForObject(name);
232 }
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 /// Return the THashTable's list (bucket) in which obj can be found based on
236 /// its hash; see THashTable::GetListForObject().
237 
239 {
240  return fTable->GetListForObject(obj);
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Remove object from this collection and recursively remove the object
245 /// from all other objects (and collections).
246 /// This function overrides TCollection::RecursiveRemove that calls
247 /// the Remove function. THashList::Remove cannot be called because
248 /// it uses the hash value of the hash table. This hash value
249 /// is not available anymore when RecursiveRemove is called from
250 /// the TObject destructor.
251 
253 {
254  if (!obj) return;
255 
256  // Remove obj in the list itself
257  TObject *object = TList::Remove(obj);
258  if (object) fTable->RemoveSlow(object);
259 
260  // Scan again the list and invoke RecursiveRemove for all objects
261  TIter next(this);
262 
263  while ((object = next())) {
264  if (object->TestBit(kNotDeleted)) object->RecursiveRemove(obj);
265  }
266 }
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 /// Rehash the hashlist. If the collision rate becomes too high (i.e.
270 /// the average size of the linked lists become too long) then lookup
271 /// efficiency decreases since relatively long lists have to be searched
272 /// every time. To improve performance rehash the hashtable. This resizes
273 /// the table to newCapacity slots and refills the table. Use
274 /// AverageCollisions() to check if you need to rehash.
275 
276 void THashList::Rehash(Int_t newCapacity)
277 {
278  fTable->Rehash(newCapacity);
279 }
280 
281 ////////////////////////////////////////////////////////////////////////////////
282 /// Remove object from the list.
283 
285 {
286  if (!obj || !fTable->FindObject(obj)) return 0;
287 
288  TList::Remove(obj);
289  return fTable->Remove(obj);
290 }
291 
292 ////////////////////////////////////////////////////////////////////////////////
293 /// Remove object via its objlink from the list.
294 
296 {
297  if (!lnk) return 0;
298 
299  TObject *obj = lnk->GetObject();
300 
301  TList::Remove(lnk);
302  return fTable->Remove(obj);
303 }
virtual void AddAt(TObject *obj, Int_t idx)
Insert object at position idx in the list.
Definition: TList.cxx:268
void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: THashList.cxx:67
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
void AddLast(TObject *obj)
Add object at the end of the list.
Definition: THashList.cxx:89
Bool_t IsOnHeap() const
Definition: TObject.h:140
void AddBefore(const TObject *before, TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:97
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
const TList * GetListForObject(const char *name) const
Return the TList corresponding to object's name based hash value.
Definition: THashTable.cxx:233
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:212
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: TList.cxx:92
void Rehash(Int_t newCapacity, Bool_t checkObjValidity=kTRUE)
Rehash the hashtable.
Definition: THashTable.cxx:279
void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: THashList.cxx:183
Float_t AverageCollisions() const
Return the average collision rate.
Definition: THashList.cxx:158
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TObject * RemoveSlow(TObject *obj)
Remove object from the hashtable without using the hash value.
Definition: THashTable.cxx:334
void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: THashList.cxx:252
THashTable * fTable
Definition: THashList.h:39
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition: TList.cxx:136
virtual void RecursiveRemove(TObject *obj)
Recursively remove this object from a list.
Definition: TObject.cxx:616
const TList * GetListForObject(const char *name) const
Return the THashTable's list (bucket) in which obj can be found based on its hash; see THashTable::Ge...
Definition: THashList.cxx:229
#define SafeDelete(p)
Definition: RConfig.h:436
THashTable implements a hash table to store TObject's.
Definition: THashTable.h:39
THashList implements a hybrid collection class consisting of a hash table and a list to store TObject...
Definition: THashList.h:36
void Clear(Option_t *option="")
Remove all objects from the table.
Definition: THashTable.cxx:148
ClassImp(THashList) THashList
Create a THashList object.
Definition: THashList.cxx:25
A doubly linked list.
Definition: TList.h:47
THashList(const THashList &)
void Clear(Option_t *option="")
Remove all objects from the list.
Definition: THashList.cxx:167
void AddAt(TObject *obj, Int_t idx)
Insert object at location idx in the list.
Definition: THashList.cxx:147
Bool_t IsOwner() const
Definition: TCollection.h:101
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:674
TObject * Remove(TObject *obj)
Remove object from the list.
Definition: THashList.cxx:284
TObjLink * fLast
pointer to first entry in linked list
Definition: TList.h:53
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
void AddAfter(const TObject *after, TObject *obj)
Insert object after object after in the list.
Definition: THashList.cxx:129
Int_t fSize
Definition: TCollection.h:63
virtual void AddBefore(const TObject *before, TObject *obj)
Insert object before object before in the list.
Definition: TList.cxx:172
Float_t AverageCollisions() const
Definition: THashTable.h:83
TObjLink * fFirst
Definition: TList.h:52
void Rehash(Int_t newCapacity)
Rehash the hashlist.
Definition: THashList.cxx:276
static void GarbageCollect(TObject *obj)
Add to the list of things to be cleaned up.
virtual ~THashList()
Delete a hashlist.
Definition: THashList.cxx:58
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:75
void AddBefore(const TObject *before, TObject *obj)
Insert object before object before in the list.
Definition: THashList.cxx:111
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition: TList.cxx:348
virtual void AddAfter(const TObject *after, TObject *obj)
Insert object after object after in the list.
Definition: TList.cxx:220
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
TObjLink * fCache
pointer to last entry in linked list
Definition: TList.h:54
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:209
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
TObject * Remove(TObject *obj)
Remove object from the hashtable.
Definition: THashTable.cxx:314