ROOT  6.06/09
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 #ifndef ROOT_TCollection
28 #include "TCollection.h"
29 #endif
30 #ifndef ROOT_TString
31 #include "TString.h"
32 #endif
33 
34 class TList;
35 class TListIter;
36 class THashTableIter;
37 
38 
39 class THashTable : public TCollection {
40 
41 friend class THashTableIter;
42 
43 private:
44  TList **fCont; //Hash table (table of lists)
45  Int_t fEntries; //Number of objects in table
46  Int_t fUsedSlots; //Number of used slots
47  Int_t fRehashLevel; //Average collision rate which triggers rehash
48 
49  Int_t GetHashValue(const TObject *obj) const;
50  Int_t GetHashValue(TString &s) const { return s.Hash() % fSize; }
51  Int_t GetHashValue(const char *str) const { return ::Hash(str) % fSize; }
52 
53  THashTable(const THashTable&); // not implemented
54  THashTable& operator=(const THashTable&); // not implemented
55 
56 public:
58  virtual ~THashTable();
59  void Add(TObject *obj);
60  void AddBefore(const TObject *before, TObject *obj);
61  virtual void AddAll(const TCollection *col);
62  Float_t AverageCollisions() const;
63  void Clear(Option_t *option="");
64  Int_t Collisions(const char *name) const;
65  Int_t Collisions(TObject *obj) const;
66  void Delete(Option_t *option="");
67  TObject *FindObject(const char *name) const;
68  TObject *FindObject(const TObject *obj) const;
69  const TList *GetListForObject(const char *name) const;
70  const TList *GetListForObject(const TObject *obj) const;
71  TObject **GetObjectRef(const TObject *obj) const;
72  Int_t GetRehashLevel() const { return fRehashLevel; }
73  Int_t GetSize() const { return fEntries; }
75  void Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE);
78  void SetRehashLevel(Int_t rehash) { fRehashLevel = rehash; }
79 
80  ClassDef(THashTable,0) //A hash table
81 };
82 
84 {
85  if (fUsedSlots)
86  return ((Float_t)fEntries)/fUsedSlots;
87  else
88  return 0.0;
89 }
90 
92 {
93  Int_t i = Int_t(obj->Hash() % fSize); // need intermediary i for Linux g++
94  return i;
95 }
96 
97 
98 //////////////////////////////////////////////////////////////////////////
99 // //
100 // THashTableIter //
101 // //
102 // Iterator of hash table. //
103 // //
104 //////////////////////////////////////////////////////////////////////////
105 
106 class THashTableIter : public TIterator {
107 
108 private:
109  const THashTable *fTable; //hash table being iterated
110  Int_t fCursor; //current position in table
111  TListIter *fListCursor; //current position in collision list
112  Bool_t fDirection; //iteration direction
113 
114  THashTableIter() : fTable(0), fCursor(0), fListCursor(0), fDirection(kIterForward) { }
115  Int_t NextSlot();
116 
117 public:
118  THashTableIter(const THashTable *ht, Bool_t dir = kIterForward);
120  ~THashTableIter();
121  TIterator &operator=(const TIterator &rhs);
123 
124  const TCollection *GetCollection() const { return fTable; }
125  TObject *Next();
126  void Reset();
127  Bool_t operator!=(const TIterator &aIter) const;
128  Bool_t operator!=(const THashTableIter &aIter) const;
129  TObject *operator*() const;
130 
131  ClassDef(THashTableIter,0) //Hash table iterator
132 };
133 
134 #endif
Int_t GetHashValue(const char *str) const
Definition: THashTable.h:51
Int_t Collisions(const char *name) const
Returns the number of collisions for an object with a certain name (i.e.
Definition: THashTable.cxx:170
Int_t GetHashValue(TString &s) const
Definition: THashTable.h:50
UInt_t Hash(const TString &s)
Definition: TString.h:459
virtual void AddAll(const TCollection *col)
Add all objects from collection col to this collection.
Definition: THashTable.cxx:121
void AddBefore(const TObject *before, TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:97
void Reset()
Reset the hashtable iterator.
Definition: THashTable.cxx:476
const TList * GetListForObject(const char *name) const
Return the TList corresponding to object's name based hash value.
Definition: THashTable.cxx:233
TList ** fCont
Definition: THashTable.h:44
float Float_t
Definition: RtypesCore.h:53
virtual ULong_t Hash() const
Return hash value for this object.
Definition: TObject.cxx:477
const char Option_t
Definition: RtypesCore.h:62
TObject * operator*() const
Return pointer to current object or nullptr.
Definition: THashTable.cxx:508
void Rehash(Int_t newCapacity, Bool_t checkObjValidity=kTRUE)
Rehash the hashtable.
Definition: THashTable.cxx:279
Int_t fUsedSlots
Definition: THashTable.h:46
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Returns a hash table iterator.
Definition: THashTable.cxx:264
TObject * RemoveSlow(TObject *obj)
Remove object from the hashtable without using the hash value.
Definition: THashTable.cxx:334
THashTable & operator=(const THashTable &)
TListIter * fListCursor
Definition: THashTable.h:111
Int_t GetHashValue(const TObject *obj) const
Definition: THashTable.h:91
const THashTable * fTable
Definition: THashTable.h:109
Iterator abstract base class.
Definition: TIterator.h:32
Iterator of linked list.
Definition: TList.h:187
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: THashTable.cxx:488
THashTable(const THashTable &)
Iterator of hash table.
Definition: THashTable.h:106
THashTable implements a hash table to store TObject's.
Definition: THashTable.h:39
#define ClassDef(name, id)
Definition: Rtypes.h:254
void Clear(Option_t *option="")
Remove all objects from the table.
Definition: THashTable.cxx:148
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
void SetRehashLevel(Int_t rehash)
Definition: THashTable.h:78
Int_t GetSize() const
Definition: THashTable.h:73
const Bool_t kIterForward
Definition: TCollection.h:43
Int_t NextSlot()
Returns index of next slot in table containing list to be iterated.
Definition: THashTable.cxx:453
~THashTableIter()
Delete hashtable iterator.
Definition: THashTable.cxx:426
Bool_t fDirection
Definition: THashTable.h:112
A doubly linked list.
Definition: TList.h:47
Int_t fEntries
Definition: THashTable.h:45
const TCollection * GetCollection() const
Definition: THashTable.h:124
Collection abstract base class.
Definition: TCollection.h:48
virtual ~THashTable()
Delete a hashtable.
Definition: THashTable.cxx:63
Int_t fSize
Definition: TCollection.h:63
Float_t AverageCollisions() const
Definition: THashTable.h:83
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition: THashTable.cxx:389
TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition: THashTable.cxx:252
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:75
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
void Delete(Option_t *option="")
Remove all objects from the table AND delete all heap based objects.
Definition: THashTable.cxx:193
Int_t GetRehashLevel() const
Definition: THashTable.h:72
Int_t fRehashLevel
Definition: THashTable.h:47
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:209
TObject * Next()
Return next object in hashtable. Returns 0 when no more objects in table.
Definition: THashTable.cxx:434
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
TObject * Remove(TObject *obj)
Remove object from the hashtable.
Definition: THashTable.cxx:314
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:605