summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-04-16 07:42:49 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-04-16 07:42:49 +0000
commit94a1bece4a4adb5c0f8f09fb9b95356bae24912b (patch)
tree87c751178430d6471e391108c73d92cf25ad4af9 /array.c
parente0041160736f1c0f092400eb75336311ab10fa1e (diff)
flock,flatten,signal to main_thread
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@168 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/array.c b/array.c
index f706a04..d88c333 100644
--- a/array.c
+++ b/array.c
@@ -1234,6 +1234,44 @@ ary_nitems(ary)
return INT2FIX(n);
}
+static VALUE
+ary_flatten_bang(ary)
+ VALUE ary;
+{
+ int i;
+ int mod = 0;
+
+ ary_modify(ary);
+ for (;;) {
+ int lmod = 0;
+
+ for (i=0; i<RARRAY(ary)->len; i++) {
+ if (TYPE(RARRAY(ary)->ptr[i]) == T_ARRAY) {
+ VALUE ary2 = RARRAY(ary)->ptr[i];
+
+ ary_replace(ary, i, RARRAY(ary2)->len, ary2);
+ i += RARRAY(ary2)->len - 1;
+ lmod++;
+ }
+ }
+ if (lmod == 0) break;
+ mod = lmod;
+ }
+
+ if (mod == 0) return Qnil;
+ return ary;
+}
+
+static VALUE
+ary_flatten(ary)
+ VALUE ary;
+{
+ VALUE v = ary_flatten_bang(ary_clone(ary));
+
+ if (NIL_P(v)) return ary;
+ return v;
+}
+
extern VALUE mEnumerable;
void
@@ -1304,6 +1342,8 @@ Init_Array()
rb_define_method(cArray, "uniq!", ary_uniq_bang, 0);
rb_define_method(cArray, "compact", ary_compact, 0);
rb_define_method(cArray, "compact!", ary_compact_bang, 0);
+ rb_define_method(cArray, "flatten", ary_flatten, 0);
+ rb_define_method(cArray, "flatten!", ary_flatten_bang, 0);
rb_define_method(cArray, "nitems", ary_nitems, 0);
cmp = rb_intern("<=>");