-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash.c
43 lines (35 loc) · 1.01 KB
/
hash.c
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
#include "load_so.h"
#include "st.h"
static VALUE (*rb_hash_aref_)(VALUE, VALUE);
static VALUE (*rb_hash_aset_)(VALUE, VALUE, VALUE);
const struct st_hash_type *ptr_objhash;
VALUE rb_hash_new() {
return rb_class_new_instance(0, NULL, rb_cHash);
}
struct st_table *rb_hash_tbl(VALUE hash) {
if (!RHASH(hash)->ntbl) {
RHASH(hash)->ntbl = st_init_table(ptr_objhash);
}
return RHASH(hash)->ntbl;
}
VALUE rb_hash_aref(VALUE hash, VALUE key) {
return rb_hash_aref_(hash, key);
}
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val) {
return rb_hash_aset_(hash, key, val);
}
VALUE
rb_hash_has_key(VALUE hash, VALUE key)
{
if (!RHASH(hash)->ntbl)
return Qfalse;
if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
return Qtrue;
}
return Qfalse;
}
void Init_Hash() {
ptr_objhash = RHASH(rb_class_new_instance(0, NULL, rb_cHash))->ntbl->type;
rb_hash_aref_ = get_instance_method(rb_cHash, "[]");
rb_hash_aset_ = get_instance_method(rb_cHash, "[]=");
}