Logo ROOT   6.07/09
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 #ifndef ROOT_TCollection
29 #include "TCollection.h"
30 #endif
31 #ifndef ROOT_THashTable
32 #include "THashTable.h"
33 #endif
34 
35 #include <iterator>
36 
37 
38 class THashTableIter;
39 class TMapIter;
40 class TPair;
41 class TBrowser;
42 
43 
44 class TMap : public TCollection {
45 
46 friend class TMapIter;
47 
48 private:
49  THashTable *fTable; //Hash table used to store TPair's
50 
51  TMap(const TMap& map); // not implemented
52  TMap& operator=(const TMap& map); // not implemented
53 
54 protected:
55  enum { kIsOwnerValue = BIT(15) };
56 
57  virtual void PrintCollectionEntry(TObject* entry, Option_t* option, Int_t recurse) const;
58 
59 public:
61 
62  TMap(Int_t capacity = TCollection::kInitHashTableCapacity, Int_t rehash = 0);
63  virtual ~TMap();
64  void Add(TObject *obj);
65  void Add(TObject *key, TObject *value);
66  Float_t AverageCollisions() const;
67  Int_t Capacity() const;
68  void Clear(Option_t *option="");
69  Int_t Collisions(const char *keyname) const;
70  Int_t Collisions(TObject *key) const;
71  void Delete(Option_t *option="");
72  void DeleteKeys() { Delete(); }
73  void DeleteValues();
74  void DeleteAll();
76  TObject *FindObject(const char *keyname) const;
77  TObject *FindObject(const TObject *key) const;
78  TObject **GetObjectRef(const TObject *obj) const { return fTable->GetObjectRef(obj); }
79  const THashTable *GetTable() const { return fTable; }
80  TObject *GetValue(const char *keyname) const;
81  TObject *GetValue(const TObject *key) const;
83  TObject *operator()(const char *keyname) const { return GetValue(keyname); }
84  TObject *operator()(const TObject *key) const { return GetValue(key); }
86  void Rehash(Int_t newCapacity, Bool_t checkObjValidity = kTRUE);
87  TObject *Remove(TObject *key);
88  TPair *RemoveEntry(TObject *key);
89  virtual void SetOwnerValue(Bool_t enable = kTRUE);
90  virtual void SetOwnerKeyValue(Bool_t ownkeys = kTRUE, Bool_t ownvals = kTRUE);
91  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0);
92  virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0) const;
93 
94  ClassDef(TMap,3) //A (key,value) map
95 };
96 
97 
98 //////////////////////////////////////////////////////////////////////////
99 // //
100 // TPair //
101 // //
102 // Class used by TMap to store (key,value) pairs. //
103 // //
104 //////////////////////////////////////////////////////////////////////////
105 
106 class TPair : public TObject {
107 
108 private:
111 
112  TPair& operator=(const TPair&); // Not implemented
113 
114 public:
115  TPair(TObject *key, TObject *value) : fKey(key), fValue(value) { }
116  TPair(const TPair &a) : TObject(), fKey(a.fKey), fValue(a.fValue) { }
117  virtual ~TPair() { }
118  Bool_t IsFolder() const { return kTRUE;}
119  virtual void Browse(TBrowser *b);
120  const char *GetName() const { return fKey->GetName(); }
121  const char *GetTitle() const { return fKey->GetTitle(); }
122  ULong_t Hash() const { return fKey->Hash(); }
123  Bool_t IsEqual(const TObject *obj) const { return fKey->IsEqual(obj); }
124  TObject *Key() const { return fKey; }
125  TObject *Value() const { return fValue; }
126  void SetValue(TObject *val) { fValue = val; }
127 
128  ClassDef(TPair,0); // Pair TObject*, TObject*
129 };
130 
131 typedef TPair TAssoc; // for backward compatibility
132 
133 
134 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TMapIter destructor.
135 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
136 #pragma GCC diagnostic push
137 #pragma GCC diagnostic ignored "-Weffc++"
138 #endif
139 
140 //////////////////////////////////////////////////////////////////////////
141 // //
142 // TMapIter //
143 // //
144 // Iterator of a map. //
145 // //
146 //////////////////////////////////////////////////////////////////////////
147 
148 class TMapIter : public TIterator,
149  public std::iterator<std::bidirectional_iterator_tag,
150  TObject*, std::ptrdiff_t,
151  const TObject**, const TObject*&> {
152 
153 private:
154  const TMap *fMap; //map being iterated
155  THashTableIter *fCursor; //current position in map
156  Bool_t fDirection; //iteration direction
157 
158  TMapIter() : fMap(0), fCursor(0), fDirection(kIterForward) { }
159 
160 public:
161  TMapIter(const TMap *map, Bool_t dir = kIterForward);
162  TMapIter(const TMapIter &iter);
163  ~TMapIter();
164  TIterator &operator=(const TIterator &rhs);
165  TMapIter &operator=(const TMapIter &rhs);
166 
167  const TCollection *GetCollection() const { return fMap; }
168  TObject *Next();
169  void Reset();
170  Bool_t operator!=(const TIterator &aIter) const;
171  Bool_t operator!=(const TMapIter &aIter) const;
172  TObject *operator*() const;
173 
174  ClassDef(TMapIter,0) //Map iterator
175 };
176 
177 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
178 #pragma GCC diagnostic pop
179 #endif
180 
181 #endif
TPair(TObject *key, TObject *value)
Definition: TMap.h:115
TPair * RemoveEntry(TObject *key)
Remove (key,value) pair with key from the map.
Definition: TMap.cxx:319
TObject * fValue
Definition: TMap.h:110
float Float_t
Definition: RtypesCore.h:53
virtual ULong_t Hash() const
Return hash value for this object.
Definition: TObject.cxx:478
const char Option_t
Definition: RtypesCore.h:62
TPair(const TPair &a)
Definition: TMap.h:116
ULong_t Hash() const
Return hash value for this object.
Definition: TMap.h:122
TObject * Key() const
Definition: TMap.h:124
TObject * operator()(const TObject *key) const
Definition: TMap.h:84
const THashTable * GetTable() const
Definition: TMap.h:79
TMapIter()
Definition: TMap.h:158
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
TObject ** GetObjectRef(const TObject *obj) const
Definition: TMap.h:78
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
const char * GetTitle() const
Returns title of object.
Definition: TMap.h:121
THashTableIter * fCursor
Definition: TMap.h:155
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 Bool_t IsEqual(const TObject *obj) const
Default equal comparison (objects are equal if they have the same address in memory).
Definition: TObject.cxx:528
virtual ~TMap()
TMap dtor.
Definition: TMap.cxx:43
Iterator abstract base class.
Definition: TIterator.h:32
TMapIter Iterator_t
Definition: TMap.h:60
const TMap * fMap
Definition: TMap.h:154
Iterator of hash table.
Definition: THashTable.h:106
TObject * operator()(const char *keyname) const
Definition: TMap.h:83
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:39
#define ClassDef(name, id)
Definition: Rtypes.h:254
Bool_t fDirection
Definition: TMap.h:156
Bool_t IsEqual(const TObject *obj) const
Default equal comparison (objects are equal if they have the same address in memory).
Definition: TMap.h:123
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
TObject * GetValue(const char *keyname) const
Returns a pointer to the value associated with keyname as name of the key.
Definition: TMap.cxx:235
const Bool_t kIterForward
Definition: TCollection.h:43
TPair TAssoc
Definition: TMap.h:131
Int_t Capacity() const
Return number of slots in the hashtable.
Definition: TMap.cxx:81
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Definition: TDatime.h:103
virtual ~TPair()
Definition: TMap.h:117
Float_t AverageCollisions() const
Return the ratio of entries vs occupied slots.
Definition: TMap.cxx:72
TObject * FindObject(const char *keyname) const
Check if a (key,value) pair exists with keyname as name of the key.
Definition: TMap.cxx:214
friend class TMapIter
Definition: TMap.h:46
Iterator of map.
Definition: TMap.h:148
void SetValue(TObject *val)
Definition: TMap.h:126
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:72
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
virtual void SetOwnerKeyValue(Bool_t ownkeys=kTRUE, Bool_t ownvals=kTRUE)
Set ownership for keys and values.
Definition: TMap.cxx:351
TObject * Value() const
Definition: TMap.h:125
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
PyObject * fValue
TTime operator*(const TTime &t1, const TTime &t2)
Definition: TTime.h:87
TIterator * MakeIterator(Bool_t dir=kIterForward) const
Create an iterator for TMap.
Definition: TMap.cxx:257
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:48
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:159
Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: TMap.h:118
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:106
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:416
TMap implements an associative array of (key,value) pairs using a THashTable for efficient retrieval ...
Definition: TMap.h:44
unsigned long ULong_t
Definition: RtypesCore.h:51
TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition: THashTable.cxx:253
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
THashTable * fTable
Definition: TMap.h:49
virtual void PrintCollectionEntry(TObject *entry, Option_t *option, Int_t recurse) const
Print the collection entry.
Definition: TMap.cxx:265
#define BIT(n)
Definition: Rtypes.h:120
Mother of all ROOT objects.
Definition: TObject.h:44
Bool_t IsOwnerValue() const
Definition: TMap.h:82
const TCollection * GetCollection() const
Definition: TMap.h:167
const char * GetName() const
Returns name of object.
Definition: TMap.h:120
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 * fKey
Definition: TMap.h:109
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:460
char name[80]
Definition: TGX11.cxx:109
void Clear(Option_t *option="")
Remove all (key,value) pairs from the map.
Definition: TMap.cxx:96
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
TMap & operator=(const TMap &map)