Ruby  1.9.3p551(2014-11-13revision48407)
numhash.c
Go to the documentation of this file.
1 #include <ruby.h>
2 #include <ruby/st.h>
3 
4 static void
6 {
7  if (ptr) st_free_table(ptr);
8 }
9 
10 static VALUE
12 {
13  return Data_Wrap_Struct(klass, 0, numhash_free, 0);
14 }
15 
16 static VALUE
18 {
19  st_table *tbl = (st_table *)DATA_PTR(self);
20  if (tbl) st_free_table(tbl);
21  DATA_PTR(self) = st_init_numtable();
22  return self;
23 }
24 
25 static VALUE
27 {
29  if (!SPECIAL_CONST_P(key)) rb_raise(rb_eArgError, "not a special const");
30  if (st_lookup((st_table *)DATA_PTR(self), (st_data_t)key, &data))
31  return (VALUE)data;
32  return Qnil;
33 }
34 
35 static VALUE
37 {
38  if (!SPECIAL_CONST_P(key)) rb_raise(rb_eArgError, "not a special const");
39  if (!SPECIAL_CONST_P(data)) rb_raise(rb_eArgError, "not a special const");
40  st_insert((st_table *)DATA_PTR(self), (st_data_t)key, (st_data_t)data);
41  return self;
42 }
43 
44 static int
46 {
47  VALUE ret;
48  if (key == 0 && value == 0 && error == 1) rb_raise(rb_eRuntimeError, "numhash modified");
49  ret = rb_yield_values(3, (VALUE)key, (VALUE)value, (VALUE)arg);
50  if (ret == Qtrue) return ST_CHECK;
51  return ST_CONTINUE;
52 }
53 
54 static VALUE
56 {
57  return st_foreach((st_table *)DATA_PTR(self), numhash_i, self) ? Qtrue : Qfalse;
58 }
59 
60 void
62 {
63  VALUE st = rb_define_class_under(rb_define_module("Bug"), "StNumHash", rb_cData);
65  rb_define_method(st, "initialize", numhash_init, 0);
66  rb_define_method(st, "[]", numhash_aref, 1);
67  rb_define_method(st, "[]=", numhash_aset, 2);
68  rb_define_method(st, "each", numhash_each, 0);
69 }
RUBY_EXTERN VALUE rb_cData
Definition: ruby.h:1253
Definition: st.h:77
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:792
#define st_foreach
Definition: regint.h:150
static VALUE numhash_aref(VALUE self, VALUE key)
Definition: numhash.c:26
return ST_CHECK
Definition: tkutil.c:271
int ret
Definition: tcltklib.c:276
return ST_CONTINUE
Definition: tkutil.c:1273
static void numhash_free(void *ptr)
Definition: numhash.c:5
unsigned long VALUE
Definition: ruby.h:88
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:515
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1574
return Qtrue
Definition: tcltklib.c:9597
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
#define st_lookup
Definition: regint.h:149
VALUE tbl
Definition: tkutil.c:1279
#define Qnil
Definition: ruby.h:367
return Data_Wrap_Struct(CLASS_OF(interp), 0, ip_free, slave)
void * data
Definition: yaml2byte.c:131
return Qfalse
Definition: tcltklib.c:6768
VALUE rb_eRuntimeError
Definition: error.c:466
static int VALUE key
Definition: tkutil.c:265
volatile VALUE value
Definition: tcltklib.c:9429
static VALUE numhash_each(VALUE self)
Definition: numhash.c:55
return ptr
Definition: tcltklib.c:780
long st_data_t
Definition: syck.h:69
arg
Definition: ripper.y:1287
#define st_init_numtable
Definition: regint.h:142
DATA_PTR(self)
klass
Definition: tcltklib.c:3493
static VALUE numhash_alloc(VALUE klass)
Definition: numhash.c:11
static VALUE numhash_init(VALUE self)
Definition: numhash.c:17
#define st_insert
Definition: regint.h:148
#define st_free_table
Definition: regint.h:152
void Init_numhash(void)
Definition: numhash.c:61
#define SPECIAL_CONST_P(x)
Definition: ruby.h:953
VALUE rb_define_module(const char *name)
Definition: class.c:587
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1210
VALUE rb_eArgError
Definition: error.c:468
static VALUE numhash_aset(VALUE self, VALUE key, VALUE data)
Definition: numhash.c:36
Tcl_Interp *int * st
Definition: stubs.c:508
static int numhash_i(st_data_t key, st_data_t value, st_data_t arg, int error)
Definition: numhash.c:45