Class: ObjectSpace::WeakMap

Inherits:
Object show all
Includes:
Enumerable
Defined in:
gc.c,
gc.c

Overview

An ObjectSpace::WeakMap object holds references to any objects, but those objects can get garbage collected.

This class is mostly used internally by WeakRef, please use lib/weakref.rb for the public interface.

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chain, #chunk, #chunk_while, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #filter, #filter_map, #find, #find_all, #find_index, #first, #flat_map, #grep, #grep_v, #group_by, #inject, #lazy, #map, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_a, #to_h, #uniq, #zip

Instance Method Details

#[](wmap) ⇒ Object

Retrieves a weakly referenced object with the given key



10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
# File 'gc.c', line 10720

static VALUE
wmap_aref(VALUE self, VALUE wmap)
{
    st_data_t data;
    VALUE obj;
    struct weakmap *w;
    rb_objspace_t *objspace = &rb_objspace;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    if (!st_lookup(w->wmap2obj, (st_data_t)wmap, &data)) return Qnil;
    obj = (VALUE)data;
    if (!wmap_live_p(objspace, obj)) return Qnil;
    return obj;
}

#[]=(wmap, orig) ⇒ Object

Creates a weak reference from the given key to the given value



10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
# File 'gc.c', line 10701

static VALUE
wmap_aset(VALUE self, VALUE wmap, VALUE orig)
{
    struct weakmap *w;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    if (FL_ABLE(orig)) {
        define_final0(orig, w->final);
    }
    if (FL_ABLE(wmap)) {
        define_final0(wmap, w->final);
    }

    st_update(w->obj2wmap, (st_data_t)orig, wmap_aset_update, wmap);
    st_insert(w->wmap2obj, (st_data_t)wmap, (st_data_t)orig);
    return nonspecial_obj_id(orig);
}

#eachObject

Iterates over keys and objects in a weakly referenced object



10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
# File 'gc.c', line 10568

static VALUE
wmap_each(VALUE self)
{
    struct weakmap *w;
    rb_objspace_t *objspace = &rb_objspace;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    st_foreach(w->wmap2obj, wmap_each_i, (st_data_t)objspace);
    return self;
}

#each_keyObject

Iterates over keys and objects in a weakly referenced object



10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
# File 'gc.c', line 10591

static VALUE
wmap_each_key(VALUE self)
{
    struct weakmap *w;
    rb_objspace_t *objspace = &rb_objspace;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    st_foreach(w->wmap2obj, wmap_each_key_i, (st_data_t)objspace);
    return self;
}

#each_pairObject

Iterates over keys and objects in a weakly referenced object



10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
# File 'gc.c', line 10568

static VALUE
wmap_each(VALUE self)
{
    struct weakmap *w;
    rb_objspace_t *objspace = &rb_objspace;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    st_foreach(w->wmap2obj, wmap_each_i, (st_data_t)objspace);
    return self;
}

#each_valueObject

Iterates over keys and objects in a weakly referenced object



10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
# File 'gc.c', line 10614

static VALUE
wmap_each_value(VALUE self)
{
    struct weakmap *w;
    rb_objspace_t *objspace = &rb_objspace;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    st_foreach(w->wmap2obj, wmap_each_value_i, (st_data_t)objspace);
    return self;
}

#include?(key) ⇒ Boolean

Returns true if key is registered

Returns:

  • (Boolean)


10736
10737
10738
10739
10740
# File 'gc.c', line 10736

static VALUE
wmap_has_key(VALUE self, VALUE key)
{
    return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
}

#inspectObject



10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
# File 'gc.c', line 10539

static VALUE
wmap_inspect(VALUE self)
{
    VALUE str;
    VALUE c = rb_class_name(CLASS_OF(self));
    struct weakmap *w;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void *)self);
    if (w->wmap2obj) {
	st_foreach(w->wmap2obj, wmap_inspect_i, str);
    }
    RSTRING_PTR(str)[0] = '#';
    rb_str_cat2(str, ">");
    return str;
}

#key?(key) ⇒ Boolean

Returns true if key is registered

Returns:

  • (Boolean)


10736
10737
10738
10739
10740
# File 'gc.c', line 10736

static VALUE
wmap_has_key(VALUE self, VALUE key)
{
    return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
}

#keysObject

Iterates over keys and objects in a weakly referenced object



10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
# File 'gc.c', line 10639

static VALUE
wmap_keys(VALUE self)
{
    struct weakmap *w;
    struct wmap_iter_arg args;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    args.objspace = &rb_objspace;
    args.value = rb_ary_new();
    st_foreach(w->wmap2obj, wmap_keys_i, (st_data_t)&args);
    return args.value;
}

#lengthObject

Returns the number of referenced objects



10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
# File 'gc.c', line 10743

static VALUE
wmap_size(VALUE self)
{
    struct weakmap *w;
    st_index_t n;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    n = w->wmap2obj->num_entries;
#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
    return ULONG2NUM(n);
#else
    return ULL2NUM(n);
#endif
}

#member?(key) ⇒ Boolean

Returns true if key is registered

Returns:

  • (Boolean)


10736
10737
10738
10739
10740
# File 'gc.c', line 10736

static VALUE
wmap_has_key(VALUE self, VALUE key)
{
    return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
}

#sizeObject

Returns the number of referenced objects



10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
# File 'gc.c', line 10743

static VALUE
wmap_size(VALUE self)
{
    struct weakmap *w;
    st_index_t n;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    n = w->wmap2obj->num_entries;
#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
    return ULONG2NUM(n);
#else
    return ULL2NUM(n);
#endif
}

#valuesObject

Iterates over values and objects in a weakly referenced object



10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
# File 'gc.c', line 10666

static VALUE
wmap_values(VALUE self)
{
    struct weakmap *w;
    struct wmap_iter_arg args;

    TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
    args.objspace = &rb_objspace;
    args.value = rb_ary_new();
    st_foreach(w->wmap2obj, wmap_values_i, (st_data_t)&args);
    return args.value;
}