DualMap.WCN (dual_map_ex v0.2.1)

A DualMap.WCN is like a DualMap but with column names. So you need to reference columns (called master keys) in most of the calls.

How does it work?

A DualMap.WCN, as a DualMap stores 2 maps, (a direct and an inverted one), but at the same time it also stores metadata about the column names of the datas (called master keys).

To create a new DualMap.WCN you must use the DualMap.WCN.new function. You must pass to it a pair of names that will be the names of the columns.

DualMap.WCN.new({:hostname, :ip})

The order of the master keys is important. If you later want to make insertions into the DualMap.WCN and you use the DualMap.WCN.put_ordered function the value pairs will assume that they are ordered as defined at the time of creating the DualMap.WCN with DualMap.WCN.new.

Let's see some examples:

iex> dm = DualMap.WCN.new({:hostname, :ip})
[]
iex> DualMap.WCN.put_ordered(dm, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]
iex> DualMap.WCN.delete(dm, :ip, "192.168.0.3")
[
  {"ns1", "192.168.0.2"},
  {"ns3", "192.168.0.4"}
]

Summary

Types

t()

DualMap.WCN struct

Functions

Return de size of the DualMap.WCN counting the number of pairs.

Delete one or more pairs of datas and returns the DualMap.WCN without that pairs. The pairs are found looking for key in the the internal map indexed by master_key.

Checks if two DualMap.WCNs are equal.

Just as get/4 but do not accept default parameter. If the key exists, will return the tuple {:ok, value} and if not :error.

Work equals to fetch/3 but erroring out if key doesn't exists.

Returns the value asociated to key taking the internal map indexed by master_key. If key does not exists in the map, default or nil is returned.

Return the complete internal map indexed by master_key.

Checks if key exists within DualMap.WCN.

Return a list with all the keys of the internal map indexed by master_key.

Checks if the pair key_value (tuple size 2) exists within DualMap.WCN either as key => value or as value => key.

Returns an empty DualMap.WCN struct. The order of the master keys are important for posterior operations with the struct.

Returns a DualMap.WCN struct initialized with the values indicated in the second argument. As the new/1 function, the order of the master keys are important for posterior operations with the struct.

Insert or replace one or more pairs of datas in a DualMap.WCN struc. If the third parameters is a list of tuples, every one is inserted/replaced in the DualMap.WCN secuentialy. With this function you need pass the a master_key to indicate which value of the tuple will be interpreted as key and which one as value.

This function is similar to put/3 but you does not need to pass a master key because the function will assume that you are sending the keys and values in the same order as you defined when you created the DualMap.WCN with new/1 or new/2.

Return a list of the pairs {key, value} taking the map indexed by the first master_key defined with new/1 or new/2. This function is used by inspect to print the DualMap.WCN.

Return a list with all the values of the internal map indexed by master_key.

Types

t()

@type t() :: %DualMap.WCN{
  __data: term(),
  __master_keys_map: term(),
  __ordered_master_keys: term()
}

DualMap.WCN struct

Functions

count(dual_map)

@spec count(t()) :: pos_integer()

Return de size of the DualMap.WCN counting the number of pairs.

delete(dual_map, master_key, list)

@spec delete(t(), master_key :: any(), keys :: any()) :: t()

Delete one or more pairs of datas and returns the DualMap.WCN without that pairs. The pairs are found looking for key in the the internal map indexed by master_key.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

iex> DualMap.WCN.delete(dm, :ip, "192.168.0.3")
[
  {"ns1", "192.168.0.2"},
  {"ns3", "192.168.0.4"}
]

iex> DualMap.WCN.delete(dm, :ip, ["192.168.0.3", "192.168.0.2"])
[{"ns3", "192.168.0.4"}]

equal?(dual_map1, dual_map2)

@spec equal?(t(), t()) :: boolean()

Checks if two DualMap.WCNs are equal.

Two maps are considered to be equal if both internal maps contains the same keys and values.

fetch(dual_map, master_key, key)

@spec fetch(t(), any(), any()) :: {:ok, any()} | :error

Just as get/4 but do not accept default parameter. If the key exists, will return the tuple {:ok, value} and if not :error.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])

iex> DualMap.WCN.fetch(dm, :ip, "192.168.0.4")
{:ok, "ns3"}

iex> DualMap.WCN.fetch(dm, :ip, "192.168.0.6")
:error

fetch!(dual_map, master_key, key)

@spec fetch!(t(), any(), any()) :: any()

Work equals to fetch/3 but erroring out if key doesn't exists.

get(dual_map, master_key, key, default \\ nil)

@spec get(t(), master_key :: any(), key :: any(), default :: any()) :: any()

Returns the value asociated to key taking the internal map indexed by master_key. If key does not exists in the map, default or nil is returned.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]
iex> DualMap.WCN.get(dm, :ip, "192.168.0.4")
"ns3"

iex> DualMap.WCN.get(dm, :hostname, "ns3")
"192.168.0.4"

get_map(dual_map, master_key)

@spec get_map(t(), master_key :: any()) :: map()

Return the complete internal map indexed by master_key.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

iex> DualMap.WCN.get_map(dm, :hostname)
%{
  "ns1" => "192.168.0.2",
  "ns2" => "192.168.0.3",
  "ns3" => "192.168.0.4"
}

has?(dual_map, key_value)

@spec has?(t(), key :: any()) :: boolean()

Checks if key exists within DualMap.WCN.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

iex> DualMap.WCN.has?(dm, "192.168.0.4")
true

iex> DualMap.WCN.has?(dm, "ns2")
true

iex> DualMap.WCN.has?(dm, "ns5")
false

keys(dual_map, master_key)

@spec keys(t(), master_key :: any()) :: list()

Return a list with all the keys of the internal map indexed by master_key.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])

iex> DualMap.WCN.keys(dm, :hostname)
["ns1", "ns2", "ns3"]

member?(dual_map, arg)

@spec member?(t(), key_value :: {any(), any()}) :: boolean()

Checks if the pair key_value (tuple size 2) exists within DualMap.WCN either as key => value or as value => key.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

iex> DualMap.WCN.member?(dm, {"192.168.0.4", "ns3"})
true

iex> DualMap.WCN.member?(dm, {"ns3", "192.168.0.4"})
true

iex> DualMap.WCN.member?(dm, {"ns1", "192.168.0.4"})
false

new(arg)

@spec new(master_keys :: {master_key1 :: any(), master_key2 :: any()}) :: t()

Returns an empty DualMap.WCN struct. The order of the master keys are important for posterior operations with the struct.

Examples

iex> DualMap.WCN.new({:hostname, :ip})
[]

new(master_keys, values)

@spec new(
  {master_key1 :: any(), master_key2 :: any()},
  {any(), any()} | [tuple()]
) :: t()

Returns a DualMap.WCN struct initialized with the values indicated in the second argument. As the new/1 function, the order of the master keys are important for posterior operations with the struct.

Examples

# Initializing with one pair of values
iex> DualMap.WCN.new({:hostname, :ip}, {"ns1", "192.168.0.2"})
[{"ns1", "192.168.0.2"}]

# Initializing with more than one pair of values
iex> DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

put(dual_map, master_key1, arg3)

@spec put(t(), master_key :: any(), {key :: any(), value :: any()} | [tuple()]) :: t()

Insert or replace one or more pairs of datas in a DualMap.WCN struc. If the third parameters is a list of tuples, every one is inserted/replaced in the DualMap.WCN secuentialy. With this function you need pass the a master_key to indicate which value of the tuple will be interpreted as key and which one as value.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip})
[]

# Inserting/replacing many
iex> DualMap.WCN.put(dm, :ip, [
  {"192.168.0.4", "ns3"},
  {"192.168.0.3", "ns2"},
  {"192.168.0.2", "ns1"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

Or inserting just one
iex> DualMap.WCN.put(dm, :ip, {"192.168.0.4", "ns3"})
[{"ns3", "192.168.0.4"}]

put_ordered(dual_map, pair)

@spec put_ordered(t(), {any(), any()} | [tuple()]) :: t()

This function is similar to put/3 but you does not need to pass a master key because the function will assume that you are sending the keys and values in the same order as you defined when you created the DualMap.WCN with new/1 or new/2.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip})
[]

iex> DualMap.WCN.put_ordered(dm, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

put_ordered(dual_map, value1, value2)

@spec put_ordered(t(), any(), any()) :: t()

to_list(dual_map, option \\ nil)

Return a list of the pairs {key, value} taking the map indexed by the first master_key defined with new/1 or new/2. This function is used by inspect to print the DualMap.WCN.

If you also pass an option :pairs_inverted, the list will have the pairs with key/value inverted because will take the internal map indexed by the second master_key defined with new/1 or new/2.

values(dual_map, master_key)

@spec values(t(), master_key :: any()) :: list()

Return a list with all the values of the internal map indexed by master_key.

Examples

iex> dm = DualMap.WCN.new({:hostname, :ip}, [
  {"ns3", "192.168.0.4"},
  {"ns2", "192.168.0.3"},
  {"ns1", "192.168.0.2"}
])
[
  {"ns1", "192.168.0.2"},
  {"ns2", "192.168.0.3"},
  {"ns3", "192.168.0.4"}
]

iex> DualMap.WCN.values(dm, :hostname)
["192.168.0.2", "192.168.0.3", "192.168.0.4"]