TMap.h

Go to the documentation of this file.
00001 // @(#)root/cont:$Id: TMap.h 34744 2010-08-07 06:16:36Z brun $
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 
00016 //////////////////////////////////////////////////////////////////////////
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 //                                                                      //
00026 //////////////////////////////////////////////////////////////////////////
00027 
00028 #ifndef ROOT_TCollection
00029 #include "TCollection.h"
00030 #endif
00031 #ifndef ROOT_THashTable
00032 #include "THashTable.h"
00033 #endif
00034 
00035 #include <iterator>
00036 
00037 
00038 class THashTableIter;
00039 class TMapIter;
00040 class TPair;
00041 class TBrowser;
00042 
00043 
00044 class TMap : public TCollection {
00045 
00046 friend class  TMapIter;
00047 
00048 private:
00049    THashTable   *fTable;     //Hash table used to store TPair's
00050 
00051    TMap(const TMap& map);             // not implemented
00052    TMap& operator=(const TMap& map);  // not implemented
00053 
00054 protected:
00055    enum { kIsOwnerValue = BIT(15) };
00056 
00057    virtual void        PrintCollectionEntry(TObject* entry, Option_t* option, Int_t recurse) const;
00058 
00059 public:
00060    typedef TMapIter Iterator_t;
00061 
00062    TMap(Int_t capacity = TCollection::kInitHashTableCapacity, Int_t rehash = 0);
00063    virtual           ~TMap();
00064    void              Add(TObject *obj);
00065    void              Add(TObject *key, TObject *value);
00066    Float_t           AverageCollisions() const;
00067    Int_t             Capacity() const;
00068    void              Clear(Option_t *option="");
00069    Int_t             Collisions(const char *keyname) const;
00070    Int_t             Collisions(TObject *key) const;
00071    void              Delete(Option_t *option="");
00072    void              DeleteKeys() { Delete(); }
00073    void              DeleteValues();
00074    void              DeleteAll();
00075    Bool_t            DeleteEntry(TObject *key);
00076    TObject          *FindObject(const char *keyname) const;
00077    TObject          *FindObject(const TObject *key) const;
00078    TObject         **GetObjectRef(const TObject *obj) const { return fTable->GetObjectRef(obj); }
00079    const THashTable *GetTable() const { return fTable; }
00080    TObject          *GetValue(const char *keyname) const;
00081    TObject          *GetValue(const TObject *key) const;
00082    Bool_t            IsOwnerValue() const { return TestBit(kIsOwnerValue); }
00083    TObject          *operator()(const char *keyname) const { return GetValue(keyname); }
00084    TObject          *operator()(const TObject *key) const { return GetValue(key); }
00085    TIterator        *MakeIterator(Bool_t dir = kIterForward) const;
00086    void              Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE);
00087    TObject          *Remove(TObject *key);
00088    TPair            *RemoveEntry(TObject *key);
00089    virtual void      SetOwnerValue(Bool_t enable = kTRUE);
00090    virtual void      SetOwnerKeyValue(Bool_t ownkeys = kTRUE, Bool_t ownvals = kTRUE);
00091 
00092    ClassDef(TMap,3)  //A (key,value) map
00093 };
00094 
00095 
00096 //////////////////////////////////////////////////////////////////////////
00097 //                                                                      //
00098 // TPair                                                                //
00099 //                                                                      //
00100 // Class used by TMap to store (key,value) pairs.                       //
00101 //                                                                      //
00102 //////////////////////////////////////////////////////////////////////////
00103 
00104 class TPair : public TObject {
00105 
00106 private:
00107    TObject  *fKey;
00108    TObject  *fValue;
00109 
00110    TPair& operator=(const TPair&); // Not implemented
00111 
00112 public:
00113    TPair(TObject *key, TObject *value) : fKey(key), fValue(value) { }
00114    TPair(const TPair &a) : TObject(), fKey(a.fKey), fValue(a.fValue) { }
00115    virtual               ~TPair() { }
00116    Bool_t                IsFolder() const { return kTRUE;}
00117    virtual void          Browse(TBrowser *b);
00118    const char           *GetName() const { return fKey->GetName(); }
00119    const char           *GetTitle() const { return fKey->GetTitle(); }
00120    ULong_t               Hash() const { return fKey->Hash(); }
00121    Bool_t                IsEqual(const TObject *obj) const { return fKey->IsEqual(obj); }
00122    TObject              *Key() const { return fKey; }
00123    TObject              *Value() const { return fValue; }
00124    void                  SetValue(TObject *val) { fValue = val; }
00125 
00126    ClassDef(TPair,0); // Pair TObject*, TObject*
00127 };
00128 
00129 typedef TPair   TAssoc;     // for backward compatibility
00130 
00131 
00132 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TMapIter destructor.
00133 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
00134 #pragma GCC diagnostic push
00135 #pragma GCC diagnostic ignored "-Weffc++"
00136 #endif
00137 
00138 //////////////////////////////////////////////////////////////////////////
00139 //                                                                      //
00140 // TMapIter                                                             //
00141 //                                                                      //
00142 // Iterator of a map.                                                   //
00143 //                                                                      //
00144 //////////////////////////////////////////////////////////////////////////
00145 
00146 class TMapIter : public TIterator,
00147                  public std::iterator<std::bidirectional_iterator_tag,
00148                                       TObject*, std::ptrdiff_t,
00149                                       const TObject**, const TObject*&> {
00150 
00151 private:
00152    const TMap       *fMap;         //map being iterated
00153    THashTableIter   *fCursor;      //current position in map
00154    Bool_t            fDirection;   //iteration direction
00155 
00156    TMapIter() : fMap(0), fCursor(0), fDirection(kIterForward) { }
00157 
00158 public:
00159    TMapIter(const TMap *map, Bool_t dir = kIterForward);
00160    TMapIter(const TMapIter &iter);
00161    ~TMapIter();
00162    TIterator &operator=(const TIterator &rhs);
00163    TMapIter  &operator=(const TMapIter &rhs);
00164 
00165    const TCollection *GetCollection() const { return fMap; }
00166    TObject           *Next();
00167    void               Reset();
00168    bool               operator!=(const TIterator &aIter) const;
00169    bool               operator!=(const TMapIter &aIter) const;
00170    TObject           *operator*() const;
00171 
00172    ClassDef(TMapIter,0)  //Map iterator
00173 };
00174 
00175 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
00176 #pragma GCC diagnostic pop
00177 #endif
00178 
00179 #endif

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