Logo ROOT   6.12/07
Reference Guide
THashTable.h
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 27/09/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 #ifndef ROOT_THashTable
13 #define ROOT_THashTable
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // THashTable //
19 // //
20 // THashTable implements a hash table to store TObject's. The hash //
21 // value is calculated using the value returned by the TObject's //
22 // Hash() function. Each class inheriting from TObject can override //
23 // Hash() as it sees fit. //
24 // //
25 //////////////////////////////////////////////////////////////////////////
26 
27 #include "TCollection.h"
28 #include "TString.h"
29 
30 class TList;
31 class TListIter;
32 class THashTableIter;
33 
34 
35 class THashTable : public TCollection {
36 
37 friend class THashTableIter;
38 
39 private:
40  TList **fCont; //Hash table (table of lists)
41  Int_t fEntries; //Number of objects in table
42  Int_t fUsedSlots; //Number of used slots
43  Int_t fRehashLevel; //Average collision rate which triggers rehash
44 
45  Int_t GetCheckedHashValue(TObject *obj) const;
46  Int_t GetHashValue(const TObject *obj) const;
47  Int_t GetHashValue(TString &s) const { return s.Hash() % fSize; }
48  Int_t GetHashValue(const char *str) const { return ::Hash(str) % fSize; }
49 
50  THashTable(const THashTable&); // not implemented
51  THashTable& operator=(const THashTable&); // not implemented
52 
53 public:
55  virtual ~THashTable();
56  void Add(TObject *obj);
57  void AddBefore(const TObject *before, TObject *obj);
58  virtual void AddAll(const TCollection *col);
59  Float_t AverageCollisions() const;
60  void Clear(Option_t *option="");
61  Int_t Collisions(const char *name) const;
62  Int_t Collisions(TObject *obj) const;
63  void Delete(Option_t *option="");
64  TObject *FindObject(const char *name) const;
65  TObject *FindObject(const TObject *obj) const;
66  const TList *GetListForObject(const char *name) const;
67  const TList *GetListForObject(const TObject *obj) const;
68  TObject **GetObjectRef(const TObject *obj) const;
69  Int_t GetRehashLevel() const { return fRehashLevel; }
70  Int_t GetSize() const { return fEntries; }
72  void Print(Option_t *option, Int_t recurse) const;
73  using TCollection::Print;
74  void Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE);
75  TObject *Remove(TObject *obj);
76  TObject *RemoveSlow(TObject *obj);
77  void SetRehashLevel(Int_t rehash) { fRehashLevel = rehash; }
78 
79  ClassDef(THashTable,0) //A hash table
80 };
81 
83 {
84  if (fUsedSlots)
85  return ((Float_t)fEntries)/fUsedSlots;
86  else
87  return 0.0;
88 }
89 
91 {
92  Int_t i = Int_t(obj->CheckedHash() % fSize); // need intermediary i for Linux g++
93  return i;
94 }
95 
96 inline Int_t THashTable::GetHashValue(const TObject *obj) const
97 {
98  Int_t i = Int_t(obj->Hash() % fSize); // need intermediary i for Linux g++
99  return i;
100 }
101 
102 
103 //////////////////////////////////////////////////////////////////////////
104 // //
105 // THashTableIter //
106 // //
107 // Iterator of hash table. //
108 // //
109 //////////////////////////////////////////////////////////////////////////
110 
111 class THashTableIter : public TIterator {
112 
113 private:
114  const THashTable *fTable; //hash table being iterated
115  Int_t fCursor; //current position in table
116  TListIter *fListCursor; //current position in collision list
117  Bool_t fDirection; //iteration direction
118 
119  THashTableIter() : fTable(0), fCursor(0), fListCursor(0), fDirection(kIterForward) { }
120  Int_t NextSlot();
121 
122 public:
123  THashTableIter(const THashTable *ht, Bool_t dir = kIterForward);
124  THashTableIter(const THashTableIter &iter);
125  ~THashTableIter();
126  TIterator &operator=(const TIterator &rhs);
128 
129  const TCollection *GetCollection() const { return fTable; }
130  TObject *Next();
131  void Reset();
132  Bool_t operator!=(const TIterator &aIter) const;
133  Bool_t operator!=(const THashTableIter &aIter) const;
134  TObject *operator*() const;
135 
136  ClassDef(THashTableIter,0) //Hash table iterator
137 };
138 
139 #endif
virtual ULong_t Hash() const
Return hash value for this object.
Definition: TObject.cxx:433
UInt_t Hash(const TString &s)
Definition: TString.h:462
virtual void AddAll(const TCollection *col)
Add all objects from collection col to this collection.
Definition: THashTable.cxx:127
void AddBefore(const TObject *before, TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:101
TList ** fCont
Definition: THashTable.h:40
float Float_t
Definition: RtypesCore.h:53
TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition: THashTable.cxx:282
const char Option_t
Definition: RtypesCore.h:62
Int_t GetRehashLevel() const
Definition: THashTable.h:69
void Rehash(Int_t newCapacity, Bool_t checkObjValidity=kTRUE)
Rehash the hashtable.
Definition: THashTable.cxx:351
Float_t AverageCollisions() const
Definition: THashTable.h:82
Int_t fUsedSlots
Definition: THashTable.h:42
const TList * GetListForObject(const char *name) const
Return the TList corresponding to object's name based hash value.
Definition: THashTable.cxx:254
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TObject * RemoveSlow(TObject *obj)
Remove object from the hashtable without using the hash value.
Definition: THashTable.cxx:427
THashTable & operator=(const THashTable &)
TListIter * fListCursor
Definition: THashTable.h:116
const THashTable * fTable
Definition: THashTable.h:114
Iterator abstract base class.
Definition: TIterator.h:30
Iterator of linked list.
Definition: TList.h:197
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:616
THashTable(const THashTable &)
friend class THashTableIter
Definition: THashTable.h:37
Iterator of hash table.
Definition: THashTable.h:111
THashTable implements a hash table to store TObject's.
Definition: THashTable.h:35
#define ClassDef(name, id)
Definition: Rtypes.h:320
void Clear(Option_t *option="")
Remove all objects from the table.
Definition: THashTable.cxx:156
void SetRehashLevel(Int_t rehash)
Definition: THashTable.h:77
const Bool_t kIterForward
Definition: TCollection.h:40
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Definition: TDatime.h:104
Bool_t fDirection
Definition: THashTable.h:117
A doubly linked list.
Definition: TList.h:44
Int_t fEntries
Definition: THashTable.h:41
ULong_t CheckedHash()
Checked and record whether for this class has a consistent Hash/RecursiveRemove setup (*) and then re...
Definition: TObject.h:299
TTime operator*(const TTime &t1, const TTime &t2)
Definition: TTime.h:85
Collection abstract base class.
Definition: TCollection.h:63
virtual ~THashTable()
Delete a hashtable.
Definition: THashTable.cxx:65
Int_t GetHashValue(const char *str) const
Definition: THashTable.h:48
void Reset(Detail::TBranchProxy *x)
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns a hash table iterator.
Definition: THashTable.cxx:297
Int_t Collisions(const char *name) const
Returns the number of collisions for an object with a certain name (i.e.
Definition: THashTable.cxx:180
void Print(Option_t *option, Int_t recurse) const
Print the collection header and its elements.
Definition: THashTable.cxx:314
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:227
Int_t GetSize() const
Definition: THashTable.h:70
Int_t GetHashValue(TString &s) const
Definition: THashTable.h:47
const TCollection * GetCollection() const
Definition: THashTable.h:129
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:77
static constexpr double s
Mother of all ROOT objects.
Definition: TObject.h:37
void Delete(Option_t *option="")
Remove all objects from the table AND delete all heap based objects.
Definition: THashTable.cxx:209
Int_t GetCheckedHashValue(TObject *obj) const
Definition: THashTable.h:90
Int_t fRehashLevel
Definition: THashTable.h:43
virtual void Print(Option_t *option="") const
Default print for collections, calls Print(option, 1).
const Bool_t kTRUE
Definition: RtypesCore.h:87
Int_t GetHashValue(const TObject *obj) const
Definition: THashTable.h:96
char name[80]
Definition: TGX11.cxx:109
TObject * Remove(TObject *obj)
Remove object from the hashtable.
Definition: THashTable.cxx:402