forked from mruby/mruby
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ritehash.h
203 lines (188 loc) · 11.6 KB
/
ritehash.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
/*
* Rite Hash
*
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef uint32_t khint_t;
typedef khint_t khiter_t;
#define INITIAL_HASH_SIZE 32
#define UPPER_BOUND(x) ((x)>>2|(x>>1))
//extern uint8_t __m[];
/* mask for flags */
static uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
#define __ac_isempty(e_flag, d_flag, i) (e_flag[(i)/8]&__m[(i)%8])
#define __ac_isdel(e_flag, d_flag, i) (d_flag[(i)/8]&__m[(i)%8])
#define __ac_iseither(e_flag, d_flag, i) (__ac_isempty(e_flag,d_flag,i)||__ac_isdel(e_flag,d_flag,i))
/* struct kh_xxx
name: ash name
khkey_t: key data type
khval_t: value data type
kh_is_map: (not implemented / not used in RiteVM )
__hash_func: hash function
__hash_equal: hash comparation function
*/
#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
typedef struct kh_##name { \
khint_t n_buckets; \
khint_t size; \
khint_t n_occupied; \
khint_t upper_bound; \
uint8_t *e_flags; \
uint8_t *d_flags; \
khkey_t *keys; \
khval_t *vals; \
khint_t mask; \
khint_t inc; \
mrb_state *mrb; \
} kh_##name##_t; \
static void kh_alloc_##name(kh_##name##_t *h) \
{ \
khint_t sz = h->n_buckets; \
h->size = h->n_occupied = 0; \
h->upper_bound = UPPER_BOUND(sz); \
h->e_flags = (uint8_t *)mrb_malloc(h->mrb, sizeof(uint8_t)*sz/4); \
h->d_flags = h->e_flags + sz/8; \
memset(h->e_flags, 0xff, sz/8*sizeof(uint8_t)); \
memset(h->d_flags, 0x00, sz/8*sizeof(uint8_t)); \
h->keys = (khkey_t *)mrb_malloc(h->mrb, sizeof(khkey_t)*sz); \
h->vals = (khval_t *)mrb_malloc(h->mrb, sizeof(khval_t)*sz); \
h->mask = sz-1; \
h->inc = sz/2-1; \
} \
static inline kh_##name##_t *kh_init_##name(mrb_state *mrb){ \
kh_##name##_t *h = (kh_##name##_t*)mrb_calloc(mrb, 1, sizeof(kh_##name##_t)); \
h->n_buckets = INITIAL_HASH_SIZE; \
h->mrb = mrb; \
kh_alloc_##name(h); \
return h; \
} \
static inline void kh_destroy_##name(kh_##name##_t *h) \
{ \
if( h ){ \
mrb_free(h->mrb, h->keys); \
mrb_free(h->mrb, h->vals); \
mrb_free(h->mrb, h->e_flags); \
mrb_free(h->mrb, h); \
} \
} \
static inline void kh_clear_##name(kh_##name##_t *h) \
{ \
if( h && h->e_flags ){ \
memset(h->e_flags, 0xff, h->n_buckets/8*sizeof(uint8_t)); \
memset(h->d_flags, 0x00, h->n_buckets/8*sizeof(uint8_t)); \
h->size = h->n_occupied = 0; \
} \
} \
static inline khint_t kh_get_##name(kh_##name##_t *h, khkey_t key) \
{ \
khint_t k = __hash_func(h->mrb,key) & (h->mask); \
while( !__ac_isempty(h->e_flags, h->d_flags, k) ){ \
if( !__ac_isdel(h->e_flags, h->d_flags, k) ){ \
if( __hash_equal(h->mrb,h->keys[k], key) ) return k; \
} \
k = (k+h->inc) & (h->mask); \
} \
return h->n_buckets; \
} \
static inline khint_t kh_put_##name(kh_##name##_t *h, khkey_t key); \
static void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
{ \
if( new_n_buckets<INITIAL_HASH_SIZE ){ \
new_n_buckets = INITIAL_HASH_SIZE; \
} else { \
khint_t limit = new_n_buckets; \
new_n_buckets = INITIAL_HASH_SIZE; \
while( new_n_buckets < limit ) new_n_buckets *= 2; \
} \
uint8_t *old_e_flags = h->e_flags; \
uint8_t *old_d_flags = h->d_flags; \
khkey_t *old_keys = h->keys; \
khval_t *old_vals = h->vals; \
khint_t old_n_buckets = h->n_buckets; \
h->n_buckets = new_n_buckets; \
kh_alloc_##name(h); \
/* relocate */ \
khint_t i; \
for( i=0 ; i<old_n_buckets ; i++ ){ \
if( !__ac_isempty(old_e_flags, old_d_flags, i) ){ \
khint_t k = kh_put_##name(h, old_keys[i]); \
kh_value(h,k) = old_vals[i]; \
} \
} \
} \
static inline khint_t kh_put_##name(kh_##name##_t *h, khkey_t key) \
{ \
khint_t k; \
if( h->n_occupied >= h->upper_bound ){ \
kh_resize_##name(h, h->n_buckets*2); \
} \
k = __hash_func(h->mrb,key) & (h->mask); \
while( !__ac_iseither(h->e_flags, h->d_flags, k) ){ \
if( __hash_equal(h->mrb,h->keys[k], key) ) break; \
k = (k+h->inc) & (h->mask); \
} \
if( __ac_isempty(h->e_flags, h->d_flags, k) ) { \
/* put at empty */ \
h->keys[k] = key; \
h->e_flags[k/8] &= ~__m[k%8]; \
h->size++; \
h->n_occupied++; \
} else if( __ac_isdel(h->e_flags, h->d_flags, k) ) { \
/* put at del */ \
h->keys[k] = key; \
h->d_flags[k/8] &= ~__m[k%8]; \
h->size++; \
} \
return k; \
} \
static inline void kh_del_##name(kh_##name##_t *h, khint_t x) \
{ \
h->d_flags[x/8] |= __m[x%8]; \
h->size--; \
} \
static inline void kh_debug_##name(kh_##name##_t *h) \
{ \
khint_t i; \
printf("idx:e_flag:d_flag\n"); \
for( i=0 ; i<h->n_buckets/8 ; i++ ){ \
printf("%4d:%02X:%02X\n", i, h->e_flags[i], h->d_flags[i]); \
} \
} \
#define khash_t(name) kh_##name##_t
#define kh_init(name,mrb) kh_init_##name(mrb)
#define kh_destroy(name, h) kh_destroy_##name(h)
#define kh_clear(name, h) kh_clear_##name(h)
#define kh_resize(name, h, s) kh_resize_##name(h, s)
#define kh_put(name, h, k, r) kh_put_##name(h, k)
#define kh_get(name, h, k) kh_get_##name(h, k)
#define kh_del(name, h, k) kh_del_##name(h, k)
#define kh_debug(name, h) kh_debug_##name(h)
#define kh_exist(h, x) (!__ac_iseither((h)->e_flags, (h)->d_flags, (x)))
#define kh_key(h, x) ((h)->keys[x])
#define kh_val(h, x) ((h)->vals[x])
#define kh_value(h, x) ((h)->vals[x])
#define kh_begin(h) (khint_t)(0)
#define kh_end(h) ((h)->n_buckets)
#define kh_size(h) ((h)->size)
#define kh_n_buckets(h) ((h)->n_buckets)
//#define kh_int_hash_func(mrb,key) (uint32_t)(key)
#define kh_int_hash_func(mrb,key) (uint32_t)((key)^((key)<<2)^((key)>>2))
#define kh_int_hash_equal(mrb,a, b) (a == b)
#define kh_int64_hash_func(mrb,key) (uint32_t)((key)>>33^(key)^(key)<<11)
#define kh_int64_hash_equal(mrb,a, b) (a == b)
static inline khint_t __ac_X31_hash_string(const char *s)
{
khint_t h = *s;
if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
return h;
}
#define kh_str_hash_func(mrb,key) __ac_X31_hash_string(key)
#define kh_str_hash_equal(mrb,a, b) (strcmp(a, b) == 0)
#define KHASH_MAP_INIT_INT(name, khval_t) \
KHASH_INIT(name, uint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
typedef const char *kh_cstr_t;
#define KHASH_MAP_INIT_STR(name, khval_t) \
KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)