Therefore, this view generates all the sequences of elements constructed by associating en element for each one of the original collections.
It is equivalent to doing nesting for
for each collection.
var xs = [1, 2, 3]
var ys = [8, 9]
var xys = new CartesianCollection[Int]([xs, ys])
assert xys.length == 6
assert xys.to_a == [[1,8], [1,9], [2,8], [2,9], [3,8], [3,9]]
The pattern of the generate sequences produces a lexicographical order.
Because it is a generator, it is memory-efficient and the sequences are created only when needed.
Note: because it is a view, changes on the base collections are reflected on the view.
assert xs.pop == 3
assert ys.pop == 9
assert xys.to_a == [[1,8], [2,8]]
combinations :: CartesianCollection :: collections
The base collections used to generate the sequences.combinations :: CartesianCollection :: collections=
The base collections used to generate the sequences.combinations $ CartesianCollection :: SELF
Type of this instance, automatically specialized in every classcombinations $ CartesianCollection :: iterator
Get a new iterator on the collection.combinations $ CartesianCollection :: length
Number of items in the collection.core :: Collection :: CONCURRENT
Type of the concurrent variant of this collectioncore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
combinations :: CartesianCollection :: collections
The base collections used to generate the sequences.combinations :: CartesianCollection :: collections=
The base collections used to generate the sequences.core :: Collection :: combinations
Allr
-length combinations on self (in same order) without repeated elements.
core :: Collection :: combinations_with_replacement
Allr
-length combination on self (in same order) with repeated elements.
core :: Object :: defaultinit
core :: Collection :: defaultinit
core :: Collection :: has_all
Does the collection contain at least each element ofother
?
core :: Collection :: has_any
Does the collection contain at least one element ofother
?
core :: Collection :: has_exactly
Does the collection contain exactly all the elements ofother
?
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).core :: Collection :: permutations
Allr
-length permutations on self (all possible ordering) without repeated elements.
core :: Collection :: product
Cartesian product, overr
times self
.
core :: Collection :: to_concurrent
Wrapsself
in a thread-safe collection
core :: Collection :: to_counter
Create and fill up a counter with the elements of `self.core :: Collection :: to_curlslist
Convert Collection[String] to CURLSListcore :: Collection :: to_shuffle
Return a new array made of elements in a random order.
# A view of a Cartesian-product collection over homogeneous collections.
#
# Therefore, this view *generates* all the sequences of elements constructed by associating
# en element for each one of the original collections.
#
# It is equivalent to doing nesting `for` for each collection.
#
# ~~~~
# var xs = [1, 2, 3]
# var ys = [8, 9]
# var xys = new CartesianCollection[Int]([xs, ys])
# assert xys.length == 6
# assert xys.to_a == [[1,8], [1,9], [2,8], [2,9], [3,8], [3,9]]
# ~~~~
#
# The pattern of the generate sequences produces a lexicographical order.
#
# Because it is a generator, it is memory-efficient and the sequences are created only when needed.
#
# Note: because it is a view, changes on the base collections are reflected on the view.
#
# ~~~~
# assert xs.pop == 3
# assert ys.pop == 9
# assert xys.to_a == [[1,8], [2,8]]
# ~~~~
class CartesianCollection[E]
super Collection[SequenceRead[E]]
# The base collections used to generate the sequences.
var collections: SequenceRead[Collection[E]]
redef fun length
do
var res = 1
for c in collections do res = res * c.length
return res
end
redef fun iterator do return new CartesianIterator[E](self)
end
lib/combinations/combinations.nit:76,1--116,3