forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htable.inc
195 lines (189 loc) · 13.3 KB
/
htable.inc
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
//-*- mode:c -*-
/*
include this file and call HTIMPL to generate an implementation
*/
#define hash_size(h) ((h)->size/2)
// compute empirical max-probe for a given size
#define max_probe(size) ((size)<=(HT_N_INLINE*2) ? (HT_N_INLINE/2) : (size)>>3)
#define _HTIMPL_EX(HTNAME, HFUNC, EQFUNC, _STATIC) \
static void **HTNAME##_lookup_bp_r(htable_t *h, void *key, void *ctx) \
{ \
uint_t hv; \
size_t i, orig, index, iter; \
size_t newsz, sz = hash_size(h); \
size_t maxprobe = max_probe(sz); \
void **tab = h->table; \
void **ol; \
\
hv = HFUNC((uintptr_t)key, ctx); \
retry_bp: \
iter = 0; \
index = (size_t)(hv & (sz-1)) * 2; \
sz *= 2; \
orig = index; \
\
do { \
if (tab[index+1] == HT_NOTFOUND) { \
tab[index] = key; \
return &tab[index+1]; \
} \
\
if (EQFUNC(key, tab[index], ctx)) \
return &tab[index+1]; \
\
index = (index+2) & (sz-1); \
iter++; \
if (iter > maxprobe) \
break; \
} while (index != orig); \
\
/* table full */ \
/* quadruple size, rehash, retry the insert */ \
/* it's important to grow the table really fast; otherwise we waste */ \
/* lots of time rehashing all the keys over and over. */ \
sz = h->size; \
ol = h->table; \
if (sz >= (1<<19) || (sz <= (1<<8))) \
newsz = sz<<1; \
else if (sz <= HT_N_INLINE) \
newsz = HT_N_INLINE; \
else \
newsz = sz<<2; \
/*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \
tab = (void**)LLT_ALLOC(newsz*sizeof(void*)); \
if (tab == NULL) \
return NULL; \
for(i=0; i < newsz; i++) \
tab[i] = HT_NOTFOUND; \
h->table = tab; \
h->size = newsz; \
for(i=0; i < sz; i+=2) { \
if (ol[i+1] != HT_NOTFOUND) { \
(*HTNAME##_lookup_bp_r(h, ol[i], ctx)) = ol[i+1]; \
} \
} \
if (ol != &h->_space[0]) \
LLT_FREE(ol); \
\
sz = hash_size(h); \
maxprobe = max_probe(sz); \
tab = h->table; \
\
goto retry_bp; \
\
return NULL; \
} \
\
_STATIC void HTNAME##_put_r(htable_t *h, void *key, void *val, void *ctx) \
{ \
void **bp = HTNAME##_lookup_bp_r(h, key, ctx); \
\
*bp = val; \
} \
\
_STATIC void **HTNAME##_bp_r(htable_t *h, void *key, void *ctx) \
{ \
return HTNAME##_lookup_bp_r(h, key, ctx); \
} \
\
/* returns bp if key is in hash, otherwise NULL */ \
/* if return is non-NULL and *bp == HT_NOTFOUND then key was deleted */ \
static void **HTNAME##_peek_bp_r(htable_t *h, void *key, void *ctx) \
{ \
size_t sz = hash_size(h); \
size_t maxprobe = max_probe(sz); \
void **tab = h->table; \
size_t index = (size_t)(HFUNC((uintptr_t)key, ctx) & (sz-1)) * 2; \
sz *= 2; \
size_t orig = index; \
size_t iter = 0; \
\
do { \
if (tab[index] == HT_NOTFOUND) \
return NULL; \
if (EQFUNC(key, tab[index], ctx)) \
return &tab[index+1]; \
\
index = (index+2) & (sz-1); \
iter++; \
if (iter > maxprobe) \
break; \
} while (index != orig); \
\
return NULL; \
} \
\
_STATIC void *HTNAME##_get_r(htable_t *h, void *key, void *ctx) \
{ \
void **bp = HTNAME##_peek_bp_r(h, key, ctx); \
if (bp == NULL) \
return HT_NOTFOUND; \
return *bp; \
} \
\
_STATIC int HTNAME##_has_r(htable_t *h, void *key, void *ctx) \
{ \
return (HTNAME##_get_r(h, key, ctx) != HT_NOTFOUND); \
} \
\
_STATIC int HTNAME##_remove_r(htable_t *h, void *key, void *ctx) \
{ \
void **bp = HTNAME##_peek_bp_r(h, key, ctx); \
if (bp != NULL) { \
*bp = HT_NOTFOUND; \
return 1; \
} \
return 0; \
} \
\
_STATIC void HTNAME##_adjoin_r(htable_t *h, void *key, void *val, void *ctx) \
{ \
void **bp = HTNAME##_lookup_bp_r(h, key, ctx); \
if (*bp == HT_NOTFOUND) \
*bp = val; \
}
#define HTIMPL(HTNAME, HFUNC, EQFUNC) \
static uint_t HTNAME##_hfunc_wrapper(uintptr_t key, void *ctx) \
{ \
(void)ctx; \
return HFUNC(key); \
} \
\
static uint_t HTNAME##_eqfunc_wrapper(void *key1, void *key2, void *ctx) \
{ \
(void)ctx; \
return EQFUNC(key1, key2); \
} \
\
_HTIMPL_EX(HTNAME, HTNAME##_hfunc_wrapper, HTNAME##_eqfunc_wrapper, static) \
\
void HTNAME##_put(htable_t *h, void *key, void *val) \
{ \
HTNAME##_put_r(h, key, val, NULL); \
} \
\
void **HTNAME##_bp(htable_t *h, void *key) \
{ \
return HTNAME##_bp_r(h, key, NULL); \
} \
\
void *HTNAME##_get(htable_t *h, void *key) \
{ \
return HTNAME##_get_r(h, key, NULL); \
} \
\
int HTNAME##_has(htable_t *h, void *key) \
{ \
return HTNAME##_has_r(h, key, NULL); \
} \
\
int HTNAME##_remove(htable_t *h, void *key) \
{ \
return HTNAME##_remove_r(h, key, NULL); \
} \
\
void HTNAME##_adjoin(htable_t *h, void *key, void *val) \
{ \
HTNAME##_adjoin_r(h, key, val, NULL); \
}
#define HTIMPL_R(HTNAME, HFUNC, EQFUNC) _HTIMPL_EX(HTNAME, HFUNC, EQFUNC, )