Commit cd681904 authored by crimsonwoods's avatar crimsonwoods

split declaration and definition for 'khash_xxx'.

parent 7744315d
...@@ -32,16 +32,14 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -32,16 +32,14 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
#define __ac_iseither(e_flag, d_flag, i) (__ac_isempty(e_flag,d_flag,i)||__ac_isdel(e_flag,d_flag,i)) #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 /* declare struct kh_xxx and kh_xxx_funcs
name: ash name name: ash name
khkey_t: key data type khkey_t: key data type
khval_t: value data type khval_t: value data type
kh_is_map: (not implemented / not used in RiteVM ) 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) \ #define KHASH_DECLARE(name, khkey_t, khval_t, kh_is_map) \
typedef struct kh_##name { \ typedef struct kh_##name { \
khint_t n_buckets; \ khint_t n_buckets; \
khint_t size; \ khint_t size; \
...@@ -55,7 +53,26 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -55,7 +53,26 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
khint_t inc; \ khint_t inc; \
mrb_state *mrb; \ mrb_state *mrb; \
} kh_##name##_t; \ } kh_##name##_t; \
static void kh_alloc_##name(kh_##name##_t *h) \ void kh_alloc_##name(kh_##name##_t *h); \
kh_##name##_t *kh_init_##name(mrb_state *mrb); \
void kh_destroy_##name(kh_##name##_t *h); \
void kh_clear_##name(kh_##name##_t *h); \
khint_t kh_get_##name(kh_##name##_t *h, khkey_t key); \
khint_t kh_put_##name(kh_##name##_t *h, khkey_t key); \
void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
void kh_del_##name(kh_##name##_t *h, khint_t x); \
/* define kh_xxx_funcs
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_DEFINE(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
void kh_alloc_##name(kh_##name##_t *h) \
{ \ { \
khint_t sz = h->n_buckets; \ khint_t sz = h->n_buckets; \
h->size = h->n_occupied = 0; \ h->size = h->n_occupied = 0; \
...@@ -69,14 +86,14 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -69,14 +86,14 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
h->mask = sz-1; \ h->mask = sz-1; \
h->inc = sz/2-1; \ h->inc = sz/2-1; \
} \ } \
static inline kh_##name##_t *kh_init_##name(mrb_state *mrb){ \ kh_##name##_t *kh_init_##name(mrb_state *mrb){ \
kh_##name##_t *h = (kh_##name##_t*)mrb_calloc(mrb, 1, sizeof(kh_##name##_t)); \ kh_##name##_t *h = (kh_##name##_t*)mrb_calloc(mrb, 1, sizeof(kh_##name##_t)); \
h->n_buckets = INITIAL_HASH_SIZE; \ h->n_buckets = INITIAL_HASH_SIZE; \
h->mrb = mrb; \ h->mrb = mrb; \
kh_alloc_##name(h); \ kh_alloc_##name(h); \
return h; \ return h; \
} \ } \
static inline void kh_destroy_##name(kh_##name##_t *h) \ void kh_destroy_##name(kh_##name##_t *h) \
{ \ { \
if( h ){ \ if( h ){ \
mrb_free(h->mrb, h->keys); \ mrb_free(h->mrb, h->keys); \
...@@ -85,7 +102,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -85,7 +102,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
mrb_free(h->mrb, h); \ mrb_free(h->mrb, h); \
} \ } \
} \ } \
static inline void kh_clear_##name(kh_##name##_t *h) \ void kh_clear_##name(kh_##name##_t *h) \
{ \ { \
if( h && h->e_flags ){ \ if( h && h->e_flags ){ \
memset(h->e_flags, 0xff, h->n_buckets/8*sizeof(uint8_t)); \ memset(h->e_flags, 0xff, h->n_buckets/8*sizeof(uint8_t)); \
...@@ -93,7 +110,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -93,7 +110,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
h->size = h->n_occupied = 0; \ h->size = h->n_occupied = 0; \
} \ } \
} \ } \
static inline khint_t kh_get_##name(kh_##name##_t *h, khkey_t key) \ khint_t kh_get_##name(kh_##name##_t *h, khkey_t key) \
{ \ { \
khint_t k = __hash_func(h->mrb,key) & (h->mask); \ khint_t k = __hash_func(h->mrb,key) & (h->mask); \
while( !__ac_isempty(h->e_flags, h->d_flags, k) ){ \ while( !__ac_isempty(h->e_flags, h->d_flags, k) ){ \
...@@ -104,8 +121,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -104,8 +121,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
} \ } \
return h->n_buckets; \ return h->n_buckets; \
} \ } \
static inline khint_t kh_put_##name(kh_##name##_t *h, khkey_t key); \ void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
static void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
{ \ { \
if( new_n_buckets<INITIAL_HASH_SIZE ){ \ if( new_n_buckets<INITIAL_HASH_SIZE ){ \
new_n_buckets = INITIAL_HASH_SIZE; \ new_n_buckets = INITIAL_HASH_SIZE; \
...@@ -134,7 +150,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -134,7 +150,7 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
mrb_free(h->mrb, old_vals); \ mrb_free(h->mrb, old_vals); \
} \ } \
} \ } \
static inline khint_t kh_put_##name(kh_##name##_t *h, khkey_t key) \ khint_t kh_put_##name(kh_##name##_t *h, khkey_t key) \
{ \ { \
khint_t k; \ khint_t k; \
if( h->n_occupied >= h->upper_bound ){ \ if( h->n_occupied >= h->upper_bound ){ \
...@@ -159,12 +175,13 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; ...@@ -159,12 +175,13 @@ static const uint8_t __m[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
} \ } \
return k; \ return k; \
} \ } \
static inline void kh_del_##name(kh_##name##_t *h, khint_t x) \ void kh_del_##name(kh_##name##_t *h, khint_t x) \
{ \ { \
h->d_flags[x/8] |= __m[x%8]; \ h->d_flags[x/8] |= __m[x%8]; \
h->size--; \ h->size--; \
} }
#define khash_t(name) kh_##name##_t #define khash_t(name) kh_##name##_t
#define kh_init(name,mrb) kh_init_##name(mrb) #define kh_init(name,mrb) kh_init_##name(mrb)
...@@ -197,11 +214,13 @@ static inline khint_t __ac_X31_hash_string(const char *s) ...@@ -197,11 +214,13 @@ static inline khint_t __ac_X31_hash_string(const char *s)
#define kh_str_hash_func(mrb,key) __ac_X31_hash_string(key) #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 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; 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) /* declare common hash types. */
#include "mruby.h"
KHASH_DECLARE(mt, mrb_sym, struct RProc*, 1)
KHASH_DECLARE(iv, mrb_sym, mrb_value, 1)
#if defined(__cplusplus) #if defined(__cplusplus)
} /* extern "C" { */ } /* extern "C" { */
......
...@@ -17,9 +17,6 @@ ...@@ -17,9 +17,6 @@
#include "mruby/khash.h" #include "mruby/khash.h"
KHASH_INIT(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal)
KHASH_INIT(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal)
typedef struct fc_result { typedef struct fc_result {
mrb_sym name; mrb_sym name;
struct RClass * klass; struct RClass * klass;
......
...@@ -31,7 +31,8 @@ mrb_hash_ht_hash_equal(mrb_state *mrb, mrb_value a, mrb_value b) ...@@ -31,7 +31,8 @@ mrb_hash_ht_hash_equal(mrb_state *mrb, mrb_value a, mrb_value b)
return mrb_eql(mrb, a, b); return mrb_eql(mrb, a, b);
} }
KHASH_INIT(ht, mrb_value, mrb_value, 1, mrb_hash_ht_hash_func, mrb_hash_ht_hash_equal); KHASH_DECLARE(ht, mrb_value, mrb_value, 1);
KHASH_DEFINE (ht, mrb_value, mrb_value, 1, mrb_hash_ht_hash_func, mrb_hash_ht_hash_equal);
static void mrb_hash_modify(mrb_state *mrb, mrb_value hash); static void mrb_hash_modify(mrb_state *mrb, mrb_value hash);
......
...@@ -34,9 +34,6 @@ typedef enum { ...@@ -34,9 +34,6 @@ typedef enum {
NOEX_RESPONDS = 0x80 NOEX_RESPONDS = 0x80
} mrb_method_flag_t; } mrb_method_flag_t;
KHASH_INIT(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal)
KHASH_INIT(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal)
struct obj_ivar_tag { struct obj_ivar_tag {
mrb_value obj; mrb_value obj;
int (*func)(mrb_sym key, mrb_value val, void * arg); int (*func)(mrb_sym key, mrb_value val, void * arg);
......
#include "mruby/khash.h"
KHASH_DEFINE(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal)
KHASH_DEFINE(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal)
...@@ -36,7 +36,8 @@ sym_hash_func(mrb_state *mrb, const symbol_name s) ...@@ -36,7 +36,8 @@ sym_hash_func(mrb_state *mrb, const symbol_name s)
} }
#define sym_hash_equal(mrb,a, b) (a.len == b.len && memcmp(a.name, b.name, a.len) == 0) #define sym_hash_equal(mrb,a, b) (a.len == b.len && memcmp(a.name, b.name, a.len) == 0)
KHASH_INIT(n2s, symbol_name, mrb_sym, 1, sym_hash_func, sym_hash_equal) KHASH_DECLARE(n2s, symbol_name, mrb_sym, 1)
KHASH_DEFINE (n2s, symbol_name, mrb_sym, 1, sym_hash_func, sym_hash_equal)
/* ------------------------------------------------------ */ /* ------------------------------------------------------ */
mrb_sym mrb_sym
mrb_intern2(mrb_state *mrb, const char *name, int len) mrb_intern2(mrb_state *mrb, const char *name, int len)
......
...@@ -19,8 +19,6 @@ ...@@ -19,8 +19,6 @@
#include "st.h" #include "st.h"
#endif #endif
KHASH_INIT(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal)
static void static void
mark_tbl(mrb_state *mrb, struct kh_iv *h) mark_tbl(mrb_state *mrb, struct kh_iv *h)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment