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

ruby-changes:43637

From: nobu <ko1@a...>
Date: Wed, 20 Jul 2016 17:44:12 +0900 (JST)
Subject: [ruby-changes:43637] nobu:r55710 (trunk): enumerator.c: Enumerator::Lazy#uniq

nobu	2016-07-20 17:44:08 +0900 (Wed, 20 Jul 2016)

  New Revision: 55710

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55710

  Log:
    enumerator.c: Enumerator::Lazy#uniq
    
    * enumerator.c (lazy_uniq): new method Enumerator::Lazy#uniq.
      [Feature #11090]

  Modified files:
    trunk/ChangeLog
    trunk/enumerator.c
    trunk/test/ruby/test_enumerator.rb
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 55709)
+++ enumerator.c	(revision 55710)
@@ -1959,6 +1959,41 @@ lazy_drop_while(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L1959
 }
 
 static VALUE
+lazy_uniq_i(VALUE i, VALUE hash, int argc, const VALUE *argv, VALUE yielder)
+{
+    if (rb_hash_add_new_element(hash, i, Qfalse))
+	return Qnil;
+    return rb_funcall2(yielder, id_yield, argc, argv);
+}
+
+static VALUE
+lazy_uniq_func(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
+{
+    VALUE yielder = (--argc, *argv++);
+    i = rb_enum_values_pack(argc, argv);
+    return lazy_uniq_i(i, hash, argc, argv, yielder);
+}
+
+static VALUE
+lazy_uniq_iter(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
+{
+    VALUE yielder = (--argc, *argv++);
+    i = rb_yield_values2(argc, argv);
+    return lazy_uniq_i(i, hash, argc, argv, yielder);
+}
+
+static VALUE
+lazy_uniq(VALUE obj)
+{
+    rb_block_call_func *const func =
+	rb_block_given_p() ? lazy_uniq_iter : lazy_uniq_func;
+    VALUE hash = rb_obj_hide(rb_hash_new());
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 func, hash),
+			   0, 0);
+}
+
+static VALUE
 lazy_super(int argc, VALUE *argv, VALUE lazy)
 {
     return enumerable_lazy(rb_call_super(argc, argv));
@@ -2074,6 +2109,7 @@ InitVM_Enumerator(void) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L2109
     rb_define_method(rb_cLazy, "slice_before", lazy_super, -1);
     rb_define_method(rb_cLazy, "slice_after", lazy_super, -1);
     rb_define_method(rb_cLazy, "slice_when", lazy_super, -1);
+    rb_define_method(rb_cLazy, "uniq", lazy_uniq, 0);
 
     rb_define_alias(rb_cLazy, "force", "to_a");
 
Index: test/ruby/test_enumerator.rb
===================================================================
--- test/ruby/test_enumerator.rb	(revision 55709)
+++ test/ruby/test_enumerator.rb	(revision 55710)
@@ -646,5 +646,10 @@ class TestEnumerator < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enumerator.rb#L646
     e.next
     assert_raise(StopIteration) { e.peek }
   end
+
+  def test_uniq
+    assert_equal([1, 2, 3, 4, 5, 10],
+                 (1..Float::INFINITY).lazy.uniq{|x| (x**2) % 10 }.first(6))
+  end
 end
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55709)
+++ ChangeLog	(revision 55710)
@@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Wed Jul 20 17:39:11 2016  Nobuyoshi Nakada  <nobu@r...>
+Wed Jul 20 17:44:07 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* enumerator.c (lazy_uniq): new method Enumerator::Lazy#uniq.
+	  [Feature #11090]
 
 	* enum.c (enum_uniq): new method Enumerable#uniq.
 	  [Feature #11090]

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

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