powerSet property

CustomSet<CustomSet<T>> get powerSet

Returns the power set of this set.

Notation: P(A) or 2^A

The power set contains all possible subsets of this set, including the empty set and the set itself. If |A| = n, then |P(A)| = 2^n.

Example:

final a = CustomSet<int>([1, 2]);
final powerSet = a.powerSet;
// P(A) = {∅, {1}, {2}, {1, 2}}
print(powerSet.cardinality); // Output: 4

Implementation

CustomSet<CustomSet<T>> get powerSet {
  final List<CustomSet<T>> subsets = [];
  final List<T> elementList = _elements.toList();
  final int total = 1 << elementList.length; // 2^n

  for (int i = 0; i < total; i++) {
    final List<T> subset = [];
    for (int j = 0; j < elementList.length; j++) {
      if ((i & (1 << j)) != 0) subset.add(elementList[j]);
    }
    subsets.add(CustomSet(subset));
  }

  return CustomSet<CustomSet<T>>(subsets);
}