nuclear@1: /************************************************************************************ nuclear@1: nuclear@1: PublicHeader: None nuclear@1: Filename : OVR_Hash.h nuclear@1: Content : Template hash-table/set implementation nuclear@1: Created : September 19, 2012 nuclear@1: Notes : nuclear@1: nuclear@1: Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. nuclear@1: nuclear@1: Use of this software is subject to the terms of the Oculus license nuclear@1: agreement provided at the time of installation or download, or which nuclear@1: otherwise accompanies this software in either electronic or hard copy form. nuclear@1: nuclear@1: ************************************************************************************/ nuclear@1: nuclear@1: #ifndef OVR_Hash_h nuclear@1: #define OVR_Hash_h nuclear@1: nuclear@1: #include "OVR_ContainerAllocator.h" nuclear@1: #include "OVR_Alg.h" nuclear@1: nuclear@1: // 'new' operator is redefined/used in this file. nuclear@1: #undef new nuclear@1: nuclear@1: namespace OVR { nuclear@1: nuclear@1: //----------------------------------------------------------------------------------- nuclear@1: // ***** Hash Table Implementation nuclear@1: nuclear@1: // HastSet and Hash. nuclear@1: // nuclear@1: // Hash table, linear probing, internal chaining. One interesting/nice thing nuclear@1: // about this implementation is that the table itself is a flat chunk of memory nuclear@1: // containing no pointers, only relative indices. If the key and value types nuclear@1: // of the Hash contain no pointers, then the Hash can be serialized using raw IO. nuclear@1: // nuclear@1: // Never shrinks, unless you explicitly Clear() it. Expands on nuclear@1: // demand, though. For best results, if you know roughly how big your nuclear@1: // table will be, default it to that size when you create it. nuclear@1: // nuclear@1: // Key usability feature: nuclear@1: // nuclear@1: // 1. Allows node hash values to either be cached or not. nuclear@1: // nuclear@1: // 2. Allows for alternative keys with methods such as GetAlt(). Handy nuclear@1: // if you need to search nodes by their components; no need to create nuclear@1: // temporary nodes. nuclear@1: // nuclear@1: nuclear@1: nuclear@1: // *** Hash functors: nuclear@1: // nuclear@1: // IdentityHash - use when the key is already a good hash nuclear@1: // HFixedSizeHash - general hash based on object's in-memory representation. nuclear@1: nuclear@1: nuclear@1: // Hash is just the input value; can use this for integer-indexed hash tables. nuclear@1: template nuclear@1: class IdentityHash nuclear@1: { nuclear@1: public: nuclear@1: UPInt operator()(const C& data) const nuclear@1: { return (UPInt) data; } nuclear@1: }; nuclear@1: nuclear@1: // Computes a hash of an object's representation. nuclear@1: template nuclear@1: class FixedSizeHash nuclear@1: { nuclear@1: public: nuclear@1: // Alternative: "sdbm" hash function, suggested at same web page nuclear@1: // above, http::/www.cs.yorku.ca/~oz/hash.html nuclear@1: // This is somewhat slower then Bernstein, but it works way better than the above nuclear@1: // hash function for hashing large numbers of 32-bit ints. nuclear@1: static OVR_FORCE_INLINE UPInt SDBM_Hash(const void* data_in, UPInt size, UPInt seed = 5381) nuclear@1: { nuclear@1: const UByte* data = (const UByte*) data_in; nuclear@1: UPInt h = seed; nuclear@1: while (size > 0) nuclear@1: { nuclear@1: size--; nuclear@1: h = (h << 16) + (h << 6) - h + (UPInt)data[size]; nuclear@1: } nuclear@1: return h; nuclear@1: } nuclear@1: nuclear@1: UPInt operator()(const C& data) const nuclear@1: { nuclear@1: unsigned char* p = (unsigned char*) &data; nuclear@1: int size = sizeof(C); nuclear@1: nuclear@1: return SDBM_Hash(p, size); nuclear@1: } nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: nuclear@1: // *** HashsetEntry Entry types. nuclear@1: nuclear@1: // Compact hash table Entry type that re-computes hash keys during hash traversal. nuclear@1: // Good to use if the hash function is cheap or the hash value is already cached in C. nuclear@1: template nuclear@1: class HashsetEntry nuclear@1: { nuclear@1: public: nuclear@1: // Internal chaining for collisions. nuclear@1: SPInt NextInChain; nuclear@1: C Value; nuclear@1: nuclear@1: HashsetEntry() nuclear@1: : NextInChain(-2) { } nuclear@1: HashsetEntry(const HashsetEntry& e) nuclear@1: : NextInChain(e.NextInChain), Value(e.Value) { } nuclear@1: HashsetEntry(const C& key, SPInt next) nuclear@1: : NextInChain(next), Value(key) { } nuclear@1: nuclear@1: bool IsEmpty() const { return NextInChain == -2; } nuclear@1: bool IsEndOfChain() const { return NextInChain == -1; } nuclear@1: nuclear@1: // Cached hash value access - can be optimized bu storing hash locally. nuclear@1: // Mask value only needs to be used if SetCachedHash is not implemented. nuclear@1: UPInt GetCachedHash(UPInt maskValue) const { return HashF()(Value) & maskValue; } nuclear@1: void SetCachedHash(UPInt) {} nuclear@1: nuclear@1: void Clear() nuclear@1: { nuclear@1: Value.~C(); // placement delete nuclear@1: NextInChain = -2; nuclear@1: } nuclear@1: // Free is only used from dtor of hash; Clear is used during regular operations: nuclear@1: // assignment, hash reallocations, value reassignments, so on. nuclear@1: void Free() { Clear(); } nuclear@1: }; nuclear@1: nuclear@1: // Hash table Entry type that caches the Entry hash value for nodes, so that it nuclear@1: // does not need to be re-computed during access. nuclear@1: template nuclear@1: class HashsetCachedEntry nuclear@1: { nuclear@1: public: nuclear@1: // Internal chaining for collisions. nuclear@1: SPInt NextInChain; nuclear@1: UPInt HashValue; nuclear@1: C Value; nuclear@1: nuclear@1: HashsetCachedEntry() nuclear@1: : NextInChain(-2) { } nuclear@1: HashsetCachedEntry(const HashsetCachedEntry& e) nuclear@1: : NextInChain(e.NextInChain), HashValue(e.HashValue), Value(e.Value) { } nuclear@1: HashsetCachedEntry(const C& key, SPInt next) nuclear@1: : NextInChain(next), Value(key) { } nuclear@1: nuclear@1: bool IsEmpty() const { return NextInChain == -2; } nuclear@1: bool IsEndOfChain() const { return NextInChain == -1; } nuclear@1: nuclear@1: // Cached hash value access - can be optimized bu storing hash locally. nuclear@1: // Mask value only needs to be used if SetCachedHash is not implemented. nuclear@1: UPInt GetCachedHash(UPInt maskValue) const { OVR_UNUSED(maskValue); return HashValue; } nuclear@1: void SetCachedHash(UPInt hashValue) { HashValue = hashValue; } nuclear@1: nuclear@1: void Clear() nuclear@1: { nuclear@1: Value.~C(); nuclear@1: NextInChain = -2; nuclear@1: } nuclear@1: // Free is only used from dtor of hash; Clear is used during regular operations: nuclear@1: // assignment, hash reallocations, value reassignments, so on. nuclear@1: void Free() { Clear(); } nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: //----------------------------------------------------------------------------------- nuclear@1: // *** HashSet implementation - relies on either cached or regular entries. nuclear@1: // nuclear@1: // Use: Entry = HashsetCachedEntry if hashes are expensive to nuclear@1: // compute and thus need caching in entries. nuclear@1: // Entry = HashsetEntry if hashes are already externally cached. nuclear@1: // nuclear@1: template, nuclear@1: class AltHashF = HashF, nuclear@1: class Allocator = ContainerAllocator, nuclear@1: class Entry = HashsetCachedEntry > nuclear@1: class HashSetBase nuclear@1: { nuclear@1: enum { HashMinSize = 8 }; nuclear@1: nuclear@1: public: nuclear@1: OVR_MEMORY_REDEFINE_NEW(HashSetBase) nuclear@1: nuclear@1: typedef HashSetBase SelfType; nuclear@1: nuclear@1: HashSetBase() : pTable(NULL) { } nuclear@1: HashSetBase(int sizeHint) : pTable(NULL) { SetCapacity(this, sizeHint); } nuclear@1: HashSetBase(const SelfType& src) : pTable(NULL) { Assign(this, src); } nuclear@1: nuclear@1: ~HashSetBase() nuclear@1: { nuclear@1: if (pTable) nuclear@1: { nuclear@1: // Delete the entries. nuclear@1: for (UPInt i = 0, n = pTable->SizeMask; i <= n; i++) nuclear@1: { nuclear@1: Entry* e = &E(i); nuclear@1: if (!e->IsEmpty()) nuclear@1: e->Free(); nuclear@1: } nuclear@1: nuclear@1: Allocator::Free(pTable); nuclear@1: pTable = NULL; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: nuclear@1: void Assign(const SelfType& src) nuclear@1: { nuclear@1: Clear(); nuclear@1: if (src.IsEmpty() == false) nuclear@1: { nuclear@1: SetCapacity(src.GetSize()); nuclear@1: nuclear@1: for (ConstIterator it = src.Begin(); it != src.End(); ++it) nuclear@1: { nuclear@1: Add(*it); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: nuclear@1: // Remove all entries from the HashSet table. nuclear@1: void Clear() nuclear@1: { nuclear@1: if (pTable) nuclear@1: { nuclear@1: // Delete the entries. nuclear@1: for (UPInt i = 0, n = pTable->SizeMask; i <= n; i++) nuclear@1: { nuclear@1: Entry* e = &E(i); nuclear@1: if (!e->IsEmpty()) nuclear@1: e->Clear(); nuclear@1: } nuclear@1: nuclear@1: Allocator::Free(pTable); nuclear@1: pTable = NULL; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: // Returns true if the HashSet is empty. nuclear@1: bool IsEmpty() const nuclear@1: { nuclear@1: return pTable == NULL || pTable->EntryCount == 0; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: // Set a new or existing value under the key, to the value. nuclear@1: // Pass a different class of 'key' so that assignment reference object nuclear@1: // can be passed instead of the actual object. nuclear@1: template nuclear@1: void Set(const CRef& key) nuclear@1: { nuclear@1: UPInt hashValue = HashF()(key); nuclear@1: SPInt index = (SPInt)-1; nuclear@1: nuclear@1: if (pTable != NULL) nuclear@1: index = findIndexCore(key, hashValue & pTable->SizeMask); nuclear@1: nuclear@1: if (index >= 0) nuclear@1: { nuclear@1: E(index).Value = key; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: // Entry under key doesn't exist. nuclear@1: add(key, hashValue); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: inline void Add(const CRef& key) nuclear@1: { nuclear@1: UPInt hashValue = HashF()(key); nuclear@1: add(key, hashValue); nuclear@1: } nuclear@1: nuclear@1: // Remove by alternative key. nuclear@1: template nuclear@1: void RemoveAlt(const K& key) nuclear@1: { nuclear@1: if (pTable == NULL) nuclear@1: return; nuclear@1: nuclear@1: UPInt hashValue = AltHashF()(key); nuclear@1: SPInt index = hashValue & pTable->SizeMask; nuclear@1: nuclear@1: Entry* e = &E(index); nuclear@1: nuclear@1: // If empty node or occupied by collider, we have nothing to remove. nuclear@1: if (e->IsEmpty() || (e->GetCachedHash(pTable->SizeMask) != (UPInt)index)) nuclear@1: return; nuclear@1: nuclear@1: // Save index nuclear@1: SPInt naturalIndex = index; nuclear@1: SPInt prevIndex = -1; nuclear@1: nuclear@1: while ((e->GetCachedHash(pTable->SizeMask) != (UPInt)naturalIndex) || !(e->Value == key)) nuclear@1: { nuclear@1: // Keep looking through the chain. nuclear@1: prevIndex = index; nuclear@1: index = e->NextInChain; nuclear@1: if (index == -1) nuclear@1: return; // End of chain, item not found nuclear@1: e = &E(index); nuclear@1: } nuclear@1: nuclear@1: // Found it - our item is at index nuclear@1: if (naturalIndex == index) nuclear@1: { nuclear@1: // If we have a follower, move it to us nuclear@1: if (!e->IsEndOfChain()) nuclear@1: { nuclear@1: Entry* enext = &E(e->NextInChain); nuclear@1: e->Clear(); nuclear@1: new (e) Entry(*enext); nuclear@1: // Point us to the follower's cell that will be cleared nuclear@1: e = enext; nuclear@1: } nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: // We are not at natural index, so deal with the prev items next index nuclear@1: E(prevIndex).NextInChain = e->NextInChain; nuclear@1: } nuclear@1: nuclear@1: // Clear us, of the follower cell that was moved. nuclear@1: e->Clear(); nuclear@1: pTable->EntryCount --; nuclear@1: // Should we check the size to condense hash? ... nuclear@1: } nuclear@1: nuclear@1: // Remove by main key. nuclear@1: template nuclear@1: void Remove(const CRef& key) nuclear@1: { nuclear@1: RemoveAlt(key); nuclear@1: } nuclear@1: nuclear@1: // Retrieve the pointer to a value under the given key. nuclear@1: // - If there's no value under the key, then return NULL. nuclear@1: // - If there is a value, return the pointer. nuclear@1: template nuclear@1: C* Get(const K& key) nuclear@1: { nuclear@1: SPInt index = findIndex(key); nuclear@1: if (index >= 0) nuclear@1: return &E(index).Value; nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: const C* Get(const K& key) const nuclear@1: { nuclear@1: SPInt index = findIndex(key); nuclear@1: if (index >= 0) nuclear@1: return &E(index).Value; nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: // Alternative key versions of Get. Used by Hash. nuclear@1: template nuclear@1: const C* GetAlt(const K& key) const nuclear@1: { nuclear@1: SPInt index = findIndexAlt(key); nuclear@1: if (index >= 0) nuclear@1: return &E(index).Value; nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: C* GetAlt(const K& key) nuclear@1: { nuclear@1: SPInt index = findIndexAlt(key); nuclear@1: if (index >= 0) nuclear@1: return &E(index).Value; nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: bool GetAlt(const K& key, C* pval) const nuclear@1: { nuclear@1: SPInt index = findIndexAlt(key); nuclear@1: if (index >= 0) nuclear@1: { nuclear@1: if (pval) nuclear@1: *pval = E(index).Value; nuclear@1: return true; nuclear@1: } nuclear@1: return false; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: UPInt GetSize() const nuclear@1: { nuclear@1: return pTable == NULL ? 0 : (UPInt)pTable->EntryCount; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: // Resize the HashSet table to fit one more Entry. Often this nuclear@1: // doesn't involve any action. nuclear@1: void CheckExpand() nuclear@1: { nuclear@1: if (pTable == NULL) nuclear@1: { nuclear@1: // Initial creation of table. Make a minimum-sized table. nuclear@1: setRawCapacity(HashMinSize); nuclear@1: } nuclear@1: else if (pTable->EntryCount * 5 > (pTable->SizeMask + 1) * 4) nuclear@1: { nuclear@1: // pTable is more than 5/4 ths full. Expand. nuclear@1: setRawCapacity((pTable->SizeMask + 1) * 2); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: // Hint the bucket count to >= n. nuclear@1: void Resize(UPInt n) nuclear@1: { nuclear@1: // Not really sure what this means in relation to nuclear@1: // STLport's hash_map... they say they "increase the nuclear@1: // bucket count to at least n" -- but does that mean nuclear@1: // their real capacity after Resize(n) is more like nuclear@1: // n*2 (since they do linked-list chaining within nuclear@1: // buckets?). nuclear@1: SetCapacity(n); nuclear@1: } nuclear@1: nuclear@1: // Size the HashSet so that it can comfortably contain the given nuclear@1: // number of elements. If the HashSet already contains more nuclear@1: // elements than newSize, then this may be a no-op. nuclear@1: void SetCapacity(UPInt newSize) nuclear@1: { nuclear@1: UPInt newRawSize = (newSize * 5) / 4; nuclear@1: if (newRawSize <= GetSize()) nuclear@1: return; nuclear@1: setRawCapacity(newRawSize); nuclear@1: } nuclear@1: nuclear@1: // Disable inappropriate 'operator ->' warning on MSVC6. nuclear@1: #ifdef OVR_CC_MSVC nuclear@1: #if (OVR_CC_MSVC < 1300) nuclear@1: # pragma warning(disable : 4284) nuclear@1: #endif nuclear@1: #endif nuclear@1: nuclear@1: // Iterator API, like STL. nuclear@1: struct ConstIterator nuclear@1: { nuclear@1: const C& operator * () const nuclear@1: { nuclear@1: OVR_ASSERT(Index >= 0 && Index <= (SPInt)pHash->pTable->SizeMask); nuclear@1: return pHash->E(Index).Value; nuclear@1: } nuclear@1: nuclear@1: const C* operator -> () const nuclear@1: { nuclear@1: OVR_ASSERT(Index >= 0 && Index <= (SPInt)pHash->pTable->SizeMask); nuclear@1: return &pHash->E(Index).Value; nuclear@1: } nuclear@1: nuclear@1: void operator ++ () nuclear@1: { nuclear@1: // Find next non-empty Entry. nuclear@1: if (Index <= (SPInt)pHash->pTable->SizeMask) nuclear@1: { nuclear@1: Index++; nuclear@1: while ((UPInt)Index <= pHash->pTable->SizeMask && nuclear@1: pHash->E(Index).IsEmpty()) nuclear@1: { nuclear@1: Index++; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: bool operator == (const ConstIterator& it) const nuclear@1: { nuclear@1: if (IsEnd() && it.IsEnd()) nuclear@1: { nuclear@1: return true; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: return (pHash == it.pHash) && (Index == it.Index); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: bool operator != (const ConstIterator& it) const nuclear@1: { nuclear@1: return ! (*this == it); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: bool IsEnd() const nuclear@1: { nuclear@1: return (pHash == NULL) || nuclear@1: (pHash->pTable == NULL) || nuclear@1: (Index > (SPInt)pHash->pTable->SizeMask); nuclear@1: } nuclear@1: nuclear@1: ConstIterator() nuclear@1: : pHash(NULL), Index(0) nuclear@1: { } nuclear@1: nuclear@1: public: nuclear@1: // Constructor was intentionally made public to allow create nuclear@1: // iterator with arbitrary index. nuclear@1: ConstIterator(const SelfType* h, SPInt index) nuclear@1: : pHash(h), Index(index) nuclear@1: { } nuclear@1: nuclear@1: const SelfType* GetContainer() const nuclear@1: { nuclear@1: return pHash; nuclear@1: } nuclear@1: SPInt GetIndex() const nuclear@1: { nuclear@1: return Index; nuclear@1: } nuclear@1: nuclear@1: protected: nuclear@1: friend class HashSetBase; nuclear@1: nuclear@1: const SelfType* pHash; nuclear@1: SPInt Index; nuclear@1: }; nuclear@1: nuclear@1: friend struct ConstIterator; nuclear@1: nuclear@1: nuclear@1: // Non-const Iterator; Get most of it from ConstIterator. nuclear@1: struct Iterator : public ConstIterator nuclear@1: { nuclear@1: // Allow non-const access to entries. nuclear@1: C& operator*() const nuclear@1: { nuclear@1: OVR_ASSERT(ConstIterator::Index >= 0 && ConstIterator::Index <= (SPInt)ConstIterator::pHash->pTable->SizeMask); nuclear@1: return const_cast(ConstIterator::pHash)->E(ConstIterator::Index).Value; nuclear@1: } nuclear@1: nuclear@1: C* operator->() const nuclear@1: { nuclear@1: return &(operator*()); nuclear@1: } nuclear@1: nuclear@1: Iterator() nuclear@1: : ConstIterator(NULL, 0) nuclear@1: { } nuclear@1: nuclear@1: // Removes current element from Hash nuclear@1: void Remove() nuclear@1: { nuclear@1: RemoveAlt(operator*()); nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: void RemoveAlt(const K& key) nuclear@1: { nuclear@1: SelfType* phash = const_cast(ConstIterator::pHash); nuclear@1: //Entry* ee = &phash->E(ConstIterator::Index); nuclear@1: //const C& key = ee->Value; nuclear@1: nuclear@1: UPInt hashValue = AltHashF()(key); nuclear@1: SPInt index = hashValue & phash->pTable->SizeMask; nuclear@1: nuclear@1: Entry* e = &phash->E(index); nuclear@1: nuclear@1: // If empty node or occupied by collider, we have nothing to remove. nuclear@1: if (e->IsEmpty() || (e->GetCachedHash(phash->pTable->SizeMask) != (UPInt)index)) nuclear@1: return; nuclear@1: nuclear@1: // Save index nuclear@1: SPInt naturalIndex = index; nuclear@1: SPInt prevIndex = -1; nuclear@1: nuclear@1: while ((e->GetCachedHash(phash->pTable->SizeMask) != (UPInt)naturalIndex) || !(e->Value == key)) nuclear@1: { nuclear@1: // Keep looking through the chain. nuclear@1: prevIndex = index; nuclear@1: index = e->NextInChain; nuclear@1: if (index == -1) nuclear@1: return; // End of chain, item not found nuclear@1: e = &phash->E(index); nuclear@1: } nuclear@1: nuclear@1: if (index == (SPInt)ConstIterator::Index) nuclear@1: { nuclear@1: // Found it - our item is at index nuclear@1: if (naturalIndex == index) nuclear@1: { nuclear@1: // If we have a follower, move it to us nuclear@1: if (!e->IsEndOfChain()) nuclear@1: { nuclear@1: Entry* enext = &phash->E(e->NextInChain); nuclear@1: e->Clear(); nuclear@1: new (e) Entry(*enext); nuclear@1: // Point us to the follower's cell that will be cleared nuclear@1: e = enext; nuclear@1: --ConstIterator::Index; nuclear@1: } nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: // We are not at natural index, so deal with the prev items next index nuclear@1: phash->E(prevIndex).NextInChain = e->NextInChain; nuclear@1: } nuclear@1: nuclear@1: // Clear us, of the follower cell that was moved. nuclear@1: e->Clear(); nuclear@1: phash->pTable->EntryCount --; nuclear@1: } nuclear@1: else nuclear@1: OVR_ASSERT(0); //? nuclear@1: } nuclear@1: nuclear@1: private: nuclear@1: friend class HashSetBase; nuclear@1: nuclear@1: Iterator(SelfType* h, SPInt i0) nuclear@1: : ConstIterator(h, i0) nuclear@1: { } nuclear@1: }; nuclear@1: nuclear@1: friend struct Iterator; nuclear@1: nuclear@1: Iterator Begin() nuclear@1: { nuclear@1: if (pTable == 0) nuclear@1: return Iterator(NULL, 0); nuclear@1: nuclear@1: // Scan till we hit the First valid Entry. nuclear@1: UPInt i0 = 0; nuclear@1: while (i0 <= pTable->SizeMask && E(i0).IsEmpty()) nuclear@1: { nuclear@1: i0++; nuclear@1: } nuclear@1: return Iterator(this, i0); nuclear@1: } nuclear@1: Iterator End() { return Iterator(NULL, 0); } nuclear@1: nuclear@1: ConstIterator Begin() const { return const_cast(this)->Begin(); } nuclear@1: ConstIterator End() const { return const_cast(this)->End(); } nuclear@1: nuclear@1: template nuclear@1: Iterator Find(const K& key) nuclear@1: { nuclear@1: SPInt index = findIndex(key); nuclear@1: if (index >= 0) nuclear@1: return Iterator(this, index); nuclear@1: return Iterator(NULL, 0); nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: Iterator FindAlt(const K& key) nuclear@1: { nuclear@1: SPInt index = findIndexAlt(key); nuclear@1: if (index >= 0) nuclear@1: return Iterator(this, index); nuclear@1: return Iterator(NULL, 0); nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: ConstIterator Find(const K& key) const { return const_cast(this)->Find(key); } nuclear@1: nuclear@1: template nuclear@1: ConstIterator FindAlt(const K& key) const { return const_cast(this)->FindAlt(key); } nuclear@1: nuclear@1: private: nuclear@1: // Find the index of the matching Entry. If no match, then return -1. nuclear@1: template nuclear@1: SPInt findIndex(const K& key) const nuclear@1: { nuclear@1: if (pTable == NULL) nuclear@1: return -1; nuclear@1: UPInt hashValue = HashF()(key) & pTable->SizeMask; nuclear@1: return findIndexCore(key, hashValue); nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: SPInt findIndexAlt(const K& key) const nuclear@1: { nuclear@1: if (pTable == NULL) nuclear@1: return -1; nuclear@1: UPInt hashValue = AltHashF()(key) & pTable->SizeMask; nuclear@1: return findIndexCore(key, hashValue); nuclear@1: } nuclear@1: nuclear@1: // Find the index of the matching Entry. If no match, then return -1. nuclear@1: template nuclear@1: SPInt findIndexCore(const K& key, UPInt hashValue) const nuclear@1: { nuclear@1: // Table must exist. nuclear@1: OVR_ASSERT(pTable != 0); nuclear@1: // Hash key must be 'and-ed' by the caller. nuclear@1: OVR_ASSERT((hashValue & ~pTable->SizeMask) == 0); nuclear@1: nuclear@1: UPInt index = hashValue; nuclear@1: const Entry* e = &E(index); nuclear@1: nuclear@1: // If empty or occupied by a collider, not found. nuclear@1: if (e->IsEmpty() || (e->GetCachedHash(pTable->SizeMask) != index)) nuclear@1: return -1; nuclear@1: nuclear@1: while(1) nuclear@1: { nuclear@1: OVR_ASSERT(e->GetCachedHash(pTable->SizeMask) == hashValue); nuclear@1: nuclear@1: if (e->GetCachedHash(pTable->SizeMask) == hashValue && e->Value == key) nuclear@1: { nuclear@1: // Found it. nuclear@1: return index; nuclear@1: } nuclear@1: // Values can not be equal at this point. nuclear@1: // That would mean that the hash key for the same value differs. nuclear@1: OVR_ASSERT(!(e->Value == key)); nuclear@1: nuclear@1: // Keep looking through the chain. nuclear@1: index = e->NextInChain; nuclear@1: if (index == (UPInt)-1) nuclear@1: break; // end of chain nuclear@1: nuclear@1: e = &E(index); nuclear@1: OVR_ASSERT(!e->IsEmpty()); nuclear@1: } nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: // Add a new value to the HashSet table, under the specified key. nuclear@1: template nuclear@1: void add(const CRef& key, UPInt hashValue) nuclear@1: { nuclear@1: CheckExpand(); nuclear@1: hashValue &= pTable->SizeMask; nuclear@1: nuclear@1: pTable->EntryCount++; nuclear@1: nuclear@1: SPInt index = hashValue; nuclear@1: Entry* naturalEntry = &(E(index)); nuclear@1: nuclear@1: if (naturalEntry->IsEmpty()) nuclear@1: { nuclear@1: // Put the new Entry in. nuclear@1: new (naturalEntry) Entry(key, -1); nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: // Find a blank spot. nuclear@1: SPInt blankIndex = index; nuclear@1: do { nuclear@1: blankIndex = (blankIndex + 1) & pTable->SizeMask; nuclear@1: } while(!E(blankIndex).IsEmpty()); nuclear@1: nuclear@1: Entry* blankEntry = &E(blankIndex); nuclear@1: nuclear@1: if (naturalEntry->GetCachedHash(pTable->SizeMask) == (UPInt)index) nuclear@1: { nuclear@1: // Collision. Link into this chain. nuclear@1: nuclear@1: // Move existing list head. nuclear@1: new (blankEntry) Entry(*naturalEntry); // placement new, copy ctor nuclear@1: nuclear@1: // Put the new info in the natural Entry. nuclear@1: naturalEntry->Value = key; nuclear@1: naturalEntry->NextInChain = blankIndex; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: // Existing Entry does not naturally nuclear@1: // belong in this slot. Existing nuclear@1: // Entry must be moved. nuclear@1: nuclear@1: // Find natural location of collided element (i.e. root of chain) nuclear@1: SPInt collidedIndex = naturalEntry->GetCachedHash(pTable->SizeMask); nuclear@1: OVR_ASSERT(collidedIndex >= 0 && collidedIndex <= (SPInt)pTable->SizeMask); nuclear@1: for (;;) nuclear@1: { nuclear@1: Entry* e = &E(collidedIndex); nuclear@1: if (e->NextInChain == index) nuclear@1: { nuclear@1: // Here's where we need to splice. nuclear@1: new (blankEntry) Entry(*naturalEntry); nuclear@1: e->NextInChain = blankIndex; nuclear@1: break; nuclear@1: } nuclear@1: collidedIndex = e->NextInChain; nuclear@1: OVR_ASSERT(collidedIndex >= 0 && collidedIndex <= (SPInt)pTable->SizeMask); nuclear@1: } nuclear@1: nuclear@1: // Put the new data in the natural Entry. nuclear@1: naturalEntry->Value = key; nuclear@1: naturalEntry->NextInChain = -1; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: // Record hash value: has effect only if cached node is used. nuclear@1: naturalEntry->SetCachedHash(hashValue); nuclear@1: } nuclear@1: nuclear@1: // Index access helpers. nuclear@1: Entry& E(UPInt index) nuclear@1: { nuclear@1: // Must have pTable and access needs to be within bounds. nuclear@1: OVR_ASSERT(index <= pTable->SizeMask); nuclear@1: return *(((Entry*) (pTable + 1)) + index); nuclear@1: } nuclear@1: const Entry& E(UPInt index) const nuclear@1: { nuclear@1: OVR_ASSERT(index <= pTable->SizeMask); nuclear@1: return *(((Entry*) (pTable + 1)) + index); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: // Resize the HashSet table to the given size (Rehash the nuclear@1: // contents of the current table). The arg is the number of nuclear@1: // HashSet table entries, not the number of elements we should nuclear@1: // actually contain (which will be less than this). nuclear@1: void setRawCapacity(UPInt newSize) nuclear@1: { nuclear@1: if (newSize == 0) nuclear@1: { nuclear@1: // Special case. nuclear@1: Clear(); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: // Minimum size; don't incur rehashing cost when expanding nuclear@1: // very small tables. Not that we perform this check before nuclear@1: // 'log2f' call to avoid fp exception with newSize == 1. nuclear@1: if (newSize < HashMinSize) nuclear@1: newSize = HashMinSize; nuclear@1: else nuclear@1: { nuclear@1: // Force newSize to be a power of two. nuclear@1: int bits = Alg::UpperBit(newSize-1) + 1; // Chop( Log2f((float)(newSize-1)) + 1); nuclear@1: OVR_ASSERT((UPInt(1) << bits) >= newSize); nuclear@1: newSize = UPInt(1) << bits; nuclear@1: } nuclear@1: nuclear@1: SelfType newHash; nuclear@1: newHash.pTable = (TableType*) nuclear@1: Allocator::Alloc( nuclear@1: sizeof(TableType) + sizeof(Entry) * newSize); nuclear@1: // Need to do something on alloc failure! nuclear@1: OVR_ASSERT(newHash.pTable); nuclear@1: nuclear@1: newHash.pTable->EntryCount = 0; nuclear@1: newHash.pTable->SizeMask = newSize - 1; nuclear@1: UPInt i, n; nuclear@1: nuclear@1: // Mark all entries as empty. nuclear@1: for (i = 0; i < newSize; i++) nuclear@1: newHash.E(i).NextInChain = -2; nuclear@1: nuclear@1: // Copy stuff to newHash nuclear@1: if (pTable) nuclear@1: { nuclear@1: for (i = 0, n = pTable->SizeMask; i <= n; i++) nuclear@1: { nuclear@1: Entry* e = &E(i); nuclear@1: if (e->IsEmpty() == false) nuclear@1: { nuclear@1: // Insert old Entry into new HashSet. nuclear@1: newHash.Add(e->Value); nuclear@1: // placement delete of old element nuclear@1: e->Clear(); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: // Delete our old data buffer. nuclear@1: Allocator::Free(pTable); nuclear@1: } nuclear@1: nuclear@1: // Steal newHash's data. nuclear@1: pTable = newHash.pTable; nuclear@1: newHash.pTable = NULL; nuclear@1: } nuclear@1: nuclear@1: struct TableType nuclear@1: { nuclear@1: UPInt EntryCount; nuclear@1: UPInt SizeMask; nuclear@1: // Entry array follows this structure nuclear@1: // in memory. nuclear@1: }; nuclear@1: TableType* pTable; nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: nuclear@1: //----------------------------------------------------------------------------------- nuclear@1: template, nuclear@1: class AltHashF = HashF, nuclear@1: class Allocator = ContainerAllocator, nuclear@1: class Entry = HashsetCachedEntry > nuclear@1: class HashSet : public HashSetBase nuclear@1: { nuclear@1: public: nuclear@1: typedef HashSetBase BaseType; nuclear@1: typedef HashSet SelfType; nuclear@1: nuclear@1: HashSet() { } nuclear@1: HashSet(int sizeHint) : BaseType(sizeHint) { } nuclear@1: HashSet(const SelfType& src) : BaseType(src) { } nuclear@1: ~HashSet() { } nuclear@1: nuclear@1: void operator = (const SelfType& src) { BaseType::Assign(src); } nuclear@1: nuclear@1: // Set a new or existing value under the key, to the value. nuclear@1: // Pass a different class of 'key' so that assignment reference object nuclear@1: // can be passed instead of the actual object. nuclear@1: template nuclear@1: void Set(const CRef& key) nuclear@1: { nuclear@1: BaseType::Set(key); nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: inline void Add(const CRef& key) nuclear@1: { nuclear@1: BaseType::Add(key); nuclear@1: } nuclear@1: nuclear@1: // Hint the bucket count to >= n. nuclear@1: void Resize(UPInt n) nuclear@1: { nuclear@1: BaseType::SetCapacity(n); nuclear@1: } nuclear@1: nuclear@1: // Size the HashSet so that it can comfortably contain the given nuclear@1: // number of elements. If the HashSet already contains more nuclear@1: // elements than newSize, then this may be a no-op. nuclear@1: void SetCapacity(UPInt newSize) nuclear@1: { nuclear@1: BaseType::SetCapacity(newSize); nuclear@1: } nuclear@1: nuclear@1: }; nuclear@1: nuclear@1: // HashSet with uncached hash code; declared for convenience. nuclear@1: template, nuclear@1: class AltHashF = HashF, nuclear@1: class Allocator = ContainerAllocator > nuclear@1: class HashSetUncached : public HashSet > nuclear@1: { nuclear@1: public: nuclear@1: nuclear@1: typedef HashSetUncached SelfType; nuclear@1: typedef HashSet > BaseType; nuclear@1: nuclear@1: // Delegated constructors. nuclear@1: HashSetUncached() { } nuclear@1: HashSetUncached(int sizeHint) : BaseType(sizeHint) { } nuclear@1: HashSetUncached(const SelfType& src) : BaseType(src) { } nuclear@1: ~HashSetUncached() { } nuclear@1: nuclear@1: void operator = (const SelfType& src) nuclear@1: { nuclear@1: BaseType::operator = (src); nuclear@1: } nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: //----------------------------------------------------------------------------------- nuclear@1: // ***** Hash hash table implementation nuclear@1: nuclear@1: // Node for Hash - necessary so that Hash can delegate its implementation nuclear@1: // to HashSet. nuclear@1: template nuclear@1: struct HashNode nuclear@1: { nuclear@1: typedef HashNode SelfType; nuclear@1: typedef C FirstType; nuclear@1: typedef U SecondType; nuclear@1: nuclear@1: C First; nuclear@1: U Second; nuclear@1: nuclear@1: // NodeRef is used to allow passing of elements into HashSet nuclear@1: // without using a temporary object. nuclear@1: struct NodeRef nuclear@1: { nuclear@1: const C* pFirst; nuclear@1: const U* pSecond; nuclear@1: nuclear@1: NodeRef(const C& f, const U& s) : pFirst(&f), pSecond(&s) { } nuclear@1: NodeRef(const NodeRef& src) : pFirst(src.pFirst), pSecond(src.pSecond) { } nuclear@1: nuclear@1: // Enable computation of ghash_node_hashf. nuclear@1: inline UPInt GetHash() const { return HashF()(*pFirst); } nuclear@1: // Necessary conversion to allow HashNode::operator == to work. nuclear@1: operator const C& () const { return *pFirst; } nuclear@1: }; nuclear@1: nuclear@1: // Note: No default constructor is necessary. nuclear@1: HashNode(const HashNode& src) : First(src.First), Second(src.Second) { } nuclear@1: HashNode(const NodeRef& src) : First(*src.pFirst), Second(*src.pSecond) { } nuclear@1: void operator = (const NodeRef& src) { First = *src.pFirst; Second = *src.pSecond; } nuclear@1: nuclear@1: template nuclear@1: bool operator == (const K& src) const { return (First == src); } nuclear@1: nuclear@1: template nuclear@1: static UPInt CalcHash(const K& data) { return HashF()(data); } nuclear@1: inline UPInt GetHash() const { return HashF()(First); } nuclear@1: nuclear@1: // Hash functors used with this node. A separate functor is used for alternative nuclear@1: // key lookup so that it does not need to access the '.First' element. nuclear@1: struct NodeHashF nuclear@1: { nuclear@1: template nuclear@1: UPInt operator()(const K& data) const { return data.GetHash(); } nuclear@1: }; nuclear@1: struct NodeAltHashF nuclear@1: { nuclear@1: template nuclear@1: UPInt operator()(const K& data) const { return HashNode::CalcHash(data); } nuclear@1: }; nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: nuclear@1: // **** Extra hashset_entry types to allow NodeRef construction. nuclear@1: nuclear@1: // The big difference between the below types and the ones used in hash_set is that nuclear@1: // these allow initializing the node with 'typename C::NodeRef& keyRef', which nuclear@1: // is critical to avoid temporary node allocation on stack when using placement new. nuclear@1: nuclear@1: // Compact hash table Entry type that re-computes hash keys during hash traversal. nuclear@1: // Good to use if the hash function is cheap or the hash value is already cached in C. nuclear@1: template nuclear@1: class HashsetNodeEntry nuclear@1: { nuclear@1: public: nuclear@1: // Internal chaining for collisions. nuclear@1: SPInt NextInChain; nuclear@1: C Value; nuclear@1: nuclear@1: HashsetNodeEntry() nuclear@1: : NextInChain(-2) { } nuclear@1: HashsetNodeEntry(const HashsetNodeEntry& e) nuclear@1: : NextInChain(e.NextInChain), Value(e.Value) { } nuclear@1: HashsetNodeEntry(const C& key, SPInt next) nuclear@1: : NextInChain(next), Value(key) { } nuclear@1: HashsetNodeEntry(const typename C::NodeRef& keyRef, SPInt next) nuclear@1: : NextInChain(next), Value(keyRef) { } nuclear@1: nuclear@1: bool IsEmpty() const { return NextInChain == -2; } nuclear@1: bool IsEndOfChain() const { return NextInChain == -1; } nuclear@1: UPInt GetCachedHash(UPInt maskValue) const { return HashF()(Value) & maskValue; } nuclear@1: void SetCachedHash(UPInt hashValue) { OVR_UNUSED(hashValue); } nuclear@1: nuclear@1: void Clear() nuclear@1: { nuclear@1: Value.~C(); // placement delete nuclear@1: NextInChain = -2; nuclear@1: } nuclear@1: // Free is only used from dtor of hash; Clear is used during regular operations: nuclear@1: // assignment, hash reallocations, value reassignments, so on. nuclear@1: void Free() { Clear(); } nuclear@1: }; nuclear@1: nuclear@1: // Hash table Entry type that caches the Entry hash value for nodes, so that it nuclear@1: // does not need to be re-computed during access. nuclear@1: template nuclear@1: class HashsetCachedNodeEntry nuclear@1: { nuclear@1: public: nuclear@1: // Internal chaining for collisions. nuclear@1: SPInt NextInChain; nuclear@1: UPInt HashValue; nuclear@1: C Value; nuclear@1: nuclear@1: HashsetCachedNodeEntry() nuclear@1: : NextInChain(-2) { } nuclear@1: HashsetCachedNodeEntry(const HashsetCachedNodeEntry& e) nuclear@1: : NextInChain(e.NextInChain), HashValue(e.HashValue), Value(e.Value) { } nuclear@1: HashsetCachedNodeEntry(const C& key, SPInt next) nuclear@1: : NextInChain(next), Value(key) { } nuclear@1: HashsetCachedNodeEntry(const typename C::NodeRef& keyRef, SPInt next) nuclear@1: : NextInChain(next), Value(keyRef) { } nuclear@1: nuclear@1: bool IsEmpty() const { return NextInChain == -2; } nuclear@1: bool IsEndOfChain() const { return NextInChain == -1; } nuclear@1: UPInt GetCachedHash(UPInt maskValue) const { OVR_UNUSED(maskValue); return HashValue; } nuclear@1: void SetCachedHash(UPInt hashValue) { HashValue = hashValue; } nuclear@1: nuclear@1: void Clear() nuclear@1: { nuclear@1: Value.~C(); nuclear@1: NextInChain = -2; nuclear@1: } nuclear@1: // Free is only used from dtor of hash; Clear is used during regular operations: nuclear@1: // assignment, hash reallocations, value reassignments, so on. nuclear@1: void Free() { Clear(); } nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: //----------------------------------------------------------------------------------- nuclear@1: template, nuclear@1: class Allocator = ContainerAllocator, nuclear@1: class HashNode = OVR::HashNode, nuclear@1: class Entry = HashsetCachedNodeEntry, nuclear@1: class Container = HashSet > nuclear@1: class Hash nuclear@1: { nuclear@1: public: nuclear@1: OVR_MEMORY_REDEFINE_NEW(Hash) nuclear@1: nuclear@1: // Types used for hash_set. nuclear@1: typedef U ValueType; nuclear@1: typedef Hash SelfType; nuclear@1: nuclear@1: // Actual hash table itself, implemented as hash_set. nuclear@1: Container mHash; nuclear@1: nuclear@1: public: nuclear@1: Hash() { } nuclear@1: Hash(int sizeHint) : mHash(sizeHint) { } nuclear@1: Hash(const SelfType& src) : mHash(src.mHash) { } nuclear@1: ~Hash() { } nuclear@1: nuclear@1: void operator = (const SelfType& src) { mHash = src.mHash; } nuclear@1: nuclear@1: // Remove all entries from the Hash table. nuclear@1: inline void Clear() { mHash.Clear(); } nuclear@1: // Returns true if the Hash is empty. nuclear@1: inline bool IsEmpty() const { return mHash.IsEmpty(); } nuclear@1: nuclear@1: // Access (set). nuclear@1: inline void Set(const C& key, const U& value) nuclear@1: { nuclear@1: typename HashNode::NodeRef e(key, value); nuclear@1: mHash.Set(e); nuclear@1: } nuclear@1: inline void Add(const C& key, const U& value) nuclear@1: { nuclear@1: typename HashNode::NodeRef e(key, value); nuclear@1: mHash.Add(e); nuclear@1: } nuclear@1: nuclear@1: // Removes an element by clearing its Entry. nuclear@1: inline void Remove(const C& key) nuclear@1: { nuclear@1: mHash.RemoveAlt(key); nuclear@1: } nuclear@1: template nuclear@1: inline void RemoveAlt(const K& key) nuclear@1: { nuclear@1: mHash.RemoveAlt(key); nuclear@1: } nuclear@1: nuclear@1: // Retrieve the value under the given key. nuclear@1: // - If there's no value under the key, then return false and leave *pvalue alone. nuclear@1: // - If there is a value, return true, and Set *Pvalue to the Entry's value. nuclear@1: // - If value == NULL, return true or false according to the presence of the key. nuclear@1: bool Get(const C& key, U* pvalue) const nuclear@1: { nuclear@1: const HashNode* p = mHash.GetAlt(key); nuclear@1: if (p) nuclear@1: { nuclear@1: if (pvalue) nuclear@1: *pvalue = p->Second; nuclear@1: return true; nuclear@1: } nuclear@1: return false; nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: bool GetAlt(const K& key, U* pvalue) const nuclear@1: { nuclear@1: const HashNode* p = mHash.GetAlt(key); nuclear@1: if (p) nuclear@1: { nuclear@1: if (pvalue) nuclear@1: *pvalue = p->Second; nuclear@1: return true; nuclear@1: } nuclear@1: return false; nuclear@1: } nuclear@1: nuclear@1: // Retrieve the pointer to a value under the given key. nuclear@1: // - If there's no value under the key, then return NULL. nuclear@1: // - If there is a value, return the pointer. nuclear@1: inline U* Get(const C& key) nuclear@1: { nuclear@1: HashNode* p = mHash.GetAlt(key); nuclear@1: return p ? &p->Second : 0; nuclear@1: } nuclear@1: inline const U* Get(const C& key) const nuclear@1: { nuclear@1: const HashNode* p = mHash.GetAlt(key); nuclear@1: return p ? &p->Second : 0; nuclear@1: } nuclear@1: nuclear@1: template nuclear@1: inline U* GetAlt(const K& key) nuclear@1: { nuclear@1: HashNode* p = mHash.GetAlt(key); nuclear@1: return p ? &p->Second : 0; nuclear@1: } nuclear@1: template nuclear@1: inline const U* GetAlt(const K& key) const nuclear@1: { nuclear@1: const HashNode* p = mHash.GetAlt(key); nuclear@1: return p ? &p->Second : 0; nuclear@1: } nuclear@1: nuclear@1: // Sizing methods - delegate to Hash. nuclear@1: inline UPInt GetSize() const { return mHash.GetSize(); } nuclear@1: inline void Resize(UPInt n) { mHash.Resize(n); } nuclear@1: inline void SetCapacity(UPInt newSize) { mHash.SetCapacity(newSize); } nuclear@1: nuclear@1: // Iterator API, like STL. nuclear@1: typedef typename Container::ConstIterator ConstIterator; nuclear@1: typedef typename Container::Iterator Iterator; nuclear@1: nuclear@1: inline Iterator Begin() { return mHash.Begin(); } nuclear@1: inline Iterator End() { return mHash.End(); } nuclear@1: inline ConstIterator Begin() const { return mHash.Begin(); } nuclear@1: inline ConstIterator End() const { return mHash.End(); } nuclear@1: nuclear@1: Iterator Find(const C& key) { return mHash.FindAlt(key); } nuclear@1: ConstIterator Find(const C& key) const { return mHash.FindAlt(key); } nuclear@1: nuclear@1: template nuclear@1: Iterator FindAlt(const K& key) { return mHash.FindAlt(key); } nuclear@1: template nuclear@1: ConstIterator FindAlt(const K& key) const { return mHash.FindAlt(key); } nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: nuclear@1: // Hash with uncached hash code; declared for convenience. nuclear@1: template, class Allocator = ContainerAllocator > nuclear@1: class HashUncached nuclear@1: : public Hash, nuclear@1: HashsetNodeEntry, typename HashNode::NodeHashF> > nuclear@1: { nuclear@1: public: nuclear@1: typedef HashUncached SelfType; nuclear@1: typedef Hash, nuclear@1: HashsetNodeEntry, nuclear@1: typename HashNode::NodeHashF> > BaseType; nuclear@1: nuclear@1: // Delegated constructors. nuclear@1: HashUncached() { } nuclear@1: HashUncached(int sizeHint) : BaseType(sizeHint) { } nuclear@1: HashUncached(const SelfType& src) : BaseType(src) { } nuclear@1: ~HashUncached() { } nuclear@1: void operator = (const SelfType& src) { BaseType::operator = (src); } nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: nuclear@1: // And identity hash in which keys serve as hash value. Can be uncached, nuclear@1: // since hash computation is assumed cheap. nuclear@1: template, class HashF = IdentityHash > nuclear@1: class HashIdentity nuclear@1: : public HashUncached nuclear@1: { nuclear@1: public: nuclear@1: typedef HashIdentity SelfType; nuclear@1: typedef HashUncached BaseType; nuclear@1: nuclear@1: // Delegated constructors. nuclear@1: HashIdentity() { } nuclear@1: HashIdentity(int sizeHint) : BaseType(sizeHint) { } nuclear@1: HashIdentity(const SelfType& src) : BaseType(src) { } nuclear@1: ~HashIdentity() { } nuclear@1: void operator = (const SelfType& src) { BaseType::operator = (src); } nuclear@1: }; nuclear@1: nuclear@1: nuclear@1: } // OVR nuclear@1: nuclear@1: nuclear@1: #ifdef OVR_DEFINE_NEW nuclear@1: #define new OVR_DEFINE_NEW nuclear@1: #endif nuclear@1: nuclear@1: #endif