Permutations: Difference between revisions

(→‎{{header|Wren}}: Added a library based version.)
Line 6,172:
===Single yield iterator===
<lang nim>
iterator quickperminplacePermutations[T](xs: var seq[intT]): var seq[intT] =
assert nxs.len <= 2324, "permutation of array longer than 2324 is not supported"
let n = xs.len
assert n <= 23, "permutation of array longer than 23 is not supported"
let n = xs.len - 1
var
i: int = 1
# 21! > 2^64, practically enough
# use int8 for better utilization of registers
# as iterator is supposed to be mixed with other inline codes
# ~15% slower than int, but still faster than c = newSeq[int8](n+1)
c: array[24, int8]
i: int = 10
 
for i in 0 .. n: c[i] = int8(i+1)
 
for i in 0 .. n: c[i] = int8(i)
while true:
yield xs
Line 6,190 ⟶ 6,187:
 
c[i] -= 1
let j = if (i and 1) == 01: 0 else: int(c[i])
swap(xs[i+1], xs[j])
 
i = 10
while c[i] == 0:
c[i]let t = int8(i)+1
c[i] += 1 int8(t)
i = t
</lang>
verification