Iptrie.reduce

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

Specs

reduce(t(), any(), (bitstring(), any(), any() -> any())) :: any()

Invoke fun on all prefix,value-pairs in all radix trees in the trie.

Example

iex> ipt = new()
...> |> put("1.1.1.0/24", 1)
...> |> put("2.2.2.0/24", 2)
...> |> put("acdc:1975::/32", 3)
...> |> put("acdc:2021::/32", 4)
iex>
iex> reduce(ipt, 0, fn _key, value, acc -> acc + value end)
10
iex>
iex> reduce(ipt, %{}, fn key, value, acc -> Map.put(acc, key, value) end)
%{<<1, 1, 1>> => 1, <<2, 2, 2>> => 2, <<172, 220, 25, 117>> => 3, <<172, 220, 32, 33>> => 4}
Link to this function

reduce(trie, type, acc, fun)

View Source

Specs

reduce(
  t(),
  non_neg_integer() | [non_neg_integer()],
  any(),
  (bitstring(), any(), any() -> any())
) :: any()

Invoke fun on each prefix,value-pair in the radix tree for given type

This simply wraps Radix.reduce/3 for the radix tree in trie at given type. The function fun is called with the radix key, value and acc accumulator and should return an updated accumulator. The result is the last acc accumulator returned.

If type is a list of type's, the acc accumulator is updated across all radix trees of type's. Probably not entirely usefull, but there you go.

Example

iex> ipt = new()
...> |> put("1.1.1.0/24", 1)
...> |> put("2.2.2.0/24", 2)
...> |> put("acdc:1975::/32", 3)
...> |> put("acdc:2021::/32", 4)
iex>
iex> reduce(ipt, 32, 0, fn _key, value, acc -> acc + value end)
3
iex> reduce(ipt, 48, 0, fn _key, value, acc -> acc + value end)
0
iex> reduce(ipt, 128, 0, fn _key, value, acc -> acc + value end)
7
iex>
iex> reduce(ipt, [32, 48, 128], 0, fn _key, value, acc -> acc + value end)
10