Internal helper that generates indices for powerset subsets using bitset iteration. Returns an iterator of index vectors, where each vector contains the indices of elements to include in that subset.
(len: usize)
| 70 | /// Returns an iterator of index vectors, where each vector contains the indices |
| 71 | /// of elements to include in that subset. |
| 72 | fn powerset_indices(len: usize) -> impl Iterator<Item = Vec<usize>> { |
| 73 | (0..(1 << len)).map(move |mask| { |
| 74 | let mut indices = vec![]; |
| 75 | let mut bitset = mask; |
| 76 | while bitset > 0 { |
| 77 | let rightmost: u64 = bitset & !(bitset - 1); |
| 78 | let idx = rightmost.trailing_zeros() as usize; |
| 79 | indices.push(idx); |
| 80 | bitset &= bitset - 1; |
| 81 | } |
| 82 | indices |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | /// The [power set] (or powerset) of a set S is the set of all subsets of S, \ |
| 87 | /// including the empty set and S itself. |