THashTable.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Id: THashTable.h 23198 2008-04-14 09:23:08Z rdm $
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 
00016 //////////////////////////////////////////////////////////////////////////
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 //                                                                      //
00025 //////////////////////////////////////////////////////////////////////////
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    THashTable(const THashTable&);             // not implemented
00054    THashTable& operator=(const THashTable&);  // not implemented
00055 
00056 public:
00057    THashTable(Int_t capacity = TCollection::kInitHashTableCapacity, Int_t rehash = 0);
00058    virtual       ~THashTable();
00059    void          Add(TObject *obj);
00060    virtual void  AddAll(const TCollection *col);
00061    Float_t       AverageCollisions() const;
00062    void          Clear(Option_t *option="");
00063    Int_t         Collisions(const char *name) const;
00064    Int_t         Collisions(TObject *obj) const;
00065    void          Delete(Option_t *option="");
00066    TObject      *FindObject(const char *name) const;
00067    TObject      *FindObject(const TObject *obj) const;
00068    TList        *GetListForObject(const char *name) const;
00069    TList        *GetListForObject(const TObject *obj) const;
00070    TObject     **GetObjectRef(const TObject *obj) const;
00071    Int_t         GetRehashLevel() const { return fRehashLevel; }
00072    Int_t         GetSize() const { return fEntries; }
00073    TIterator    *MakeIterator(Bool_t dir = kIterForward) const;
00074    void          Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE);
00075    TObject      *Remove(TObject *obj);
00076    TObject      *RemoveSlow(TObject *obj);
00077    void          SetRehashLevel(Int_t rehash) { fRehashLevel = rehash; }
00078 
00079    ClassDef(THashTable,0)  //A hash table
00080 };
00081 
00082 inline Float_t THashTable::AverageCollisions() const
00083 {
00084    if (fUsedSlots)
00085       return ((Float_t)fEntries)/fUsedSlots;
00086    else
00087       return 0.0;
00088 }
00089 
00090 inline Int_t THashTable::GetHashValue(const TObject *obj) const
00091 {
00092    Int_t i = Int_t(obj->Hash() % fSize);  // need intermediary i for Linux g++
00093    return i;
00094 }
00095 
00096 
00097 //////////////////////////////////////////////////////////////////////////
00098 //                                                                      //
00099 // THashTableIter                                                       //
00100 //                                                                      //
00101 // Iterator of hash table.                                              //
00102 //                                                                      //
00103 //////////////////////////////////////////////////////////////////////////
00104 
00105 class THashTableIter : public TIterator {
00106 
00107 private:
00108    const THashTable *fTable;       //hash table being iterated
00109    Int_t             fCursor;      //current position in table
00110    TListIter        *fListCursor;  //current position in collision list
00111    Bool_t            fDirection;   //iteration direction
00112 
00113    THashTableIter() : fTable(0), fCursor(0), fListCursor(0), fDirection(kIterForward) { }
00114    Int_t             NextSlot();
00115 
00116 public:
00117    THashTableIter(const THashTable *ht, Bool_t dir = kIterForward);
00118    THashTableIter(const THashTableIter &iter);
00119    ~THashTableIter();
00120    TIterator      &operator=(const TIterator &rhs);
00121    THashTableIter &operator=(const THashTableIter &rhs);
00122 
00123    const TCollection *GetCollection() const { return fTable; }
00124    TObject           *Next();
00125    void               Reset();
00126    bool               operator!=(const TIterator &aIter) const;
00127    bool               operator!=(const THashTableIter &aIter) const;
00128    TObject           *operator*() const;
00129 
00130    ClassDef(THashTableIter,0)  //Hash table iterator
00131 };
00132 
00133 #endif

Generated on Tue Jul 5 14:11:27 2011 for ROOT_528-00b_version by  doxygen 1.5.1