Iptrie.prune

You're seeing just the function prune, go back to Iptrie module for more information.
Link to this function

prune(trie, fun, opts \\ [])

View Source

Specs

prune(t(), (tuple() -> nil | {:ok, any()}), Keyword.t()) :: t()

Prunes given trie by calling fun on neighboring prefixes, possibly replacing them with their parent.

The callback fun is invoked with either a 5- or a 6-tuple:

  • {p0, p1, v1, p2, v2} for neighboring p1, p2, their parent p0 is not in trie
  • {p0, v0, p1, v1, p2, v2} for the parent, its value and that of its two neighboring children.

The callback decides what happens by returning either:

  • {:ok, value}, value will be stored under p0 and the neighboring prefixes p1, p2 are deleted
  • nil (or anything else really), in which case the tree is not changed.

Examples

iex> adder = fn
...>   {_p0, _p1, v1, _p2, v2} -> {:ok, v1 + v2}
...>   {_p0, v0, _p1, v1, _p2, v2} -> {:ok, v0 + v1 + v2}
...> end
iex> ipt = new()
...> |> put("1.1.1.0/26", 0)
...> |> put("1.1.1.64/26", 1)
...> |> put("1.1.1.128/26", 2)
...> |> put("1.1.1.192/26", 3)
iex> prune(ipt, adder)
...> |> to_list()
...> |> Enum.map(fn {p, v} -> {"#{p}", v} end)
[
  {"1.1.1.0/25", 1},
  {"1.1.1.128/25", 5}
]
iex>
iex> prune(ipt, adder, recurse: true)
...> |> to_list()
...> |> Enum.map(fn {p, v} -> {"#{p}", v} end)
[{"1.1.1.0/24", 6}]

# summerize all /24's inside 10.10.0.0/16
# -> only 10.10.40.0/24 is missing
iex> slash24s = Pfx.partition("10.10.0.0/16", 24)
...> |> Enum.with_index()
...> |> new()
...> |> delete("10.10.40.0/24")
iex>
iex> prune(slash24s, fn _ -> {:ok, 0} end, recurse: true)
...> |> to_list()
...> |> Enum.map(fn {p, v} -> {"#{p}", v} end)
[
  {"10.10.0.0/19", 0},
  {"10.10.32.0/21", 0},
  {"10.10.41.0/24", 41},
  {"10.10.42.0/23", 0},
  {"10.10.44.0/22", 0},
  {"10.10.48.0/20", 0},
  {"10.10.64.0/18", 0},
  {"10.10.128.0/17", 0}
]