00001 // @(#)root/cont:$Name: $:$Id: THashTable.h,v 1.7 2002/08/07 10:56:20 brun Exp $ 00002 // Author: Fons Rademakers 27/09/95 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 #ifndef ROOT_THashTable 00013 #define ROOT_THashTable 00014 00015 00017 // // 00018 // THashTable // 00019 // // 00020 // THashTable implements a hash table to store TObject's. The hash // 00021 // value is calculated using the value returned by the TObject's // 00022 // Hash() function. Each class inheriting from TObject can override // 00023 // Hash() as it sees fit. // 00024 // // 00026 00027 #ifndef ROOT_TCollection 00028 #include "TCollection.h" 00029 #endif 00030 #ifndef ROOT_TString 00031 #include "TString.h" 00032 #endif 00033 00034 class TList; 00035 class TListIter; 00036 class THashTableIter; 00037 00038 00039 class THashTable : public TCollection { 00040 00041 friend class THashTableIter; 00042 00043 private: 00044 TList **fCont; //Hash table (table of lists) 00045 Int_t fEntries; //Number of objects in table 00046 Int_t fUsedSlots; //Number of used slots 00047 Int_t fRehashLevel; //Average collision rate which triggers rehash 00048 00049 Int_t GetHashValue(const TObject *obj) const; 00050 Int_t GetHashValue(TString &s) const { return s.Hash() % fSize; } 00051 Int_t GetHashValue(const char *str) const { return ::Hash(str) % fSize; } 00052 00053 public: 00054 THashTable(Int_t capacity = TCollection::kInitHashTableCapacity, Int_t rehash = 0); 00055 virtual ~THashTable(); 00056 void Add(TObject *obj); 00057 Float_t AverageCollisions() const; 00058 void Clear(Option_t *option=""); 00059 Int_t Collisions(const char *name) const; 00060 Int_t Collisions(TObject *obj) const; 00061 void Delete(Option_t *option=""); 00062 TObject *FindObject(const char *name) const; 00063 TObject *FindObject(const TObject *obj) const; 00064 TObject **GetObjectRef(const TObject *obj) const; 00065 Int_t GetSize() const { return fEntries; } 00066 TIterator *MakeIterator(Bool_t dir = kIterForward) const; 00067 void Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE); 00068 TObject *Remove(TObject *obj); 00069 TObject *RemoveSlow(TObject *obj); 00070 00071 ClassDef(THashTable,0) //A hash table 00072 }; 00073 00074 inline Float_t THashTable::AverageCollisions() const 00075 { 00076 if (fUsedSlots) 00077 return ((Float_t)fEntries)/fUsedSlots; 00078 else 00079 return 0.0; 00080 } 00081 00082 inline Int_t THashTable::GetHashValue(const TObject *obj) const 00083 { 00084 Int_t i = Int_t(obj->Hash() % fSize); // need intermediary i for Linux g++ 00085 return i; 00086 } 00087 00088 00090 // // 00091 // THashTableIter // 00092 // // 00093 // Iterator of hash table. // 00094 // // 00096 00097 class THashTableIter : public TIterator { 00098 00099 private: 00100 const THashTable *fTable; //hash table being iterated 00101 Int_t fCursor; //current position in table 00102 TListIter *fListCursor; //current position in collision list 00103 Bool_t fDirection; //iteration direction 00104 00105 THashTableIter() : fTable(0), fListCursor(0) { } 00106 Int_t NextSlot(); 00107 00108 public: 00109 THashTableIter(const THashTable *ht, Bool_t dir = kIterForward); 00110 THashTableIter(const THashTableIter &iter); 00111 ~THashTableIter(); 00112 TIterator &operator=(const TIterator &rhs); 00113 THashTableIter &operator=(const THashTableIter &rhs); 00114 00115 const TCollection *GetCollection() const { return fTable; } 00116 TObject *Next(); 00117 void Reset(); 00118 00119 ClassDef(THashTableIter,0) //Hash table iterator 00120 }; 00121 00122 #endif
1.2.16