permutation(p1 = v1)
public
When invoked with a block, yield all permutations of length n of the elements of
ary, then return the array itself. If n is not specified,
yield all permutations of all elements. The implementation makes no
guarantees about the order in which the permutations are yielded.
If no block is given, an enumerator is returned instead.
Examples:
a = [1, 2, 3]
a.permutation.to_a
a.permutation(1).to_a
a.permutation(2).to_a
a.permutation(3).to_a
a.permutation(0).to_a
a.permutation(4).to_a
Show source
static VALUE
rb_ary_permutation(int argc, VALUE *argv, VALUE ary)
{
VALUE num;
long r, n, i;
n = RARRAY_LEN(ary); /* Array length */
RETURN_ENUMERATOR(ary, argc, argv); /* Return enumerator if no block */
rb_scan_args(argc, argv, "01", &num);
r = NIL_P(num) ? n : NUM2LONG(num); /* Permutation size from argument */
if (r < 0 || n < r) {
/* no permutations: yield nothing */
}
else if (r == 0) { /* exactly one permutation: the zero-length array */
rb_yield(rb_ary_new2(0));
}
else if (r == 1) { /* this is a special, easy case */
for (i = 0; i < RARRAY_LEN(ary); i++) {
rb_yield(rb_ary_new3(1, RARRAY_PTR(ary)[i]));
}
}
else { /* this is the general case */
volatile VALUE t0 = tmpbuf(n,sizeof(long));
long *p = (long*)RSTRING_PTR(t0);
volatile VALUE t1 = tmpbuf(n,sizeof(char));
char *used = (char*)RSTRING_PTR(t1);
VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
RBASIC(ary0)->klass = 0;
MEMZERO(used, char, n); /* initialize array */
permute0(n, r, p, 0, used, ary0); /* compute and yield permutations */
tmpbuf_discard(t0);
tmpbuf_discard(t1);
RBASIC(ary0)->klass = rb_cArray;
}
return ary;
}