A Cartesian product over two collections is just a collection of pairs. Therefore, this view contains all the pairs of elements constructed by associating each element of the first collection to each element of the second collection.
However the view is memory-efficient and the pairs are created only when needed.
A simple Cartesian product
var c1 = [1,2]
var c2 = ["a","b","c"]
var c12 = new Cartesian[Int,String](c1, c2)
assert c12.length == 6
assert c12.join(";") == "(1,a);(1,b);(1,c);(2,a);(2,b);(2,c)"
Note: because it is a view, changes on the base collections are reflected on the view.
E.g. c12 is a view on c1 and c2, so if c1 changes, then c12 "changes".
assert c2.pop == "c"
assert c12.length == 4
assert c12.join(";") == "(1,a);(1,b);(2,a);(2,b)"
Cartesian objects are collections, so can be used to build another Cartesian object.
var c3 = [1000..2000[
var c123 = new Cartesian[Pair[Int,String],Int](c12, c3)
assert c123.length == 4000
All methods of Collection are inherited, it is so great!
E.g. search elements?
var p12 = new Pair[Int,String](2,"b")
assert c12.has(p12) == true
var p123 = new Pair[Pair[Int, String], Int](p12, 1500)
var p123bis = new Pair[Pair[Int, String], Int](p12, 0)
assert c123.has(p123) == true
assert c123.has(p123bis) == false
cartesian :: Cartesian :: defaultinit
core :: Collection :: CONCURRENT
Type of the concurrent variant of this collectioncore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
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.
cartesian :: Cartesian :: defaultinit
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 two collections.
#
# A Cartesian product over two collections is just a collection of pairs.
# Therefore, this view *contains* all the pairs of elements constructed by associating each
# element of the first collection to each element of the second collection.
#
# However the view is memory-efficient and the pairs are created only when needed.
#
# A simple Cartesian product
# ~~~~
# var c1 = [1,2]
# var c2 = ["a","b","c"]
# var c12 = new Cartesian[Int,String](c1, c2)
# assert c12.length == 6
# assert c12.join(";") == "(1,a);(1,b);(1,c);(2,a);(2,b);(2,c)" # All the 6 pairs
# ~~~~
#
# Note: because it is a view, changes on the base collections are reflected on the view.
#
# E.g. c12 is a view on c1 and c2, so if c1 changes, then c12 "changes".
# ~~~~
# assert c2.pop == "c"
# assert c12.length == 4
# assert c12.join(";") == "(1,a);(1,b);(2,a);(2,b)" # All the 4 remaining pairs
# ~~~~
#
# Cartesian objects are collections, so can be used to build another Cartesian object.
# ~~~~
# var c3 = [1000..2000[
# var c123 = new Cartesian[Pair[Int,String],Int](c12, c3)
# assert c123.length == 4000
# ~~~~
#
# All methods of Collection are inherited, it is so great!
#
# E.g. search elements?
# ~~~~
# var p12 = new Pair[Int,String](2,"b")
# assert c12.has(p12) == true
# var p123 = new Pair[Pair[Int, String], Int](p12, 1500)
# var p123bis = new Pair[Pair[Int, String], Int](p12, 0)
# assert c123.has(p123) == true
# assert c123.has(p123bis) == false
# ~~~~
class Cartesian[E, F]
super Collection[Pair[E,F]]
# The first collection
var ce: Collection[E]
# The second collection
var cf: Collection[F]
redef fun length do return ce.length * cf.length # optional, but so efficient...
redef fun iterator do return new CartesianIterator[E,F](self)
# Returns a new Cartesian where the first collection is the second.
# Because the full collection is virtual, the operation is cheap!
fun swap: Cartesian[F, E] do return new Cartesian[F, E](cf, ce)
end
lib/cartesian/cartesian.nit:63,1--123,3