NamedDual.WCN (dual_map_ex v0.2.0)
A NamedDual.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 NamedDual.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 NamedDual.WCN you must use the NamedDual.WCN.new
function. You must pass to it a pair of names that will be the names of the columns.
NamedDual.WCN.new({:hostname, :ip})
The order of the master keys is important. If you later want to make insertions into the NamedDual.WCN and you use the NamedDual.WCN.put_ordered
function the value pairs will assume that they are ordered as defined at the time of creating the NamedDual.WCN with NamedDual.WCN.new
.
Let's see some examples:
iex> dm = NamedDual.WCN.new({:hostname, :ip})
[]
iex> NamedDual.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> NamedDual.WCN.delete(dm, :ip, "192.168.0.3")
[
{"ns1", "192.168.0.2"},
{"ns3", "192.168.0.4"}
]
Summary
Functions
Return de size of the NamedDual.WCN counting the number of pairs.
Delete one or more pairs of datas and returns the NamedDual.WCN without that pairs. The pairs are found looking for key
in the the internal map indexed by master_key
.
Checks if two NamedDual.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 NamedDual.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 NamedDual.WCN either as key => value or as value => key.
Returns an empty NamedDual.WCN struct. The order of the master keys are important for posterior operations with the struct.
Returns a NamedDual.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 NamedDual.WCN struc. If the third parameters is a list of tuples, every one is inserted/replaced in the NamedDual.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.
Return a list with all the values of the internal map indexed by master_key
.
Types
Functions
@spec count(t()) :: pos_integer()
Return de size of the NamedDual.WCN counting the number of pairs.
Delete one or more pairs of datas and returns the NamedDual.WCN without that pairs. The pairs are found looking for key
in the the internal map indexed by master_key
.
Examples
iex> dm = NamedDual.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> NamedDual.WCN.delete(dm, :ip, "192.168.0.3")
[
{"ns1", "192.168.0.2"},
{"ns3", "192.168.0.4"}
]
iex> NamedDual.WCN.delete(dm, :ip, ["192.168.0.3", "192.168.0.2"])
[{"ns3", "192.168.0.4"}]
Checks if two NamedDual.WCNs are equal.
Two maps are considered to be equal if both internal maps contains the same keys and values.
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 = NamedDual.WCN.new({:hostname, :ip}, [
{"ns3", "192.168.0.4"},
{"ns2", "192.168.0.3"},
{"ns1", "192.168.0.2"}
])
iex> NamedDual.WCN.fetch(dm, :ip, "192.168.0.4")
{:ok, "ns3"}
iex> NamedDual.WCN.fetch(dm, :ip, "192.168.0.6")
: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.
Examples
iex> dm = NamedDual.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> NamedDual.WCN.get(dm, :ip, "192.168.0.4")
"ns3"
iex> NamedDual.WCN.get(dm, :hostname, "ns3")
"192.168.0.4"
Return the complete internal map indexed by master_key
.
Examples
iex> dm = NamedDual.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> NamedDual.WCN.get_map(dm, :hostname)
%{
"ns1" => "192.168.0.2",
"ns2" => "192.168.0.3",
"ns3" => "192.168.0.4"
}
Checks if key
exists within NamedDual.WCN.
Examples
iex> dm = NamedDual.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> NamedDual.WCN.has?(dm, "192.168.0.4")
true
iex> NamedDual.WCN.has?(dm, "ns2")
true
iex> NamedDual.WCN.has?(dm, "ns5")
false
Return a list with all the keys of the internal map indexed by master_key
.
Examples
iex> dm = NamedDual.WCN.new({:hostname, :ip}, [
{"ns3", "192.168.0.4"},
{"ns2", "192.168.0.3"},
{"ns1", "192.168.0.2"}
])
iex> NamedDual.WCN.keys(dm, :hostname)
["ns1", "ns2", "ns3"]
Checks if the pair key_value
(tuple size 2) exists within NamedDual.WCN either as key => value or as value => key.
Examples
iex> dm = NamedDual.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> NamedDual.WCN.member?(dm, {"192.168.0.4", "ns3"})
true
iex> NamedDual.WCN.member?(dm, {"ns3", "192.168.0.4"})
true
iex> NamedDual.WCN.member?(dm, {"ns1", "192.168.0.4"})
false
Returns an empty NamedDual.WCN struct. The order of the master keys are important for posterior operations with the struct.
Examples
iex> NamedDual.WCN.new({:hostname, :ip})
[]
Returns a NamedDual.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> NamedDual.WCN.new({:hostname, :ip}, {"ns1", "192.168.0.2"})
[{"ns1", "192.168.0.2"}]
# Initializing with more than one pair of values
iex> NamedDual.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"}
]
Insert or replace one or more pairs of datas in a NamedDual.WCN struc. If the third parameters is a list of tuples, every one is inserted/replaced in the NamedDual.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 = NamedDual.WCN.new({:hostname, :ip})
[]
# Inserting/replacing many
iex> NamedDual.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> NamedDual.WCN.put(dm, :ip, {"192.168.0.4", "ns3"})
[{"ns3", "192.168.0.4"}]
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 NamedDual.WCN with new/1
or new/2
.
Examples
iex> dm = NamedDual.WCN.new({:hostname, :ip})
[]
iex> NamedDual.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"}
]
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 NamedDual.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
.
Return a list with all the values of the internal map indexed by master_key
.
Examples
iex> dm = NamedDual.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> NamedDual.WCN.values(dm, :hostname)
["192.168.0.2", "192.168.0.3", "192.168.0.4"]