Power set: Difference between revisions

Line 2,223:
using Base.Iterators
 
function bitmask(u, max_size)
function powerset(x::Vector{T})::Vector{Vector{T}} where T
res = BitArray(undef, max_size)
bitstring_map(x) = Iterators.map(bitstring, x)
res.chunks[1] = u%UInt64
reverse_map(x) = Iterators.map(reverse, x)
res
end
 
function powerset(xinput_collection::Vector{T})::Vector{Vector{T}} where T
bit_strings = range(start = 0, stop = 2^length(x)-1) |>
num_elements = length(input_collection)
bitstring_map |>
reverse_mapbitmask_map(x) = Iterators.map(reversey -> bitmask(y, num_elements), x)
reverse_map
bitstring_mapgetindex_map(x) = Iterators.map(bitstringy -> input_collection[y], x)
[[a for (a, b) in zip(x, bits) if b == '1'] for bits in bit_strings]
 
UnitRange(0, (2^num_elements)-1) |>
bitstring_mapbitmask_map |>
getindex_map |>
reverse_mapcollect
end
</syntaxhighlight>
7

edits