summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-array-ext')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb141
-rw-r--r--mrbgems/mruby-array-ext/src/array.c177
-rw-r--r--mrbgems/mruby-array-ext/test/array.rb22
3 files changed, 246 insertions, 94 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index ecd09aa6e..f7576cbf7 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -150,7 +150,7 @@ class Array
# [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]
#
def &(elem)
- raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array
+ raise TypeError, "cannot convert #{elem.class} into Array" unless elem.class == Array
hash = {}
array = []
@@ -204,7 +204,7 @@ class Array
# a.intersect?(b) #=> true
# a.intersect?(c) #=> false
def intersect?(ary)
- raise TypeError, "can't convert #{ary.class} into Array" unless ary.class == Array
+ raise TypeError, "cannot convert #{ary.class} into Array" unless ary.class == Array
hash = {}
if self.length > ary.length
@@ -294,41 +294,6 @@ class Array
end
end
- ##
- # call-seq:
- # ary.compact -> new_ary
- #
- # Returns a copy of +self+ with all +nil+ elements removed.
- #
- # [ "a", nil, "b", nil, "c", nil ].compact
- # #=> [ "a", "b", "c" ]
- #
- def compact
- result = self.dup
- result.compact!
- result
- end
-
- ##
- # call-seq:
- # ary.compact! -> ary or nil
- #
- # Removes +nil+ elements from the array.
- # Returns +nil+ if no changes were made, otherwise returns
- # <i>ary</i>.
- #
- # [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
- # [ "a", "b", "c" ].compact! #=> nil
- #
- def compact!
- result = self.select { |e| !e.nil? }
- if result.size == self.size
- nil
- else
- self.replace(result)
- end
- end
-
# for efficiency
def reverse_each(&block)
return to_enum :reverse_each unless block
@@ -421,7 +386,6 @@ class Array
end
beg = len = 0
- ary = []
if block
if arg0.nil? && arg1.nil? && arg2.nil?
# ary.fill { |index| block } -> ary
@@ -485,57 +449,6 @@ class Array
##
# call-seq:
- # ary.rotate(count=1) -> new_ary
- #
- # Returns a new array by rotating +self+ so that the element at +count+ is
- # the first element of the new array.
- #
- # If +count+ is negative then it rotates in the opposite direction, starting
- # from the end of +self+ where +-1+ is the last element.
- #
- # a = [ "a", "b", "c", "d" ]
- # a.rotate #=> ["b", "c", "d", "a"]
- # a #=> ["a", "b", "c", "d"]
- # a.rotate(2) #=> ["c", "d", "a", "b"]
- # a.rotate(-3) #=> ["b", "c", "d", "a"]
-
- def rotate(count=1)
- ary = []
- len = self.length
-
- if len > 0
- idx = (count < 0) ? (len - (~count % len) - 1) : (count % len) # rotate count
- len.times do
- ary << self[idx]
- idx += 1
- idx = 0 if idx > len-1
- end
- end
- ary
- end
-
- ##
- # call-seq:
- # ary.rotate!(count=1) -> ary
- #
- # Rotates +self+ in place so that the element at +count+ comes first, and
- # returns +self+.
- #
- # If +count+ is negative then it rotates in the opposite direction, starting
- # from the end of the array where +-1+ is the last element.
- #
- # a = [ "a", "b", "c", "d" ]
- # a.rotate! #=> ["b", "c", "d", "a"]
- # a #=> ["b", "c", "d", "a"]
- # a.rotate!(2) #=> ["d", "a", "b", "c"]
- # a.rotate!(-3) #=> ["a", "b", "c", "d"]
-
- def rotate!(count=1)
- self.replace(self.rotate(count))
- end
-
- ##
- # call-seq:
# ary.delete_if { |item| block } -> ary
# ary.delete_if -> Enumerator
#
@@ -746,7 +659,6 @@ class Array
return to_enum :keep_if unless block
idx = 0
- len = self.size
while idx < self.size do
if block.call(self[idx])
idx += 1
@@ -961,7 +873,7 @@ class Array
# ary.to_h -> Hash
# ary.to_h{|item| ... } -> Hash
#
- # Returns the result of interpreting <i>aray</i> as an array of
+ # Returns the result of interpreting <i>array</i> as an array of
# <tt>[key, value]</tt> pairs. If a block is given, it should
# return <tt>[key, value]</tt> pairs to construct a hash.
#
@@ -984,4 +896,51 @@ class Array
alias append push
alias prepend unshift
alias filter! select!
+
+ ##
+ # call-seq:
+ # ary.product(*arys) -> array
+ # ary.product(*arys) { |item| ... } -> self
+ def product(*arys, &block)
+ size = arys.size
+ i = size
+ while i > 0
+ i -= 1
+ unless arys[i].kind_of?(Array)
+ raise TypeError, "no implicit conversion into Array"
+ end
+ end
+
+ i = size
+ total = self.size
+ total *= arys[i -= 1].size while i > 0
+
+ if block
+ result = self
+ list = ->(*, e) { block.call e }
+ class << list; alias []= call; end
+ else
+ result = [nil] * total
+ list = result
+ end
+
+ i = 0
+ while i < total
+ group = [nil] * (size + 1)
+ j = size
+ n = i
+ while j > 0
+ j -= 1
+ a = arys[j]
+ b = a.size
+ group[j + 1] = a[n % b]
+ n /= b
+ end
+ group[0] = self[n]
+ list[i] = group
+ i += 1
+ end
+
+ result
+ end
end
diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c
index 8df9d7f30..d97778642 100644
--- a/mrbgems/mruby-array-ext/src/array.c
+++ b/mrbgems/mruby-array-ext/src/array.c
@@ -95,6 +95,12 @@ mrb_ary_at(mrb_state *mrb, mrb_value ary)
}
static mrb_value
+ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n)
+{
+ return mrb_ary_entry(ary, n);
+}
+
+static mrb_value
mrb_ary_values_at(mrb_state *mrb, mrb_value self)
{
mrb_int argc;
@@ -102,10 +108,9 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc);
- return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref);
+ return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, ary_ref);
}
-
/*
* call-seq:
* ary.slice!(index) -> obj or nil
@@ -183,6 +188,168 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self)
return ary;
}
+/*
+ * call-seq:
+ * ary.compact -> new_ary
+ *
+ * Returns a copy of +self+ with all +nil+ elements removed.
+ *
+ * [ "a", nil, "b", nil, "c", nil ].compact
+ * #=> [ "a", "b", "c" ]
+ */
+
+static mrb_value
+mrb_ary_compact(mrb_state *mrb, mrb_value self)
+{
+ mrb_value ary = mrb_ary_new(mrb);
+ mrb_int len = RARRAY_LEN(self);
+ mrb_value *p = RARRAY_PTR(self);
+
+ for (mrb_int i = 0; i < len; ++i) {
+ if (!mrb_nil_p(p[i])) {
+ mrb_ary_push(mrb, ary, p[i]);
+ }
+ }
+ return ary;
+}
+
+/*
+ * call-seq:
+ * ary.compact! -> ary or nil
+ *
+ * Removes +nil+ elements from the array.
+ * Returns +nil+ if no changes were made, otherwise returns
+ * <i>ary</i>.
+ *
+ * [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
+ * [ "a", "b", "c" ].compact! #=> nil
+ */
+static mrb_value
+mrb_ary_compact_bang(mrb_state *mrb, mrb_value self)
+{
+ struct RArray *a = mrb_ary_ptr(self);
+ mrb_int i, j = 0;
+ mrb_int len = ARY_LEN(a);
+ mrb_value *p = ARY_PTR(a);
+
+ mrb_ary_modify(mrb, a);
+ for (i = 0; i < len; ++i) {
+ if (!mrb_nil_p(p[i])) {
+ if (i != j) p[j] = p[i];
+ j++;
+ }
+ }
+ if (i == j) return mrb_nil_value();
+ if (j < len) ARY_SET_LEN(RARRAY(self), j);
+ return self;
+}
+
+
+/*
+ * call-seq:
+ * ary.rotate(count=1) -> new_ary
+ *
+ * Returns a new array by rotating +self+ so that the element at +count+ is
+ * the first element of the new array.
+ *
+ * If +count+ is negative then it rotates in the opposite direction, starting
+ * from the end of +self+ where +-1+ is the last element.
+ *
+ * a = [ "a", "b", "c", "d" ]
+ * a.rotate #=> ["b", "c", "d", "a"]
+ * a #=> ["a", "b", "c", "d"]
+ * a.rotate(2) #=> ["c", "d", "a", "b"]
+ * a.rotate(-3) #=> ["b", "c", "d", "a"]
+ */
+static mrb_value
+mrb_ary_rotate(mrb_state *mrb, mrb_value self)
+{
+ mrb_value ary = mrb_ary_new(mrb);
+ mrb_int len = RARRAY_LEN(self);
+ mrb_value *p = RARRAY_PTR(self);
+ mrb_int count=1, idx;
+
+ mrb_get_args(mrb, "|i", &count);
+ if (len <= 0) return ary;
+ if (count < 0) {
+ idx = len - (~count % len) - 1;
+ }
+ else {
+ idx = count % len;
+ }
+ for (mrb_int i = 0; i<len; i++) {
+ mrb_ary_push(mrb, ary, p[idx++]);
+ if (idx == len) idx = 0;
+ }
+ return ary;
+}
+
+static void
+rev(mrb_value *p, mrb_int beg, mrb_int end)
+{
+ for (mrb_int i=beg,j=end-1; i<j; i++,j--) {
+ mrb_value v = p[i];
+ p[i] = p[j];
+ p[j] = v;
+ }
+}
+
+/*
+ * call-seq:
+ * ary.rotate!(count=1) -> ary
+ *
+ * Rotates +self+ in place so that the element at +count+ comes first, and
+ * returns +self+.
+ *
+ * If +count+ is negative then it rotates in the opposite direction, starting
+ * from the end of the array where +-1+ is the last element.
+ *
+ * a = [ "a", "b", "c", "d" ]
+ * a.rotate! #=> ["b", "c", "d", "a"]
+ * a #=> ["b", "c", "d", "a"]
+ * a.rotate!(2) #=> ["d", "a", "b", "c"]
+ * a.rotate!(-3) #=> ["a", "b", "c", "d"]
+ */
+static mrb_value
+mrb_ary_rotate_bang(mrb_state *mrb, mrb_value self)
+{
+ struct RArray *a = mrb_ary_ptr(self);
+ mrb_int len = ARY_LEN(a);
+ mrb_value *p = ARY_PTR(a);
+ mrb_int count=1, idx;
+
+ mrb_get_args(mrb, "|i", &count);
+ mrb_ary_modify(mrb, a);
+ if (len == 0 || count == 0) return self;
+ if (count == 1) {
+ mrb_value v = p[0];
+ for (mrb_int i=1; i<len; i++) {
+ p[i-1] = p[i];
+ }
+ p[len-1] = v;
+ return self;
+ }
+ if (count < 0) {
+ idx = len - (~count % len) - 1;
+ }
+ else {
+ idx = count % len;
+ }
+ /* e.g. [1,2,3,4,5].rotate!(2) -> [3,4,5,1,2] */
+ /* first, reverse the whole array */
+ /* [1,2,3,4,5] -> [5,4,3,2,1] */
+ rev(p, 0, len);
+ /* then, re-reverse part before idx */
+ /* [5,4,3,2,1] -> [3,4,5,2,1] */
+ /* ^idx ~~~~~ */
+ rev(p, 0, len-idx);
+ /* finally, re-reverse part after idx */
+ /* [3,4,5,2,1] -> [3,4,5,1,2] */
+ /* ^idx ~~~ */
+ rev(p, len-idx, len);
+ return self;
+}
+
void
mrb_mruby_array_ext_gem_init(mrb_state* mrb)
{
@@ -192,7 +359,11 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1));
mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1));
mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY());
- mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang, MRB_ARGS_ARG(1,1));
+ mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang, MRB_ARGS_ARG(1,1));
+ mrb_define_method(mrb, a, "compact", mrb_ary_compact, MRB_ARGS_NONE());
+ mrb_define_method(mrb, a, "compact!", mrb_ary_compact_bang, MRB_ARGS_NONE());
+ mrb_define_method(mrb, a, "rotate", mrb_ary_rotate, MRB_ARGS_OPT(1));
+ mrb_define_method(mrb, a, "rotate!", mrb_ary_rotate_bang, MRB_ARGS_OPT(1));
}
void
diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb
index 3f73ad8b9..879980c7e 100644
--- a/mrbgems/mruby-array-ext/test/array.rb
+++ b/mrbgems/mruby-array-ext/test/array.rb
@@ -421,3 +421,25 @@ assert('Array#transpose') do
assert_raise(TypeError) { [1].transpose }
assert_raise(IndexError) { [[1], [2,3,4]].transpose }
end
+
+assert "Array#product" do
+ assert_equal [[1], [2], [3]], [1, 2, 3].product
+ assert_equal [], [1, 2, 3].product([])
+ assert_equal [], [1, 2, 3].product([4, 5, 6], [])
+
+ expect = [[1, 5, 8], [1, 5, 9], [1, 6, 8], [1, 6, 9], [1, 7, 8], [1, 7, 9],
+ [2, 5, 8], [2, 5, 9], [2, 6, 8], [2, 6, 9], [2, 7, 8], [2, 7, 9],
+ [3, 5, 8], [3, 5, 9], [3, 6, 8], [3, 6, 9], [3, 7, 8], [3, 7, 9],
+ [4, 5, 8], [4, 5, 9], [4, 6, 8], [4, 6, 9], [4, 7, 8], [4, 7, 9]]
+ assert_equal expect, [1, 2, 3, 4].product([5, 6, 7], [8, 9])
+
+ expect = [[1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 5, 7], [1, 5, 8], [1, 5, 9], [1, 6, 7], [1, 6, 8], [1, 6, 9],
+ [2, 4, 7], [2, 4, 8], [2, 4, 9], [2, 5, 7], [2, 5, 8], [2, 5, 9], [2, 6, 7], [2, 6, 8], [2, 6, 9],
+ [3, 4, 7], [3, 4, 8], [3, 4, 9], [3, 5, 7], [3, 5, 8], [3, 5, 9], [3, 6, 7], [3, 6, 8], [3, 6, 9]]
+
+ assert_equal expect, [1, 2, 3].product([4, 5, 6], [7, 8, 9])
+ base = [1, 2, 3]
+ x = []
+ assert_equal base, base.product([4, 5, 6], [7, 8, 9]) { |e| x << e }
+ assert_equal expect, x
+end