cartesianProductN<T> static method
Returns the Cartesian product of n sets.
Notation: A₁ × A₂ × ... × Aₙ = {(a₁, a₂, ..., aₙ) | aᵢ ∈ Aᵢ}
Returns all possible n-tuples where each element comes from the corresponding set in the list.
If any set is empty or the list is empty, returns an empty set.
Implementation
static CustomSet<List<T>> cartesianProductN<T>(List<CustomSet<T>> sets) {
if (sets.isEmpty) return CustomSet.empty();
if (sets.any((s) => s.isEmpty)) return CustomSet.empty();
List<List<T>> result = [[]];
for (var set in sets) {
final List<List<T>> newResult = [];
for (var prefix in result) {
for (var element in set.elements) {
newResult.add([...prefix, element]);
}
}
result = newResult;
}
return CustomSet(result);
}