Treex v0.1.0 Treex View Source
Convenient module for using the gb_trees
from erlang’s
standard library.
It reorders the arguments so the tree is always passed first and therefore, it can be used with the pipe operators.
And therefore by using Treex instead, code can be written as:
t = Treex.empty()
|> Treex.enter("hello", :world)
|> Treex.enter(:hello, "world")
As an additional facility this module implements a stream/1
to traverse all {key, value}
from the given tree.
Its functions also hints when errors can be raised. Most of them
will be FunctionClauseError
Function’s documentation is provided for the IDE’s to get it. For full details, refer to the official one at reference documents
Link to this section Summary
Functions
Rebalances tree
. Notice that this is rarely necessary,
but can be motivated when many nodes have been deleted
from the tree without further insertions. Rebalancing
can then be forced to minimize lookup times, as deletion
does not rebalance the tree
Returns true
if key
is present in tree
, otherwise false
Removes the node with key key
from tree
and returns the
new tree. Assumes that the key is present in the tree,
crashes otherwise
Removes the node with key key
from tree
if the key is
present in the tree, otherwise does nothing
Returns a new empty tree
Returns true
if tree
is an empty tree, othwewise false
Inserts key
with value Value into tree
if the key
is not present in the tree, otherwise updates Key
to value value
in tree
Retrieves the value stored with key
in tree
Inserts key
with value value
into tree
and returns the new tree
Returns an iterator that can be used for traversing
the entries of tree
; see next/1
Returns the keys in tree
as an ordered list
Returns {key, value}
, where key
is the largest key in tree
,
and value
is the value associated with this key
Looks up key
in tree
. Returns {:value, value}
,
or :none if key
is not present
Maps function fn(k, v) -> v2
to all key-value
pairs of tree tree
Returns {key, value, iter2}
, where key
is the
smallest key referred to by iterator it
, and
iter2
is the new iterator to be used for traversing
the remaining nodes, or the atom :none
if no nodes remain
Returns the number of nodes in tree
Returns {key, value}
, where key
is the
smallest key in tree
, and value
is the value
associated with this key
Implements a stream for tree
Returns a {value, tree}
tuple from node with key
key
and new tree without the node with this value
Returns a {value, tree2}
from node with key key
;
new tree without the node with this value
Returns {key, value, tree2}
, where key
is the largest
key in tree
, value
is the value associated with this key,
and tree2
is this tree with the corresponding node deleted
Returns {key, value, tree2}
, where key
is the smallest key
in tree
, value
is the value associated with this key,
and tree2
is this tree with the corresponding node
deleted
Converts a tree into an ordered list of key-value tuples
Updates key
to value value
in tree
and returns the new tree
Returns the values in tree
as an ordered list, sorted
by their corresponding keys
Link to this section Types
Link to this section Functions
Rebalances tree
. Notice that this is rarely necessary,
but can be motivated when many nodes have been deleted
from the tree without further insertions. Rebalancing
can then be forced to minimize lookup times, as deletion
does not rebalance the tree.
Returns true
if key
is present in tree
, otherwise false
.
Removes the node with key key
from tree
and returns the
new tree. Assumes that the key is present in the tree,
crashes otherwise.
Removes the node with key key
from tree
if the key is
present in the tree, otherwise does nothing.
Returns the new tree.
Returns a new empty tree.
Returns true
if tree
is an empty tree, othwewise false
.
Inserts key
with value Value into tree
if the key
is not present in the tree, otherwise updates Key
to value value
in tree
.
Returns the new tree.
Retrieves the value stored with key
in tree
.
Assumes that the key is present in the tree, crashes otherwise.
Inserts key
with value value
into tree
and returns the new tree.
Assumes that the key is not present in the tree, crashes otherwise.
Returns an iterator that can be used for traversing the
entries of tree
; see next/1
. The implementation of
this is very efficient; traversing the whole tree using
next/1
is only slightly slower than getting the list
of all elements using to_list/1
and traversing that.
The main advantage of the iterator approach is that it does not require the complete list of all elements to be built in memory at one time.
Returns an iterator that can be used for traversing
the entries of tree
; see next/1
.
The difference as compared to the iterator
returned by iterator/1
is that the first key greater
than or equal to key
is returned.
Returns the keys in tree
as an ordered list.
Returns {key, value}
, where key
is the largest key in tree
,
and value
is the value associated with this key.
Assumes that the tree is not empty.
Looks up key
in tree
. Returns {:value, value}
,
or :none if key
is not present.
Maps function fn(k, v) -> v2
to all key-value
pairs of tree tree
.
Returns a new tree with the same set of keys as
tree
and the new set of values v2
.
next(:gb_trees.iter(any(), any())) :: :none | {any(), any(), :gb_trees.iter(any(), any())}
Returns {key, value, iter2}
, where key
is the
smallest key referred to by iterator it
, and
iter2
is the new iterator to be used for traversing
the remaining nodes, or the atom :none
if no nodes remain.
Returns the number of nodes in tree
.
Returns {key, value}
, where key
is the
smallest key in tree
, and value
is the value
associated with this key.
Assumes that the tree is not empty.
Implements a stream for tree
.
Each value returned by the stream shall be a {key, value}
tuple. The exact same behaviour can be implemented
by using the iterator/1.
Example:
Treex.empty()
|> Treex.enter(:key1, 1)
|> Treex.enter(:key2, 2)
|> Treex.enter(:key3, 3)
|> Treex.stream()
|> Enum.reduce(0, fn({_k, v}, acc) -> acc+v end)
returns 6
Returns a {value, tree}
tuple from node with key
key
and new tree without the node with this value.
Assumes that the node with key
is present in the
tree, crashes otherwise.
Returns a {value, tree2}
from node with key key
;
new tree without the node with this value.
Returns error if the node with the key is not present in the tree.
Returns {key, value, tree2}
, where key
is the largest
key in tree
, value
is the value associated with this key,
and tree2
is this tree with the corresponding node deleted.
Assumes that the tree is not empty.
Returns {key, value, tree2}
, where key
is the smallest key
in tree
, value
is the value associated with this key,
and tree2
is this tree with the corresponding node
deleted.
Assumes that the tree is not empty.
Converts a tree into an ordered list of key-value tuples.
Updates key
to value value
in tree
and returns the new tree.
Assumes that the key is present in the tree.
Returns the values in tree
as an ordered list, sorted
by their corresponding keys.
Duplicates are not removed.