Iptrie.merge

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

Specs

merge(t(), t()) :: t()

Merges trie1 and trie2 into a new Iptrie.

Adds all prefix,value-pairs of trie2 to trie1, overwriting any existing entries when prefixes match (based on exact match).

Example

iex> t1 = new([{"1.1.1.0/24", 1}, {"2.2.2.0/24", 2}])
iex> t2 = new([{"2.2.2.0/24", 22}, {"3.3.3.0/24", 3}])
iex> t = merge(t1, t2)
iex> count(t)
3
iex> get(t, "1.1.1.0/24")
{"1.1.1.0/24", 1}
iex> get(t, "2.2.2.0/24")
{"2.2.2.0/24", 22}
iex> get(t, "3.3.3.0/24")
{"3.3.3.0/24", 3}
Link to this function

merge(trie1, trie2, fun)

View Source

Specs

merge(t(), t(), (prefix(), any(), any() -> any())) :: t()

Merges trie1 and trie2 into a new Iptrie, resolving conflicts through fun.

In cases where a prefix is present in both tries, the conflict is resolved by calling fun with the prefix (a Pfx.t/0), its value in trie1 and its value in trie2. The function's return value will be stored under the prefix in the merged trie.

Example

 iex> t1 = new([{"1.1.1.0/24", 1}, {"2.2.2.0/24", 2}, {"acdc:1975::/32", 3}])
 iex> t2 = new([{"3.3.3.0/24", 4}, {"2.2.2.0/24", 5}, {"acdc:2021::/32", 6}])
 iex> t = merge(t1, t2, fn _pfx, v1, v2 -> v1 + v2 end)
 iex> count(t)
 5
 iex> get(t, "2.2.2.0/24")
 {"2.2.2.0/24", 7}
 iex> for ip4 <- keys(t, 32), do: "#{ip4}"
 ["1.1.1.0/24", "2.2.2.0/24", "3.3.3.0/24"]
 iex> for ip6 <- keys(t, 128), do: "#{ip6}"
 ["acdc:1975:0:0:0:0:0:0/32", "acdc:2021:0:0:0:0:0:0/32"]
 iex> values(t) |> Enum.sum()
 1 + 7 + 3 + 4 + 6