-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhashbag.h
198 lines (189 loc) · 9.38 KB
/
hashbag.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
#include "dynarr.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#define HASHBAG_MINIMUM_NUMBER_OF_BUCKETS 4
#define _DECLARE_HASHBAG_DS(HASHBAG_TYPE, \
HASHBAG_NAME, \
HASHBAG_TYPE_dynarr, \
HASHBAG_TYPE_dynarr_dynarr, \
HASHBAG_TYPE_dynarr_eq, \
MODIFIER, \
MALLOC_FUN_NAME, \
FREE_FUN_NAME, \
EQ_FUN_NAME, \
HASH_FUN_NAME) \
\
\
DECLARE_DYNARR_DS(HASHBAG_TYPE, MODIFIER, MALLOC_FUN_NAME, FREE_FUN_NAME, EQ_FUN_NAME, 1) \
\
MODIFIER bool HASHBAG_TYPE_dynarr_eq(HASHBAG_TYPE_dynarr* o1, HASHBAG_TYPE_dynarr* o2) { \
DS_SUPPRESS_WARNING(HASHBAG_TYPE_dynarr_eq); \
assert(0 && "should never be used"); \
return false; \
} \
\
DECLARE_DYNARR_DS(HASHBAG_TYPE_dynarr, MODIFIER, MALLOC_FUN_NAME, FREE_FUN_NAME, HASHBAG_TYPE_dynarr_eq, HASHBAG_MINIMUM_NUMBER_OF_BUCKETS) \
\
typedef struct HASHBAG_NAME { \
size_t size; \
HASHBAG_TYPE_dynarr_dynarr buckets; \
} HASHBAG_NAME; \
\
MODIFIER void HASHBAG_NAME ## _init(HASHBAG_NAME* h) { \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _init); \
h->size = 0; \
HASHBAG_TYPE ## _dynarr_dynarr_init(&h->buckets); \
HASHBAG_TYPE ## _dynarr_dynarr_push_void(&h->buckets, h->buckets.capacity); \
size_t number_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
for (size_t i = 0; i < number_of_buckets; i++) { \
HASHBAG_TYPE ## _dynarr_init(HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, i)); \
} \
} \
\
MODIFIER void HASHBAG_NAME ## _print(HASHBAG_NAME* h); \
\
MODIFIER HASHBAG_NAME* HASHBAG_NAME ## _new(){ \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _new); \
HASHBAG_NAME* new_hashbag = (HASHBAG_NAME*)MALLOC_FUN_NAME(sizeof(HASHBAG_NAME)); \
HASHBAG_NAME ## _init(new_hashbag); \
return new_hashbag; \
} \
\
MODIFIER void HASHBAG_NAME ## _destroy(HASHBAG_NAME* h) { \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _destroy); \
size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
for (size_t i = 0; i < nr_of_buckets; i++) { \
HASHBAG_TYPE ## _dynarr_destroy(HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, i)); \
} \
HASHBAG_TYPE ## _dynarr_dynarr_destroy(&h->buckets); \
} \
\
MODIFIER void HASHBAG_NAME ## _free(HASHBAG_NAME* h) { \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _free); \
HASHBAG_NAME ## _destroy(h); \
FREE_FUN_NAME(h); \
} \
\
MODIFIER void ___ ## HASHBAG_NAME ## _adjust_number_of_buckets(HASHBAG_NAME* h, \
size_t nr_of_buckets, \
size_t new_nr_of_buckets) { \
DS_SUPPRESS_WARNING(___ ## HASHBAG_NAME ## _adjust_number_of_buckets); \
HASHBAG_TYPE ## _dynarr_dynarr new_buckets; \
HASHBAG_TYPE ## _dynarr_dynarr_init(&new_buckets); \
HASHBAG_TYPE ## _dynarr_dynarr_push_void(&new_buckets, new_nr_of_buckets); \
for (size_t i = 0; i < new_nr_of_buckets; i++) { \
HASHBAG_TYPE ## _dynarr_init(HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&new_buckets, i)); \
} \
for (size_t i = 0; i < nr_of_buckets; i++) { \
HASHBAG_TYPE_dynarr* bucket = HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, i); \
size_t bucket_size = HASHBAG_TYPE ## _dynarr_size(bucket); \
for (size_t j = 0; j < bucket_size; j++) { \
HASHBAG_TYPE* item = HASHBAG_TYPE ## _dynarr_item_at_ptr(bucket, j); \
size_t hash_v = HASH_FUN_NAME(item); \
size_t target_bucket_index = hash_v % new_nr_of_buckets; \
HASHBAG_TYPE_dynarr * target_bucket = HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&new_buckets, target_bucket_index); \
HASHBAG_TYPE ## _dynarr_push(target_bucket, *item); \
} \
HASHBAG_TYPE ## _dynarr_destroy(bucket); \
} \
HASHBAG_TYPE ## _dynarr_dynarr_destroy(&h->buckets); \
h->buckets = new_buckets; \
HASHBAG_TYPE ## _dynarr_dynarr_fix_after_move(&h->buckets); \
if (new_nr_of_buckets == HASHBAG_MINIMUM_NUMBER_OF_BUCKETS) { \
for(size_t i = 0; i < HASHBAG_MINIMUM_NUMBER_OF_BUCKETS; i++) { \
HASHBAG_TYPE ## _dynarr_fix_after_move(HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, i)); \
} \
} \
} \
\
MODIFIER void ___ ## HASHBAG_NAME ## _expand(HASHBAG_NAME* h) { \
DS_SUPPRESS_WARNING( ___ ## HASHBAG_NAME ## _expand); \
size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
if (nr_of_buckets > (h->size * 2)) { \
return; \
} \
size_t new_nr_of_buckets = nr_of_buckets * 2; \
___ ## HASHBAG_NAME ## _adjust_number_of_buckets(h, nr_of_buckets, new_nr_of_buckets); \
} \
\
MODIFIER void ___ ## HASHBAG_NAME ## _shrink(HASHBAG_NAME* h) { \
DS_SUPPRESS_WARNING( ___ ## HASHBAG_NAME ## _shrink); \
size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
if (nr_of_buckets == HASHBAG_MINIMUM_NUMBER_OF_BUCKETS || \
((h->size * 8) > nr_of_buckets)) { \
return; \
} \
size_t new_nr_of_buckets = nr_of_buckets / 2; \
___ ## HASHBAG_NAME ## _adjust_number_of_buckets(h, nr_of_buckets, new_nr_of_buckets); \
} \
\
MODIFIER HASHBAG_NAME* HASHBAG_NAME ## _put(HASHBAG_NAME* h, \
HASHBAG_TYPE object) { \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _put); \
size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
size_t hash_v = HASH_FUN_NAME(&object); \
size_t target_bucket_index = hash_v % nr_of_buckets; \
HASHBAG_TYPE_dynarr * bucket = HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, target_bucket_index); \
HASHBAG_TYPE ## _dynarr_push(bucket, object); \
h->size += 1; \
___ ## HASHBAG_NAME ## _expand(h); \
return h; \
} \
\
MODIFIER HASHBAG_TYPE * HASHBAG_NAME ## _get(HASHBAG_NAME* h, \
HASHBAG_TYPE object) { \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _get); \
size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
size_t hash_v = HASH_FUN_NAME(&object); \
size_t target_bucket_index = hash_v % nr_of_buckets; \
HASHBAG_TYPE_dynarr * bucket = HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, target_bucket_index); \
return HASHBAG_TYPE ## _dynarr_ptr_to(bucket, object); \
} \
\
MODIFIER HASHBAG_NAME* HASHBAG_NAME ## _remove(HASHBAG_NAME* h, \
HASHBAG_TYPE object) { \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _remove); \
size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
size_t hash_v = HASH_FUN_NAME(&object); \
size_t target_bucket_index = hash_v % nr_of_buckets; \
HASHBAG_TYPE_dynarr * bucket = HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, target_bucket_index); \
HASHBAG_TYPE ## _dynarr_remove(bucket, object); \
h->size -= 1; \
___ ## HASHBAG_NAME ## _shrink(h); \
return h; \
} \
\
MODIFIER HASHBAG_NAME* HASHBAG_NAME ## _put_replace(HASHBAG_NAME* h, \
HASHBAG_TYPE object) { \
DS_SUPPRESS_WARNING(HASHBAG_NAME ## _put_replace); \
size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \
size_t hash_v = HASH_FUN_NAME(&object); \
size_t target_bucket_index = hash_v % nr_of_buckets; \
HASHBAG_TYPE_dynarr* bucket = HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, target_bucket_index); \
size_t bucket_size_before = HASHBAG_TYPE ## _dynarr_size(bucket); \
HASHBAG_TYPE ## _dynarr_remove(bucket, object); \
HASHBAG_TYPE ## _dynarr_push(bucket, object); \
h-> size += (HASHBAG_TYPE ## _dynarr_size(bucket) - bucket_size_before); \
return h; \
}
// Printing the table for debugging
/* MODIFIER void HASHBAG_NAME ## _print(HASHBAG_NAME* h) { \ */
/* DS_SUPPRESS_WARNING(HASHBAG_NAME ## _print); \ */
/* printf("============================================================================\n"); \ */
/* size_t nr_of_buckets = HASHBAG_TYPE ## _dynarr_dynarr_size(&h->buckets); \ */
/* for (size_t i = 0; i < nr_of_buckets; i++) { \ */
/* HASHBAG_TYPE_dynarr* bucket = HASHBAG_TYPE ## _dynarr_dynarr_item_at_ptr(&h->buckets, i); \ */
/* printf("BUCKET %lu %p:\n", i, bucket); \ */
/* size_t bucket_size = HASHBAG_TYPE ## _dynarr_size(bucket); \ */
/* for (size_t j = 0; j < bucket_size; j++) { \ */
/* HASHBAG_TYPE* item = HASHBAG_TYPE ## _dynarr_item_at_ptr(bucket, j); \ */
/* printf(" ITEM %ld\n", *item); \ */
/* } \ */
/* } \ */
/* \ */
/* printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); \ */
/* } */
#define DECLARE_HASHBAG_DS(HASHBAG_TYPE, MODIFIER, MALLOC_FUN_NAME, FREE_FUN_NAME, EQ_FUN_NAME, HASH_FUN_NAME) \
_DECLARE_HASHBAG_DS(HASHBAG_TYPE, HASHBAG_TYPE ## _hash, HASHBAG_TYPE ## _dynarr, HASHBAG_TYPE ## _dynarr_dynarr, HASHBAG_TYPE ## _dynarr_eq, MODIFIER, MALLOC_FUN_NAME, FREE_FUN_NAME, EQ_FUN_NAME, HASH_FUN_NAME)