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

ruby-changes:25468

From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:19:26 +0900 (JST)
Subject: [ruby-changes:25468] marcandRe: r37525 (trunk): * enumerator.c: Support for lazy.cycle.size

marcandre	2012-11-07 02:16:58 +0900 (Wed, 07 Nov 2012)

  New Revision: 37525

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

  Log:
    * enumerator.c: Support for lazy.cycle.size
      [Feature #6636]

  Modified files:
    trunk/enum.c
    trunk/enumerator.c
    trunk/internal.h
    trunk/test/ruby/test_lazy_enumerator.rb

Index: enumerator.c
===================================================================
--- enumerator.c	(revision 37524)
+++ enumerator.c	(revision 37525)
@@ -1741,6 +1741,11 @@
 }
 
 static VALUE
+lazy_cycle_size(VALUE lazy) {
+    return enum_cycle_size(rb_ivar_get(lazy, id_receiver), rb_ivar_get(lazy, id_arguments));
+}
+
+static VALUE
 lazy_cycle_func(VALUE val, VALUE m, int argc, VALUE *argv)
 {
     return rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
@@ -1764,7 +1769,7 @@
     return lazy_set_method(rb_block_call(rb_cLazy, id_new, len,
 					 RARRAY_PTR(args), lazy_cycle_func,
 					 args /* prevent from GC */),
-			   rb_ary_new4(argc, argv), 0);
+			   rb_ary_new4(argc, argv), lazy_cycle_size);
 }
 
 static VALUE
Index: enum.c
===================================================================
--- enum.c	(revision 37524)
+++ enum.c	(revision 37525)
@@ -2235,7 +2235,7 @@
     return Qnil;
 }
 
-static VALUE
+VALUE
 enum_cycle_size(VALUE self, VALUE args)
 {
     long mul;
Index: internal.h
===================================================================
--- internal.h	(revision 37524)
+++ internal.h	(revision 37525)
@@ -87,6 +87,9 @@
 /* encoding.c */
 void rb_gc_mark_encodings(void);
 
+/* enum.c */
+VALUE enum_cycle_size(VALUE self, VALUE args);
+
 /* error.c */
 NORETURN(PRINTF_ARGS(void rb_compile_bug(const char*, int, const char*, ...), 3, 4));
 VALUE rb_check_backtrace(VALUE);
Index: test/ruby/test_lazy_enumerator.rb
===================================================================
--- test/ruby/test_lazy_enumerator.rb	(revision 37524)
+++ test/ruby/test_lazy_enumerator.rb	(revision 37525)
@@ -346,5 +346,14 @@
     assert_equal 0, lazy.drop(4).size
     assert_equal Float::INFINITY, loop.lazy.drop(4).size
     assert_equal nil, lazy.select{}.drop(4).size
+
+    assert_equal 0, lazy.cycle(0).size
+    assert_equal 6, lazy.cycle(2).size
+    assert_equal 3 << 80, 4.times.inject(lazy){|enum| enum.cycle(1 << 20)}.size
+    assert_equal Float::INFINITY, lazy.cycle.size
+    assert_equal Float::INFINITY, loop.lazy.cycle(4).size
+    assert_equal Float::INFINITY, loop.lazy.cycle.size
+    assert_equal nil, lazy.select{}.cycle(4).size
+    assert_equal nil, lazy.select{}.cycle.size
   end
 end

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

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