-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
58 lines (52 loc) · 1.47 KB
/
hash.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
#include "hash.h"
#include "action.h"
#include <stdlib.h>
#include <string.h>
parent_action_t* action_hash_table[HASH_TABLE_SIZE];
static size_t action_name_hash(const char* key) {
size_t result = 0;
for (int i = 0; key[i]; i++)
result ^= (size_t)(key[i]) << i * 5 % sizeof(size_t);
return result % HASH_TABLE_SIZE;
}
parent_action_t* get_action(const char* key) {
parent_action_t* current = action_hash_table[action_name_hash(key)];
while (current) {
if (!strcmp(key, current->name))
break;
current = current->value_next;
}
return current;
}
void put_action(parent_action_t* val) {
const char* key = val->name;
parent_action_t** toput = &action_hash_table[action_name_hash(key)];
parent_action_t* current = *toput;
while (current) {
if (!strcmp(key, current->name)) {
val->value_next = current->value_next;
free(current->name);
free(current->command);
free(current);
break;
}
toput = ¤t->value_next;
current = *toput;
}
*toput = val;
}
//this is definitely not right but i'm just putting it here for reference
/* for(int i =0; i < HASH_TABLE_SIZE; i++ ) { */
/* free_elements(action_hash_table[i]); */
/* } */
/* } */
/* void free_elements(parent_action_t *parent) { */
/* if(parent) { */
/* free_elements(parent->value_next); */
/* free(parent->children); */
/* free(parent->command); */
/* free(parent->name); */
/* free(parent->command); */
/* free(parent); */
/* } */
/* } */