-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashSet.h
222 lines (206 loc) · 5.26 KB
/
HashSet.h
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
#ifndef _HASHSET_H_
#define _HASHSET_H_
#include "Node.h"
#include "helper.h"
#define DEFAULT_INT_HASHSET {0, 0, NULL, HashSet_int_contains, HashSet_int_insert, \
HashSet_int_erase, HashSet_size, HashSet_int_destroy}
#define DEFAULT_LONG_HASHSET {0, 0, NULL, HashSet_long_contains, HashSet_long_insert, \
HashSet_long_erase, HashSet_size, HashSet_long_destroy}
#define DEFAULT_STRING_HASHSET {0, 0, NULL, HashSet_String_contains, HashSet_String_insert, \
HashSet_String_erase, HashSet_size, HashSet_String_destroy}
#define DEFAULT_PTR_HASHSET {0, 0, NULL, HashSet_ptr_contains, HashSet_ptr_insert, \
HashSet_ptr_erase, HashSet_size, HashSet_ptr_destroy}
#define HashSet(T) struct HashSet_##T { \
int size_; \
int max_; \
Node_##T ** data_; \
int (*contains)(T); \
void (*insert)(T); \
void (*erase)(T); \
int (*size)(); \
void (*destroy)(); \
}
int HashSet_size() {
return *(int*)this;
}
#define HashSet_insert(T) void HashSet_##T##_insert(T val) { \
int * size_ptr = this; \
int * max_ptr = this + 4; \
void ** data_ptr = this + 8; \
Node_##T ** data = *(data_ptr); \
if (*size_ptr == *max_ptr / 4 * 3) { \
int max = *max_ptr; \
int new_max = 2 * max > 16 ? 2 * max : 16; \
Node_##T ** new_data = malloc(new_max * sizeof(ptr)); \
for (int i = 0; i < new_max; ++i) \
new_data[i] = NULL; \
if (data != NULL) { \
for (int i = 0; i < max; ++i) { \
Node_##T * head = data[i]; \
while (head) { \
Node_##T * next = head->next; \
int bucket = hash_##T(head->value) & (new_max - 1); \
if (data[bucket] == NULL) { \
data[bucket] = head; \
head->pre = NULL; \
head->next = NULL; \
} else { \
head->next = data[bucket]; \
head->pre = NULL; \
data[bucket]->pre = head; \
data[bucket] = head; \
} \
head = next; \
} \
} \
free(data); \
} \
*data_ptr = new_data; \
data = new_data; \
*max_ptr = new_max; \
} \
int bucket = hash_##T(val) & (*max_ptr - 1); \
Node_##T * head = data[bucket]; \
while (head) { \
if (equals_##T(head->value, val)) return; \
head = head->next; \
} \
Node_##T * node = malloc(sizeof(Node_##T)); \
node->pre = NULL; \
node->value = val; \
if (data[bucket] == NULL) { \
data[bucket] = node; \
node->next = NULL; \
} else { \
node->next = data[bucket]; \
data[bucket]->pre = node; \
data[bucket] = node; \
} \
++(*size_ptr); \
}
#define HashSet_contains(T) int HashSet_##T##_contains(T val) { \
int mask = *(int*)(this + 4) - 1; \
Node_##T ** data = *(void**)(this + 8); \
int flag = 0; \
if (data == NULL) \
return 0; \
int bucket = hash_##T(val) & mask; \
Node_##T * head = data[bucket]; \
while (head != NULL) { \
if (equals_##T(head->value, val)) { \
flag = 1; \
break; \
} \
head = head->next; \
} \
return flag; \
}
#define HashSet_erase(T) void HashSet_##T##_erase(T val) { \
int mask = *(int*)(this + 4) - 1; \
Node_##T ** data = *(void**)(this + 8); \
if (data == NULL) \
return; \
int bucket = hash_##T(val) & mask; \
Node_##T * head = data[bucket]; \
while (head != NULL) { \
if (equals_##T(head->value, val)) { \
if (head->pre == NULL) { \
data[bucket] = head->next; \
} else if (head->next == NULL) { \
head->pre->next = NULL; \
} else { \
head->pre->next = head->next; \
head->next->pre = head->pre; \
} \
free(head); \
--(*(int*)this); \
break; \
} \
head = head->next; \
} \
return; \
}
#define HashSet_destroy(T) void HashSet_##T##_destroy() { \
int * size_ptr = this; \
int * max_ptr = this + 4; \
void ** data_ptr = this + 8; \
Node_##T ** data = *(data_ptr); \
int max = *max_ptr; \
for (int i = 0; i < max; ++i) { \
Node_##T * head = data[i]; \
while (head) { \
Node_##T * next = head->next; \
free(head); \
head = next; \
} \
} \
free(data); \
*data_ptr = NULL; \
*max_ptr = *size_ptr = 0; \
}
void HashSet_String_erase(String s) {
void * cur_this = this;
int mask = *(int*)(this + 4) - 1;
Node_String ** data = *(void**)(this + 8);
if (data == NULL)
return;
int bucket = hash_String(s) & mask;
Node_String * head = data[bucket];
while (head != NULL) {
if (equals_String(head->value, s)) {
if (head->pre == NULL) {
data[bucket] = head->next;
} else if (head->next == NULL) {
head->pre->next = NULL;
} else {
head->pre->next = head->next;
head->next->pre = head->pre;
}
Object.load(&head->value);
head->value.destroy();
this = cur_this;
free(head);
--(*(int*)this);
break;
}
head = head->next;
}
return;
}
void HashSet_String_destroy() {
void * cur_this = this;
int * size_ptr = this;
int * max_ptr = this + 4;
void ** data_ptr = this + 8;
Node_String ** data = *(data_ptr);
int max = *max_ptr;
for (int i = 0; i < max; ++i) {
Node_String * head = data[i];
while (head) {
Node_String * next = head->next;
Object.load(&head->value);
head->value.destroy();
free(head);
head = next;
}
}
free(data);
*data_ptr = NULL;
*max_ptr = *size_ptr = 0;
this = cur_this;
}
HashSet_contains(int);
HashSet_contains(long);
HashSet_contains(ptr);
HashSet_contains(String);
HashSet_insert(int);
HashSet_insert(long);
HashSet_insert(ptr);
HashSet_insert(String);
HashSet_erase(int);
HashSet_erase(long);
HashSet_erase(ptr);
HashSet_destroy(int);
HashSet_destroy(long);
HashSet_destroy(ptr);
#endif