| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #pragma once
- //////////////////////////////////////////////////////////////////////////
- // Template implementation of dictionary based on .NET BCL implementation.
- //
- // Buckets and entries are allocated as contiguous arrays to maintain good locality of reference.
- //
- // COLLISION STRATEGY
- // This dictionary uses a chaining collision resolution strategy. Chains are implemented using indexes to the 'buckets' array.
- //
- // STORAGE (TAllocator)
- // This dictionary works for both arena and recycler based allocation using TAllocator template parameter.
- // It supports storing of both value and pointer types. Using template specialization, value types (built-in fundamental
- // types and structs) are stored as leaf nodes by default.
- //
- // INITIAL SIZE and BUCKET MAPPING (SizePolicy)
- // This can be specified using TSizePolicy template parameter. There are 2 implementations:
- // - PrimeSizePolicy (Better distribution): Initial size is a prime number. Mapping to bucket is done using modulus operation (costlier).
- // - PowerOf2SizePolicy (faster): Initial size is a power of 2. Mapping to bucket is done by a fast truncating the MSB bits up to the size of the table.
- //
- // COMPARISONS AND HASHCODE (Comparer)
- // Enables custom comparisons for TKey and TValue. For example, for strings we use string comparison instead of comparing pointers.
- //
- #if PROFILE_DICTIONARY
- #include "DictionaryStats.h"
- #endif
- namespace Js
- {
- template <class TDictionary>
- class RemoteDictionary;
- }
- namespace JsDiag
- {
- template <class TDictionary>
- struct RemoteDictionary;
- }
- namespace JsUtil
- {
- class NoResizeLock
- {
- public:
- void BeginResize() {}
- void EndResize() {}
- };
- class AsymetricResizeLock
- {
- public:
- void BeginResize() { cs.Enter(); }
- void EndResize() { cs.Leave(); }
- void LockResize() { cs.Enter(); }
- void UnlockResize() { cs.Leave(); }
- private:
- CriticalSection cs;
- };
- template <class TKey, class TValue> class SimpleDictionaryEntry;
- template <
- class TKey,
- class TValue,
- class TAllocator,
- class SizePolicy = PowerOf2SizePolicy,
- template <typename ValueOrKey> class Comparer = DefaultComparer,
- template <typename K, typename V> class Entry = SimpleDictionaryEntry,
- typename Lock = NoResizeLock
- >
- class BaseDictionary : protected Lock
- {
- public:
- typedef TKey KeyType;
- typedef TValue ValueType;
- typedef typename AllocatorInfo<TAllocator, TValue>::AllocatorType AllocatorType;
- typedef SizePolicy CurrentSizePolicy;
- typedef Entry<
- Field(TKey, TAllocator),
- Field(TValue, TAllocator)> EntryType;
- template<class TDictionary> class EntryIterator;
- template<class TDictionary> class BucketEntryIterator;
- protected:
- typedef typename AllocatorInfo<TAllocator, TValue>::AllocatorFunc EntryAllocatorFuncType;
- friend class Js::RemoteDictionary<BaseDictionary>;
- template <typename ValueOrKey> struct ComparerType { typedef Comparer<ValueOrKey> Type; }; // Used by diagnostics to access Comparer type
- Field(int*, TAllocator) buckets;
- Field(EntryType*, TAllocator) entries;
- FieldNoBarrier(AllocatorType*) alloc;
- Field(int) size;
- Field(uint) bucketCount;
- Field(int) count;
- Field(int) freeList;
- Field(int) freeCount;
- #if PROFILE_DICTIONARY
- FieldNoBarrier(DictionaryStats*) stats;
- #endif
- enum InsertOperations
- {
- Insert_Add , // FatalInternalError if the item already exist in debug build
- Insert_AddNew, // Ignore add if the item already exist
- Insert_Item // Replace the item if it already exist
- };
- class AutoDoResize
- {
- public:
- AutoDoResize(Lock& lock) : lock(lock) { lock.BeginResize(); };
- ~AutoDoResize() { lock.EndResize(); };
- private:
- Lock& lock;
- };
- public:
- BaseDictionary(AllocatorType* allocator, int capacity = 0)
- : buckets (nullptr),
- size(0),
- bucketCount(0),
- entries(nullptr),
- count(0),
- freeCount(0),
- alloc(allocator)
- {
- Assert(allocator);
- #if PROFILE_DICTIONARY
- stats = nullptr;
- #endif
- // If initial capacity is negative or 0, lazy initialization on
- // the first insert operation is performed.
- if (capacity > 0)
- {
- Initialize(capacity);
- }
- }
- BaseDictionary(const BaseDictionary &other) : alloc(other.alloc)
- {
- if(other.Count() == 0)
- {
- size = 0;
- bucketCount = 0;
- buckets = nullptr;
- entries = nullptr;
- count = 0;
- freeCount = 0;
- #if PROFILE_DICTIONARY
- stats = nullptr;
- #endif
- return;
- }
- Assert(other.bucketCount != 0);
- Assert(other.size != 0);
- buckets = AllocateBuckets(other.bucketCount);
- Assert(buckets); // no-throw allocators are currently not supported
- try
- {
- entries = AllocateEntries(other.size, false /* zeroAllocate */);
- Assert(entries); // no-throw allocators are currently not supported
- }
- catch(...)
- {
- DeleteBuckets(buckets, other.bucketCount);
- throw;
- }
- size = other.size;
- bucketCount = other.bucketCount;
- count = other.count;
- freeList = other.freeList;
- freeCount = other.freeCount;
- CopyArray(buckets, bucketCount, other.buckets, bucketCount);
- CopyArray<EntryType, Field(ValueType, TAllocator), TAllocator>(
- entries, size, other.entries, size);
- #if PROFILE_DICTIONARY
- stats = DictionaryStats::Create(typeid(this).name(), size);
- #endif
- }
- ~BaseDictionary()
- {
- if (buckets)
- {
- DeleteBuckets(buckets, bucketCount);
- }
- if (entries)
- {
- DeleteEntries(entries, size);
- }
- }
- AllocatorType *GetAllocator() const
- {
- return alloc;
- }
- inline int Capacity() const
- {
- return size;
- }
- inline int Count() const
- {
- return count - freeCount;
- }
- TValue Item(const TKey& key)
- {
- int i = FindEntry(key);
- Assert(i >= 0);
- return entries[i].Value();
- }
- const TValue Item(const TKey& key) const
- {
- int i = FindEntry(key);
- Assert(i >= 0);
- return entries[i].Value();
- }
- int Add(const TKey& key, const TValue& value)
- {
- return Insert<Insert_Add>(key, value);
- }
- int AddNew(const TKey& key, const TValue& value)
- {
- return Insert<Insert_AddNew>(key, value);
- }
- int Item(const TKey& key, const TValue& value)
- {
- return Insert<Insert_Item>(key, value);
- }
- bool Contains(KeyValuePair<TKey, TValue> keyValuePair)
- {
- int i = FindEntry(keyValuePair.Key());
- if( i >= 0 && Comparer<TValue>::Equals(entries[i].Value(), keyValuePair.Value()))
- {
- return true;
- }
- return false;
- }
- bool Remove(KeyValuePair<TKey, TValue> keyValuePair)
- {
- int i, last;
- uint targetBucket;
- if(FindEntryWithKey(keyValuePair.Key(), &i, &last, &targetBucket))
- {
- const TValue &value = entries[i].Value();
- if(Comparer<TValue>::Equals(value, keyValuePair.Value()))
- {
- RemoveAt(i, last, targetBucket);
- return true;
- }
- }
- return false;
- }
- void Clear()
- {
- if (count > 0)
- {
- memset(buckets, -1, bucketCount * sizeof(buckets[0]));
- memset(entries, 0, sizeof(EntryType) * size);
- count = 0;
- freeCount = 0;
- #if PROFILE_DICTIONARY
- // To not loose previously collected data, we will treat cleared dictionary as a separate instance for stats tracking purpose
- stats = DictionaryStats::Create(typeid(this).name(), size);
- #endif
- }
- }
- void ResetNoDelete()
- {
- this->size = 0;
- this->bucketCount = 0;
- this->buckets = nullptr;
- this->entries = nullptr;
- this->count = 0;
- this->freeCount = 0;
- }
- void Reset()
- {
- if(bucketCount != 0)
- {
- DeleteBuckets(buckets, bucketCount);
- buckets = nullptr;
- bucketCount = 0;
- }
- else
- {
- Assert(!buckets);
- }
- if(size != 0)
- {
- DeleteEntries(entries, size);
- entries = nullptr;
- freeCount = count = size = 0;
- }
- else
- {
- Assert(!entries);
- Assert(count == 0);
- Assert(freeCount == 0);
- }
- }
- bool ContainsKey(const TKey& key) const
- {
- return FindEntry(key) >= 0;
- }
- template <typename TLookup>
- inline const TValue& LookupWithKey(const TLookup& key, const TValue& defaultValue) const
- {
- int i = FindEntryWithKey(key);
- if (i >= 0)
- {
- return entries[i].Value();
- }
- return defaultValue;
- }
- inline const TValue& Lookup(const TKey& key, const TValue& defaultValue) const
- {
- return LookupWithKey<TKey>(key, defaultValue);
- }
- template <typename TLookup>
- bool TryGetValue(const TLookup& key, TValue* value) const
- {
- int i = FindEntryWithKey(key);
- if (i >= 0)
- {
- *value = entries[i].Value();
- return true;
- }
- return false;
- }
- bool TryGetValueAndRemove(const TKey& key, TValue* value)
- {
- int i, last;
- uint targetBucket;
- if (FindEntryWithKey(key, &i, &last, &targetBucket))
- {
- *value = entries[i].Value();
- RemoveAt(i, last, targetBucket);
- return true;
- }
- return false;
- }
- template <typename TLookup>
- bool TryGetReference(const TLookup& key, const TValue** value) const
- {
- int i;
- return TryGetReference(key, value, &i);
- }
- template <typename TLookup>
- bool TryGetReference(const TLookup& key, TValue** value) const
- {
- int i;
- return TryGetReference(key, value, &i);
- }
- template <typename TLookup>
- bool TryGetReference(const TLookup& key, const TValue** value, int* index) const
- {
- int i = FindEntryWithKey(key);
- if (i >= 0)
- {
- *value = AddressOf(entries[i].Value());
- *index = i;
- return true;
- }
- return false;
- }
- template <typename TLookup>
- bool TryGetReference(const TLookup& key, TValue** value, int* index) const
- {
- int i = FindEntryWithKey(key);
- if (i >= 0)
- {
- *value = &entries[i].Value();
- *index = i;
- return true;
- }
- return false;
- }
- const TValue& GetValueAt(const int index) const
- {
- Assert(index >= 0);
- Assert(index < count);
- return entries[index].Value();
- }
- TValue* GetReferenceAt(const int index) const
- {
- Assert(index >= 0);
- Assert(index < count);
- return &entries[index].Value();
- }
- TKey const& GetKeyAt(const int index) const
- {
- Assert(index >= 0);
- Assert(index < count);
- return entries[index].Key();
- }
- bool TryGetValueAt(const int index, TValue const ** value) const
- {
- if (index >= 0 && index < count)
- {
- *value = &entries[index].Value();
- return true;
- }
- return false;
- }
- bool TryGetValueAt(int index, TValue * value) const
- {
- if (index >= 0 && index < count)
- {
- *value = entries[index].Value();
- return true;
- }
- return false;
- }
- bool Remove(const TKey& key)
- {
- int i, last;
- uint targetBucket;
- if(FindEntryWithKey(key, &i, &last, &targetBucket))
- {
- RemoveAt(i, last, targetBucket);
- return true;
- }
- return false;
- }
- EntryIterator<const BaseDictionary> GetIterator() const
- {
- return EntryIterator<const BaseDictionary>(*this);
- }
- EntryIterator<BaseDictionary> GetIterator()
- {
- return EntryIterator<BaseDictionary>(*this);
- }
- BucketEntryIterator<BaseDictionary> GetIteratorWithRemovalSupport()
- {
- return BucketEntryIterator<BaseDictionary>(*this);
- }
- template<class Fn>
- bool AnyValue(Fn fn) const
- {
- for (uint i = 0; i < bucketCount; i++)
- {
- if(buckets[i] != -1)
- {
- for (int currentIndex = buckets[i] ; currentIndex != -1 ; currentIndex = entries[currentIndex].next)
- {
- if (fn(entries[currentIndex].Value()))
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- template<class Fn>
- void EachValue(Fn fn) const
- {
- for (uint i = 0; i < bucketCount; i++)
- {
- if(buckets[i] != -1)
- {
- for (int currentIndex = buckets[i] ; currentIndex != -1 ; currentIndex = entries[currentIndex].next)
- {
- fn(entries[currentIndex].Value());
- }
- }
- }
- }
- template<class Fn>
- void MapReference(Fn fn)
- {
- MapUntilReference([fn](TKey const& key, TValue& value)
- {
- fn(key, value);
- return false;
- });
- }
- template<class Fn>
- bool MapUntilReference(Fn fn) const
- {
- return MapEntryUntil([fn](EntryType &entry) -> bool
- {
- return fn(entry.Key(), entry.Value());
- });
- }
- template<class Fn>
- void MapAddress(Fn fn) const
- {
- MapUntilAddress([fn](TKey const& key, TValue * value) -> bool
- {
- fn(key, value);
- return false;
- });
- }
- template<class Fn>
- bool MapUntilAddress(Fn fn) const
- {
- return MapEntryUntil([fn](EntryType &entry) -> bool
- {
- return fn(entry.Key(), &entry.Value());
- });
- }
- template<class Fn>
- void Map(Fn fn) const
- {
- MapUntil([fn](TKey const& key, TValue const& value) -> bool
- {
- fn(key, value);
- return false;
- });
- }
- template<class Fn>
- bool MapUntil(Fn fn) const
- {
- return MapEntryUntil([fn](EntryType const& entry) -> bool
- {
- return fn(entry.Key(), entry.Value());
- });
- }
- template<class Fn>
- void MapAndRemoveIf(Fn fn)
- {
- for (uint i = 0; i < bucketCount; i++)
- {
- if (buckets[i] != -1)
- {
- for (int currentIndex = buckets[i], lastIndex = -1; currentIndex != -1;)
- {
- // If the predicate says we should remove this item
- if (fn(entries[currentIndex]) == true)
- {
- const int nextIndex = entries[currentIndex].next;
- RemoveAt(currentIndex, lastIndex, i);
- currentIndex = nextIndex;
- }
- else
- {
- lastIndex = currentIndex;
- currentIndex = entries[currentIndex].next;
- }
- }
- }
- }
- }
- template <class Fn>
- bool RemoveIf(TKey const& key, Fn fn)
- {
- return RemoveIfWithKey<TKey>(key, fn);
- }
- template <typename LookupType, class Fn>
- bool RemoveIfWithKey(LookupType const& lookupKey, Fn fn)
- {
- int i, last;
- uint targetBucket;
- if (FindEntryWithKey<LookupType>(lookupKey, &i, &last, &targetBucket))
- {
- if (fn(entries[i].Key(), entries[i].Value()))
- {
- RemoveAt(i, last, targetBucket);
- return true;
- }
- }
- return false;
- }
- // Returns whether the dictionary was resized or not
- bool EnsureCapacity()
- {
- if (freeCount == 0 && count == size)
- {
- Resize();
- return true;
- }
- return false;
- }
- int GetNextIndex()
- {
- if (freeCount != 0)
- {
- Assert(freeCount > 0);
- Assert(freeList >= 0);
- Assert(freeList < count);
- return freeList;
- }
- return count;
- }
- int GetLastIndex()
- {
- return count - 1;
- }
- BaseDictionary *Clone()
- {
- return AllocatorNew(AllocatorType, alloc, BaseDictionary, *this);
- }
- void Copy(const BaseDictionary *const other)
- {
- DoCopy(other);
- }
- void LockResize()
- {
- __super::LockResize();
- }
- void UnlockResize()
- {
- __super::UnlockResize();
- }
- protected:
- template<class T>
- void DoCopy(const T *const other)
- {
- Assert(size == 0);
- Assert(bucketCount == 0);
- Assert(!buckets);
- Assert(!entries);
- Assert(count == 0);
- Assert(freeCount == 0);
- #if PROFILE_DICTIONARY
- Assert(!stats);
- #endif
- if(other->Count() == 0)
- {
- return;
- }
- Assert(other->bucketCount != 0);
- Assert(other->size != 0);
- buckets = AllocateBuckets(other->bucketCount);
- Assert(buckets); // no-throw allocators are currently not supported
- try
- {
- entries = AllocateEntries(other->size, false /* zeroAllocate */);
- Assert(entries); // no-throw allocators are currently not supported
- }
- catch(...)
- {
- DeleteBuckets(buckets, other->bucketCount);
- buckets = nullptr;
- throw;
- }
- size = other->size;
- bucketCount = other->bucketCount;
- count = other->count;
- freeList = other->freeList;
- freeCount = other->freeCount;
- CopyArray(buckets, bucketCount, other->buckets, bucketCount);
- CopyArray<EntryType, Field(ValueType, TAllocator), TAllocator>(
- entries, size, other->entries, size);
- #if PROFILE_DICTIONARY
- stats = DictionaryStats::Create(typeid(this).name(), size);
- #endif
- }
- protected:
- template<class Fn>
- bool MapEntryUntil(Fn fn) const
- {
- for (uint i = 0; i < bucketCount; i++)
- {
- if(buckets[i] != -1)
- {
- int nextIndex = -1;
- for (int currentIndex = buckets[i] ; currentIndex != -1 ; currentIndex = nextIndex)
- {
- nextIndex = entries[currentIndex].next;
- if (fn(entries[currentIndex]))
- {
- return true; // fn condition succeeds
- }
- }
- }
- }
- return false;
- }
- private:
- template <typename TLookup>
- static hash_t GetHashCodeWithKey(const TLookup& key)
- {
- // set last bit to 1 to avoid false positive to make hash appears to be a valid recycler address.
- // In the same line, 0 should be use to indicate a non-existing entry.
- return TAGHASH(Comparer<TLookup>::GetHashCode(key));
- }
- static hash_t GetHashCode(const TKey& key)
- {
- return GetHashCodeWithKey<TKey>(key);
- }
- static uint GetBucket(hash_t hashCode, int bucketCount)
- {
- return SizePolicy::GetBucket(UNTAGHASH(hashCode), bucketCount);
- }
- uint GetBucket(uint hashCode) const
- {
- return GetBucket(hashCode, this->bucketCount);
- }
- static bool IsFreeEntry(const EntryType &entry)
- {
- // A free entry's next index will be (-2 - nextIndex), such that it is always <= -2, for fast entry iteration
- // allowing for skipping over free entries. -1 is reserved for the end-of-chain marker for a used entry.
- return entry.next <= -2;
- }
- void SetNextFreeEntryIndex(EntryType &freeEntry, const int nextFreeEntryIndex)
- {
- Assert(!IsFreeEntry(freeEntry));
- Assert(nextFreeEntryIndex >= -1);
- Assert(nextFreeEntryIndex < count);
- // The last entry in the free list chain will have a next of -2 to indicate that it is a free entry. The end of the
- // free list chain is identified using freeCount.
- freeEntry.next = nextFreeEntryIndex >= 0 ? -2 - nextFreeEntryIndex : -2;
- }
- static int GetNextFreeEntryIndex(const EntryType &freeEntry)
- {
- Assert(IsFreeEntry(freeEntry));
- return -2 - freeEntry.next;
- }
- template <typename LookupType>
- inline int FindEntryWithKey(const LookupType& key) const
- {
- #if PROFILE_DICTIONARY
- uint depth = 0;
- #endif
- int * localBuckets = buckets;
- if (localBuckets != nullptr)
- {
- hash_t hashCode = GetHashCodeWithKey<LookupType>(key);
- uint targetBucket = this->GetBucket(hashCode);
- EntryType * localEntries = entries;
- for (int i = localBuckets[targetBucket]; i >= 0; i = localEntries[i].next)
- {
- if (localEntries[i].template KeyEquals<Comparer<TKey>>(key, hashCode))
- {
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Lookup(depth);
- #endif
- return i;
- }
- #if PROFILE_DICTIONARY
- depth += 1;
- #endif
- }
- }
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Lookup(depth);
- #endif
- return -1;
- }
- inline int FindEntry(const TKey& key) const
- {
- return FindEntryWithKey<TKey>(key);
- }
- template <typename LookupType>
- inline bool FindEntryWithKey(const LookupType& key, int *const i, int *const last, uint *const targetBucket)
- {
- #if PROFILE_DICTIONARY
- uint depth = 0;
- #endif
- int * localBuckets = buckets;
- if (localBuckets != nullptr)
- {
- uint hashCode = GetHashCodeWithKey<LookupType>(key);
- *targetBucket = this->GetBucket(hashCode);
- *last = -1;
- EntryType * localEntries = entries;
- for (*i = localBuckets[*targetBucket]; *i >= 0; *last = *i, *i = localEntries[*i].next)
- {
- if (localEntries[*i].template KeyEquals<Comparer<TKey>>(key, hashCode))
- {
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Lookup(depth);
- #endif
- return true;
- }
- #if PROFILE_DICTIONARY
- depth += 1;
- #endif
- }
- }
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Lookup(depth);
- #endif
- return false;
- }
- void Initialize(int capacity)
- {
- // minimum capacity is 4
- int initSize = max(capacity, 4);
- uint initBucketCount = SizePolicy::GetBucketSize(initSize);
- AssertMsg(initBucketCount > 0, "Size returned by policy should be greater than 0");
- int* newBuckets = nullptr;
- EntryType* newEntries = nullptr;
- Allocate(&newBuckets, &newEntries, initBucketCount, initSize);
- // Allocation can throw - assign only after allocation has succeeded.
- this->buckets = newBuckets;
- this->entries = newEntries;
- this->bucketCount = initBucketCount;
- this->size = initSize;
- Assert(this->freeCount == 0);
- #if PROFILE_DICTIONARY
- stats = DictionaryStats::Create(typeid(this).name(), size);
- #endif
- }
- template <InsertOperations op>
- int Insert(const TKey& key, const TValue& value)
- {
- int * localBuckets = buckets;
- if (localBuckets == nullptr)
- {
- Initialize(0);
- localBuckets = buckets;
- }
- #if DBG || PROFILE_DICTIONARY
- // Always search and verify
- const bool needSearch = true;
- #else
- const bool needSearch = (op != Insert_Add);
- #endif
- hash_t hashCode = GetHashCode(key);
- uint targetBucket = this->GetBucket(hashCode);
- if (needSearch)
- {
- #if PROFILE_DICTIONARY
- uint depth = 0;
- #endif
- EntryType * localEntries = entries;
- for (int i = localBuckets[targetBucket]; i >= 0; i = localEntries[i].next)
- {
- if (localEntries[i].template KeyEquals<Comparer<TKey>>(key, hashCode))
- {
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Lookup(depth);
- #endif
- Assert(op != Insert_Add);
- if (op == Insert_Item)
- {
- localEntries[i].SetValue(value);
- return i;
- }
- return -1;
- }
- #if PROFILE_DICTIONARY
- depth += 1;
- #endif
- }
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Lookup(depth);
- #endif
- }
- // Ideally we'd do cleanup only if weak references have been collected since the last resize
- // but that would require us to use an additional field to store the last recycler cleanup id
- // that we saw
- // We can add that optimization later if we have to.
- if (EntryType::SupportsCleanup() && freeCount == 0 && count == size)
- {
- this->MapAndRemoveIf([](EntryType& entry)
- {
- return EntryType::NeedsCleanup(entry);
- });
- }
- int index;
- if (freeCount != 0)
- {
- Assert(freeCount > 0);
- Assert(freeList >= 0);
- Assert(freeList < count);
- index = freeList;
- freeCount--;
- if(freeCount != 0)
- {
- freeList = GetNextFreeEntryIndex(entries[index]);
- }
- }
- else
- {
- // If there's nothing free, then in general, we set index to count, and increment count
- // If we resize, we also need to recalculate the target
- // However, if cleanup is supported, then before resize, we should try and clean up and see
- // if something got freed, and if it did, reuse that index
- if (count == size)
- {
- Resize();
- targetBucket = this->GetBucket(hashCode);
- index = count;
- count++;
- }
- else
- {
- index = count;
- count++;
- }
- Assert(count <= size);
- Assert(index < size);
- }
- entries[index].Set(key, value, hashCode);
- entries[index].next = buckets[targetBucket];
- buckets[targetBucket] = index;
- #if PROFILE_DICTIONARY
- int profileIndex = index;
- uint depth = 1; // need to recalculate depth in case there was a resize (also 1-based for stats->Insert)
- while(entries[profileIndex].next != -1)
- {
- profileIndex = entries[profileIndex].next;
- ++depth;
- }
- if (stats)
- stats->Insert(depth);
- #endif
- return index;
- }
- void Resize()
- {
- AutoDoResize autoDoResize(*this);
- int newSize = SizePolicy::GetNextSize(count);
- uint newBucketCount = SizePolicy::GetBucketSize(newSize);
- __analysis_assume(newSize > count);
- int* newBuckets = nullptr;
- EntryType* newEntries = nullptr;
- if (newBucketCount == bucketCount)
- {
- // no need to rehash
- newEntries = AllocateEntries(newSize);
- CopyArray<EntryType, Field(ValueType, TAllocator), TAllocator>(
- newEntries, newSize, entries, count);
- DeleteEntries(entries, size);
- this->entries = newEntries;
- this->size = newSize;
- return;
- }
- Allocate(&newBuckets, &newEntries, newBucketCount, newSize);
- CopyArray<EntryType, Field(ValueType, TAllocator), TAllocator>(
- newEntries, newSize, entries, count);
- // When TAllocator is of type Recycler, it is possible that the Allocate above causes a collection, which
- // in turn can cause entries in the dictionary to be removed - i.e. the dictionary contains weak references
- // that remove themselves when no longer valid. This means the free list might not be empty anymore.
- for (int i = 0; i < count; i++)
- {
- __analysis_assume(i < newSize);
- if (!IsFreeEntry(newEntries[i]))
- {
- uint hashCode = newEntries[i].template GetHashCode<Comparer<TKey>>();
- int bucket = GetBucket(hashCode, newBucketCount);
- newEntries[i].next = newBuckets[bucket];
- newBuckets[bucket] = i;
- }
- }
- DeleteBuckets(buckets, bucketCount);
- DeleteEntries(entries, size);
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Resize(newSize, /*emptyBuckets=*/ newSize - size);
- #endif
- this->buckets = newBuckets;
- this->entries = newEntries;
- bucketCount = newBucketCount;
- size = newSize;
- }
- __ecount(bucketCount) int *AllocateBuckets(DECLSPEC_GUARD_OVERFLOW const uint bucketCount)
- {
- return
- AllocateArray<AllocatorType, int, false>(
- TRACK_ALLOC_INFO(alloc, int, AllocatorType, 0, bucketCount),
- TypeAllocatorFunc<AllocatorType, int>::GetAllocFunc(),
- bucketCount);
- }
- __ecount(size) EntryType * AllocateEntries(DECLSPEC_GUARD_OVERFLOW int size, const bool zeroAllocate = true)
- {
- // Note that the choice of leaf/non-leaf node is decided for the EntryType on the basis of TValue. By default, if
- // TValue is a pointer, a non-leaf allocation is done. This behavior can be overridden by specializing
- // TypeAllocatorFunc for TValue.
- return
- AllocateArray<AllocatorType, EntryType, false>(
- TRACK_ALLOC_INFO(alloc, EntryType, AllocatorType, 0, size),
- zeroAllocate ? EntryAllocatorFuncType::GetAllocZeroFunc() : EntryAllocatorFuncType::GetAllocFunc(),
- size);
- }
- void DeleteBuckets(__in_ecount(bucketCount) int *const buckets, const uint bucketCount)
- {
- Assert(buckets);
- Assert(bucketCount != 0);
- AllocatorFree(alloc, (TypeAllocatorFunc<AllocatorType, int>::GetFreeFunc()), buckets, bucketCount * sizeof(int));
- }
- void DeleteEntries(__in_ecount(size) EntryType *const entries, const int size)
- {
- Assert(entries);
- Assert(size != 0);
- AllocatorFree(alloc, EntryAllocatorFuncType::GetFreeFunc(), entries, size * sizeof(EntryType));
- }
- void Allocate(__deref_out_ecount(bucketCount) int** ppBuckets, __deref_out_ecount(size) EntryType** ppEntries, DECLSPEC_GUARD_OVERFLOW uint bucketCount, DECLSPEC_GUARD_OVERFLOW int size)
- {
- int *const buckets = AllocateBuckets(bucketCount);
- Assert(buckets); // no-throw allocators are currently not supported
- EntryType *entries;
- try
- {
- entries = AllocateEntries(size);
- Assert(entries); // no-throw allocators are currently not supported
- }
- catch(...)
- {
- DeleteBuckets(buckets, bucketCount);
- throw;
- }
- memset(buckets, -1, bucketCount * sizeof(buckets[0]));
- *ppBuckets = buckets;
- *ppEntries = entries;
- }
- inline void RemoveAt(const int i, const int last, const uint targetBucket)
- {
- if (last < 0)
- {
- buckets[targetBucket] = entries[i].next;
- }
- else
- {
- entries[last].next = entries[i].next;
- }
- entries[i].Clear();
- SetNextFreeEntryIndex(entries[i], freeCount == 0 ? -1 : freeList);
- freeList = i;
- freeCount++;
- #if PROFILE_DICTIONARY
- if (stats)
- stats->Remove(buckets[targetBucket] == -1);
- #endif
- }
- #if DBG_DUMP
- public:
- void Dump()
- {
- printf("Dumping Dictionary\n");
- printf("-------------------\n");
- for (uint i = 0; i < bucketCount; i++)
- {
- printf("Bucket value: %d\n", buckets[i]);
- for (int j = buckets[i]; j >= 0; j = entries[j].next)
- {
- printf("%d => %d Next: %d\n", entries[j].Key(), entries[j].Value(), entries[j].next);
- }
- }
- }
- #endif
- protected:
- template<class TDictionary, class Leaf>
- class IteratorBase _ABSTRACT
- {
- protected:
- EntryType *const entries;
- int entryIndex;
- #if DBG
- protected:
- TDictionary &dictionary;
- private:
- int usedEntryCount;
- #endif
- protected:
- IteratorBase(TDictionary &dictionary, const int entryIndex)
- : entries(dictionary.entries),
- entryIndex(entryIndex)
- #if DBG
- ,
- dictionary(dictionary),
- usedEntryCount(dictionary.Count())
- #endif
- {
- }
- protected:
- void OnEntryRemoved()
- {
- DebugOnly(--usedEntryCount);
- }
- private:
- bool IsValid_Virtual() const
- {
- return static_cast<const Leaf *>(this)->IsValid();
- }
- protected:
- bool IsValid() const
- {
- Assert(dictionary.entries == entries);
- Assert(dictionary.Count() == usedEntryCount);
- return true;
- }
- public:
- EntryType &Current() const
- {
- Assert(IsValid_Virtual());
- Assert(!IsFreeEntry(entries[entryIndex]));
- return entries[entryIndex];
- }
- TKey CurrentKey() const
- {
- return Current().Key();
- }
- const TValue &CurrentValue() const
- {
- return Current().Value();
- }
- TValue &CurrentValueReference() const
- {
- return Current().Value();
- }
- void SetCurrentValue(const TValue &value) const
- {
- #if DBG
- // For BaseHashSet, save the key before changing the value to verify that the key does not change
- const TKey previousKey = CurrentKey();
- const hash_t previousHashCode = GetHashCode(previousKey);
- #endif
- Current().SetValue(value);
- Assert(Current().KeyEquals<Comparer<TKey>>(previousKey, previousHashCode));
- }
- };
- public:
- template<class TDictionary>
- class EntryIterator sealed : public IteratorBase<TDictionary, EntryIterator<TDictionary>>
- {
- private:
- typedef IteratorBase<TDictionary, EntryIterator<TDictionary>> Base;
- private:
- const int entryCount;
- public:
- EntryIterator(TDictionary &dictionary) : Base(dictionary, 0), entryCount(dictionary.count)
- {
- if(IsValid() && IsFreeEntry(this->entries[this->entryIndex]))
- {
- MoveNext();
- }
- }
- public:
- bool IsValid() const
- {
- Assert(this->dictionary.count == this->entryCount);
- Assert(this->entryIndex >= 0);
- Assert(this->entryIndex <= entryCount);
- return Base::IsValid() && this->entryIndex < this->entryCount;
- }
- public:
- void MoveNext()
- {
- Assert(IsValid());
- do
- {
- ++(this->entryIndex);
- } while(IsValid() && IsFreeEntry(this->entries[this->entryIndex]));
- }
- };
- public:
- template<class TDictionary>
- class BucketEntryIterator sealed : public IteratorBase<TDictionary, BucketEntryIterator<TDictionary>>
- {
- private:
- typedef IteratorBase<TDictionary, BucketEntryIterator<TDictionary>> Base;
- private:
- TDictionary &dictionary;
- int *const buckets;
- const uint bucketCount;
- uint bucketIndex;
- int previousEntryIndexInBucket;
- int indexOfEntryAfterRemovedEntry;
- public:
- BucketEntryIterator(TDictionary &dictionary)
- : Base(dictionary, -1),
- dictionary(dictionary),
- buckets(dictionary.buckets),
- bucketCount(dictionary.bucketCount),
- bucketIndex(0u - 1)
- #if DBG
- ,
- previousEntryIndexInBucket(-2),
- indexOfEntryAfterRemovedEntry(-2)
- #endif
- {
- if(dictionary.Count() != 0)
- {
- MoveNextBucket();
- }
- }
- public:
- bool IsValid() const
- {
- Assert(dictionary.buckets == buckets);
- Assert(dictionary.bucketCount == bucketCount);
- Assert(this->entryIndex >= -1);
- Assert(this->entryIndex < dictionary.count);
- Assert(bucketIndex == 0u - 1 || bucketIndex <= bucketCount);
- Assert(previousEntryIndexInBucket >= -2);
- Assert(previousEntryIndexInBucket < dictionary.count);
- Assert(indexOfEntryAfterRemovedEntry >= -2);
- Assert(indexOfEntryAfterRemovedEntry < dictionary.count);
- return Base::IsValid() && this->entryIndex >= 0;
- }
- public:
- void MoveNext()
- {
- if(IsValid())
- {
- previousEntryIndexInBucket = this->entryIndex;
- this->entryIndex = this->Current().next;
- }
- else
- {
- Assert(indexOfEntryAfterRemovedEntry >= -1);
- this->entryIndex = indexOfEntryAfterRemovedEntry;
- }
- if(!IsValid())
- {
- MoveNextBucket();
- }
- }
- private:
- void MoveNextBucket()
- {
- Assert(!IsValid());
- while(++bucketIndex < bucketCount)
- {
- this->entryIndex = buckets[bucketIndex];
- if(IsValid())
- {
- previousEntryIndexInBucket = -1;
- break;
- }
- }
- }
- public:
- void RemoveCurrent()
- {
- Assert(previousEntryIndexInBucket >= -1);
- indexOfEntryAfterRemovedEntry = this->Current().next;
- dictionary.RemoveAt(this->entryIndex, previousEntryIndexInBucket, bucketIndex);
- this->OnEntryRemoved();
- this->entryIndex = -1;
- }
- };
- template<class TDictionary, class Leaf> friend class IteratorBase;
- template<class TDictionary> friend class EntryIterator;
- template<class TDictionary> friend class BucketEntryIterator;
- PREVENT_ASSIGN(BaseDictionary);
- };
- template <class TKey, class TValue> class SimpleHashedEntry;
- template <
- class TElement,
- class TAllocator,
- class SizePolicy = PowerOf2SizePolicy,
- class TKey = TElement,
- template <typename ValueOrKey> class Comparer = DefaultComparer,
- template <typename, typename> class Entry = SimpleHashedEntry,
- typename Lock = NoResizeLock
- >
- class BaseHashSet : protected BaseDictionary<TKey, TElement, TAllocator, SizePolicy, Comparer, Entry, Lock>
- {
- typedef BaseDictionary<TKey, TElement, TAllocator, SizePolicy, Comparer, Entry, Lock> Base;
- typedef typename Base::EntryType EntryType;
- typedef typename Base::AllocatorType AllocatorType;
- friend struct JsDiag::RemoteDictionary<BaseHashSet<TElement, TAllocator, SizePolicy, TKey, Comparer, Entry, Lock>>;
- public:
- BaseHashSet(AllocatorType * allocator, int capacity = 0) : Base(allocator, capacity) {}
- using Base::GetAllocator;
- int Count() const
- {
- return __super::Count();
- }
- int Add(TElement const& element)
- {
- return __super::Add(ValueToKey<TKey, TElement>::ToKey(element), element);
- }
- // Add only if there isn't an existing element
- int AddNew(TElement const& element)
- {
- return __super::AddNew(ValueToKey<TKey, TElement>::ToKey(element), element);
- }
- int Item(TElement const& element)
- {
- return __super::Item(ValueToKey<TKey, TElement>::ToKey(element), element);
- }
- void Clear()
- {
- __super::Clear();
- }
- using Base::Reset;
- TElement const& Lookup(TKey const& key)
- {
- // Use a static to pass the null default value, since the
- // default value may get returned out of the current scope by ref.
- static const TElement nullElement = nullptr;
- return __super::Lookup(key, nullElement);
- }
- template <typename KeyType>
- TElement const& LookupWithKey(KeyType const& key)
- {
- static const TElement nullElement = nullptr;
- return __super::LookupWithKey(key, nullElement);
- }
- bool Contains(TElement const& element) const
- {
- return ContainsKey(ValueToKey<TKey, TElement>::ToKey(element));
- }
- using Base::ContainsKey;
- using Base::TryGetValue;
- using Base::TryGetReference;
- bool RemoveKey(const TKey &key)
- {
- return Base::Remove(key);
- }
- bool Remove(TElement const& element)
- {
- return __super::Remove(ValueToKey<TKey, TElement>::ToKey(element));
- }
- typename Base::template EntryIterator<const BaseHashSet> GetIterator() const
- {
- return typename Base::template EntryIterator<const BaseHashSet>(*this);
- }
- typename Base::template EntryIterator<BaseHashSet> GetIterator()
- {
- return typename Base::template EntryIterator<BaseHashSet>(*this);
- }
- typename Base::template BucketEntryIterator<BaseHashSet> GetIteratorWithRemovalSupport()
- {
- return typename Base::template BucketEntryIterator<BaseHashSet>(*this);
- }
- template<class Fn>
- void Map(Fn fn)
- {
- MapUntil([fn](TElement const& value) -> bool
- {
- fn(value);
- return false;
- });
- }
- template<class Fn>
- void MapAndRemoveIf(Fn fn)
- {
- __super::MapAndRemoveIf([fn](EntryType const& entry) -> bool
- {
- return fn(entry.Value());
- });
- }
- template<class Fn>
- bool MapUntil(Fn fn)
- {
- return __super::MapEntryUntil([fn](EntryType const& entry) -> bool
- {
- return fn(entry.Value());
- });
- }
- bool EnsureCapacity()
- {
- return __super::EnsureCapacity();
- }
- int GetNextIndex()
- {
- return __super::GetNextIndex();
- }
- int GetLastIndex()
- {
- return __super::GetLastIndex();
- }
- using Base::GetValueAt;
- bool TryGetValueAt(int index, TElement * value) const
- {
- return __super::TryGetValueAt(index, value);
- }
- BaseHashSet *Clone()
- {
- return AllocatorNew(AllocatorType, this->alloc, BaseHashSet, *this);
- }
- void Copy(const BaseHashSet *const other)
- {
- this->DoCopy(other);
- }
- void LockResize()
- {
- __super::LockResize();
- }
- void UnlockResize()
- {
- __super::UnlockResize();
- }
- friend Base;
- PREVENT_ASSIGN(BaseHashSet);
- };
- template <
- class TKey,
- class TValue,
- class TAllocator,
- class SizePolicy = PowerOf2SizePolicy,
- template <typename ValueOrKey> class Comparer = DefaultComparer,
- template <typename K, typename V> class Entry = SimpleDictionaryEntry,
- class LockPolicy = Js::DefaultContainerLockPolicy, // Controls lock policy for read/map/write/add/remove items
- class SyncObject = CriticalSection
- >
- class SynchronizedDictionary: protected BaseDictionary<TKey, TValue, TAllocator, SizePolicy, Comparer, Entry>
- {
- private:
- FieldNoBarrier(SyncObject*) syncObj;
- typedef BaseDictionary<TKey, TValue, TAllocator, SizePolicy, Comparer, Entry> Base;
- public:
- typedef TKey KeyType;
- typedef TValue ValueType;
- typedef typename Base::AllocatorType AllocatorType;
- typedef typename Base::EntryType EntryType;
- typedef SynchronizedDictionary<TKey, TValue, TAllocator, SizePolicy, Comparer, Entry, LockPolicy, SyncObject> DictionaryType;
- private:
- friend class Js::RemoteDictionary<DictionaryType>;
- public:
- SynchronizedDictionary(AllocatorType * allocator, int capacity, SyncObject* syncObject):
- Base(allocator, capacity),
- syncObj(syncObject)
- {}
- #ifdef DBG
- void Dump()
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- __super::Dump();
- }
- #endif
- TAllocator *GetAllocator() const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::GetAllocator();
- }
- inline int Count() const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::Count();
- }
- inline int Capacity() const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::Capacity();
- }
- TValue Item(const TKey& key)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::Item(key);
- }
- bool IsInAdd()
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::IsInAdd();
- }
- int Add(const TKey& key, const TValue& value)
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::Add(key, value);
- }
- int AddNew(const TKey& key, const TValue& value)
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::AddNew(key, value);
- }
- int Item(const TKey& key, const TValue& value)
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::Item(key, value);
- }
- bool Contains(KeyValuePair<TKey, TValue> keyValuePair)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::Contains(keyValuePair);
- }
- bool Remove(KeyValuePair<TKey, TValue> keyValuePair)
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::Remove(keyValuePair);
- }
- void Clear()
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::Clear();
- }
- void Reset()
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::Reset();
- }
- bool ContainsKey(const TKey& key)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::ContainsKey(key);
- }
- template <typename TLookup>
- inline const TValue& LookupWithKey(const TLookup& key, const TValue& defaultValue)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::LookupWithKey(key, defaultValue);
- }
- inline const TValue& Lookup(const TKey& key, const TValue& defaultValue)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::Lookup(key, defaultValue);
- }
- template <typename TLookup>
- bool TryGetValue(const TLookup& key, TValue* value)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::TryGetValue(key, value);
- }
- bool TryGetValueAndRemove(const TKey& key, TValue* value)
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::TryGetValueAndRemove(key, value);
- }
- template <typename TLookup>
- inline bool TryGetReference(const TLookup& key, TValue** value)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::TryGetReference(key, value);
- }
- template <typename TLookup>
- inline bool TryGetReference(const TLookup& key, TValue** value, int* index)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::TryGetReference(key, value, index);
- }
- const TValue& GetValueAt(const int& index) const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::GetValueAt(index);
- }
- TValue* GetReferenceAt(const int& index)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::GetReferenceAt(index);
- }
- TKey const& GetKeyAt(const int& index)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::GetKeyAt(index);
- }
- bool Remove(const TKey& key)
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::Remove(key);
- }
- template<class Fn>
- void MapReference(Fn fn)
- {
- // TODO: Verify that Map doesn't actually modify the list
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::MapReference(fn);
- }
- template<class Fn>
- bool MapUntilReference(Fn fn) const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::MapUntilReference(fn);
- }
- template<class Fn>
- void MapAddress(Fn fn) const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::MapAddress(fn);
- }
- template<class Fn>
- bool MapUntilAddress(Fn fn) const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::MapUntilAddress(fn);
- }
- template<class Fn>
- void Map(Fn fn) const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::Map(fn);
- }
- template<class Fn>
- bool MapUntil(Fn fn) const
- {
- typename LockPolicy::ReadLock autoLock(syncObj);
- return __super::MapUntil(fn);
- }
- template<class Fn>
- void MapAndRemoveIf(Fn fn)
- {
- typename LockPolicy::AddRemoveLock autoLock(syncObj);
- return __super::MapAndRemoveIf(fn);
- }
- PREVENT_COPY(SynchronizedDictionary);
- };
- }
|