Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
30class TList;
31class TListIter;
32class THashTableIter;
33
34
35class THashTable : public TCollection {
36
37friend class THashTableIter;
38
39private:
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
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 void AddImpl(Int_t slot, TObject *object);
51
52 THashTable(const THashTable&) = delete;
53 THashTable& operator=(const THashTable&) = delete;
54
55public:
57 virtual ~THashTable();
58 void Add(TObject *obj) override;
59 void Add(TObject *obj, Option_t *) override { Add(obj); };
60 void AddBefore(const TObject *before, TObject *obj);
61 void AddAll(const TCollection *col) override;
63 void Clear(Option_t *option="") override;
64 Int_t Collisions(const char *name) const;
65 Int_t Collisions(TObject *obj) const;
66 void Delete(Option_t *option="") override;
67 Bool_t Empty() const { return fEntries == 0; }
68 TObject *FindObject(const char *name) const override;
69 TObject *FindObject(const TObject *obj) const override;
70 const TList *GetListForObject(const char *name) const;
71 const TList *GetListForObject(const TObject *obj) const;
72 TObject **GetObjectRef(const TObject *obj) const override;
73 Int_t GetRehashLevel() const { return fRehashLevel; }
74 Int_t GetSize() const override { return fEntries; }
75 TIterator *MakeIterator(Bool_t dir = kIterForward) const override;
77 void Print(Option_t *option, Int_t recurse) const override;
79 TObject *Remove(TObject *obj) override;
82
83 ClassDefOverride(THashTable,0) //A hash table
84};
85
87{
88 if (fUsedSlots)
90 else
91 return 0.0;
92}
93
95{
96 Int_t i = Int_t(obj->CheckedHash() % fSize); // need intermediary i for Linux g++
97 return i;
98}
99
100inline Int_t THashTable::GetHashValue(const TObject *obj) const
101{
102 Int_t i = Int_t(obj->Hash() % fSize); // need intermediary i for Linux g++
103 return i;
104}
105
106
107//////////////////////////////////////////////////////////////////////////
108// //
109// THashTableIter //
110// //
111// Iterator of hash table. //
112// //
113//////////////////////////////////////////////////////////////////////////
114
115class THashTableIter : public TIterator {
116
117private:
118 const THashTable *fTable; //hash table being iterated
119 Int_t fCursor; //current position in table
120 TListIter *fListCursor; //current position in collision list
121 Bool_t fDirection; //iteration direction
122
124 Int_t NextSlot();
125
126public:
128 THashTableIter(const THashTableIter &iter);
130 TIterator &operator=(const TIterator &rhs) override;
132
133 const TCollection *GetCollection() const override { return fTable; }
134 TObject *Next() override;
135 void Reset() override;
136 Bool_t operator!=(const TIterator &aIter) const override;
138 TObject *operator*() const override;
139
140 ClassDefOverride(THashTableIter,0) //Hash table iterator
141};
142
143#endif
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
const Bool_t kIterForward
Definition TCollection.h:42
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
Collection abstract base class.
Definition TCollection.h:65
@ kInitHashTableCapacity
void Print(Option_t *option="") const override
Default print for collections, calls Print(option, 1).
Iterator of hash table.
Definition THashTable.h:115
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
const THashTable * fTable
Definition THashTable.h:118
~THashTableIter()
Delete hashtable iterator.
void Reset() override
Reset the hashtable iterator.
TObject * operator*() const override
Return pointer to current object or nullptr.
const TCollection * GetCollection() const override
Definition THashTable.h:133
Bool_t fDirection
Definition THashTable.h:121
TObject * Next() override
Return next object in hashtable. Returns 0 when no more objects in table.
TIterator & operator=(const TIterator &rhs) override
Overridden assignment operator.
Int_t NextSlot()
Returns index of next slot in table containing list to be iterated.
TListIter * fListCursor
Definition THashTable.h:120
THashTable implements a hash table to store TObject's.
Definition THashTable.h:35
Float_t AverageCollisions() const
Definition THashTable.h:86
THashTable & operator=(const THashTable &)=delete
void Add(TObject *obj) override
Add object to the hash table.
const TList * GetListForObject(const char *name) const
Return the TList corresponding to object's name based hash value.
Int_t fUsedSlots
Definition THashTable.h:42
void AddImpl(Int_t slot, TObject *object)
Helper function doing the actual add to the table give a slot and object.
TList ** fCont
Definition THashTable.h:40
TObject * Remove(TObject *obj) override
Remove object from the hashtable.
void Rehash(Int_t newCapacity, Bool_t checkObjValidity=kTRUE)
Rehash the hashtable.
TObject * RemoveSlow(TObject *obj)
Remove object from the hashtable without using the hash value.
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Returns a hash table iterator.
TObject ** GetObjectRef(const TObject *obj) const override
Return address of pointer to obj.
THashTable(const THashTable &)=delete
void AddBefore(const TObject *before, TObject *obj)
Add object to the hash table.
void AddAll(const TCollection *col) override
Add all objects from collection col to this collection.
TObject * FindObject(const char *name) const override
Find object using its name.
Int_t fEntries
Definition THashTable.h:41
Int_t GetSize() const override
Return the capacity of the collection, i.e.
Definition THashTable.h:74
Int_t GetRehashLevel() const
Definition THashTable.h:73
Int_t GetHashValue(TString &s) const
Definition THashTable.h:47
void Clear(Option_t *option="") override
Remove all objects from the table.
Int_t Collisions(const char *name) const
Returns the number of collisions for an object with a certain name (i.e.
void SetRehashLevel(Int_t rehash)
Definition THashTable.h:81
Int_t GetHashValue(const char *str) const
Definition THashTable.h:48
void Print(Option_t *option, Int_t recurse) const override
Print the collection header and its elements.
Int_t fRehashLevel
Definition THashTable.h:43
void Delete(Option_t *option="") override
Remove all objects from the table AND delete all heap based objects.
virtual ~THashTable()
Delete a hashtable.
void Add(TObject *obj, Option_t *) override
Definition THashTable.h:59
Int_t GetCheckedHashValue(TObject *obj) const
Definition THashTable.h:94
Int_t GetHashValue(const TObject *obj) const
Definition THashTable.h:100
Bool_t Empty() const
Definition THashTable.h:67
Iterator abstract base class.
Definition TIterator.h:30
Iterator of linked list.
Definition TList.h:196
A doubly linked list.
Definition TList.h:38
Mother of all ROOT objects.
Definition TObject.h:41
ULong_t CheckedHash()
Check and record whether this class has a consistent Hash/RecursiveRemove setup (*) and then return t...
Definition TObject.h:327
virtual ULong_t Hash() const
Return hash value for this object.
Definition TObject.cxx:533
Basic string class.
Definition TString.h:138
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:684