mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* array.c (rb_ary_uniq_bang): the array is already unique if the
length is zero or one. (rb_ary_uniq): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26987 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c9dd4823d9
commit
9ce419a45c
3 changed files with 53 additions and 0 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Sat Mar 20 13:26:09 2010 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* array.c (rb_ary_uniq_bang): the array is already unique if the
|
||||||
|
length is zero or one.
|
||||||
|
(rb_ary_uniq): ditto.
|
||||||
|
|
||||||
Sat Mar 20 12:30:54 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Mar 20 12:30:54 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* lib: fixed typo. a patch by Sho Hashimoto in [ruby-dev:40716].
|
* lib: fixed typo. a patch by Sho Hashimoto in [ruby-dev:40716].
|
||||||
|
|
4
array.c
4
array.c
|
@ -3367,6 +3367,8 @@ rb_ary_uniq_bang(VALUE ary)
|
||||||
long i, j;
|
long i, j;
|
||||||
|
|
||||||
rb_ary_modify_check(ary);
|
rb_ary_modify_check(ary);
|
||||||
|
if (RARRAY_LEN(ary) <= 1)
|
||||||
|
return Qnil;
|
||||||
if (rb_block_given_p()) {
|
if (rb_block_given_p()) {
|
||||||
hash = ary_make_hash_by(ary);
|
hash = ary_make_hash_by(ary);
|
||||||
if (RARRAY_LEN(ary) == (i = RHASH_SIZE(hash))) {
|
if (RARRAY_LEN(ary) == (i = RHASH_SIZE(hash))) {
|
||||||
|
@ -3412,6 +3414,8 @@ rb_ary_uniq(VALUE ary)
|
||||||
VALUE hash, uniq, v;
|
VALUE hash, uniq, v;
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
|
if (RARRAY_LEN(ary) <= 1)
|
||||||
|
return rb_ary_dup(ary);
|
||||||
if (rb_block_given_p()) {
|
if (rb_block_given_p()) {
|
||||||
hash = ary_make_hash_by(ary);
|
hash = ary_make_hash_by(ary);
|
||||||
uniq = ary_new(rb_obj_class(ary), RHASH_SIZE(hash));
|
uniq = ary_new(rb_obj_class(ary), RHASH_SIZE(hash));
|
||||||
|
|
|
@ -1308,6 +1308,30 @@ class TestArray < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_uniq
|
def test_uniq
|
||||||
|
a = []
|
||||||
|
b = a.uniq
|
||||||
|
assert_equal([], a)
|
||||||
|
assert_equal([], b)
|
||||||
|
assert_not_same(a, b)
|
||||||
|
|
||||||
|
a = [1]
|
||||||
|
b = a.uniq
|
||||||
|
assert_equal([1], a)
|
||||||
|
assert_equal([1], b)
|
||||||
|
assert_not_same(a, b)
|
||||||
|
|
||||||
|
a = [1,1]
|
||||||
|
b = a.uniq
|
||||||
|
assert_equal([1,1], a)
|
||||||
|
assert_equal([1], b)
|
||||||
|
assert_not_same(a, b)
|
||||||
|
|
||||||
|
a = [1,2]
|
||||||
|
b = a.uniq
|
||||||
|
assert_equal([1,2], a)
|
||||||
|
assert_equal([1,2], b)
|
||||||
|
assert_not_same(a, b)
|
||||||
|
|
||||||
a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
|
a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
|
||||||
b = a.dup
|
b = a.dup
|
||||||
assert_equal(@cls[1, 2, 3, 4, nil], a.uniq)
|
assert_equal(@cls[1, 2, 3, 4, nil], a.uniq)
|
||||||
|
@ -1322,6 +1346,25 @@ class TestArray < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_uniq!
|
def test_uniq!
|
||||||
|
a = []
|
||||||
|
b = a.uniq!
|
||||||
|
assert_equal(nil, b)
|
||||||
|
|
||||||
|
a = [1]
|
||||||
|
b = a.uniq!
|
||||||
|
assert_equal(nil, b)
|
||||||
|
|
||||||
|
a = [1,1]
|
||||||
|
b = a.uniq!
|
||||||
|
assert_equal([1], a)
|
||||||
|
assert_equal([1], b)
|
||||||
|
assert_same(a, b)
|
||||||
|
|
||||||
|
a = [1,2]
|
||||||
|
b = a.uniq!
|
||||||
|
assert_equal([1,2], a)
|
||||||
|
assert_equal(nil, b)
|
||||||
|
|
||||||
a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
|
a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
|
||||||
assert_equal(@cls[1, 2, 3, 4, nil], a.uniq!)
|
assert_equal(@cls[1, 2, 3, 4, nil], a.uniq!)
|
||||||
assert_equal(@cls[1, 2, 3, 4, nil], a)
|
assert_equal(@cls[1, 2, 3, 4, nil], a)
|
||||||
|
|
Loading…
Add table
Reference in a new issue