[macruby-changes] [4998] MacRuby/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Wed Dec 8 16:43:57 PST 2010
Revision: 4998
http://trac.macosforge.org/projects/ruby/changeset/4998
Author: watson1978 at gmail.com
Date: 2010-12-08 16:43:53 -0800 (Wed, 08 Dec 2010)
Log Message:
-----------
Array#uniq will not remove the element of Array when object type is different.
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
}}}
Modified Paths:
--------------
MacRuby/trunk/array.c
MacRuby/trunk/spec/frozen/tags/macruby/core/array/uniq_tags.txt
Modified: MacRuby/trunk/array.c
===================================================================
--- MacRuby/trunk/array.c 2010-12-08 20:56:25 UTC (rev 4997)
+++ MacRuby/trunk/array.c 2010-12-09 00:43:53 UTC (rev 4998)
@@ -2734,25 +2734,30 @@
static VALUE
rary_uniq_bang(VALUE ary, SEL sel)
{
+ VALUE hash, v;
+ long i, j;
+
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;
-
- while (pos < n && (pos = rary_index_of_item(ary, pos, item))
- != NOT_FOUND) {
- rary_erase(ary, pos, 1);
- n--;
- changed = true;
- }
+ 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;
}
Modified: MacRuby/trunk/spec/frozen/tags/macruby/core/array/uniq_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/array/uniq_tags.txt 2010-12-08 20:56:25 UTC (rev 4997)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/array/uniq_tags.txt 2010-12-09 00:43:53 UTC (rev 4998)
@@ -1,2 +0,0 @@
-fails:Array#uniq uses eql? semantics
-fails:Array#uniq compares elements with matching hash codes with #eql?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101208/005d0f99/attachment-0001.html>
More information about the macruby-changes
mailing list