ruby-changes:10519
From: nobu <ko1@a...>
Date: Thu, 5 Feb 2009 12:24:37 +0900 (JST)
Subject: [ruby-changes:10519] Ruby:r22073 (trunk): * array.c (rb_ary_uniq): gets rid of copying.
nobu 2009-02-05 12:24:20 +0900 (Thu, 05 Feb 2009) New Revision: 22073 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22073 Log: * array.c (rb_ary_uniq): gets rid of copying. Modified files: trunk/ChangeLog trunk/array.c Index: array.c =================================================================== --- array.c (revision 22072) +++ array.c (revision 22073) @@ -3014,7 +3014,7 @@ static VALUE rb_ary_uniq_bang(VALUE ary) { - VALUE hash, v, vv; + VALUE hash, v; long i, j; hash = ary_make_hash(ary); @@ -3023,8 +3023,8 @@ return Qnil; } for (i=j=0; i<RARRAY_LEN(ary); i++) { - v = vv = rb_ary_elt(ary, i); - if (st_delete(RHASH_TBL(hash), (st_data_t*)&vv, 0)) { + st_data_t vv = (st_data_t)(v = rb_ary_elt(ary, i)); + if (st_delete(RHASH_TBL(hash), &vv, 0)) { rb_ary_store(ary, j++, v); } } @@ -3047,9 +3047,19 @@ static VALUE rb_ary_uniq(VALUE ary) { - ary = rb_ary_dup(ary); - rb_ary_uniq_bang(ary); - return ary; + VALUE hash = ary_make_hash(ary), v; + VALUE uniq = ary_new(rb_obj_class(ary), RHASH_SIZE(hash)); + long i; + + for (i=0; i<RARRAY_LEN(ary); i++) { + st_data_t vv = (st_data_t)(v = rb_ary_elt(ary, i)); + if (st_delete(RHASH_TBL(hash), &vv, 0)) { + rb_ary_push(uniq, v); + } + } + ary_recycle_hash(hash); + + return uniq; } /* Index: ChangeLog =================================================================== --- ChangeLog (revision 22072) +++ ChangeLog (revision 22073) @@ -1,3 +1,7 @@ +Thu Feb 5 12:24:18 2009 Nobuyoshi Nakada <nobu@r...> + + * array.c (rb_ary_uniq): gets rid of copying. + Thu Feb 5 12:01:53 2009 Nobuyoshi Nakada <nobu@r...> * array.c (ary_add_hash): split from ary_make_hash(). -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/