class SparseArray

Constants

VERSION

Public Instance Methods

[](p1) click to toggle source
static VALUE
sparse_array_get(VALUE self, VALUE ri)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    spar_lookup(table, i, &res);
    return (VALUE)res;
}
[]=(p1, p2) click to toggle source
static VALUE
sparse_array_set(VALUE self, VALUE ri, VALUE val)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    GetSparseArray(self, table);
    spar_insert(table, i, val);
    return val;
}
clear() click to toggle source
static VALUE
sparse_array_clear(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    spar_clear(table);
    return self;
}
count()
Alias for: size
delete(p1) click to toggle source
static VALUE
sparse_array_del(VALUE self, VALUE ri)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    if (spar_delete(table, i, &res))
        return (VALUE)res;
    return Qnil;
}
each() click to toggle source
static VALUE
sparse_array_each(VALUE self)
{
    spar_table *table;
    VALUE y[2] = {Qnil, Qnil};
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    SPAR_FOREACH_START(table);
    y[0] = UINT2NUM(entry->key);
    y[1] = (VALUE)value;
    rb_yield_values2(2, y);
    SPAR_FOREACH_END();
    return self;
}
Also aliased as: each_pair
each_key() click to toggle source
static VALUE
sparse_array_each_key(VALUE self)
{
    spar_table *table;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    SPAR_FOREACH_START(table);
    (void)value;
    rb_yield(UINT2NUM(entry->key));
    SPAR_FOREACH_END();
    return self;
}
each_pair()
Alias for: each
each_value() click to toggle source
static VALUE
sparse_array_each_value(VALUE self)
{
    spar_table *table;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    SPAR_FOREACH_START(table);
    rb_yield((VALUE)(value));
    SPAR_FOREACH_END();
    return self;
}
empty?() click to toggle source
static VALUE
sparse_array_empty_p(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    return table->num_entries ? Qfalse : Qtrue;
}
fetch(p1, p2) click to toggle source
static VALUE
sparse_array_fetch(VALUE self, VALUE ri, VALUE def)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    if (spar_lookup(table, i, &res))
        return (VALUE)res;
    else
        return def;
}
has_key?(p1)
Alias for: include?
include?(p1) click to toggle source
static VALUE
sparse_array_include(VALUE self, VALUE ri)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    if (spar_lookup(table, i, &res))
        return Qtrue;
    else
        return Qfalse;
}
Also aliased as: has_key?
initialize_copy(p1) click to toggle source
static VALUE
sparse_array_init_copy(VALUE self, VALUE orig)
{
    spar_table *table;
    spar_table *original;
    GetSparseArray(self, table);
    GetSparseArray(orig, original);
    rb_obj_init_copy(self, orig);
    spar_copy_to(original, table);
    return self;
}
inspect() click to toggle source
static VALUE
sparse_array_inspect(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    if (table->num_entries == 0)
        return rb_usascii_str_new2("<SparseArray>");
    return rb_exec_recursive(sparse_array_inspect_rec, self, 0);
}
keys() click to toggle source
static VALUE
sparse_array_keys(VALUE self)
{
    spar_table *table;
    VALUE res;
    GetSparseArray(self, table);
    res = rb_ary_new2(table->num_entries);
    SPAR_FOREACH_START(table);
    (void)value;
    rb_ary_push(res, UINT2NUM(entry->key));
    SPAR_FOREACH_END();
    return res;
}
size() click to toggle source
static VALUE
sparse_array_size(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    return UINT2NUM(table->num_entries);
}
Also aliased as: count
values() click to toggle source
static VALUE
sparse_array_values(VALUE self)
{
    spar_table *table;
    VALUE res;
    GetSparseArray(self, table);
    res = rb_ary_new2(table->num_entries);
    SPAR_FOREACH_START(table);
    rb_ary_push(res, (VALUE)value);
    SPAR_FOREACH_END();
    return res;
}