summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-10-13 06:44:42 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-10-13 06:44:42 +0000
commit2e23ebc601fd5c8d7de232e470e2374df8888d11 (patch)
tree15b69a971374cd5d1fd5413ba6d9d659f56c27f0 /array.c
parentbe1fea072cd0d22788ef8a931c0c6b64a2503b5d (diff)
*** empty log message ***
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@540 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c47
1 files changed, 35 insertions, 12 deletions
diff --git a/array.c b/array.c
index 9cc0ac1815..a34545dfae 100644
--- a/array.c
+++ b/array.c
@@ -417,6 +417,29 @@ rb_ary_aref(argc, argv, ary)
}
static VALUE
+rb_ary_at(ary, pos)
+ VALUE ary, pos;
+{
+ return rb_ary_entry(ary, NUM2LONG(pos));
+}
+
+static VALUE
+rb_ary_first(ary)
+ VALUE ary;
+{
+ if (RARRAY(ary)->len == 0) return Qnil;
+ return RARRAY(ary)->ptr[0];
+}
+
+static VALUE
+rb_ary_last(ary)
+ VALUE ary;
+{
+ if (RARRAY(ary)->len == 0) return Qnil;
+ return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
+}
+
+static VALUE
rb_ary_index(ary, val)
VALUE ary;
VALUE val;
@@ -940,22 +963,19 @@ rb_ary_delete_at(ary, at)
VALUE ary;
VALUE at;
{
- long i1, i2, pos;
+ long i, pos = NUM2LONG(at), len = RARRAY(ary)->len;
VALUE del = Qnil;
+ if (pos >= len) return Qnil;
+ if (pos < 0) pos += len;
+ if (pos < 0) return Qnil;
+
rb_ary_modify(ary);
- pos = NUM2LONG(at);
- for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
- if (i1 == pos) {
- del = RARRAY(ary)->ptr[i1];
- continue;
- }
- if (i1 != i2) {
- RARRAY(ary)->ptr[i2] = RARRAY(ary)->ptr[i1];
- }
- i2++;
+ del = RARRAY(ary)->ptr[pos];
+ for (i = pos + 1; i < len; i++, pos++) {
+ RARRAY(ary)->ptr[pos] = RARRAY(ary)->ptr[i];
}
- RARRAY(ary)->len = i2;
+ RARRAY(ary)->len = pos;
return del;
}
@@ -1445,6 +1465,9 @@ Init_Array()
rb_define_method(rb_cArray, "[]", rb_ary_aref, -1);
rb_define_method(rb_cArray, "[]=", rb_ary_aset, -1);
+ rb_define_method(rb_cArray, "at", rb_ary_at, 1);
+ rb_define_method(rb_cArray, "first", rb_ary_first, 0);
+ rb_define_method(rb_cArray, "last", rb_ary_last, 0);
rb_define_method(rb_cArray, "concat", rb_ary_concat, 1);
rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
rb_define_method(rb_cArray, "push", rb_ary_push_method, -1);