33 TMap::TMap(Int_t capacity, Int_t rehashlevel)
36 fTable =
new THashTable(capacity, rehashlevel);
53 void TMap::Add(TObject *)
55 MayNotUse(
"Add(TObject *obj)");
61 void TMap::Add(TObject *key, TObject *value)
63 if (IsArgNull(
"Add", key))
return;
65 fTable->Add(
new TPair(key, value));
72 Float_t TMap::AverageCollisions()
const
74 return fTable->AverageCollisions();
81 Int_t TMap::Capacity()
const
83 return fTable->Capacity();
96 void TMap::Clear(Option_t *option)
98 if (IsOwner() && IsOwnerValue())
102 else if (IsOwnerValue())
105 fTable->Delete(option);
115 Int_t TMap::Collisions(
const char *keyname)
const
117 return fTable->Collisions(keyname);
124 Int_t TMap::Collisions(TObject *key)
const
126 return fTable->Collisions(key);
133 void TMap::Delete(Option_t *option)
138 while ((a = (TPair *)next()))
139 if (a->Key() && a->Key()->IsOnHeap())
140 TCollection::GarbageCollect(a->Key());
142 fTable->Delete(option);
150 void TMap::DeleteValues()
155 while ((a = (TPair *)next()))
156 if (a->Value() && a->Value()->IsOnHeap())
157 TCollection::GarbageCollect(a->Value());
167 void TMap::DeleteAll()
172 while ((a = (TPair *)next())) {
173 if (a->Key() && a->Key()->IsOnHeap())
174 TCollection::GarbageCollect(a->Key());
175 if (a->Value() && a->Value()->IsOnHeap())
176 TCollection::GarbageCollect(a->Value());
189 Bool_t TMap::DeleteEntry(TObject *key)
191 if (!key)
return kFALSE;
194 if ((a = (TPair *)fTable->FindObject(key))) {
195 if (fTable->Remove(key)) {
196 if (IsOwner() && a->Key() && a->Key()->IsOnHeap())
197 TCollection::GarbageCollect(a->Key());
198 if (IsOwnerValue() && a->Value() && a->Value()->IsOnHeap())
199 TCollection::GarbageCollect(a->Value());
214 TObject *TMap::FindObject(
const char *keyname)
const
216 return fTable->FindObject(keyname);
225 TObject *TMap::FindObject(
const TObject *key)
const
227 if (IsArgNull(
"FindObject", key))
return 0;
229 return fTable->FindObject(key);
235 TObject *TMap::GetValue(
const char *keyname)
const
237 TPair *a = (TPair *)fTable->FindObject(keyname);
238 if (a)
return a->Value();
245 TObject *TMap::GetValue(
const TObject *key)
const
247 if (IsArgNull(
"GetValue", key))
return 0;
249 TPair *a = (TPair *)fTable->FindObject(key);
250 if (a)
return a->Value();
257 TIterator *TMap::MakeIterator(Bool_t dir)
const
259 return new TMapIter(
this, dir);
265 void TMap::PrintCollectionEntry(TObject* entry, Option_t* option, Int_t recurse)
const
267 TObject* val = GetValue(entry);
269 TROOT::IndentLevel();
272 TROOT::IndentLevel();
274 TCollection* coll =
dynamic_cast<TCollection*
>(val);
276 coll->Print(option, recurse);
285 void TMap::Rehash(Int_t newCapacity, Bool_t checkObjValidity)
287 fTable->Rehash(newCapacity, checkObjValidity);
295 TObject *TMap::Remove(TObject *key)
300 if ((a = (TPair *)fTable->FindObject(key))) {
301 if (fTable->Remove(key)) {
302 if (IsOwnerValue() && a->Value() && a->Value()->IsOnHeap())
303 TCollection::GarbageCollect(a->Value());
304 TObject *kobj = a->Key();
319 TPair *TMap::RemoveEntry(TObject *key)
324 if ((a = (TPair *)fTable->FindObject(key))) {
325 if (fTable->Remove(key)) {
340 void TMap::SetOwnerValue(Bool_t enable)
343 SetBit(kIsOwnerValue);
345 ResetBit(kIsOwnerValue);
351 void TMap::SetOwnerKeyValue(Bool_t ownkeys, Bool_t ownvals)
354 SetOwnerValue(ownvals);
360 void TMap::Streamer(TBuffer &b)
369 Version_t v = b.ReadVersion(&R__s, &R__c);
371 TObject::Streamer(b);
375 for (Int_t i = 0; i < nobjects; i++) {
378 if (obj) Add(obj, value);
380 b.CheckByteCount(R__s, R__c,TMap::IsA());
382 R__c = b.WriteVersion(TMap::IsA(), kTRUE);
383 TObject::Streamer(b);
388 while ((a = (TPair*) next())) {
392 b.SetByteCount(R__c, kTRUE);
404 Int_t TMap::Write(
const char *name, Int_t option, Int_t bsize)
const
406 if ((option & kSingleKey)) {
407 return TObject::Write(name, option, bsize);
409 option &= ~kSingleKey;
413 while ((a = (TPair*) next())) {
415 nbytes += a->Key()->Write(name, option, bsize);
417 nbytes += a->Value()->Write(name, option, bsize);
431 Int_t TMap::Write(
const char *name, Int_t option, Int_t bsize)
433 return ((
const TMap*)
this)->Write(name,option,bsize);
446 ROOT::CallRecursiveRemoveIfNeeded(*
this);
452 void TPair::Browse(TBrowser *b)
455 if (fKey) b->Add(fKey);
456 if (fValue) b->Add(fValue);
458 if (fKey) fKey->Browse(b);
459 if (fValue) fValue->Browse(b);
472 TMapIter::TMapIter(
const TMap *m, Bool_t dir)
482 TMapIter::TMapIter(
const TMapIter &iter) : TIterator(iter)
485 fDirection = iter.fDirection;
488 fCursor = (THashTableIter *)iter.fCursor->GetCollection()->MakeIterator();
490 fCursor->operator=(*iter.fCursor);
497 TIterator &TMapIter::operator=(
const TIterator &rhs)
499 if (
this != &rhs && rhs.IsA() == TMapIter::Class()) {
500 const TMapIter &rhs1 = (
const TMapIter &)rhs;
502 fDirection = rhs1.fDirection;
504 fCursor = (THashTableIter *)rhs1.fCursor->GetCollection()->MakeIterator();
506 fCursor->operator=(*rhs1.fCursor);
515 TMapIter &TMapIter::operator=(
const TMapIter &rhs)
519 fDirection = rhs.fDirection;
521 fCursor = (THashTableIter *)rhs.fCursor->GetCollection()->MakeIterator();
523 fCursor->operator=(*rhs.fCursor);
532 TMapIter::~TMapIter()
541 TObject *TMapIter::Next()
544 fCursor =
new THashTableIter(fMap->fTable, fDirection);
546 TPair *a = (TPair *)fCursor->Next();
547 if (a)
return a->Key();
554 void TMapIter::Reset()
562 Bool_t TMapIter::operator!=(
const TIterator &aIter)
const
564 if (aIter.IsA() == TMapIter::Class()) {
565 const TMapIter &iter(dynamic_cast<const TMapIter &>(aIter));
566 return (fCursor->operator*() != iter.fCursor->operator*());
574 Bool_t TMapIter::operator!=(
const TMapIter &aIter)
const
576 return (fCursor->operator*() != aIter.fCursor->operator*());
582 TObject *TMapIter::operator*()
const
584 return (fCursor ? fCursor->operator*() :
nullptr);