diff options
| author | Hiroshi Mimaki <[email protected]> | 2019-10-18 14:46:03 +0900 |
|---|---|---|
| committer | Hiroshi Mimaki <[email protected]> | 2019-10-18 14:46:03 +0900 |
| commit | b6546835457d1935a9c77965686b2a1256874d96 (patch) | |
| tree | 724cfd71a7c956b0648e8c58f3717d797fff5f29 /mrbgems/mruby-array-ext | |
| parent | 8ee516436b8d174a50764939bee23a442aa00b3f (diff) | |
| parent | 20d01f118ddb7e7f2f36926a7a3db35573611857 (diff) | |
| download | mruby-b6546835457d1935a9c77965686b2a1256874d96.tar.gz mruby-b6546835457d1935a9c77965686b2a1256874d96.zip | |
Merge master.
Diffstat (limited to 'mrbgems/mruby-array-ext')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrbgem.rake | 1 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 56 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/src/array.c | 6 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/test/array.rb | 73 |
4 files changed, 100 insertions, 36 deletions
diff --git a/mrbgems/mruby-array-ext/mrbgem.rake b/mrbgems/mruby-array-ext/mrbgem.rake index 58d4428d4..882caf1ab 100644 --- a/mrbgems/mruby-array-ext/mrbgem.rake +++ b/mrbgems/mruby-array-ext/mrbgem.rake @@ -2,5 +2,4 @@ MRuby::Gem::Specification.new('mruby-array-ext') do |spec| spec.license = 'MIT' spec.author = 'mruby developers' spec.summary = 'Array class extension' - spec.add_test_dependency 'mruby-enumerator', core: 'mruby-enumerator' end diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 387bd6c90..5492ba2eb 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -90,6 +90,22 @@ class Array ## # call-seq: + # ary.difference(other_ary1, other_ary2, ...) -> new_ary + # + # Returns a new array that is a copy of the original array, removing all + # occurrences of any item that also appear in +other_ary+. The order is + # preserved from the original array. + # + def difference(*args) + ary = self + args.each do |x| + ary = ary - x + end + ary + end + + ## + # call-seq: # ary | other_ary -> new_ary # # Set Union---Returns a new array by joining this array with @@ -159,6 +175,24 @@ class Array ## # call-seq: + # ary.intersection(other_ary,...) -> new_ary + # + # Set Intersection---Returns a new array containing elements common to + # this array and <i>other_ary</i>s, removing duplicates. The order is + # preserved from the original array. + # + # [1, 2, 3].intersection([3, 4, 1], [1, 3, 5]) #=> [1, 3] + # + def intersection(*args) + ary = self + args.each do |x| + ary = ary & x + end + ary + end + + ## + # call-seq: # ary.flatten -> new_ary # ary.flatten(level) -> new_ary # @@ -815,12 +849,11 @@ class Array # a.permutation(0).to_a #=> [[]] # one permutation of length 0 # a.permutation(4).to_a #=> [] # no permutations of length 4 def permutation(n=self.size, &block) - size = self.size return to_enum(:permutation, n) unless block - return if n > size + size = self.size if n == 0 - yield [] - else + yield [] + elsif 0 < n && n <= size i = 0 while i<size result = [self[i]] @@ -835,6 +868,7 @@ class Array i += 1 end end + self end ## @@ -861,9 +895,8 @@ class Array # a.combination(5).to_a #=> [] # no combinations of length 5 def combination(n, &block) - size = self.size return to_enum(:combination, n) unless block - return if n > size + size = self.size if n == 0 yield [] elsif n == 1 @@ -872,7 +905,7 @@ class Array yield [self[i]] i += 1 end - else + elsif n <= size i = 0 while i<size result = [self[i]] @@ -882,6 +915,7 @@ class Array i += 1 end end + self end ## @@ -903,8 +937,8 @@ class Array column_count = nil self.each do |row| raise TypeError unless row.is_a?(Array) - column_count ||= row.count - raise IndexError, 'element size differs' unless column_count == row.count + column_count ||= row.size + raise IndexError, 'element size differs' unless column_count == row.size end Array.new(column_count) do |column_index| @@ -936,4 +970,8 @@ class Array end h end + + alias append push + alias prepend unshift + alias filter! select! end diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index b6d9c9c80..ab6d99133 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -65,7 +65,7 @@ mrb_ary_rassoc(mrb_state *mrb, mrb_value ary) for (i = 0; i < RARRAY_LEN(ary); ++i) { v = RARRAY_PTR(ary)[i]; - if (mrb_type(v) == MRB_TT_ARRAY && + if (mrb_array_p(v) && RARRAY_LEN(v) > 1 && mrb_equal(mrb, RARRAY_PTR(v)[1], value)) return v; @@ -145,7 +145,7 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "o|i", &index, &len); switch (mrb_type(index)) { case MRB_TT_RANGE: - if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == 1) { + if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) { goto delete_pos_len; } else { @@ -194,7 +194,7 @@ 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_ANY()); + mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang, MRB_ARGS_ARG(1,1)); } void diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 853554bcc..cb76559c7 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -1,6 +1,23 @@ ## # Array(Ext) Test +def assert_permutation_combination(exp, receiver, meth, *args) + act = [] + ret = receiver.__send__(meth, *args) { |v| act << v } + assert "assert_#{meth}" do + assert_equal(exp, act.sort) + assert_same(receiver, ret) + end +end + +def assert_permutation(exp, receiver, *args) + assert_permutation_combination(exp, receiver, :permutation, *args) +end + +def assert_combination(exp, receiver, *args) + assert_permutation_combination(exp, receiver, :combination, *args) +end + assert("Array#assoc") do s1 = [ "colors", "red", "blue", "green" ] s2 = [ "letters", "a", "b", "c" ] @@ -76,6 +93,14 @@ assert("Array#union") do assert_equal [1, 2, 3, 4, 5], a.union(b,c) end +assert("Array#difference") do + a = [1, 2, 3, 1, 6, 7] + b = [1, 4, 6] + c = [1, 5, 7] + + assert_equal [2, 3], a.difference(b,c) +end + assert("Array#&") do a = [1, 2, 3, 1] b = [1, 4] @@ -86,6 +111,14 @@ assert("Array#&") do assert_equal [1, 2, 3, 1], a end +assert("Array#intersection") do + a = [1, 2, 3, 1, 8, 6, 7, 8] + b = [1, 4, 6, 8] + c = [1, 5, 7, 8] + + assert_equal [1, 8], a.intersection(b,c) +end + assert("Array#flatten") do assert_equal [1, 2, "3", {4=>5}, :'6'], [1, 2, "3", {4=>5}, :'6'].flatten assert_equal [1, 2, 3, 4, 5, 6], [1, 2, [3, 4, 5], 6].flatten @@ -268,9 +301,9 @@ assert("Array#bsearch") do end end -assert("Array#bsearch_index") do - # tested through Array#bsearch -end +# tested through Array#bsearch +#assert("Array#bsearch_index") do +#end assert("Array#delete_if") do a = [1, 2, 3, 4, 5] @@ -369,30 +402,24 @@ end assert("Array#permutation") do a = [1, 2, 3] - assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], - a.permutation.to_a) - assert_equal([[1],[2],[3]], - a.permutation(1).to_a) - assert_equal([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], - a.permutation(2).to_a) - assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], - a.permutation(3).to_a) - assert_equal([[]], a.permutation(0).to_a) - assert_equal([], a.permutation(4).to_a) + assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a) + assert_permutation([[1],[2],[3]], a, 1) + assert_permutation([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], a, 2) + assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a, 3) + assert_permutation([[]], a, 0) + assert_permutation([], a, 4) + assert_permutation([], a, -1) end assert("Array#combination") do a = [1, 2, 3, 4] - assert_equal([[1],[2],[3],[4]], - a.combination(1).to_a) - assert_equal([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], - a.combination(2).to_a) - assert_equal([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], - a.combination(3).to_a) - assert_equal([[1,2,3,4]], - a.combination(4).to_a) - assert_equal([[]], a.combination(0).to_a) - assert_equal([], a.combination(5).to_a) + assert_combination([[1],[2],[3],[4]], a, 1) + assert_combination([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], a, 2) + assert_combination([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], a, 3) + assert_combination([[1,2,3,4]], a, 4) + assert_combination([[]], a, 0) + assert_combination([], a, 5) + assert_combination([], a, -1) end assert('Array#transpose') do |
