summaryrefslogtreecommitdiff
path: root/src/ordhash.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-11-27 10:13:44 +0000
committerMike Buland <eichlan@xagasoft.com>2006-11-27 10:13:44 +0000
commit3025ed54309f793c6afbcbc9a564f71cc741f2ef (patch)
treeb579210f2f894bfeb7562e3339aea58c377c26b7 /src/ordhash.h
parentdd049c4b3bbe6a605e41b043d933c02cb8497968 (diff)
downloadlibbu++-3025ed54309f793c6afbcbc9a564f71cc741f2ef.tar.gz
libbu++-3025ed54309f793c6afbcbc9a564f71cc741f2ef.tar.bz2
libbu++-3025ed54309f793c6afbcbc9a564f71cc741f2ef.tar.xz
libbu++-3025ed54309f793c6afbcbc9a564f71cc741f2ef.zip
Added the new OrdHash, check the test file for an example.
Diffstat (limited to 'src/ordhash.h')
-rw-r--r--src/ordhash.h104
1 files changed, 104 insertions, 0 deletions
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 @@
1#ifndef ORD_HASH_H
2#define ORD_HASH_H
3
4#include "hash.h"
5#include "tqsort.h"
6
7template<typename key, typename value, typename cmpfnc, typename sizecalc = __calcNextTSize_fast, typename keyalloc = std::allocator<key>, typename valuealloc = std::allocator<value>, typename challoc = std::allocator<uint32_t> >
8class OrdHash : public Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>
9{
10public:
11 OrdHash() :
12 bSorted( false ),
13 aData( NULL )
14 {
15 }
16
17 virtual ~OrdHash()
18 {
19 }
20
21protected:
22 virtual void invalidate()
23 {
24 bSorted = false;
25 delete[] aData;
26 aData = NULL;
27 }
28
29 virtual void onInsert()
30 {
31 invalidate();
32 }
33
34 virtual void onUpdate()
35 {
36 invalidate();
37 }
38
39 virtual void onDelete()
40 {
41 invalidate();
42 }
43
44 virtual void onReHash()
45 {
46 invalidate();
47 }
48
49 virtual std::pair<key,value> getAtPos( uint32_t nPos )
50 {
51 return Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>::getAtPos( aData[nPos].nIndex );
52 }
53
54 virtual void buildIndex()
55 {
56 aData = new struct ind[this->nFilled];
57 uint32_t k = 0;
58 for( uint32_t j = 0; j < this->nCapacity; j++ )
59 {
60 if( this->isFilled( j ) )
61 {
62 if( !this->isDeleted( j ) )
63 {
64 aData[k].pVal = &(this->aValues[j]);
65 aData[k].nIndex = j;
66 k++;
67 }
68 }
69 }
70
71 tqsort<typename OrdHash<key, value, cmpfnc, sizecalc, keyalloc, valuealloc, challoc>::ind, cmpfnc, value **>( aData, this->nFilled );
72
73 bSorted = true;
74 }
75
76 virtual uint32_t getFirstPos( bool &bFinished )
77 {
78 if( bSorted == false )
79 buildIndex();
80
81 return 0;
82 }
83
84 virtual uint32_t getNextPos( uint32_t nPos, bool &bFinished )
85 {
86 if( nPos+1 >= this->nFilled )
87 {
88 bFinished = true;
89 return 0;
90 }
91 return ++nPos;
92 }
93public:
94 typedef struct ind
95 {
96 value *pVal;
97 uint32_t nIndex;
98 } ind;
99private:
100 bool bSorted;
101 ind *aData;
102};
103
104#endif