Project

General

Profile

Bug #13136 » rb_ary_sample.patch

tompng (tomoya ishida), 01/18/2017 01:20 PM

View differences:

array.c
VALUE opts, randgen = rb_cRandom;
long n, len, i, j, k, idx[10];
long rnds[numberof(idx)];
long memo_threshold;
if (OPTHASH_GIVEN_P(opts)) {
VALUE rnd;
......
}
return rb_ary_new_from_args(3, RARRAY_AREF(ary, i), RARRAY_AREF(ary, j), RARRAY_AREF(ary, k));
}
memo_threshold = len < 2560 ? len / 128 : len < 5120 ? len / 64 : len < 10240 ? len / 32 : len / 16;
if (n <= numberof(idx)) {
long sorted[numberof(idx)];
sorted[0] = idx[0] = rnds[0];
......
}
});
}
else if (n <= memo_threshold) {
st_table *table = st_init_numtable_with_size(n);
long i2, j2;
st_data_t value;
result = rb_ary_new_capa(n);
RARRAY_PTR_USE(ary, ptr_ary, {
RARRAY_PTR_USE(result, ptr_result, {
for (i=0; i<n; i++){
j2 = j = RAND_UPTO(len-i) + i;
i2 = i;
if (st_lookup(table, (st_data_t)i, &value)) i2 = value;
if (st_lookup(table, (st_data_t)j, &value)) j2 = value;
st_insert(table, (st_data_t)j, (st_data_t)i2);
ptr_result[i] = ptr_ary[j2];
}
});
});
st_free_table(table);
}
else {
result = rb_ary_dup(ary);
RBASIC_CLEAR_CLASS(result);
(1-1/2)