Ruby
1.9.3p392(2013-02-22revision39386)
Main Page
Modules
Data Structures
Files
File List
Globals
ext
-test-
st
numhash
numhash.c
Go to the documentation of this file.
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
VALUE
11
numhash_alloc
(
VALUE
klass)
12
{
13
return
Data_Wrap_Struct
(klass, 0,
numhash_free
, 0);
14
}
15
16
static
VALUE
17
numhash_init
(
VALUE
self
)
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
26
numhash_aref
(
VALUE
self
,
VALUE
key
)
27
{
28
st_data_t
data;
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
36
numhash_aset
(
VALUE
self
,
VALUE
key
,
VALUE
data)
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
45
numhash_i
(
st_data_t
key
,
st_data_t
value,
st_data_t
arg
,
int
error)
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
55
numhash_each
(
VALUE
self
)
56
{
57
return
st_foreach
((
st_table
*)
DATA_PTR
(
self
),
numhash_i
,
self
) ?
Qtrue
:
Qfalse
;
58
}
59
60
void
61
Init_numhash
(
void
)
62
{
63
VALUE
st =
rb_define_class_under
(
rb_define_module
(
"Bug"
),
"StNumHash"
,
rb_cData
);
64
rb_define_alloc_func
(st,
numhash_alloc
);
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
}
70
Generated on Tue Feb 26 2013 08:42:53 for Ruby by
1.8.3