Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

TMap.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Name:  $:$Id: TMap.h,v 1.14 2003/06/23 22:18:37 rdm Exp $
00002 // Author: Fons Rademakers   12/11/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_TMap
00013 #define ROOT_TMap
00014 
00015 
00017 //                                                                      //
00018 // TMap                                                                 //
00019 //                                                                      //
00020 // TMap implements an associative array of (key,value) pairs using a    //
00021 // hash table for efficient retrieval (therefore TMap does not conserve //
00022 // the order of the entries). The hash value is calculated              //
00023 // using the value returned by the keys Hash() function. Both key and   //
00024 // value need to inherit from TObject.                                  //
00025 //                                                                      //
00027 
00028 #ifndef ROOT_TCollection
00029 #include "TCollection.h"
00030 #endif
00031 #ifndef ROOT_THashTable
00032 #include "THashTable.h"
00033 #endif
00034 
00035 class THashTableIter;
00036 class TMapIter;
00037 class TBrowser;
00038 
00039 
00040 class TMap : public TCollection {
00041 
00042 friend class  TMapIter;
00043 
00044 private:
00045    THashTable   *fTable;     //Hash table used to store TPair's
00046 
00047 public:
00048    TMap(Int_t capacity = TCollection::kInitHashTableCapacity, Int_t rehash = 0);
00049    virtual           ~TMap();
00050    void              Add(TObject *obj);
00051    void              Add(TObject *key, TObject *value);
00052    Float_t           AverageCollisions() const;
00053    Int_t             Capacity() const;
00054    void              Clear(Option_t *option="");
00055    Int_t             Collisions(const char *keyname) const;
00056    Int_t             Collisions(TObject *key) const;
00057    void              Delete(Option_t *option="");
00058    void              DeleteKeys() { Delete(); }
00059    void              DeleteValues();
00060    void              DeleteAll();
00061    TObject          *FindObject(const char *keyname) const;
00062    TObject          *FindObject(const TObject *key) const;
00063    TObject         **GetObjectRef(const TObject *obj) const { return fTable->GetObjectRef(obj); }
00064    const THashTable *GetTable() const { return fTable; }
00065    TObject          *GetValue(const TObject *key) const;
00066    TIterator        *MakeIterator(Bool_t dir = kIterForward) const;
00067    void              Print(Option_t *option="") const;
00068    void              Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE);
00069    TObject          *Remove(TObject *key);
00070 
00071    ClassDef(TMap,3)  //A (key,value) map
00072 };
00073 
00074 
00076 //                                                                      //
00077 // TPair                                                                //
00078 //                                                                      //
00079 // Class used by TMap to store (key,value) pairs.                       //
00080 //                                                                      //
00082 
00083 class TPair : public TObject {
00084 
00085 private:
00086    TObject  *fKey;
00087    TObject  *fValue;
00088 
00089 public:
00090    TPair(TObject *key, TObject *value) : fKey(key), fValue(value) { }
00091    TPair(const TPair &a) : TObject(), fKey(a.fKey), fValue(a.fValue) { }
00092    virtual               ~TPair() { }
00093    Bool_t                IsFolder() const { return kTRUE;}
00094    virtual void          Browse(TBrowser *b);
00095    const char           *GetName() const { return fKey->GetName(); }
00096    ULong_t               Hash() const { return fKey->Hash(); }
00097    Bool_t                IsEqual(const TObject *obj) const { return fKey->IsEqual(obj); }
00098    TObject              *Key() const { return fKey; }
00099    TObject              *Value() const { return fValue; }
00100 
00101    ClassDef(TPair,0);
00102 };
00103 
00104 typedef TPair   TAssoc;     // for backward compatibility
00105 
00106 
00108 //                                                                      //
00109 // TMapIter                                                             //
00110 //                                                                      //
00111 // Iterator of a map.                                                   //
00112 //                                                                      //
00114 
00115 class TMapIter : public TIterator {
00116 
00117 private:
00118    const TMap       *fMap;         //map being iterated
00119    THashTableIter   *fCursor;      //current position in map
00120    Bool_t            fDirection;   //iteration direction
00121 
00122    TMapIter() : fMap(0), fCursor(0) { }
00123 
00124 public:
00125    TMapIter(const TMap *map, Bool_t dir = kIterForward);
00126    TMapIter(const TMapIter &iter);
00127    ~TMapIter();
00128    TIterator &operator=(const TIterator &rhs);
00129    TMapIter  &operator=(const TMapIter &rhs);
00130 
00131    const TCollection *GetCollection() const { return fMap; }
00132    TObject           *Next();
00133    void               Reset();
00134 
00135    ClassDef(TMapIter,0)  //Map iterator
00136 };
00137 
00138 #endif

Generated on Thu Dec 18 14:52:21 2003 for ROOT by doxygen1.2.16