[前][次][番号順一覧][スレッド一覧]

ruby-changes:32014

From: nobu <ko1@a...>
Date: Mon, 9 Dec 2013 16:13:47 +0900 (JST)
Subject: [ruby-changes:32014] nobu:r44093 (trunk): gc.c: ObjectSpace::WeakMap#size

nobu	2013-12-09 16:13:40 +0900 (Mon, 09 Dec 2013)

  New Revision: 44093

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44093

  Log:
    gc.c: ObjectSpace::WeakMap#size
    
    * gc.c (wmap_size): add ObjectSpace::WeakMap#size and #length.

  Modified files:
    trunk/ChangeLog
    trunk/gc.c
    trunk/test/ruby/test_weakmap.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44092)
+++ ChangeLog	(revision 44093)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Dec  9 16:13:31 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* gc.c (wmap_size): add ObjectSpace::WeakMap#size and #length.
+
 Mon Dec  9 15:26:17 2013  Shugo Maeda  <shugo@r...>
 
 	* test/test_curses.rb: removed.
Index: gc.c
===================================================================
--- gc.c	(revision 44092)
+++ gc.c	(revision 44093)
@@ -6470,6 +6470,20 @@ wmap_has_key(VALUE self, VALUE key) https://github.com/ruby/ruby/blob/trunk/gc.c#L6470
     return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
 }
 
+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
+}
 
 /*
   ------------------------------ GC profiler ------------------------------
@@ -7304,6 +7318,8 @@ Init_GC(void) https://github.com/ruby/ruby/blob/trunk/gc.c#L7318
 	rb_define_method(rb_cWeakMap, "each_value", wmap_each_value, 0);
 	rb_define_method(rb_cWeakMap, "keys", wmap_keys, 0);
 	rb_define_method(rb_cWeakMap, "values", wmap_values, 0);
+	rb_define_method(rb_cWeakMap, "size", wmap_size, 0);
+	rb_define_method(rb_cWeakMap, "length", wmap_size, 0);
 	rb_define_private_method(rb_cWeakMap, "finalize", wmap_finalize, 1);
 	rb_include_module(rb_cWeakMap, rb_mEnumerable);
     }
Index: test/ruby/test_weakmap.rb
===================================================================
--- test/ruby/test_weakmap.rb	(revision 44092)
+++ test/ruby/test_weakmap.rb	(revision 44093)
@@ -93,4 +93,18 @@ class TestWeakMap < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_weakmap.rb#L93
     end
     assert_equal(2, n)
   end
+
+  def test_size
+    m = __callee__[/test_(.*)/, 1]
+    assert_equal(0, @wm.__send__(m))
+    x1 = "foo"
+    k1 = Object.new
+    @wm[k1] = x1
+    assert_equal(1, @wm.__send__(m))
+    x2 = "bar"
+    k2 = Object.new
+    @wm[k2] = x2
+    assert_equal(2, @wm.__send__(m))
+  end
+  alias test_length test_size
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]