private class CartesianIterator[E]
super Iterator[SequenceRead[E]]
var collection: CartesianCollection[E]
# The array of iterations that will be increased in the lexicographic order.
var iterators = new Array[Iterator[E]]
init
do
for c in collection.collections do
var it = c.iterator
iterators.add it
if not it.is_ok then is_ok = false
end
end
redef var is_ok = true
redef fun item
do
var len = iterators.length
var res = new Array[E].with_capacity(len)
for i in [0..len[ do
var it = iterators[i]
res.add(it.item)
end
return res
end
redef fun next
do
var rank = iterators.length - 1
# Odometer-like increment starting from the last iterator
loop
var it = iterators[rank]
it.next
if it.is_ok then return
# The iterator if over
if rank == 0 then
# It it is the first, then the whole thing is over
is_ok = false
return
end
# If not, restart the iterator and increment the previous one
# (like a carry)
iterators[rank] = collection.collections[rank].iterator
rank -= 1
end
end
end
lib/combinations/combinations.nit:118,1--170,3