OSDN Git Service

IntGroup#index is implemented
authortoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Sun, 16 Feb 2014 08:07:24 +0000 (08:07 +0000)
committertoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Sun, 16 Feb 2014 08:07:24 +0000 (08:07 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/molby/trunk@436 a2be9bc6-48de-4e38-9406-05402d4bc13c

Documents/src/molby_rb/IntGroup.html
MolLib/Ruby_bind/ruby_types.c

index e530822..5610636 100644 (file)
@@ -204,6 +204,22 @@ self.
 </div>
 </div>
 
+<div id="method-index" class="method-detail">
+<a name="index"></a>
+<div class="method-heading">
+<span class="method-name">index(val) &rarr; Integer<br />
+<span class="method-name">index { |item| ... } &rarr; Integer<br />
+</span>
+</div>
+<div class="method-description">
+<p>
+Returns the index of the first element that is equal to the given value.
+In the second form, returns the index of the first element for which the associated block returns true.
+If no such element was found, returns nil.
+</p>
+</div>
+</div>
+
 <div id="method-M000034" class="method-detail">
 <a name="M000034"></a>
 <div class="method-heading">
index d011470..7593ec5 100644 (file)
@@ -2805,6 +2805,52 @@ s_IntGroup_Offset(VALUE self, VALUE ofs)
 
 /*
  *  call-seq:
+ *     index(val) -> Integer
+ *     index {|item| ...} -> Integer
+ *
+ *  Returns the index of the first element that is equal to the given value.
+ *  In the second form, returns the index of the first element for which the
+ *  associated block returns true.
+ *  If no element was found, returns nil.
+ */
+static VALUE
+s_IntGroup_Index(int argc, VALUE *argv, VALUE self)
+{
+       IntGroup *ig;
+       VALUE val;
+       int ival, idx;
+       Data_Get_Struct(self, IntGroup, ig);
+       if (argc == 1) {
+               /*  First form  */
+               ival = NUM2INT(rb_Integer(argv[0]));
+               idx = IntGroupLookupPoint(ig, ival);
+               if (idx >= 0)
+                       return INT2NUM(idx);
+               else return Qnil;
+       } else if (argc == 0 && rb_block_given_p()) {
+               IntGroupIterator iter;
+               IntGroupIteratorInit(ig, &iter);
+               idx = 0;
+               while ((ival = IntGroupIteratorNext(&iter)) >= 0) {
+                       val = rb_yield(INT2NUM(ival));
+                       if (RTEST(val)) {
+                               val = INT2NUM(idx);
+                               break;
+                       }
+                       idx++;
+               }
+               IntGroupIteratorRelease(&iter);
+               if (RTEST(val))
+                       return val;
+               else return Qnil;
+       } else {
+               rb_raise(rb_eArgError, "should be called as index(val) or index {|item| ...}");
+               return Qnil;  /*  Not reached  */
+       }
+}
+
+/*
+ *  call-seq:
  *     inspect -> String
  *
  *  Create a String in the form "IntGroup[...]".
@@ -2979,6 +3025,7 @@ Init_MolbyTypes(void)
        rb_define_alias(rb_cIntGroup, "^", "sym_difference");
        rb_define_method(rb_cIntGroup, "range_at", s_IntGroup_RangeAt, 1);
        rb_define_method(rb_cIntGroup, "each_range", s_IntGroup_Equal, 0);
+       rb_define_method(rb_cIntGroup, "index", s_IntGroup_Index, -1);
        rb_define_method(rb_cIntGroup, "inspect", s_IntGroup_Inspect, 0);
        rb_define_alias(rb_cIntGroup, "to_s", "inspect");
        rb_define_singleton_method(rb_cIntGroup, "[]", s_IntGroup_Create, -1);