ruby-changes:25786
From: shirosaki <ko1@a...>
Date: Sun, 25 Nov 2012 22:39:38 +0900 (JST)
Subject: [ruby-changes:25786] shirosaki:r37843 (trunk): array.c: refactoring of rb_ary_delete_same()
shirosaki 2012-11-25 22:39:26 +0900 (Sun, 25 Nov 2012) New Revision: 37843 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37843 Log: 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. Modified files: trunk/ChangeLog trunk/array.c trunk/gc.c trunk/internal.h Index: array.c =================================================================== --- array.c (revision 37842) +++ array.c (revision 37843) @@ -2720,6 +2720,19 @@ 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 @@ 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 @@ 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 Index: ChangeLog =================================================================== --- ChangeLog (revision 37842) +++ ChangeLog (revision 37843) @@ -1,3 +1,17 @@ +Sun Nov 25 22:34:00 2012 Hiroshi Shirosaki <h.shirosaki@g...> + + * 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. + Sun Nov 25 22:27:33 2012 Benoit Daloze <eregontp@g...> * array.c: fixes for the updated documentation in r35858: Index: gc.c =================================================================== --- gc.c (revision 37842) +++ gc.c (revision 37843) @@ -3752,7 +3752,7 @@ VALUE wmap, ary; if (!existing) return ST_STOP; wmap = (VALUE)arg, ary = (VALUE)*value; - rb_ary_delete_same_obj(ary, wmap); + rb_ary_delete_same(ary, wmap); if (!RARRAY_LEN(ary)) return ST_DELETE; return ST_CONTINUE; } Index: internal.h =================================================================== --- internal.h (revision 37842) +++ internal.h (revision 37843) @@ -48,7 +48,7 @@ VALUE rb_ary_last(int, VALUE *, VALUE); void rb_ary_set_len(VALUE, long); VALUE rb_ary_cat(VALUE, const VALUE *, long); -VALUE rb_ary_delete_same_obj(VALUE, VALUE); +void rb_ary_delete_same(VALUE, VALUE); /* bignum.c */ VALUE rb_big_fdiv(VALUE x, VALUE y); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/