//===========================================================================// // File: hash.hh // // Project: MUNGA Brick: Connection Library // // Contents: // //---------------------------------------------------------------------------// // Date Who Modification // // -------- --- ---------------------------------------------------------- // // 12/15/94 ECH Initial coding. // // 06/07/96 ECH Implemented socket based remove functionality // //---------------------------------------------------------------------------// // Copyright (C) 1994-1995, Virtual World Entertainment, Inc. // // PROPRIETARY AND CONFIDENTIAL // //===========================================================================// #pragma once #include "Stuff.hpp" #include "SortedSocket.hpp" #include "SortedChain.hpp" namespace GetHashFunctions { inline unsigned GetHashValue(int value_to_hash) { return value_to_hash; } }; namespace Stuff { //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hash ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class _declspec(novtable) Hash: public SortedSocket { friend class HashIterator; public: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Public interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // // //-------------------------------------------------------------------- // Constructor, Destructor and Testing //-------------------------------------------------------------------- // Hash( unsigned size, void *node, bool has_unique_entries ); ~Hash(); void TestInstance(); static bool TestClass(); static bool ProfileClass(); // //----------------------------------------------------------------------- // RemovePlug - Remove a plug from this socket, untyped access. //----------------------------------------------------------------------- // void DeletePlugs(); // //----------------------------------------------------------------------- // IsPlugMember - Determine if the plug is a member of this socket. //----------------------------------------------------------------------- // void* GetNthItem(unsigned index); // //----------------------------------------------------------------------- // IsEmpty - Returns true if the socket contains no plugs. //----------------------------------------------------------------------- // unsigned GetSize(); bool IsEmpty(); void AddValuePlug( Plug *plug, const void *value ); Plug *FindPlug(const void *value); protected: // //-------------------------------------------------------------------- // Protected data //-------------------------------------------------------------------- // DynamicArrayOf m_hashTable; // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Private interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // virtual SortedChain* MakeSortedChain()=0; virtual unsigned GetHashIndex(const void *value)=0; bool CheckForPrimeSize(unsigned size); }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HashOf ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template class HashOf: public Hash { public: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Public interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // HashOf( unsigned size, void *node, bool has_unique_entries ): Hash(size, NULL, has_unique_entries) {} ~HashOf(); // //-------------------------------------------------------------------- // Socket methods (see Socket for full listing) //-------------------------------------------------------------------- // void AddValue( T plug, const V &value ) {Hash::AddValuePlug(Cast_Object(Plug*,plug), &value);} T Find(const V &value) {return (T)Hash::FindPlug(&value);} protected: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Private interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // SortedChain* MakeSortedChain() { return new SortedChainOf(NULL, HasUniqueEntries()); } unsigned GetHashIndex(const void *value) { Check_Pointer(value); return (GetHashFunctions::GetHashValue(*Cast_Pointer(const V*, value)) % m_hashTable.GetLength()); } protected: void Add(T plug); }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HashOf templates ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template HashOf::~HashOf() { #if defined(_ARMOR) unsigned over_loaded_bins = 0; for (int i = 0; i < m_hashTable.GetLength(); i++) { if (m_hashTable[i] != NULL) { Check_Object(m_hashTable[i]); SortedChainIteratorOf iterator((SortedChainOf*)m_hashTable[i]); if (iterator.GetSize() > 6) over_loaded_bins++; } } Warn(over_loaded_bins != 0); #endif } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HashIterator ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class HashIterator: public SortedIterator { public: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Public interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // // //-------------------------------------------------------------------- // Constructors, Destructor and testing //-------------------------------------------------------------------- // explicit HashIterator(Hash *hash); Iterator* MakeClone(); ~HashIterator(); void TestInstance(); // //-------------------------------------------------------------------- // Iterator methods (see Iterator for full listing) //-------------------------------------------------------------------- // Iterator& First(); Iterator& Last(); Iterator& Next(); Iterator& Previous(); void* ReadAndNextItem(); void* ReadAndPreviousItem(); void* GetCurrentItem(); void* GetNthItem(unsigned index); void Remove(); Plug* FindPlug(const void *value); void* GetPlugValue(); private: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Private interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // void DeleteSortedChainIterator(); void NextSortedChainIterator(unsigned index); // //-------------------------------------------------------------------- // Private data //-------------------------------------------------------------------- // unsigned m_currentPosition; SortedChainIterator *m_vchainIterator; }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HashIteratorOf ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template class HashIteratorOf: public HashIterator { public: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Public interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // // //-------------------------------------------------------------------- // Constructors and Destructor //-------------------------------------------------------------------- // explicit HashIteratorOf(HashOf *hash): HashIterator(hash) {} Iterator* MakeClone() {return new(g_Heap) HashIteratorOf(*this);} // //-------------------------------------------------------------------- // Iterator methods (see Iterator for full listing) //-------------------------------------------------------------------- // T ReadAndNext() {return (T)HashIterator::ReadAndNextItem();} T ReadAndPrevious() {return (T)HashIterator::ReadAndPreviousItem();} T GetCurrent() {return (T)HashIterator::GetCurrentItem();} T GetNth(unsigned index) {return (T)HashIterator::GetNthItem(index);} T Find(const V &value) {return (T)HashIterator::FindPlug(&value);} operator T() {return HashIterator::GetCurrent();} }; }