aboutsummaryrefslogtreecommitdiff
path: root/src/ordhash.h
blob: e946f9570c0025308841bfc2f58eef25be90098e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef ORD_HASH_H
#define ORD_HASH_H

#include "hash.h"
#include "tqsort.h"

template<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> >
class OrdHash : public Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>
{
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<key,value> getAtPos( uint32_t nPos )
	{
		return Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>::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<typename OrdHash<key, value, cmpfnc, sizecalc, keyalloc, valuealloc, challoc>::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