-
Notifications
You must be signed in to change notification settings - Fork 302
/
hashtable.c
357 lines (281 loc) · 7.46 KB
/
hashtable.c
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <string.h>
#include "hashtable.h"
#include "common.h"
#include "utils.h"
int HashTable_CalculateAppropriateSlotCount(int ElementCount)
{
if( ElementCount > 10 )
{
ElementCount /= 3;
return ROUND(ElementCount, 10) + 6;
} else {
return 3;
}
}
static int ELFHash(char *str)
{
_32BIT_UINT h = 0;
_32BIT_UINT x = 0;
while( *str != '\0' )
{
h += *str;
h <<= 4;
x = h & 0xF0000000;
if( x != 0 )
{
h ^= (x >> 24);
}
h &= ~x;
str++;
}
return (h & 0x7FFFFFFF);
}
int HashTable_Init(HashTable *h, int DataLength, int InitialCount)
{
int loop;
int SlotCount;
if( h == NULL)
return -1;
SlotCount = HashTable_CalculateAppropriateSlotCount(InitialCount);
if( Array_Init(&(h -> NodeChunk), DataLength + sizeof(NodeHead), InitialCount, FALSE, NULL) != 0 )
return 1;
if( Array_Init(&(h -> Slots), sizeof(NodeHead), SlotCount, FALSE, NULL) != 0 )
return 2;
h -> Slots.Used = h -> Slots.Allocated;
for(loop = 0; loop != h -> Slots.Allocated; ++loop)
{
((NodeHead *)Array_GetBySubscript(&(h -> Slots), loop)) -> Next = HASHTABLE_NODE_END;
}
h -> RemovedNodes = -1;
return 0;
}
int HashTable_Init_Manually(HashTable *h,
void *SlotsStartAddress,
_32BIT_INT SlotsCount,
void *NodeChunkStartAddress,
BOOL GrowDown,
_32BIT_INT DataLength
)
{
h -> Slots.Used = SlotsCount;
h -> Slots.DataLength = sizeof(NodeHead);
h -> Slots.Data = SlotsStartAddress;
h -> Slots.Allocated = SlotsCount;
h -> NodeChunk.DataLength = DataLength;
h -> NodeChunk.Data = NodeChunkStartAddress;
h -> NodeChunk.Used = 0;
if( GrowDown == TRUE )
{
h -> NodeChunk.Allocated = -1;
} else {
h -> NodeChunk.Allocated = 0;
}
h -> RemovedNodes = -1;
return 0;
}
int HashTable_CreateNewNode(HashTable *h, NodeHead **Out, void *Boundary /* Only used by grow down array */)
{
int NewNode_i;
NodeHead *NewNode;
Array *NodeChunk;
NodeChunk = &(h -> NodeChunk);
NewNode_i = Array_PushBack(NodeChunk, NULL, Boundary);
if( NewNode_i < 0 )
{
return -1;
}
NewNode = (NodeHead *)Array_GetBySubscript(NodeChunk, NewNode_i);
NewNode -> Next = HASHTABLE_NODE_NEWLY_CREATED;
if( Out != NULL )
{
*Out = NewNode;
}
return NewNode_i;
}
_32BIT_INT HashTable_FindUnusedNode(HashTable *h,
NodeHead **Out,
_32BIT_INT Start /* Initially -1 */,
void *Boundary /* Only used by grow down array */,
BOOL AutoCreateNewNode
)
{
_32BIT_INT Subscript;
NodeHead *Node;
Array *NodeChunk;
NodeChunk = &(h -> NodeChunk);
if( Start == HASHTABLE_FINDUNUSEDNODE_START )
{
Subscript = h -> RemovedNodes;
} else if( Start >= 0 ){
Node = (NodeHead *)Array_GetBySubscript(NodeChunk, Start);
Subscript = Node -> Next;
} else {
if( Out != NULL )
{
*Out = NULL;
}
return HASHTABLE_FINDUNUSEDNODE_FAILED;
}
if( Subscript >= 0 )
{
Node = (NodeHead *)Array_GetBySubscript(NodeChunk, Subscript);
if( Out != NULL )
{
*Out = Node;
}
return Subscript;
}
if( AutoCreateNewNode == TRUE )
{
return HashTable_CreateNewNode(h, Out, Boundary);
} else {
if( Out != NULL )
{
*Out = NULL;
}
return HASHTABLE_FINDUNUSEDNODE_FAILED;
}
}
_32BIT_INT HashTable_FetchNode(HashTable *h, NodeHead *Node)
{
_32BIT_INT NextNode;
Array *NodeChunk;
if( Node -> Next == HASHTABLE_NODE_NEWLY_CREATED )
{
return -1;
}
NodeChunk = &(h -> NodeChunk);
if( Node -> Prev >= 0 )
{
NodeHead *NextRemovedNode;
NextRemovedNode = (NodeHead *)Array_GetBySubscript(NodeChunk, Node -> Prev);
NextRemovedNode -> Next = Node -> Next;
} else {
h -> RemovedNodes = Node -> Next;
}
if( Node -> Next >= 0 )
{
NodeHead *PreviousRemovedNode;
PreviousRemovedNode = (NodeHead *)Array_GetBySubscript(NodeChunk, Node -> Next);
PreviousRemovedNode -> Prev = Node -> Prev;
}
NextNode = Node -> Next;
Node -> Next = HASHTABLE_NODE_UNUSED;
Node -> Prev = HASHTABLE_NODE_UNUSED;
return NextNode;
}
int HashTable_AddByNode(HashTable *h, char *Key, int Node_index, NodeHead *Node)
{
int Slot_i;
NodeHead *Slot;
if( h == NULL || Key == NULL || Node_index < 0 || Node == NULL )
return -1;
Slot_i = ELFHash(Key) % (h -> Slots.Allocated - 1);
Slot = (NodeHead *)Array_GetBySubscript(&(h -> Slots), Slot_i);
if( Slot == NULL )
return -2;
if( Slot -> Next >= 0 )
{
((NodeHead *)Array_GetBySubscript(&(h -> NodeChunk), Slot -> Next)) -> Prev = Node_index;
}
Node -> Next = Slot -> Next;
Node -> Prev = (-1) * (Slot_i + 1);
Slot -> Next = Node_index;
return 0;
}
int HashTable_Add(HashTable *h, char *Key, void *Data)
{
_32BIT_INT Slot_i;
NodeHead *Slot;
_32BIT_INT NewNode_i;
NodeHead *NewNode = NULL;
Slot_i = ELFHash(Key) % (h -> Slots.Allocated - 1);
Slot = (NodeHead *)Array_GetBySubscript(&(h -> Slots), Slot_i);
if( Slot == NULL )
return -2;
NewNode_i = HashTable_FindUnusedNode(h, &NewNode, -1, NULL, TRUE);
if( NewNode_i < 0 )
return -3;
HashTable_FetchNode(h, NewNode);
memcpy(NewNode + 1, Data, h -> NodeChunk.DataLength - sizeof(NodeHead));
return HashTable_AddByNode(h, Key, NewNode_i, NewNode);
}
void HashTable_RemoveNode(HashTable *h, _32BIT_INT SubScriptOfNode, NodeHead *Node)
{
Array *NodeChunk;
NodeChunk = &(h -> NodeChunk);
if( SubScriptOfNode < 0 )
{
SubScriptOfNode = ((char *)Node - (char *)(NodeChunk -> Data)) / (NodeChunk -> DataLength);
if( NodeChunk -> Allocated < 0 )
{
SubScriptOfNode *= (-1);
}
}
if( Node == NULL )
{
Node = (NodeHead *)Array_GetBySubscript(&(h -> NodeChunk), SubScriptOfNode);
}
if( Node -> Next != HASHTABLE_NODE_UNUSED )
{
if( Node -> Next >= 0 )
{
((NodeHead *)Array_GetBySubscript(NodeChunk, Node -> Next)) -> Prev = Node -> Prev;
}
if( Node -> Prev < 0 )
{
/* If prev is a slot */
((NodeHead *)Array_GetBySubscript(&(h -> Slots), (-1) * (Node -> Prev) - 1)) -> Next = Node -> Next;
} else {
/* If prev is a node. */
((NodeHead *)Array_GetBySubscript(NodeChunk, Node -> Prev)) -> Next = Node -> Next;
}
if( SubScriptOfNode != NodeChunk -> Used - 1 )
{
if( h -> RemovedNodes >= 0 )
{
NodeHead *PreviousRemovedNode;
PreviousRemovedNode = (NodeHead *)Array_GetBySubscript(NodeChunk, h -> RemovedNodes);
PreviousRemovedNode -> Prev = SubScriptOfNode;
}
Node -> Next = h -> RemovedNodes;
Node -> Prev = -1;
h -> RemovedNodes = SubScriptOfNode;
} else {
--(NodeChunk -> Used);
}
} else {
if( SubScriptOfNode == NodeChunk -> Used - 1 )
{
--(NodeChunk -> Used);
}
}
}
void *HashTable_Get(HashTable *h, char *Key, void *Start)
{
int Slot_i;
NodeHead *Head;
if( h == NULL || Key == NULL)
return NULL;
if( Start == NULL )
{
Slot_i = ELFHash(Key) % (h -> Slots.Allocated - 1);
Head = (NodeHead *)Array_GetBySubscript(&(h -> Slots), Slot_i);
Head = (NodeHead *)Array_GetBySubscript(&(h -> NodeChunk), Head -> Next);
if( Head == NULL )
return NULL;
return (void *)(Head + 1);
} else {
Head = (NodeHead *)Start - 1;
Head = (NodeHead *)Array_GetBySubscript(&(h -> NodeChunk), Head -> Next);
if( Head == NULL )
return NULL;
return (void *)(Head + 1);
}
}
void HashTable_Free(HashTable *h)
{
Array_Free(&(h -> NodeChunk));
Array_Free(&(h -> Slots));
h -> RemovedNodes = -1;
}