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

ruby-changes:4480

From: ko1@a...
Date: Fri, 11 Apr 2008 16:58:25 +0900 (JST)
Subject: [ruby-changes:4480] knu - Ruby:r15972 (trunk): * enum.c (enum_find_index): Add support for find_index(obj);

knu	2008-04-11 16:58:06 +0900 (Fri, 11 Apr 2008)

  New Revision: 15972

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/enum.c
    trunk/test/ruby/test_enum.rb

  Log:
    * enum.c (enum_find_index): Add support for find_index(obj);
      [ruby-dev:34313].
    
    * array.c (rb_ary_index): Define find_index as an alias to index.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/array.c?r1=15972&r2=15971&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_enum.rb?r1=15972&r2=15971&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15972&r2=15971&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/enum.c?r1=15972&r2=15971&diff_format=u

Index: array.c
===================================================================
--- array.c	(revision 15971)
+++ array.c	(revision 15972)
@@ -877,6 +877,8 @@
  *     a.index("b")        #=> 1
  *     a.index("z")        #=> nil
  *     a.index{|x|x=="b"}  #=> 1
+ *
+ *  This is an alias of <code>#find_index</code>.
  */
 
 static VALUE
@@ -3378,6 +3380,7 @@
     rb_define_method(rb_cArray, "length", rb_ary_length, 0);
     rb_define_alias(rb_cArray,  "size", "length");
     rb_define_method(rb_cArray, "empty?", rb_ary_empty_p, 0);
+    rb_define_method(rb_cArray, "find_index", rb_ary_index, -1);
     rb_define_method(rb_cArray, "index", rb_ary_index, -1);
     rb_define_method(rb_cArray, "rindex", rb_ary_rindex, -1);
     rb_define_method(rb_cArray, "join", rb_ary_join_m, -1);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15971)
+++ ChangeLog	(revision 15972)
@@ -1,3 +1,10 @@
+Fri Apr 11 16:44:43 2008  Akinori MUSHA  <knu@i...>
+
+	* enum.c (enum_find_index): Add support for find_index(obj);
+	  [ruby-dev:34313].
+
+	* array.c (rb_ary_index): Define find_index as an alias to index.
+
 Fri Apr 11 16:42:33 2008  Yukihiro Matsumoto  <matz@r...>
 
 	* lib/yaml/store.rb (YAML::load): modified to support empty
Index: enum.c
===================================================================
--- enum.c	(revision 15971)
+++ enum.c	(revision 15972)
@@ -186,8 +186,19 @@
 }
 
 static VALUE
-find_index_i(VALUE i, VALUE *memo, int argc, VALUE *argv)
+find_index_i(VALUE i, VALUE *memo)
 {
+    if (rb_equal(i, memo[2])) {
+	memo[0] = UINT2NUM(memo[1]);
+	rb_iter_break();
+    }
+    memo[1]++;
+    return Qnil;
+}
+
+static VALUE
+find_index_iter_i(VALUE i, VALUE *memo, int argc, VALUE *argv)
+{
     if (RTEST(enum_yield(argc, argv))) {
 	memo[0] = UINT2NUM(memo[1]);
 	rb_iter_break();
@@ -198,30 +209,48 @@
 
 /*
  *  call-seq:
- *     enum.find_index()   {| obj | block }  => int
+ *     enum.find_index(value)            => int or nil
+ *     enum.find_index {| obj | block }  => int or nil
  *  
- *  Passes each entry in <i>enum</i> to <em>block</em>. Returns the
- *  index for the first for which <em>block</em> is not <code>false</code>.
- *  If no object matches, returns <code>nil</code>
+ *  Compares each entry in <i>enum</i> with <em>value</em> or passes
+ *  to <em>block</em>.  Returns the index for the first for which the
+ *  evaluated value is non-false.  If no object matches, returns
+ *  <code>nil</code>
  *     
  *     (1..10).find_index  {|i| i % 5 == 0 and i % 7 == 0 }   #=> nil
  *     (1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 }   #=> 34
+ *     (1..100).find_index(50)                                #=> 49
  *     
  */
 
 static VALUE
-enum_find_index(VALUE obj)
+enum_find_index(int argc, VALUE *argv, VALUE obj)
 {
-    VALUE memo[2];
+    VALUE memo[3];	/* [return value, current index, condition value] */
+    rb_block_call_func *func;
 
-    RETURN_ENUMERATOR(obj, 0, 0);
-    memo[0] = Qundef;
-    memo[1] = 0;
-    rb_block_call(obj, id_each, 0, 0, find_index_i, (VALUE)memo);
-    if (memo[0] != Qundef) {
-	return memo[0];
+    if (argc == 0) {
+        RETURN_ENUMERATOR(obj, 0, 0);
+        memo[0] = Qnil;
+        memo[1] = 0;
+        memo[2] = Qundef;
+        func = find_index_iter_i;
     }
-    return Qnil;
+    else {
+        VALUE item;
+
+	rb_scan_args(argc, argv, "1", &item);
+	if (rb_block_given_p()) {
+	    rb_warn("given block not used");
+	}
+        memo[0] = Qnil;
+        memo[1] = 0;
+        memo[2] = item;
+        func = find_index_i;
+    }
+
+    rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
+    return memo[0];
 }
 
 static VALUE
@@ -1688,7 +1717,7 @@
     rb_define_method(rb_mEnumerable,"count", enum_count, -1);
     rb_define_method(rb_mEnumerable,"find", enum_find, -1);
     rb_define_method(rb_mEnumerable,"detect", enum_find, -1);
-    rb_define_method(rb_mEnumerable,"find_index", enum_find_index, 0);
+    rb_define_method(rb_mEnumerable,"find_index", enum_find_index, -1);
     rb_define_method(rb_mEnumerable,"find_all", enum_find_all, 0);
     rb_define_method(rb_mEnumerable,"select", enum_find_all, 0);
     rb_define_method(rb_mEnumerable,"reject", enum_reject, 0);
Index: test/ruby/test_enum.rb
===================================================================
--- test/ruby/test_enum.rb	(revision 15971)
+++ test/ruby/test_enum.rb	(revision 15972)
@@ -43,8 +43,10 @@
   end
 
   def test_find_index
+    assert_equal(1, @obj.find_index(2))
     assert_equal(1, @obj.find_index {|x| x % 2 == 0 })
     assert_equal(nil, @obj.find_index {|x| false })
+    assert_raise(ArgumentError) { @obj.find_index(0, 1) }
   end
 
   def test_find_all

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

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