summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-29 13:14:19 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-29 13:14:19 +0000
commit806b93ed9b647956ac99f64d100674aa5cc2d6df (patch)
treebbbe5cf9235a85879a48bde718e72a6102ad5d21 /array.c
parentc5e08b764eb342538884b383f0e6428b6faf214b (diff)
* array.c (rb_ary_uniq_bang): use st_foreach() instead of for loop.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43467 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/array.c b/array.c
index 7e4d418..19878f0 100644
--- a/array.c
+++ b/array.c
@@ -4051,6 +4051,13 @@ rb_ary_or(VALUE ary1, VALUE ary2)
}
static int
+push_key(st_data_t key, st_data_t val, st_data_t ary)
+{
+ rb_ary_push((VALUE)ary, (VALUE)key);
+ return ST_CONTINUE;
+}
+
+static int
push_value(st_data_t key, st_data_t val, st_data_t ary)
{
rb_ary_push((VALUE)ary, (VALUE)val);
@@ -4085,15 +4092,16 @@ push_value(st_data_t key, st_data_t val, st_data_t ary)
static VALUE
rb_ary_uniq_bang(VALUE ary)
{
- VALUE hash, v;
- long i, j;
+ VALUE hash;
+ long hash_size;
rb_ary_modify_check(ary);
if (RARRAY_LEN(ary) <= 1)
return Qnil;
if (rb_block_given_p()) {
hash = ary_make_hash_by(ary);
- if (RARRAY_LEN(ary) == (i = RHASH_SIZE(hash))) {
+ hash_size = RHASH_SIZE(hash);
+ if (RARRAY_LEN(ary) == hash_size) {
return Qnil;
}
rb_ary_modify(ary);
@@ -4102,21 +4110,23 @@ rb_ary_uniq_bang(VALUE ary)
rb_ary_unshare(ary);
FL_SET_EMBED(ary);
}
- ary_resize_capa(ary, i);
+ ary_resize_capa(ary, hash_size);
st_foreach(rb_hash_tbl_raw(hash), push_value, ary);
}
else {
hash = ary_make_hash(ary);
- if (RARRAY_LEN(ary) == (long)RHASH_SIZE(hash)) {
+ hash_size = RHASH_SIZE(hash);
+ if (RARRAY_LEN(ary) == hash_size) {
return Qnil;
}
- for (i=j=0; i<RARRAY_LEN(ary); i++) {
- st_data_t vv = (st_data_t)(v = rb_ary_elt(ary, i));
- if (st_delete(rb_hash_tbl_raw(hash), &vv, 0)) {
- rb_ary_store(ary, j++, v);
- }
+ rb_ary_modify(ary);
+ ARY_SET_LEN(ary, 0);
+ if (ARY_SHARED_P(ary) && !ARY_EMBED_P(ary)) {
+ rb_ary_unshare(ary);
+ FL_SET_EMBED(ary);
}
- ARY_SET_LEN(ary, j);
+ ary_resize_capa(ary, hash_size);
+ st_foreach(rb_hash_tbl_raw(hash), push_key, ary);
}
ary_recycle_hash(hash);