diff options
author | shirosaki <shirosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-25 13:39:26 +0000 |
---|---|---|
committer | shirosaki <shirosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-25 13:39:26 +0000 |
commit | 56132b13465c2c766fc15b8c04a7fb116315cd45 (patch) | |
tree | 97315efbe9604955a2a47c9ef53316c2a4854228 /array.c | |
parent | 0012b0abd775c2b9f2f79e21e8668528b0af6091 (diff) | |
download | ruby-56132b13465c2c766fc15b8c04a7fb116315cd45.tar.gz |
array.c: refactoring of rb_ary_delete_same()
* array.c (ary_resize_smaller): new function to resize array.
* array.c (rb_ary_delete): refactoring to extract a function.
* array.c (rb_ary_delete_same): refactoring.
It renames function, reduces duplicated code and removes unused
code.
* gc.c (wmap_final_func): follow the above change.
* internal.h (rb_ary_delete_same): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37843 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 41 |
1 files changed, 18 insertions, 23 deletions
@@ -2720,6 +2720,19 @@ rb_ary_keep_if(VALUE ary) return ary; } +static void +ary_resize_smaller(VALUE ary, long len) +{ + rb_ary_modify(ary); + if (RARRAY_LEN(ary) > len) { + ARY_SET_LEN(ary, len); + if (len * 2 < ARY_CAPA(ary) && + ARY_CAPA(ary) > ARY_DEFAULT_SIZE) { + ary_resize_capa(ary, len * 2); + } + } +} + /* * call-seq: * ary.delete(obj) -> obj or nil @@ -2765,29 +2778,20 @@ rb_ary_delete(VALUE ary, VALUE item) return Qnil; } - rb_ary_modify(ary); - if (RARRAY_LEN(ary) > i2) { - ARY_SET_LEN(ary, i2); - if (i2 * 2 < ARY_CAPA(ary) && - ARY_CAPA(ary) > ARY_DEFAULT_SIZE) { - ary_resize_capa(ary, i2*2); - } - } + ary_resize_smaller(ary, i2); return v; } -VALUE -rb_ary_delete_same_obj(VALUE ary, VALUE item) +void +rb_ary_delete_same(VALUE ary, VALUE item) { - VALUE v = item; long i1, i2; for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) { VALUE e = RARRAY_PTR(ary)[i1]; if (e == item) { - v = e; continue; } if (i1 != i2) { @@ -2796,19 +2800,10 @@ rb_ary_delete_same_obj(VALUE ary, VALUE item) i2++; } if (RARRAY_LEN(ary) == i2) { - return Qnil; + return; } - rb_ary_modify(ary); - if (RARRAY_LEN(ary) > i2) { - ARY_SET_LEN(ary, i2); - if (i2 * 2 < ARY_CAPA(ary) && - ARY_CAPA(ary) > ARY_DEFAULT_SIZE) { - ary_resize_capa(ary, i2*2); - } - } - - return v; + ary_resize_smaller(ary, i2); } VALUE |