1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* array.c (rb_ary_sample): fixed sizes and randomness.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19924 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-10-24 15:14:44 +00:00
parent 5660e09f08
commit 4f577310ce
3 changed files with 22 additions and 20 deletions

View file

@ -1,3 +1,7 @@
Sat Oct 25 00:14:41 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (rb_ary_sample): fixed sizes and randomness.
Fri Oct 24 23:04:42 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp> Fri Oct 24 23:04:42 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
* configure.in (sitedir): considers --program-prefix and * configure.in (sitedir): considers --program-prefix and

32
array.c
View file

@ -3275,33 +3275,30 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
case 2: case 2:
i = rb_genrand_real()*len; i = rb_genrand_real()*len;
j = rb_genrand_real()*(len-1); j = rb_genrand_real()*(len-1);
if (j == i) j++; if (j >= i) j++;
return rb_ary_new3(2, ptr[i], ptr[j]); return rb_ary_new3(2, ptr[i], ptr[j]);
case 3: case 3:
i = rb_genrand_real()*len; i = rb_genrand_real()*len;
j = rb_genrand_real()*(len-1); j = rb_genrand_real()*(len-1);
k = rb_genrand_real()*(len-2); 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]); return rb_ary_new3(3, ptr[i], ptr[j], ptr[k]);
} }
if (n < sizeof(idx)/sizeof(idx[0])) { 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++) { for (i=1; i<n; i++) {
long p = i;
k = rb_genrand_real()*--len; k = rb_genrand_real()*--len;
retry: for (j = 0; j < i; ++j) {
j = 0; if (k < sorted[j]) break;
do { ++k;
if (idx[j] == k) { }
++k; memmove(&sorted[j+1], &sorted[j], sizeof(sorted[0])*(i-j));
if (p < j) goto retry; sorted[j] = idx[i] = k;
}
else if (idx[j] > k) {
if (p > j) p = j;
}
} while (++j < i);
idx[i] = k;
} }
result = rb_ary_new2(n); result = rb_ary_new2(n);
for (i=0; i<n; i++) { for (i=0; i<n; i++) {
@ -3318,6 +3315,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
RARRAY_PTR(result)[i] = nv; RARRAY_PTR(result)[i] = nv;
} }
} }
ARY_SET_LEN(result, n);
return result; return result;
} }

View file

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0" #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_VERSION_CODE 190
#define RUBY_RELEASE_CODE 20081024 #define RUBY_RELEASE_CODE 20081025
#define RUBY_PATCHLEVEL 0 #define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0 #define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2008 #define RUBY_RELEASE_YEAR 2008
#define RUBY_RELEASE_MONTH 10 #define RUBY_RELEASE_MONTH 10
#define RUBY_RELEASE_DAY 24 #define RUBY_RELEASE_DAY 25
#ifdef RUBY_EXTERN #ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[]; RUBY_EXTERN const char ruby_version[];