Radix.minimize
You're seeing just the function
minimize
, go back to Radix module for more information.
Specs
Minimizes a radix tree by removing more specific keys and pruning adjacent keys, where allowed.
The radix tree is traversed and a new tree is built. Whenever two keys can
be combined fun/2
is called with the values stored under the keys to be
combined.. If that returns {:ok, value}
that value will be stored on
the least specific key. That means more specific keys are effectively
deleted from the tree and any remaining neighbouring keys are combined to
their parent key and the more specific neighbours are deleted.
Examples
Combine neighbouring keys when they store the same value.
iex> f = fn v1, v2 -> if v1 == v2, do: {:ok, v1} end
iex> [{<<1, 0>>, 0}, {<<1, 1>>, 0}]
...> |> new()
...> |> minimize(f)
...> |> to_list()
[{<<1, 0::size(7)>>, 0}]
Remove more specific keys if they have the same value as some less specific key.
iex> f = fn v1, v2 -> if v1 == v2, do: {:ok, v1} end
iex> [{<<1>>, 0}, {<<1, 0>>, 0}, {<<1, 0, 1>>, 0}]
...> |> new()
...> |> minimize(f)
...> |> to_list()
[{<<1>>, 0}]
Summarize statistics keeping the least specific key.
iex> f = fn v1, v2 -> {:ok, v1 + v2} end
iex> [{<<1>>, 12}, {<<1, 0::size(7)>>, 10}, {<<1, 0>>, 10}, {<<1, 1>>, 10}]
...> |> new()
...> |> minimize(f)
...> |> to_list()
[{<<1>>, 42}]