Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
34class THashTableIter;
35class TMapIter;
36class TPair;
37class TBrowser;
38
39
40class TMap : public TCollection {
41
42friend class TMapIter;
43
44private:
45 THashTable *fTable; //Hash table used to store TPair's
46
47 TMap(const TMap& map) = delete;
48 TMap& operator=(const TMap& map) = delete;
49
50protected:
52
54
55public:
57
59 virtual ~TMap();
60 void Add(TObject *obj) override;
61 void Add(TObject *obj, Option_t *) override { Add(obj); };
62 void Add(TObject *key, TObject *value);
64 Int_t Capacity() const;
65 void Clear(Option_t *option="") override;
66 Int_t Collisions(const char *keyname) const;
67 Int_t Collisions(TObject *key) const;
68 void Delete(Option_t *option="") override;
69 void DeleteKeys() { Delete(); }
70 void DeleteValues();
71 void DeleteAll();
73 TObject *FindObject(const char *keyname) const override;
74 TObject *FindObject(const TObject *key) const override;
75 TObject **GetObjectRef(const TObject *obj) const override { return fTable->GetObjectRef(obj); }
76 const THashTable *GetTable() const { return fTable; }
77 TObject *GetValue(const char *keyname) const;
78 TObject *GetValue(const TObject *key) const;
80 TObject *operator()(const char *keyname) const { return GetValue(keyname); }
81 TObject *operator()(const TObject *key) const { return GetValue(key); }
82 TIterator *MakeIterator(Bool_t dir = kIterForward) const override;
84 TObject *Remove(TObject *key) override;
86 virtual void SetOwnerValue(Bool_t enable = kTRUE);
88 Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override;
89 Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) const override;
90
91 ClassDefOverride(TMap,3) //A (key,value) map
92};
93
94
95//////////////////////////////////////////////////////////////////////////
96// //
97// TPair //
98// //
99// Class used by TMap to store (key,value) pairs. //
100// //
101//////////////////////////////////////////////////////////////////////////
102
103class TPair : public TObject {
104
105private:
108
109 TPair& operator=(const TPair&) = delete;
110
111public:
113 TPair(const TPair &a) : TObject(), fKey(a.fKey), fValue(a.fValue) { }
114 virtual ~TPair();
115 Bool_t IsFolder() const override { return kTRUE;}
116 void Browse(TBrowser *b) override;
117 const char *GetName() const override { return fKey->GetName(); }
118 const char *GetTitle() const override { return fKey->GetTitle(); }
119 ULong_t Hash() const override { return fKey->Hash(); }
120 Bool_t IsEqual(const TObject *obj) const override { return fKey->IsEqual(obj); }
121 TObject *Key() const { return fKey; }
122 TObject *Value() const { return fValue; }
123 void SetValue(TObject *val) { fValue = val; }
124
125 ClassDefOverride(TPair,0); // Pair TObject*, TObject*
126};
127
128typedef TPair TAssoc; // for backward compatibility
129
130
131// Preventing warnings with -Weffc++ in GCC since it is a false positive for the TMapIter destructor.
132#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
133#pragma GCC diagnostic push
134#pragma GCC diagnostic ignored "-Weffc++"
135#endif
136
137//////////////////////////////////////////////////////////////////////////
138// //
139// TMapIter //
140// //
141// Iterator of a map. //
142// //
143//////////////////////////////////////////////////////////////////////////
144
145class TMapIter : public TIterator {
146
147private:
148 const TMap *fMap; //map being iterated
149 THashTableIter *fCursor; //current position in map
150 Bool_t fDirection; //iteration direction
151
152 TMapIter() : fMap(nullptr), fCursor(nullptr), fDirection(kIterForward) { }
153
154public:
155 using iterator_category = std::bidirectional_iterator_tag;
157 using difference_type = std::ptrdiff_t;
158 using pointer = TObject **;
159 using const_pointer = const TObject **;
160 using reference = const TObject *&;
161
162 TMapIter(const TMap *map, Bool_t dir = kIterForward);
163 TMapIter(const TMapIter &iter);
164 ~TMapIter();
165 TIterator &operator=(const TIterator &rhs) override;
167
168 const TCollection *GetCollection() const override { return fMap; }
169 TObject *Next() override;
170 void Reset() override;
171 Bool_t operator!=(const TIterator &aIter) const override;
172 Bool_t operator!=(const TMapIter &aIter) const;
173 TObject *operator*() const override;
174
175 ClassDefOverride(TMapIter,0) //Map iterator
176};
177
178#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
179#pragma GCC diagnostic pop
180#endif
181
182#endif
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define BIT(n)
Definition Rtypes.h:91
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
const Bool_t kIterForward
Definition TCollection.h:42
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
TPair TAssoc
Definition TMap.h:128
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Collection abstract base class.
Definition TCollection.h:65
@ kInitHashTableCapacity
Iterator of hash table.
Definition THashTable.h:115
THashTable implements a hash table to store TObject's.
Definition THashTable.h:35
TObject ** GetObjectRef(const TObject *obj) const override
Return address of pointer to obj.
Iterator abstract base class.
Definition TIterator.h:30
Iterator of map.
Definition TMap.h:145
std::ptrdiff_t difference_type
Definition TMap.h:157
std::bidirectional_iterator_tag iterator_category
Definition TMap.h:155
TObject * operator*() const override
Return pointer to current object (a TPair) or nullptr.
Definition TMap.cxx:581
TObject * Next() override
Returns the next key from a map.
Definition TMap.cxx:540
THashTableIter * fCursor
Definition TMap.h:149
~TMapIter()
Map iterator dtor.
Definition TMap.cxx:531
const TCollection * GetCollection() const override
Definition TMap.h:168
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
Definition TMap.cxx:561
const TMap * fMap
Definition TMap.h:148
void Reset() override
Reset the map iterator.
Definition TMap.cxx:553
Bool_t fDirection
Definition TMap.h:150
TMapIter()
Definition TMap.h:152
TIterator & operator=(const TIterator &rhs) override
Overridden assignment operator.
Definition TMap.cxx:496
TMap implements an associative array of (key,value) pairs using a THashTable for efficient retrieval ...
Definition TMap.h:40
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
TPair * RemoveEntry(TObject *key)
Remove (key,value) pair with key from the map.
Definition TMap.cxx:319
virtual ~TMap()
TMap dtor.
Definition TMap.cxx:43
void Delete(Option_t *option="") override
Remove all (key,value) pairs from the map AND delete the keys when they are allocated on the heap.
Definition TMap.cxx:133
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
THashTable * fTable
Definition TMap.h:45
void Add(TObject *obj) override
This function may not be used (but we need to provide it since it is a pure virtual in TCollection).
Definition TMap.cxx:53
virtual void SetOwnerKeyValue(Bool_t ownkeys=kTRUE, Bool_t ownvals=kTRUE)
Set ownership for keys and values.
Definition TMap.cxx:351
TObject * FindObject(const char *keyname) const override
Check if a (key,value) pair exists with keyname as name of the key.
Definition TMap.cxx:214
void PrintCollectionEntry(TObject *entry, Option_t *option, Int_t recurse) const override
Print the collection entry.
Definition TMap.cxx:265
Float_t AverageCollisions() const
Return the ratio of entries vs occupied slots.
Definition TMap.cxx:72
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
Write all objects in this map.
Definition TMap.cxx:431
TObject * Remove(TObject *key) override
Remove the (key,value) pair with key from the map.
Definition TMap.cxx:295
void DeleteKeys()
Definition TMap.h:69
TObject * operator()(const char *keyname) const
Definition TMap.h:80
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
Bool_t DeleteEntry(TObject *key)
Remove (key,value) pair with key from the map.
Definition TMap.cxx:189
void Add(TObject *obj, Option_t *) override
Definition TMap.h:61
void Clear(Option_t *option="") override
Remove all (key,value) pairs from the map.
Definition TMap.cxx:96
TObject ** GetObjectRef(const TObject *obj) const override
Definition TMap.h:75
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Create an iterator for TMap.
Definition TMap.cxx:257
EStatusBits
Definition TMap.h:51
@ kIsOwnerValue
Definition TMap.h:51
Bool_t IsOwnerValue() const
Definition TMap.h:79
virtual void SetOwnerValue(Bool_t enable=kTRUE)
Set whether this map is the owner (enable==true) of its values.
Definition TMap.cxx:340
Int_t Capacity() const
Return number of slots in the hashtable.
Definition TMap.cxx:81
TObject * GetValue(const char *keyname) const
Returns a pointer to the value associated with keyname as name of the key.
Definition TMap.cxx:235
TMap & operator=(const TMap &map)=delete
TMapIter Iterator_t
Definition TMap.h:56
TMap(const TMap &map)=delete
TObject * operator()(const TObject *key) const
Definition TMap.h:81
const THashTable * GetTable() const
Definition TMap.h:76
void Rehash(Int_t newCapacity, Bool_t checkObjValidity=kTRUE)
Rehash the underlaying THashTable (see THashTable::Rehash()).
Definition TMap.cxx:285
Mother of all ROOT objects.
Definition TObject.h:41
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:583
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:202
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:501
virtual ULong_t Hash() const
Return hash value for this object.
Definition TObject.cxx:533
Class used by TMap to store (key,value) pairs.
Definition TMap.h:103
void Browse(TBrowser *b) override
Browse the pair.
Definition TMap.cxx:452
const char * GetName() const override
Returns name of object.
Definition TMap.h:117
void SetValue(TObject *val)
Definition TMap.h:123
TObject * Value() const
Definition TMap.h:122
TObject * fValue
Definition TMap.h:107
TPair(const TPair &a)
Definition TMap.h:113
const char * GetTitle() const override
Returns title of object.
Definition TMap.h:118
TObject * fKey
Definition TMap.h:106
ULong_t Hash() const override
Return hash value for this object.
Definition TMap.h:119
virtual ~TPair()
TPair destructor.
Definition TMap.cxx:443
Bool_t IsFolder() const override
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects).
Definition TMap.h:115
TPair(TObject *key, TObject *value)
Definition TMap.h:112
TPair & operator=(const TPair &)=delete
TObject * Key() const
Definition TMap.h:121
Bool_t IsEqual(const TObject *obj) const override
Default equal comparison (objects are equal if they have the same address in memory).
Definition TMap.h:120