summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-11 07:58:06 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-11 07:58:06 +0000
commit033a32b4c136033299a027416f9039b68b32065a (patch)
tree3c59dfbfc4bf41ebc7c090bc9aed7540e86d7f6c /enum.c
parentfd0aaf71dcc7e43435564e8d735403c733434590 (diff)
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15972 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c59
1 files changed, 44 insertions, 15 deletions
diff --git a/enum.c b/enum.c
index b09e82623f..d4de80d7b5 100644
--- a/enum.c
+++ b/enum.c
@@ -186,7 +186,18 @@ enum_find(int argc, VALUE *argv, VALUE obj)
}
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]);
@@ -198,30 +209,48 @@ find_index_i(VALUE i, VALUE *memo, int argc, VALUE *argv)
/*
* 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 @@ Init_Enumerable(void)
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);