From 3025ed54309f793c6afbcbc9a564f71cc741f2ef Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 27 Nov 2006 10:13:44 +0000 Subject: Added the new OrdHash, check the test file for an example. --- src/ordhash.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/ordhash.h (limited to 'src/ordhash.h') diff --git a/src/ordhash.h b/src/ordhash.h new file mode 100644 index 0000000..e946f95 --- /dev/null +++ b/src/ordhash.h @@ -0,0 +1,104 @@ +#ifndef ORD_HASH_H +#define ORD_HASH_H + +#include "hash.h" +#include "tqsort.h" + +template, typename valuealloc = std::allocator, typename challoc = std::allocator > +class OrdHash : public Hash +{ +public: + OrdHash() : + bSorted( false ), + aData( NULL ) + { + } + + virtual ~OrdHash() + { + } + +protected: + virtual void invalidate() + { + bSorted = false; + delete[] aData; + aData = NULL; + } + + virtual void onInsert() + { + invalidate(); + } + + virtual void onUpdate() + { + invalidate(); + } + + virtual void onDelete() + { + invalidate(); + } + + virtual void onReHash() + { + invalidate(); + } + + virtual std::pair getAtPos( uint32_t nPos ) + { + return Hash::getAtPos( aData[nPos].nIndex ); + } + + virtual void buildIndex() + { + aData = new struct ind[this->nFilled]; + uint32_t k = 0; + for( uint32_t j = 0; j < this->nCapacity; j++ ) + { + if( this->isFilled( j ) ) + { + if( !this->isDeleted( j ) ) + { + aData[k].pVal = &(this->aValues[j]); + aData[k].nIndex = j; + k++; + } + } + } + + tqsort::ind, cmpfnc, value **>( aData, this->nFilled ); + + bSorted = true; + } + + virtual uint32_t getFirstPos( bool &bFinished ) + { + if( bSorted == false ) + buildIndex(); + + return 0; + } + + virtual uint32_t getNextPos( uint32_t nPos, bool &bFinished ) + { + if( nPos+1 >= this->nFilled ) + { + bFinished = true; + return 0; + } + return ++nPos; + } +public: + typedef struct ind + { + value *pVal; + uint32_t nIndex; + } ind; +private: + bool bSorted; + ind *aData; +}; + +#endif -- cgit v1.2.3