forked from InfiniteRasa/Game-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashTable_uint32Iterable.cpp
179 lines (166 loc) · 4.89 KB
/
hashTable_uint32Iterable.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<Windows.h>
#include"hashTable.h"
#ifndef memMgr_alloc
#define memMgr_alloc(x,y) malloc(y)
#define memMgr_free(x,y) free(y)
#endif
#define MAXSCAN_LENGTH 32 // maximal number of filled hashentrys in a row
// uint32 tables are growable
void hashTable_init(HashTable_uint32Iterable_t *hashTable, int itemLimit)
{
hashTable->size = itemLimit;
hashTable->count = 0;
hashTable->entrys = (_HashTable_uint32Iterable_entry_t*)memMgr_alloc(NULL, sizeof(_HashTable_uint32Iterable_entry_t)*itemLimit);
hashTable->itemKeyArray = (unsigned int*)memMgr_alloc(NULL, sizeof(unsigned int)*itemLimit);
hashTable->itemValueArray = (void**)memMgr_alloc(NULL, sizeof(void*)*itemLimit);
// reset all items
for(int i=0; i<itemLimit; i++)
{
hashTable->entrys[i].itemIndex = 0; // 1-indexed
hashTable->itemKeyArray[i] = 0;
hashTable->itemValueArray[i] = 0;
// hashTable->entrys[i].originalValue = 0xFFFFFFFF;
// hashTable->entrys[i].item = NULL;
}
}
void hashTable_destroy(HashTable_uint32Iterable_t *hashTable)
{
memMgr_free(NULL, (void*)hashTable->itemKeyArray);
memMgr_free(NULL, (void*)hashTable->itemValueArray);
memMgr_free(NULL, (void*)hashTable->entrys);
}
void hashTable_enlarge(HashTable_uint32Iterable_t *hashTable)
{
// create new hashTable with double size
HashTable_uint32Iterable_t hashTable2;
hashTable_init(&hashTable2, hashTable->size*2);
// move entrys from old table to new table
for(unsigned int i=0; i<hashTable->size; i++)
{
if( hashTable->entrys[i].itemIndex )
{
int index = hashTable->entrys[i].itemIndex - 1;
if( hashTable->entrys[i].itemIndex == -1 ) // deleted entry
continue;
hashTable_set(&hashTable2, hashTable->itemKeyArray[index], (void*)hashTable->itemValueArray[index]);//hashTable->entrys[i].item);
}
}
// destroy old table
hashTable_destroy(hashTable);
// set new table
hashTable->count = hashTable2.count;
hashTable->size = hashTable2.size;
hashTable->entrys = hashTable2.entrys;
hashTable->itemKeyArray = hashTable2.itemKeyArray;
hashTable->itemValueArray = hashTable2.itemValueArray;
}
void _hashTable_updateReference(HashTable_uint32Iterable_t *hashTable, unsigned int key, int oldReference, int newReference)
{
DWORD hashA = key + key*3 + key*7 + key*11;
// get entry
int index = hashA%hashTable->size;
for(int i=0; i<hashTable->size; i++)
{
int ridx = hashTable->entrys[index].itemIndex;
if( ridx == 0 )
{
return;
}
else if( ridx == oldReference )
{
// update entry
hashTable->entrys[index].itemIndex = newReference;
return;
}
index++;
index%=hashTable->size;
}
}
bool hashTable_set(HashTable_uint32Iterable_t *hashTable, unsigned int key, void *item)
{
DWORD hashA = key + key*3 + key*7 + key*11;
// get entry
int index = hashA%hashTable->size;
for(int i=0; i<MAXSCAN_LENGTH; i++)
{
int ridx = hashTable->entrys[index].itemIndex;
if( ridx == 0 )
{
// set entry if valid
if( item )
{
hashTable->entrys[index].itemIndex = hashTable->count+1;
hashTable->itemKeyArray[hashTable->count] = key;
hashTable->itemValueArray[hashTable->count] = item;
hashTable->count++;
if( hashTable->count >= (hashTable->size*3/4) )
{
// enlarge table
hashTable_enlarge(hashTable);
}
}
else return false;
return true;
}
else if( hashTable->itemKeyArray[ridx-1] == key )
{
// update entry
if( hashTable->itemValueArray[ridx-1] && item == NULL )
{
// remove entry
if( hashTable->count != ridx ) // is not the last entry
{
// move last entry to current
hashTable->itemKeyArray[ridx-1] = hashTable->itemKeyArray[hashTable->count-1];
hashTable->itemValueArray[ridx-1] = hashTable->itemValueArray[hashTable->count-1];
// update id
_hashTable_updateReference(hashTable, hashTable->itemKeyArray[hashTable->count-1], hashTable->count, ridx);
}
hashTable->entrys[index].itemIndex = -1; // mark as deleted
hashTable->count--;
}
return true;
}
index++;
index%=hashTable->size;
}
// no free entry
hashTable_enlarge(hashTable);
return hashTable_set(hashTable, key, item);
}
void *hashTable_get(HashTable_uint32Iterable_t *hashTable, unsigned int key)
{
DWORD hashA = key + key*3 + key*7 + key*11;
// get entry
if( hashTable->size == 0 )
return NULL; // hashTable not initialized or empty
int index = hashA%hashTable->size;
for(int i=0; i<MAXSCAN_LENGTH; i++)
{
int ridx = hashTable->entrys[index].itemIndex;
if( !ridx )
return NULL;
if( ridx != -1 )
{
if( hashTable->itemKeyArray[ridx-1] == key )
{
return hashTable->itemValueArray[ridx-1];
}
}
index++;
index %= hashTable->size;
}
return NULL;
}
void** hashTable_getValueArray(HashTable_uint32Iterable_t *hashTable)
{
return hashTable->itemValueArray;
}
unsigned int* hashTable_getKeyArray(HashTable_uint32Iterable_t *hashTable)
{
return hashTable->itemKeyArray;
}
unsigned int hashTable_getCount(HashTable_uint32Iterable_t *hashTable)
{
return hashTable->count;
}