#pragma once #include "srtskt.h" #include "vchain.h" class Hash : public SortedSocket { friend class HashIterator; public: Hash(CollectionSize size, Node *node, Logical has_unique_entries); ~Hash(); Logical TestInstance() const; static Logical TestClass(); static Logical ProfileClass(); protected: void AddImplementation(Plug *plug); void AddValueImplementation(Plug *plug, const void *value); Plug *FindImplementation(const void *value); VChain **hashTable; CollectionSize hashTableSize; private: virtual VChain* MakeVChain(); virtual IteratorPosition GetHashIndex(const void *value); void BuildHashTable(); }; template class HashOf : public Hash { public: HashOf(CollectionSize size, Node *node, Logical has_unique_entries); ~HashOf(); void AddValue(T plug, const V &value) { AddValueImplementation(Cast_Object(Plug*, plug), &value); } T Find(const V &value) { return (T)FindImplementation(&value); } private: VChain *MakeVChain(); IteratorPosition GetHashIndex(const void *value); }; template HashOf::HashOf(CollectionSize size, Node *node, Logical has_unique_entries) : Hash(size, node, has_unique_entries) { } template HashOf::~HashOf() { } template VChain *HashOf::MakeVChain() { return new VChainOf(GetReleaseNode(), HasUniqueEntries()); } template IteratorPosition HashOf::GetHashIndex(const void *value) { Check_Pointer(value); return (IteratorPosition(*(V*)value) % hashTableSize); // return ((IteratorPosition)*(V*)value % hashTableSize); } class HashIterator : public SortedIterator { public: HashIterator(Hash *hash); ~HashIterator(); Logical TestInstance() const; void First(); void Last(); void Next(); void Previous(); CollectionSize GetSize(); void Remove(); protected: #if 0 void *ReadAndNextImplementation(); void *ReadAndPreviousImplementation(); #endif void *GetCurrentImplementation(); #if 0 void *GetNthImplementation(CollectionSize index); #endif Plug *FindImplementation(const void *value); private: void ReceiveMemo(IteratorMemo memo, void *content); void DeleteVChainIterator(); void NextVChainIterator(IteratorPosition index); VChain **hashTable; CollectionSize hashTableSize; IteratorPosition currentPosition; VChainIterator *vchainIterator; }; template class HashIteratorOf : public HashIterator { public: HashIteratorOf(HashOf *hash); HashIteratorOf(HashOf &hash); ~HashIteratorOf(); T ReadAndNext() { return (T)ReadAndNextImplementation(); } T ReadAndPrevious() { return (T)ReadAndPreviousImplementation(); } T GetCurrent() { return (T)GetCurrentImplementation(); } T GetNth(CollectionSize index) { return (T)GetNthImplementation(index); } T Find(const V &value) { return (T)FindImplementation(&value); } }; template HashIteratorOf::HashIteratorOf(HashOf *hash) : HashIterator(hash) { } template HashIteratorOf::HashIteratorOf(HashOf &hash) : HashIterator(&hash) { } template HashIteratorOf::~HashIteratorOf() { }