-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmapReal.c
171 lines (153 loc) · 3.8 KB
/
hashmapReal.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned int uint;
typedef int HashType;
typedef int KeyType;
typedef struct hashnode_t {
HashType val;
struct hashnode_t *next;
KeyType key;
} HashNode;
typedef struct hashhead_t {
HashNode *first;
int size;
} HashHead;
typedef struct hashmap_t {
HashHead *values;
int length;
int size;
} HashMap;
void hashmapForEach(HashMap *map)
{
int i;
HashNode *node;
HashType *value;
for (i = 0; i < map->size; i++) {
node = map->values[i].first;
while (node) {
printf("key = %d val = %d\n", node->key, node->val);
node = node->next;
}
}
}
/*static inline uint _hashmapHash(KeyType key)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*key)
hash = hash * seed + (*key++);
return (hash & 0x7FFFFFFF);
}
static inline int _hashmapKeyEqual(KeyType key1, KeyType key2)
{
return !strcmp(key1, key2);
}
static inline void _hashmapKeyCopy(HashNode *node, KeyType key)
{
int size;
size = strlen(key) + 1;
node->key = (char *)malloc(size);
strcpy(node->key, key);
}*/
static inline uint _hashmapHash(KeyType key)
{
return key > 0 ? key : -key;
}
static inline int _hashmapKeyEqual(KeyType key1, KeyType key2)
{
return key1 == key2;
}
static inline void _hashmapKeyCopy(HashNode *node, KeyType key)
{
node->key = key;
}
HashMap *hashmapCreate(int size)
{
HashMap *map = (HashMap *)malloc(sizeof(HashMap));
int i;
map->length = size;
map->values = (HashHead *)malloc(sizeof(HashHead) * map->length);
for (i = 0; i < size; i++) {
map->values[i].first = NULL;
map->values[i].size = 0;
}
map->size = 0;
return map;
}
void hashmapPut(HashMap *map, KeyType key, HashType val)
{
uint index = _hashmapHash(key) % map->length;
HashNode *node, *curr, *last;
if (map->values[index].size == 0) {
node = (HashNode *)malloc(sizeof(HashNode));
node->next = NULL;
node->val = val;
_hashmapKeyCopy(node, key);
map->values[index].first = node;
} else {
curr = map->values[index].first;
while (curr->next) {
if (_hashmapKeyEqual(key, curr->key)) {
curr->val = val;
return;
}
curr = curr->next;
}
if (_hashmapKeyEqual(key, curr->key)) {
curr->val = val;
return;
}
node = (HashNode *)malloc(sizeof(HashNode));
node->next = NULL;
node->val = val;
_hashmapKeyCopy(node, key);
curr->next = node;
}
map->values[index].size++;
map->size++;
}
int getSize(HashMap *map)
{
return map->size;
}
void hashmapRemove(HashMap *map, KeyType key)
{
uint index = _hashmapHash(key) % map->length;
HashNode *node;
HashNode *temp;
if (!map->values[index].size) {
return;
}
if (_hashmapKeyEqual(map->values[index].first->key, key)) {
temp = map->values[index].first;
map->values[index].first = temp->next;
free(temp);
map->values[index].size--;
map->size--;
return;
}
node = map->values[index].first;
while (node->next) {
if (_hashmapKeyEqual(node->next->key, key)) {
temp = node->next;
node->next = temp->next;
free(temp);
map->values[index].size--;
map->size--;
return;
}
node = node->next;
}
}
HashType *hashmapGet(HashMap *map, KeyType key)
{
uint index = _hashmapHash(key) % map->length;
HashNode *node = map->values[index].first;
while (node) {
if (_hashmapKeyEqual(node->key, key))
return &node->val;
node = node->next;
}
return NULL;
}