forked from InfiniteRasa/Game-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashTableSynced.cpp
243 lines (229 loc) · 6.71 KB
/
hashTableSynced.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include"framework.h"
#define MAXSCAN_LENGTH 32 // maximal number of filled hashentrys in a row
// uint32 tables are growable
void hashTable_init(hashTableSynced_t *hashTable, sint32 itemLimit)
{
hashTable->size = itemLimit;
hashTable->count = 0;
hashTable->entrys = (_HashTable_uint32Iterable_entry_t*)malloc(sizeof(_HashTable_uint32Iterable_entry_t)*itemLimit);
hashTable->itemKeyArray = (uint32*)malloc(sizeof(uint32)*itemLimit);
hashTable->itemValueArray = (void**)malloc(sizeof(void*)*itemLimit);
// reset all items
for(sint32 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;
}
Thread::InitMutex(&hashTable->criticalSection);
}
void hashTable_destroy(hashTableSynced_t *hashTable)
{
free((void*)hashTable->itemKeyArray);
free((void*)hashTable->itemValueArray);
free((void*)hashTable->entrys);
}
void hashTable_clear(hashTableSynced_t *hashTable)
{
Thread::LockMutex(&hashTable->criticalSection);
hashTable->count = 0;
for(uint32 i=0; i<hashTable->size; i++)
{
hashTable->entrys[i].itemIndex = 0;
hashTable->itemKeyArray[i] = 0;
hashTable->itemValueArray[i] = 0;
}
Thread::UnlockMutex(&hashTable->criticalSection);
}
void hashTable_enlarge(hashTableSynced_t *hashTable)
{
// create new hashTable with double size
hashTableSynced_t hashTable2;
hashTable_init(&hashTable2, hashTable->size*2);
// move entrys from old table to new table
for(uint32 i=0; i<hashTable->size; i++)
{
if( hashTable->entrys[i].itemIndex )
{
sint32 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(hashTableSynced_t *hashTable, uint32 key, sint32 oldReference, sint32 newReference)
{
DWORD hashA = key + key*3 + key*7 + key*11;
// get entry
sint32 index = hashA%hashTable->size;
for(DWORD i=0; i<hashTable->size; i++)
{
sint32 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(hashTableSynced_t *hashTable, uint32 key, void *item)
{
Thread::LockMutex(&hashTable->criticalSection);
DWORD hashA = key + key*3 + key*7 + key*11;
// get entry
sint32 index = hashA%hashTable->size;
for(sint32 i=0; i<MAXSCAN_LENGTH; i++)
{
sint32 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
{
Thread::UnlockMutex(&hashTable->criticalSection);
return false;
}
Thread::UnlockMutex(&hashTable->criticalSection);
return true;
}
else if( hashTable->itemKeyArray[ridx-1] == key )
{
// update entry
//if( item )
// __debugbreak();
sint32 itemIndex = ridx-1;
hashTable->itemValueArray[itemIndex] = item;
//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--;
//}
Thread::UnlockMutex(&hashTable->criticalSection);
return true;
}
index++;
index%=hashTable->size;
}
// no free entry
hashTable_enlarge(hashTable);
#ifdef USE_PTHREADS
Thread::UnlockMutex(&hashTable->criticalSection); // pthread specific
#endif
bool result = hashTable_set(hashTable, key, item);
Thread::UnlockMutex(&hashTable->criticalSection);
return result;
}
bool hashTable_set(hashTableSynced_t *hashTable, sint8 *key, void *item)
{
uint32 keyValue = 0xF94A9E53;
uint32 keyB = 0x2D48C92E;
while( *key )
{
keyValue += (uint32)*key;
keyB ^= ((uint32)*key)*7;
keyValue = (keyValue<<3)|(keyValue>>29);
keyB = (keyB<<7)|(keyB>>25);
keyValue += keyB;
key++;
}
return hashTable_set(hashTable, keyValue, item);
}
void *hashTable_get(hashTableSynced_t *hashTable, uint32 key)
{
Thread::LockMutex(&hashTable->criticalSection);
DWORD hashA = key + key*3 + key*7 + key*11;
// get entry
if( hashTable->size == 0 )
{
Thread::UnlockMutex(&hashTable->criticalSection);
return NULL; // hashTable not initialized or empty
}
sint32 index = hashA%hashTable->size;
for(sint32 i=0; i<MAXSCAN_LENGTH; i++)
{
sint32 ridx = hashTable->entrys[index].itemIndex;
if( !ridx )
{
Thread::UnlockMutex(&hashTable->criticalSection);
return NULL;
}
if( ridx != -1 )
{
if( hashTable->itemKeyArray[ridx-1] == key )
{
Thread::UnlockMutex(&hashTable->criticalSection);
return hashTable->itemValueArray[ridx-1];
}
}
index++;
index %= hashTable->size;
}
Thread::UnlockMutex(&hashTable->criticalSection);
return NULL;
}
void *hashTable_get(hashTableSynced_t *hashTable, sint8 *key)
{
uint32 keyValue = 0xF94A9E53;
uint32 keyB = 0x2D48C92E;
while( *key )
{
keyValue += (uint32)*key;
keyB ^= ((uint32)*key)*7;
keyValue = (keyValue<<3)|(keyValue>>29);
keyB = (keyB<<7)|(keyB>>25);
keyValue += keyB;
key++;
}
return hashTable_get(hashTable, keyValue);
}
void** hashTable_getValueArray(hashTableSynced_t *hashTable)
{
return hashTable->itemValueArray;
}
uint32* hashTable_getKeyArray(hashTableSynced_t *hashTable)
{
return hashTable->itemKeyArray;
}
uint32 hashTable_getCount(hashTableSynced_t *hashTable)
{
return hashTable->count;
}