ruby-changes:8393
From: nobu <ko1@a...>
Date: Sat, 25 Oct 2008 00:15:03 +0900 (JST)
Subject: [ruby-changes:8393] Ruby:r19924 (trunk): * array.c (rb_ary_sample): fixed sizes and randomness.
nobu 2008-10-25 00:14:44 +0900 (Sat, 25 Oct 2008) New Revision: 19924 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19924 Log: * array.c (rb_ary_sample): fixed sizes and randomness. Modified files: trunk/ChangeLog trunk/array.c trunk/version.h Index: array.c =================================================================== --- array.c (revision 19923) +++ array.c (revision 19924) @@ -3275,33 +3275,30 @@ case 2: i = rb_genrand_real()*len; j = rb_genrand_real()*(len-1); - if (j == i) j++; + if (j >= i) j++; return rb_ary_new3(2, ptr[i], ptr[j]); case 3: i = rb_genrand_real()*len; j = rb_genrand_real()*(len-1); k = rb_genrand_real()*(len-2); - if (j == i) j++; - if ((k == i) ? (++k == j) : (k == j) ? (++k == i): 0) ++k; + { + long l = j, g = i; + if (j >= i) l = i, g = ++j; + if (k >= l && (++k >= g)) ++k; + } return rb_ary_new3(3, ptr[i], ptr[j], ptr[k]); } if (n < sizeof(idx)/sizeof(idx[0])) { - idx[0] = rb_genrand_real()*len; + long sorted[sizeof(idx)/sizeof(idx[0])]; + sorted[0] = idx[0] = rb_genrand_real()*len; for (i=1; i<n; i++) { - long p = i; k = rb_genrand_real()*--len; - retry: - j = 0; - do { - if (idx[j] == k) { - ++k; - if (p < j) goto retry; - } - else if (idx[j] > k) { - if (p > j) p = j; - } - } while (++j < i); - idx[i] = k; + for (j = 0; j < i; ++j) { + if (k < sorted[j]) break; + ++k; + } + memmove(&sorted[j+1], &sorted[j], sizeof(sorted[0])*(i-j)); + sorted[j] = idx[i] = k; } result = rb_ary_new2(n); for (i=0; i<n; i++) { @@ -3318,6 +3315,7 @@ RARRAY_PTR(result)[i] = nv; } } + ARY_SET_LEN(result, n); return result; } Index: ChangeLog =================================================================== --- ChangeLog (revision 19923) +++ ChangeLog (revision 19924) @@ -1,3 +1,7 @@ +Sat Oct 25 00:14:41 2008 Nobuyoshi Nakada <nobu@r...> + + * array.c (rb_ary_sample): fixed sizes and randomness. + Fri Oct 24 23:04:42 2008 Yuki Sonoda (Yugui) <yugui@y...> * configure.in (sitedir): considers --program-prefix and Index: version.h =================================================================== --- version.h (revision 19923) +++ version.h (revision 19924) @@ -1,7 +1,7 @@ #define RUBY_VERSION "1.9.0" -#define RUBY_RELEASE_DATE "2008-10-24" +#define RUBY_RELEASE_DATE "2008-10-25" #define RUBY_VERSION_CODE 190 -#define RUBY_RELEASE_CODE 20081024 +#define RUBY_RELEASE_CODE 20081025 #define RUBY_PATCHLEVEL 0 #define RUBY_VERSION_MAJOR 1 @@ -9,7 +9,7 @@ #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_YEAR 2008 #define RUBY_RELEASE_MONTH 10 -#define RUBY_RELEASE_DAY 24 +#define RUBY_RELEASE_DAY 25 #ifdef RUBY_EXTERN RUBY_EXTERN const char ruby_version[]; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/