Skip to content

Commit

Permalink
Array#uniq will not remove the element of Array when object type is d…
Browse files Browse the repository at this point in the history
…ifferent.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

a1 = [ 1, 1.0, "1.0", 2, 1.0, 1]
a2 = a1.dup
assert_equal([1, 1.0, "1.0", 2], a1.uniq)
assert_equal([1, 1.0, "1.0", 2], a2.uniq!)

puts :ok
}}}

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@4998 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
Watson1978 committed Dec 9, 2010
1 parent 295d267 commit c22ea5c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 20 additions & 15 deletions array.c
Expand Up @@ -2734,25 +2734,30 @@ rary_or(VALUE ary1, SEL sel, VALUE ary2)
static VALUE
rary_uniq_bang(VALUE ary, SEL sel)
{
rary_modify(ary);
long n = RARY(ary)->len;
bool changed = false;

for (size_t i = 0; i < n; i++) {
VALUE item = rary_elt(ary, i);
size_t pos = i + 1;
VALUE hash, v;
long i, j;

while (pos < n && (pos = rary_index_of_item(ary, pos, item))
!= NOT_FOUND) {
rary_erase(ary, pos, 1);
n--;
changed = true;
}
rary_modify(ary);
if (RARRAY_LEN(ary) <= 1) {
return Qnil;
}

if (!changed) {
if (rb_block_given_p()) {
// TODO
return Qnil;
}
else {
hash = ary_make_hash(rb_ary_new(), ary);
if (RARRAY_LEN(ary) == (long)RHASH_SIZE(hash)) {
return Qnil;
}
for (i=j=0; i<RARRAY_LEN(ary); i++) {
st_data_t vv = (st_data_t)(v = rary_elt(ary, i));
if (st_delete(RHASH_TBL(hash), &vv, 0)) {
rary_store(ary, j++, v);
}
}
rary_resize(ary, j);
}
return ary;
}

Expand Down
2 changes: 0 additions & 2 deletions spec/frozen/tags/macruby/core/array/uniq_tags.txt
@@ -1,2 +0,0 @@
fails:Array#uniq uses eql? semantics
fails:Array#uniq compares elements with matching hash codes with #eql?

0 comments on commit c22ea5c

Please sign in to comment.