Logo ROOT   6.14/05
Reference Guide
TMap.h
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 12/11/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_TMap
13 #define ROOT_TMap
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TMap //
19 // //
20 // TMap implements an associative array of (key,value) pairs using a //
21 // hash table for efficient retrieval (therefore TMap does not conserve //
22 // the order of the entries). The hash value is calculated //
23 // using the value returned by the keys Hash() function. Both key and //
24 // value need to inherit from TObject. //
25 // //
26 //////////////////////////////////////////////////////////////////////////
27 
28 #include "TCollection.h"
29 #include "THashTable.h"
30 
31 #include <iterator>
32 
33 
34 class THashTableIter;
35 class TMapIter;
36 class TPair;
37 class TBrowser;
38 
39 
40 class TMap : public TCollection {
41 
42 friend class TMapIter;
43 
44 private:
45  THashTable *fTable; //Hash table used to store TPair's
46 
47  TMap(const TMap& map); // not implemented
48  TMap& operator=(const TMap& map); // not implemented
49 
50 protected:
51  enum EStatusBits { kIsOwnerValue = BIT(15) };
52 
53  virtual void PrintCollectionEntry(TObject* entry, Option_t* option, Int_t recurse) const;
54 
55 public:
57 
58  TMap(Int_t capacity = TCollection::kInitHashTableCapacity, Int_t rehash = 0);
59  virtual ~TMap();
60  void Add(TObject *obj);
61  void Add(TObject *key, TObject *value);
62  Float_t AverageCollisions() const;
63  Int_t Capacity() const;
64  void Clear(Option_t *option="");
65  Int_t Collisions(const char *keyname) const;
66  Int_t Collisions(TObject *key) const;
67  void Delete(Option_t *option="");
68  void DeleteKeys() { Delete(); }
69  void DeleteValues();
70  void DeleteAll();
72  TObject *FindObject(const char *keyname) const;
73  TObject *FindObject(const TObject *key) const;
74  TObject **GetObjectRef(const TObject *obj) const { return fTable->GetObjectRef(obj); }
75  const THashTable *GetTable() const { return fTable; }
76  TObject *GetValue(const char *keyname) const;
77  TObject *GetValue(const TObject *key) const;
79  TObject *operator()(const char *keyname) const { return GetValue(keyname); }
80  TObject *operator()(const TObject *key) const { return GetValue(key); }
82  void Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE);
83  TObject *Remove(TObject *key);
84  TPair *RemoveEntry(TObject *key);
85  virtual void SetOwnerValue(Bool_t enable = kTRUE);
86  virtual void SetOwnerKeyValue(Bool_t ownkeys = kTRUE, Bool_t ownvals = kTRUE);
87  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0);
88  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0) const;
89 
90  ClassDef(TMap,3) //A (key,value) map
91 };
92 
93 
94 //////////////////////////////////////////////////////////////////////////
95 // //
96 // TPair //
97 // //
98 // Class used by TMap to store (key,value) pairs. //
99 // //
100 //////////////////////////////////////////////////////////////////////////
101 
102 class TPair : public TObject {
103 
104 private:
107 
108  TPair& operator=(const TPair&); // Not implemented
109 
110 public:
111  TPair(TObject *key, TObject *value) : fKey(key), fValue(value) { }
112  TPair(const TPair &a) : TObject(), fKey(a.fKey), fValue(a.fValue) { }
113  virtual ~TPair();
114  Bool_t IsFolder() const { return kTRUE;}
115  virtual void Browse(TBrowser *b);
116  const char *GetName() const { return fKey->GetName(); }
117  const char *GetTitle() const { return fKey->GetTitle(); }
118  ULong_t Hash() const { return fKey->Hash(); }
119  Bool_t IsEqual(const TObject *obj) const { return fKey->IsEqual(obj); }
120  TObject *Key() const { return fKey; }
121  TObject *Value() const { return fValue; }
122  void SetValue(TObject *val) { fValue = val; }
123 
124  ClassDef(TPair,0); // Pair TObject*, TObject*
125 };
126 
127 typedef TPair TAssoc; // for backward compatibility
128 
129 
130 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TMapIter destructor.
131 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
132 #pragma GCC diagnostic push
133 #pragma GCC diagnostic ignored "-Weffc++"
134 #endif
135 
136 //////////////////////////////////////////////////////////////////////////
137 // //
138 // TMapIter //
139 // //
140 // Iterator of a map. //
141 // //
142 //////////////////////////////////////////////////////////////////////////
143 
144 class TMapIter : public TIterator,
145  public std::iterator<std::bidirectional_iterator_tag,
146  TObject*, std::ptrdiff_t,
147  const TObject**, const TObject*&> {
148 
149 private:
150  const TMap *fMap; //map being iterated
151  THashTableIter *fCursor; //current position in map
152  Bool_t fDirection; //iteration direction
153 
154  TMapIter() : fMap(0), fCursor(0), fDirection(kIterForward) { }
155 
156 public:
157  TMapIter(const TMap *map, Bool_t dir = kIterForward);
158  TMapIter(const TMapIter &iter);
159  ~TMapIter();
160  TIterator &operator=(const TIterator &rhs);
161  TMapIter &operator=(const TMapIter &rhs);
162 
163  const TCollection *GetCollection() const { return fMap; }
164  TObject *Next();
165  void Reset();
166  Bool_t operator!=(const TIterator &aIter) const;
167  Bool_t operator!=(const TMapIter &aIter) const;
168  TObject *operator*() const;
169 
170  ClassDef(TMapIter,0) //Map iterator
171 };
172 
173 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
174 #pragma GCC diagnostic pop
175 #endif
176 
177 #endif
Int_t Capacity() const
Return number of slots in the hashtable.
Definition: TMap.cxx:81
TPair(TObject *key, TObject *value)
Definition: TMap.h:111
const char * GetName() const
Returns name of object.
Definition: TMap.h:116
virtual ULong_t Hash() const
Return hash value for this object.
Definition: TObject.cxx:433
TPair * RemoveEntry(TObject *key)
Remove (key,value) pair with key from the map.
Definition: TMap.cxx:319
TObject * fValue
Definition: TMap.h:106
TObject ** GetObjectRef(const TObject *obj) const
Definition: TMap.h:74
float Float_t
Definition: RtypesCore.h:53
TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition: THashTable.cxx:293
const char Option_t
Definition: RtypesCore.h:62
TPair(const TPair &a)
Definition: TMap.h:112
Bool_t IsEqual(const TObject *obj) const
Default equal comparison (objects are equal if they have the same address in memory).
Definition: TMap.h:119
#define BIT(n)
Definition: Rtypes.h:78
TMapIter()
Definition: TMap.h:154
void Add(TObject *obj)
This function may not be used (but we need to provide it since it is a pure virtual in TCollection)...
Definition: TMap.cxx:53
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
THashTableIter * fCursor
Definition: TMap.h:151
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this map.
Definition: TMap.cxx:431
virtual ~TMap()
TMap dtor.
Definition: TMap.cxx:43
Iterator abstract base class.
Definition: TIterator.h:30
TMapIter Iterator_t
Definition: TMap.h:56
const TMap * fMap
Definition: TMap.h:150
Iterator of hash table.
Definition: THashTable.h:113
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:35
#define ClassDef(name, id)
Definition: Rtypes.h:320
Bool_t fDirection
Definition: TMap.h:152
Bool_t IsOwnerValue() const
Definition: TMap.h:78
void DeleteAll()
Remove all (key,value) pairs from the map AND delete the keys AND values when they are allocated on t...
Definition: TMap.cxx:167
Float_t AverageCollisions() const
Return the ratio of entries vs occupied slots.
Definition: TMap.cxx:72
const Bool_t kIterForward
Definition: TCollection.h:40
const THashTable * GetTable() const
Definition: TMap.h:75
const TCollection * GetCollection() const
Definition: TMap.h:163
TPair TAssoc
Definition: TMap.h:127
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Definition: TDatime.h:104
TObject * Value() const
Definition: TMap.h:121
virtual Bool_t IsEqual(const TObject *obj) const
Default equal comparison (objects are equal if they have the same address in memory).
Definition: TObject.cxx:483
friend class TMapIter
Definition: TMap.h:42
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Create an iterator for TMap.
Definition: TMap.cxx:257
Iterator of map.
Definition: TMap.h:144
void SetValue(TObject *val)
Definition: TMap.h:122
virtual void SetOwnerValue(Bool_t enable=kTRUE)
Set whether this map is the owner (enable==true) of its values.
Definition: TMap.cxx:340
void DeleteKeys()
Definition: TMap.h:68
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
virtual void SetOwnerKeyValue(Bool_t ownkeys=kTRUE, Bool_t ownvals=kTRUE)
Set ownership for keys and values.
Definition: TMap.cxx:351
void Delete(Option_t *option="")
Remove all (key,value) pairs from the map AND delete the keys when they are allocated on the heap...
Definition: TMap.cxx:133
TMap(const TMap &map)
TObject * Remove(TObject *key)
Remove the (key,value) pair with key from the map.
Definition: TMap.cxx:295
auto * a
Definition: textangle.C:12
TObject * Key() const
Definition: TMap.h:120
PyObject * fValue
TTime operator*(const TTime &t1, const TTime &t2)
Definition: TTime.h:85
Bool_t DeleteEntry(TObject *key)
Remove (key,value) pair with key from the map.
Definition: TMap.cxx:189
Collection abstract base class.
Definition: TCollection.h:63
TObject * operator()(const TObject *key) const
Definition: TMap.h:80
void Reset(Detail::TBranchProxy *x)
void DeleteValues()
Remove all (key,value) pairs from the map AND delete the values when they are allocated on the heap...
Definition: TMap.cxx:150
void Rehash(Int_t newCapacity, Bool_t checkObjValidity=kTRUE)
Rehash the underlaying THashTable (see THashTable::Rehash()).
Definition: TMap.cxx:285
Class used by TMap to store (key,value) pairs.
Definition: TMap.h:102
EStatusBits
Definition: TObject.h:57
TMap implements an associative array of (key,value) pairs using a THashTable for efficient retrieval ...
Definition: TMap.h:40
unsigned long ULong_t
Definition: RtypesCore.h:51
ULong_t Hash() const
Return hash value for this object.
Definition: TMap.h:118
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
THashTable * fTable
Definition: TMap.h:45
Int_t Collisions(const char *keyname) const
Returns the number of collisions for a key with a certain name (i.e.
Definition: TMap.cxx:115
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:401
TObject * FindObject(const char *keyname) const
Check if a (key,value) pair exists with keyname as name of the key.
Definition: TMap.cxx:214
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
TObject * operator()(const char *keyname) const
Definition: TMap.h:79
const char * GetTitle() const
Returns title of object.
Definition: TMap.h:117
TObject * GetValue(const char *keyname) const
Returns a pointer to the value associated with keyname as name of the key.
Definition: TMap.cxx:235
Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: TMap.h:114
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
TObject * fKey
Definition: TMap.h:105
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual void PrintCollectionEntry(TObject *entry, Option_t *option, Int_t recurse) const
Print the collection entry.
Definition: TMap.cxx:265
char name[80]
Definition: TGX11.cxx:109
void Clear(Option_t *option="")
Remove all (key,value) pairs from the map.
Definition: TMap.cxx:96
TMap & operator=(const TMap &map)