| 1 |
#include <ruby.h> |
| 2 |
#include <ruby/st.h> |
| 3 |
|
| 4 |
static void |
| 5 |
numhash_free(void *ptr) |
| 6 |
{ |
| 7 |
if (ptr) st_free_table(ptr); |
| 8 |
} |
| 9 |
|
| 10 |
static size_t |
| 11 |
numhash_memsize(const void *ptr) |
| 12 |
{ |
| 13 |
return st_memsize(ptr); |
| 14 |
} |
| 15 |
|
| 16 |
static const rb_data_type_t numhash_type = { |
| 17 |
"numhash", |
| 18 |
{0, numhash_free, numhash_memsize,}, |
| 19 |
0, 0, |
| 20 |
RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED, |
| 21 |
}; |
| 22 |
|
| 23 |
static VALUE |
| 24 |
numhash_alloc(VALUE klass) |
| 25 |
{ |
| 26 |
return TypedData_Wrap_Struct(klass, &numhash_type, 0); |
| 27 |
} |
| 28 |
|
| 29 |
static VALUE |
| 30 |
numhash_init(VALUE self) |
| 31 |
{ |
| 32 |
st_table *tbl = (st_table *)Check_TypedStruct(self, &numhash_type); |
| 33 |
if (tbl) st_free_table(tbl); |
| 34 |
DATA_PTR(self) = st_init_numtable(); |
| 35 |
return self; |
| 36 |
} |
| 37 |
|
| 38 |
static VALUE |
| 39 |
numhash_aref(VALUE self, VALUE key) |
| 40 |
{ |
| 41 |
st_data_t data; |
| 42 |
st_table *tbl = (st_table *)Check_TypedStruct(self, &numhash_type); |
| 43 |
if (!SPECIAL_CONST_P(key)) rb_raise(rb_eArgError, "not a special const"); |
| 44 |
if (st_lookup(tbl, (st_data_t)key, &data)) |
| 45 |
return (VALUE)data; |
| 46 |
return Qnil; |
| 47 |
} |
| 48 |
|
| 49 |
static VALUE |
| 50 |
numhash_aset(VALUE self, VALUE key, VALUE data) |
| 51 |
{ |
| 52 |
st_table *tbl = (st_table *)Check_TypedStruct(self, &numhash_type); |
| 53 |
if (!SPECIAL_CONST_P(key)) rb_raise(rb_eArgError, "not a special const"); |
| 54 |
if (!SPECIAL_CONST_P(data)) rb_raise(rb_eArgError, "not a special const"); |
| 55 |
st_insert(tbl, (st_data_t)key, (st_data_t)data); |
| 56 |
return self; |
| 57 |
} |
| 58 |
|
| 59 |
static int |
| 60 |
numhash_i(st_data_t key, st_data_t value, st_data_t arg, int _) |
| 61 |
{ |
| 62 |
VALUE ret; |
| 63 |
ret = rb_yield_values(3, (VALUE)key, (VALUE)value, (VALUE)arg); |
| 64 |
if (ret == Qtrue) return ST_CHECK; |
| 65 |
return ST_CONTINUE; |
| 66 |
} |
| 67 |
|
| 68 |
static VALUE |
| 69 |
numhash_each(VALUE self) |
| 70 |
{ |
| 71 |
st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); |
| 72 |
st_data_t data = (st_data_t)self; |
| 73 |
return st_foreach_check(table, numhash_i, data, data) ? Qtrue : Qfalse; |
| 74 |
} |
| 75 |
|
| 76 |
static int |
| 77 |
update_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing) |
| 78 |
{ |
| 79 |
VALUE ret = rb_yield_values(existing ? 2 : 1, (VALUE)*key, (VALUE)*value); |
| 80 |
switch (ret) { |
| 81 |
case Qfalse: |
| 82 |
return ST_STOP; |
| 83 |
case Qnil: |
| 84 |
return ST_DELETE; |
| 85 |
default: |
| 86 |
*value = ret; |
| 87 |
return ST_CONTINUE; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static VALUE |
| 92 |
numhash_update(VALUE self, VALUE key) |
| 93 |
{ |
| 94 |
st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); |
| 95 |
if (st_update(table, (st_data_t)key, update_func, 0)) |
| 96 |
return Qtrue; |
| 97 |
else |
| 98 |
return Qfalse; |
| 99 |
} |
| 100 |
|
| 101 |
#if SIZEOF_LONG == SIZEOF_VOIDP |
| 102 |
# define ST2NUM(x) ULONG2NUM(x) |
| 103 |
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP |
| 104 |
# define ST2NUM(x) ULL2NUM(x) |
| 105 |
#endif |
| 106 |
|
| 107 |
static VALUE |
| 108 |
numhash_size(VALUE self) |
| 109 |
{ |
| 110 |
st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); |
| 111 |
return ST2NUM(table->num_entries); |
| 112 |
} |
| 113 |
|
| 114 |
static VALUE |
| 115 |
numhash_delete_safe(VALUE self, VALUE key) |
| 116 |
{ |
| 117 |
st_table *table = (st_table *)Check_TypedStruct(self, &numhash_type); |
| 118 |
st_data_t val, k = (st_data_t)key; |
| 119 |
if (st_delete_safe(table, &k, &val, (st_data_t)self)) { |
| 120 |
return val; |
| 121 |
} |
| 122 |
return Qnil; |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
Init_numhash(void) |
| 127 |
{ |
| 128 |
VALUE st = rb_define_class_under(rb_define_module("Bug"), "StNumHash", rb_cObject); |
| 129 |
rb_define_alloc_func(st, numhash_alloc); |
| 130 |
rb_define_method(st, "initialize", numhash_init, 0); |
| 131 |
rb_define_method(st, "[]", numhash_aref, 1); |
| 132 |
rb_define_method(st, "[]=", numhash_aset, 2); |
| 133 |
rb_define_method(st, "each", numhash_each, 0); |
| 134 |
rb_define_method(st, "update", numhash_update, 1); |
| 135 |
rb_define_method(st, "size", numhash_size, 0); |
| 136 |
rb_define_method(st, "delete_safe", numhash_delete_safe, 1); |
| 137 |
} |