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. 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

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 type gb_tree_node() View Source
gb_tree_node() :: nil | {any(), any(), any(), any()}

Link to this section Functions

Link to this function balance(tree) View Source
balance(t()) :: t()

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.

Link to this function defined?(tree, key) View Source
defined?(t(), any()) :: boolean()

Returns true if key is present in tree, otherwise false.

Link to this function delete!(tree, key) View Source
delete!(t(), any()) :: t()

Removes the node with key key from tree and returns the new tree. Assumes that the key is present in the tree, crashes otherwise.

Link to this function delete_any(tree, key) View Source
delete_any(t(), any()) :: t()

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.

Link to this function enter(tree, key, value) View Source
enter(t(), any(), any()) :: t()

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.

Link to this function get!(tree, key) View Source
get!(t(), any()) :: t()

Retrieves the value stored with key in tree.

Assumes that the key is present in the tree, crashes otherwise.

Link to this function insert!(tree, key, value) View Source
insert!(t(), any(), any()) :: t()

Inserts key with value value into tree and returns the new tree.

Assumes that the key is not present in the tree, crashes otherwise.

Link to this function iterator(tree) View Source
iterator(t()) :: [gb_tree_node()]

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.

Link to this function iterator(tree, key) View Source
iterator(t(), any()) :: [gb_tree_node()]

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.

Link to this function keys(tree) View Source
keys(t()) :: [any()]

Returns the keys in tree as an ordered list.

Link to this function largest!(tree) View Source
largest!(t()) :: {any(), any()}

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.

Link to this function lookup(tree, key) View Source
lookup(t(), any()) :: :none | {:value, any()}

Looks up key in tree. Returns {:value, value}, or :none if key is not present.

Link to this function map(tree, fun) View Source
map(t(), (any() -> any())) :: t()

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.

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.

Link to this function smallest!(tree) View Source
smallest!(t()) :: {any(), any()}

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

Link to this function take!(tree, key) View Source
take!(t(), any()) :: {any(), t()}

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.

Link to this function take_any(tree, key) View Source
take_any(t(), any()) :: :error | {any(), t()}

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.

Link to this function take_largest!(tree) View Source
take_largest!(t()) :: {any(), any()}

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.

Link to this function take_smallest!(tree) View Source
take_smallest!(t()) :: {any(), any()}

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.

Link to this function to_list(tree) View Source
to_list(t()) :: [{any(), any()}]

Converts a tree into an ordered list of key-value tuples.

Link to this function update!(tree, key, value) View Source
update!(t(), any(), any()) :: t()

Updates key to value value in tree and returns the new tree.

Assumes that the key is present in the tree.

Link to this function values(tree) View Source
values(t()) :: [any()]

Returns the values in tree as an ordered list, sorted by their corresponding keys.

Duplicates are not removed.