cartesianProduct<T, U> static method
Returns the Cartesian product of two sets.
Notation: A × B = {(a, b) | a ∈ A and b ∈ B}
The Cartesian product contains all ordered pairs where the first element is from set A and the second is from set B.
If either set is empty, the result is an empty set. |A × B| = |A| × |B|
Example:
final c = CustomSet<int>([1, 2, 3]);
final d = CustomSet<String>(['a', 'b']);
final result = AdvancedSetOperations.cartesianProduct(c, d);
// result = {(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b'), (3, 'a'), (3, 'b')}
Implementation
static CustomSet<(T, U)> cartesianProduct<T, U>(
CustomSet<T> a,
CustomSet<U> b,
) {
if (a.isEmpty || b.isEmpty) return CustomSet.empty();
final List<(T, U)> pairs = [];
for (var elementA in a.elements) {
for (var elementB in b.elements) {
pairs.add((elementA, elementB));
}
}
return CustomSet(pairs);
}