* array.c (rb_ary_sample): use RARRAY_AREF() and RARRAY_PTR_USE()

instead of RARRAY_PTR().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43550 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
glass 2013-11-06 02:50:28 +00:00
parent e90e8b2ef6
commit da10c47e49
2 changed files with 33 additions and 37 deletions

View File

@ -1,3 +1,8 @@
Wed Nov 6 11:46:36 2013 Masaki Matsushita <glass.saga@gmail.com>
* array.c (rb_ary_sample): use RARRAY_AREF() and RARRAY_PTR_USE()
instead of RARRAY_PTR().
Wed Nov 6 10:37:07 2013 Masaki Matsushita <glass.saga@gmail.com> Wed Nov 6 10:37:07 2013 Masaki Matsushita <glass.saga@gmail.com>
* array.c (rb_ary_and): defer hash creation and some refactoring. * array.c (rb_ary_and): defer hash creation and some refactoring.

65
array.c
View File

@ -4516,7 +4516,7 @@ rb_ary_shuffle(int argc, VALUE *argv, 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, *ptr; VALUE nv, result;
VALUE opts, randgen = rb_cRandom; VALUE opts, randgen = rb_cRandom;
long n, len, i, j, k, idx[10]; long n, len, i, j, k, idx[10];
long rnds[numberof(idx)]; long rnds[numberof(idx)];
@ -4524,19 +4524,14 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
if (OPTHASH_GIVEN_P(opts)) { if (OPTHASH_GIVEN_P(opts)) {
randgen = rb_hash_lookup2(opts, sym_random, randgen); randgen = rb_hash_lookup2(opts, sym_random, randgen);
} }
ptr = RARRAY_PTR(ary);
len = RARRAY_LEN(ary); len = RARRAY_LEN(ary);
if (argc == 0) { if (argc == 0) {
if (len == 0) return Qnil; if (len < 2)
if (len == 1) {
i = 0; i = 0;
} else
else {
i = RAND_UPTO(len); i = RAND_UPTO(len);
if ((len = RARRAY_LEN(ary)) <= i) return Qnil;
ptr = RARRAY_PTR(ary); return rb_ary_elt(ary, i);
}
return ptr[i];
} }
rb_scan_args(argc, argv, "1", &nv); rb_scan_args(argc, argv, "1", &nv);
n = NUM2LONG(nv); n = NUM2LONG(nv);
@ -4549,28 +4544,23 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
} }
k = len; k = len;
len = RARRAY_LEN(ary); len = RARRAY_LEN(ary);
ptr = RARRAY_PTR(ary); if (len < k && n <= numberof(idx)) {
if (len < k) { for (i = 0; i < n; ++i) {
if (n <= numberof(idx)) { if (rnds[i] >= len) return rb_ary_new_capa(0);
for (i = 0; i < n; ++i) {
if (rnds[i] >= len) {
return rb_ary_new2(0);
}
}
} }
} }
if (n > len) n = len; if (n > len) n = len;
switch (n) { switch (n) {
case 0: case 0:
return rb_ary_new2(0); return rb_ary_new_capa(0);
case 1: case 1:
i = rnds[0]; i = rnds[0];
return rb_ary_new4(1, &ptr[i]); return rb_ary_new_from_values(1, &RARRAY_AREF(ary, i));
case 2: case 2:
i = rnds[0]; i = rnds[0];
j = rnds[1]; j = rnds[1];
if (j >= i) j++; if (j >= i) j++;
return rb_ary_new3(2, ptr[i], ptr[j]); return rb_ary_new_from_args(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, j));
case 3: case 3:
i = rnds[0]; i = rnds[0];
j = rnds[1]; j = rnds[1];
@ -4580,10 +4570,9 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
if (j >= i) l = i, g = ++j; if (j >= i) l = i, g = ++j;
if (k >= l && (++k >= g)) ++k; if (k >= l && (++k >= g)) ++k;
} }
return rb_ary_new3(3, ptr[i], ptr[j], ptr[k]); return rb_ary_new_from_args(3, RARRAY_AREF(ary, i), RARRAY_AREF(ary, j), RARRAY_AREF(ary, k));
} }
if (n <= numberof(idx)) { if (n <= numberof(idx)) {
VALUE *ptr_result;
long sorted[numberof(idx)]; long sorted[numberof(idx)];
sorted[0] = idx[0] = rnds[0]; sorted[0] = idx[0] = rnds[0];
for (i=1; i<n; i++) { for (i=1; i<n; i++) {
@ -4595,24 +4584,26 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
memmove(&sorted[j+1], &sorted[j], sizeof(sorted[0])*(i-j)); memmove(&sorted[j+1], &sorted[j], sizeof(sorted[0])*(i-j));
sorted[j] = idx[i] = k; sorted[j] = idx[i] = k;
} }
result = rb_ary_new2(n); result = rb_ary_new_capa(n);
ptr_result = RARRAY_PTR(result); RARRAY_PTR_USE(result, ptr_result, {
for (i=0; i<n; i++) { for (i=0; i<n; i++) {
ptr_result[i] = ptr[idx[i]]; ptr_result[i] = RARRAY_AREF(ary, idx[i]);
} }
});
} }
else { else {
VALUE *ptr_result; result = rb_ary_subseq(ary, 0, len);
result = rb_ary_new4(len, ptr); rb_ary_modify(result);
RBASIC_CLEAR_CLASS(result); RBASIC_CLEAR_CLASS(result);
ptr_result = RARRAY_PTR(result);
RB_GC_GUARD(ary); RB_GC_GUARD(ary);
for (i=0; i<n; i++) { RARRAY_PTR_USE(result, ptr_result, {
j = RAND_UPTO(len-i) + i; for (i=0; i<n; i++) {
nv = ptr_result[j]; j = RAND_UPTO(len-i) + i;
ptr_result[j] = ptr_result[i]; nv = ptr_result[j];
ptr_result[i] = nv; ptr_result[j] = ptr_result[i];
} ptr_result[i] = nv;
}
});
RBASIC_SET_CLASS_RAW(result, rb_cArray); RBASIC_SET_CLASS_RAW(result, rb_cArray);
} }
ARY_SET_LEN(result, n); ARY_SET_LEN(result, n);