mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* array.c (rb_ary_sample): performance improvement for huge array.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18719 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6523eee836
commit
ee5c9fb983
2 changed files with 58 additions and 18 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Wed Aug 20 16:39:59 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* array.c (rb_ary_sample): performance improvement for huge array.
|
||||||
|
|
||||||
Wed Aug 20 12:28:31 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Wed Aug 20 12:28:31 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* array.c (rb_ary_sample): get rid of infinite loop. #455
|
* array.c (rb_ary_sample): get rid of infinite loop. #455
|
||||||
|
|
72
array.c
72
array.c
|
@ -3022,35 +3022,71 @@ rb_ary_shuffle(VALUE ary)
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
rb_ary_sample(int argc, VALUE *argv, VALUE ary)
|
||||||
{
|
{
|
||||||
VALUE nv, result;
|
VALUE nv, result, *ptr;
|
||||||
int n, len, i, j;
|
long n, len, i, j, k, idx[10];
|
||||||
|
|
||||||
len = RARRAY_LEN(ary);
|
len = RARRAY_LEN(ary);
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
if (len == 0) return Qnil;
|
if (len == 0) return Qnil;
|
||||||
i = rb_genrand_real()*len;
|
i = len == 1 ? 0 : rb_genrand_real()*len;
|
||||||
return RARRAY_PTR(ary)[i];
|
return RARRAY_PTR(ary)[i];
|
||||||
}
|
}
|
||||||
rb_scan_args(argc, argv, "1", &nv);
|
rb_scan_args(argc, argv, "1", &nv);
|
||||||
n = NUM2INT(nv);
|
n = NUM2LONG(nv);
|
||||||
|
ptr = RARRAY_PTR(ary);
|
||||||
|
len = RARRAY_LEN(ary);
|
||||||
if (n > len) n = len;
|
if (n > len) n = len;
|
||||||
if (n == 0) return rb_ary_new2(0);
|
switch (n) {
|
||||||
result = rb_ary_new2(n);
|
case 0: return rb_ary_new2(0);
|
||||||
for (i=0; i<n; i++) {
|
case 1:
|
||||||
retry:
|
return rb_ary_new4(1, &ptr[(long)(rb_genrand_real()*len)]);
|
||||||
j = rb_genrand_real()*len;
|
case 2:
|
||||||
nv = LONG2NUM(j);
|
i = rb_genrand_real()*len;
|
||||||
for (j=0; j<i; j++) {
|
j = rb_genrand_real()*(len-1);
|
||||||
if (RARRAY_PTR(result)[j] == nv)
|
if (j == i) j++;
|
||||||
goto retry;
|
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;
|
||||||
|
return rb_ary_new3(3, ptr[i], ptr[j], ptr[k]);
|
||||||
|
}
|
||||||
|
if (n < sizeof(idx)/sizeof(idx[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;
|
||||||
|
}
|
||||||
|
result = rb_ary_new2(n);
|
||||||
|
for (i=0; i<n; i++) {
|
||||||
|
RARRAY_PTR(result)[i] = RARRAY_PTR(ary)[idx[i]];
|
||||||
}
|
}
|
||||||
RARRAY_PTR(result)[i] = nv;
|
|
||||||
ARY_SET_LEN(result, i+1);
|
|
||||||
}
|
}
|
||||||
for (i=0; i<n; i++) {
|
else {
|
||||||
nv = RARRAY_PTR(result)[i];
|
result = rb_ary_new4(len, ptr);
|
||||||
RARRAY_PTR(result)[i] = RARRAY_PTR(ary)[NUM2LONG(nv)];
|
RB_GC_GUARD(ary);
|
||||||
|
for (i=0; i<n; i++) {
|
||||||
|
j = (long)(rb_genrand_real()*(len-i)) + i;
|
||||||
|
nv = RARRAY_PTR(result)[j];
|
||||||
|
RARRAY_PTR(result)[j] = RARRAY_PTR(result)[i];
|
||||||
|
RARRAY_PTR(result)[i] = nv;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue