class InMemoryKV::Str2Str

Public Instance Methods

[](p1) click to toggle source
static VALUE
rb_kv_get(VALUE self, VALUE vkey) {
        inmemory_kv* kv;
        const char *key;
        size_t size;
        hash_item* item;

        GetKV(self, kv);
        StringValue(vkey);
        key = RSTRING_PTR(vkey);
        size = RSTRING_LEN(vkey);
        item = kv_fetch(kv, key, size);
        if (item == NULL) return Qnil;
        return item_val_str(item);
}
[]=(p1, p2) click to toggle source
static VALUE
rb_kv_set(VALUE self, VALUE vkey, VALUE vval) {
        inmemory_kv* kv;
        const char *key, *val;
        size_t ksize, vsize;

        GetKV(self, kv);
        StringValue(vkey);
        StringValue(vval);
        key = RSTRING_PTR(vkey);
        ksize = RSTRING_LEN(vkey);
        val = RSTRING_PTR(vval);
        vsize = RSTRING_LEN(vval);

        if (kv_insert(kv, key, ksize, val, vsize) == NULL) {
                rb_raise(rb_eNoMemError, "could not malloc");
        }

        return vval;
}
clear() click to toggle source
static VALUE
rb_kv_clear(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        kv_destroy(kv);
        memset(kv, 0, sizeof(*kv));
        return self;
}
count()
Alias for: size
data_size() click to toggle source
static VALUE
rb_kv_data_size(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        return SIZET2NUM(kv->total_size);
}
delete(p1) click to toggle source
static VALUE
rb_kv_del(VALUE self, VALUE vkey) {
        inmemory_kv* kv;
        const char *key;
        size_t size;
        hash_item* item;
        VALUE res;

        GetKV(self, kv);
        StringValue(vkey);
        key = RSTRING_PTR(vkey);
        size = RSTRING_LEN(vkey);
        item = kv_fetch(kv, key, size);
        if (item == NULL) return Qnil;
        res = item_val_str(item);
        kv_delete(kv, item);
        return res;
}
down(p1) click to toggle source
static VALUE
rb_kv_down(VALUE self, VALUE vkey) {
        inmemory_kv* kv;
        const char *key;
        size_t size;
        hash_item* item;

        GetKV(self, kv);
        StringValue(vkey);
        key = RSTRING_PTR(vkey);
        size = RSTRING_LEN(vkey);
        item = kv_fetch(kv, key, size);
        if (item == NULL) return Qnil;
        kv_down(kv, item);
        return item_val_str(item);
}
each()
Alias for: each_pair
each_key() click to toggle source
static VALUE
rb_kv_each_key(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        RETURN_ENUMERATOR(self, 0, 0);
        kv_each(kv, key_i, NULL);
        return self;
}
each_pair() click to toggle source
static VALUE
rb_kv_each(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        RETURN_ENUMERATOR(self, 0, 0);
        kv_each(kv, pair_i, NULL);
        return self;
}
Also aliased as: each
each_value() click to toggle source
static VALUE
rb_kv_each_val(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        RETURN_ENUMERATOR(self, 0, 0);
        kv_each(kv, val_i, NULL);
        return self;
}
empty?() click to toggle source
static VALUE
rb_kv_empty_p(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        return kv->tab.size ? Qfalse : Qtrue;
}
entries() click to toggle source
static VALUE
rb_kv_entries(VALUE self) {
        inmemory_kv* kv;
        VALUE res;
        GetKV(self, kv);
        res = rb_ary_new2(kv->tab.size);
        kv_each(kv, pairs_i, (void*)res);
        return res;
}
first() click to toggle source
static VALUE
rb_kv_first(VALUE self) {
        inmemory_kv* kv;
        hash_item* item;
        VALUE key, val;

        GetKV(self, kv);
        item = kv_first(kv);
        if (item == NULL) return Qnil;
        key = item_key_str(item);
        val = item_val_str(item);
        return rb_assoc_new(key, val);
}
has_key?(p1)
Alias for: include?
include?(p1) click to toggle source
static VALUE
rb_kv_include(VALUE self, VALUE vkey) {
        inmemory_kv* kv;
        const char *key;
        size_t size;

        GetKV(self, kv);
        StringValue(vkey);
        key = RSTRING_PTR(vkey);
        size = RSTRING_LEN(vkey);
        return kv_fetch(kv, key, size) ? Qtrue : Qfalse;
}
Also aliased as: has_key?
initialize_copy(p1) click to toggle source
static VALUE
rb_kv_init_copy(VALUE self, VALUE orig) {
        inmemory_kv *origin, *new;
        GetKV(self, new);
        GetKV(orig, origin);
        kv_copy_to(origin, new);
        return self;
}
inspect() click to toggle source
static VALUE
rb_kv_inspect(VALUE self) {
        struct inspect_arg ins;
        inmemory_kv* kv;
        GetKV(self, kv);
        ins.str = rb_str_buf_new2("<");
        rb_str_append(ins.str, rb_class_name(CLASS_OF(self)));
        if (kv->tab.size != 0) {
                ins.tmp = rb_str_buf_new(0);
                kv_each(kv, inspect_i, &ins);
        }
        rb_str_buf_cat2(ins.str, ">");
        return ins.str;
}
keys() click to toggle source
static VALUE
rb_kv_keys(VALUE self) {
        inmemory_kv* kv;
        VALUE res;
        GetKV(self, kv);
        res = rb_ary_new2(kv->tab.size);
        kv_each(kv, keys_i, (void*)res);
        return res;
}
shift() click to toggle source
static VALUE
rb_kv_shift(VALUE self) {
        inmemory_kv* kv;
        hash_item* item;
        VALUE key, val;

        GetKV(self, kv);
        item = kv_first(kv);
        if (item == NULL) return Qnil;
        key = item_key_str(item);
        val = item_val_str(item);
        kv_delete(kv, item);
        return rb_assoc_new(key, val);
}
size() click to toggle source
static VALUE
rb_kv_size(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        return UINT2NUM(kv->tab.size);
}
Also aliased as: count
total_size() click to toggle source
static VALUE
rb_kv_total_size(VALUE self) {
        inmemory_kv* kv;
        GetKV(self, kv);
        return SIZET2NUM(rb_kv_memsize(kv));
}
unshift(p1, p2) click to toggle source
static VALUE
rb_kv_unshift(VALUE self, VALUE vkey, VALUE vval) {
        inmemory_kv* kv;
        const char *key, *val;
        size_t ksize, vsize;
        hash_item* item;

        GetKV(self, kv);
        StringValue(vkey);
        StringValue(vval);
        key = RSTRING_PTR(vkey);
        ksize = RSTRING_LEN(vkey);
        val = RSTRING_PTR(vval);
        vsize = RSTRING_LEN(vval);

        item = kv_insert(kv, key, ksize, val, vsize);
        if (item == NULL) {
                rb_raise(rb_eNoMemError, "could not malloc");
        }
        kv_down(kv, item);

        return vval;
}
up(p1) click to toggle source
static VALUE
rb_kv_up(VALUE self, VALUE vkey) {
        inmemory_kv* kv;
        const char *key;
        size_t size;
        hash_item* item;

        GetKV(self, kv);
        StringValue(vkey);
        key = RSTRING_PTR(vkey);
        size = RSTRING_LEN(vkey);
        item = kv_fetch(kv, key, size);
        if (item == NULL) return Qnil;
        kv_up(kv, item);
        return item_val_str(item);
}
values() click to toggle source
static VALUE
rb_kv_vals(VALUE self) {
        inmemory_kv* kv;
        VALUE res;
        GetKV(self, kv);
        res = rb_ary_new2(kv->tab.size);
        kv_each(kv, vals_i, (void*)res);
        return res;
}