v0.4.0

Tree<K, V> structv0.1.0

Source
An ordered collection of entries, kept balanced by colouring.
pub struct Tree<K, V> {
    // private fields
}

Owns every node and everything in them, so it prohibits copying and destroys them with ~Tree.

Type parameters

NameDescription
Kthe key type
Vthe value type

Constructors

Tree

pub func Tree(allocator: Allocator) -> Tree<K, V>

An empty tree that has allocated nothing. allocator is borrowed for the call and kept for the tree's life. Cannot fail. Runs in constant time and allocates nothing.

NameTypeDescription
allocatorAllocatorwhere the storage comes from

Returns: an empty tree that has taken no storage yet

Source

Associated functions

Minimum

pub unsafe func Minimum(from: *var TreeNode<K, V>) -> *var TreeNode<K, V>

The node holding the smallest key of the subtree at from, or null when from is null. Runs in time proportional to the height and allocates nothing. Safety: from must be null or a live node of a tree whose links all point at live nodes of it.

NameTypeDescription
from*var TreeNode<K, V>the subtree to search

Returns: the leftmost node of that subtree, or null when it is empty

Source

Maximum

pub unsafe func Maximum(from: *var TreeNode<K, V>) -> *var TreeNode<K, V>

The node holding the largest key of the subtree at from, or null when from is null. Runs in time proportional to the height and allocates nothing. Safety: from must be null or a live node of a tree whose links all point at live nodes of it.

NameTypeDescription
from*var TreeNode<K, V>the subtree to search

Returns: the rightmost node of that subtree, or null when it is empty

Source

Successor

pub unsafe func Successor(node: *var TreeNode<K, V>) -> *var TreeNode<K, V>

The node holding the next key after node's, or null when node holds the largest or is itself null. Descends into the right subtree where there is one, and otherwise climbs past every ancestor node is the right child of — arriving at the first ancestor reached from the left, whose key is the next one up. Runs in time proportional to the height for a single step, but a walk of the whole tree by successor is linear in the length: every edge is crossed exactly twice across the walk. Allocates nothing. Safety: node must be null or a live node of a tree whose links all point at live nodes of it.

NameTypeDescription
node*var TreeNode<K, V>the node to act on

Returns: the node with the next key in order, or null after the last

Source

Predecessor

pub unsafe func Predecessor(node: *var TreeNode<K, V>) -> *var TreeNode<K, V>

The node holding the key before node's, or null when node holds the smallest or is itself null. The mirror of Successor, with the same costs. Safety: node must be null or a live node of a tree whose links all point at live nodes of it.

NameTypeDescription
node*var TreeNode<K, V>the node to act on

Returns: the node with the previous key in order, or null before the first

Source

Methods

Length

pub func Length(&self) -> uint

How many entries the tree holds. self is borrowed. Cannot fail; runs in constant time and allocates nothing.

Returns: how many nodes it holds

Source

Root

pub func Root(&self) -> *var TreeNode<K, V>

The topmost node, or null for an empty tree, for code that walks the shape itself. self is borrowed. Reading through the pointer needs unsafe, and it is valid until the tree next changes; the field it reads stays private, so only the tree's own operations can move it.

Returns: the root node, or null when the tree is empty

Source

IsEmpty

pub func IsEmpty(&self) -> bool

Whether the tree holds no entries. self is borrowed. Cannot fail; runs in constant time and allocates nothing.

Returns: whether it holds nothing

Source

Find

pub func Find(
    &self,
    key: K,
    compare: func(K, K) -> Ordering
) -> *var TreeNode<K, V>

The node holding key, or null when the tree does not have it. self is borrowed. compare must impose the same order every call, and the same one every earlier insertion used — a tree searched by a different order than it was built with misses entries it holds. Runs in time proportional to the height, which the colouring holds to at most twice the logarithm of the length. Allocates nothing.

NameTypeDescription
keyKthe key to look up
comparefunc(K, K) -> Orderinghow two keys order against each other

Returns: the node with that key, or null when it is absent

Source

Above

pub func Above(
    &self,
    key: K,
    compare: func(K, K) -> Ordering
) -> *var TreeNode<K, V>

The node holding the smallest key strictly above key, or null when no key is above it. key need not be in the tree. The descent keeps the last node it passed on the high side, which is the answer wherever it ends: once it has gone left of a key, nothing further down is larger than that key. This is what lets a walk survive removing what it is standing on. Successor needs its node still linked into the tree, and removal relinks nodes around it; asking by key instead needs nothing to have stayed put, at the price of a descent per step rather than an edge. Runs in time proportional to the height and allocates nothing.

NameTypeDescription
keyKthe key to look up
comparefunc(K, K) -> Orderinghow two keys order against each other

Returns: the node with the smallest key above this one, or null

Source

Below

pub func Below(
    &self,
    key: K,
    compare: func(K, K) -> Ordering
) -> *var TreeNode<K, V>

The node holding the largest key strictly below key, or null when no key is below it. The mirror of Above, with the same costs and the same use.

NameTypeDescription
keyKthe key to look up
comparefunc(K, K) -> Orderinghow two keys order against each other

Returns: the node with the largest key below this one, or null

Source

Attach

pub func Attach(
    &var self,
    key: K,
    value: V,
    compare: func(K, K) -> Ordering
) -> V?

Puts key and value into the tree, replacing the value already stored under an equal key. Takes ownership of both. Hands back the value an equal key already had, or none when the key is new — so a caller learns whether anything was displaced without a lookup first, and gets the displaced value rather than having it quietly destroyed. When the key is already present the new key is destroyed rather than stored, since the one in the tree is equal to it and is what everything already hanging below it was ordered against. Terminates the program when the node cannot be allocated. Runs in time proportional to the height, with at most two rotations. Allocates one node for a new key and nothing at all for one already present.

NameTypeDescription
keyKthe key to look up
valueVthe value to act on
comparefunc(K, K) -> Orderinghow two keys order against each other

Returns: the value an equal key already held, or none when the key is new

Source

Detach

pub unsafe func Detach(
    &var self,
    node: *var TreeNode<K, V>
) -> KeyValue<K, V>

Takes node out of the tree and hands back what it held. The key and value become the caller's, in the pair a hash map hands its entries back in. A node with two children is not emptied into: the successor is relinked in its place instead, so the entry that stays keeps the address it had and any pointer to it stays good. That is the whole reason the nodes are separate allocations, and it is why removal moves six pointers where copying two fields would do. Cannot fail. Runs in time proportional to the height, with at most three rotations, and allocates nothing. Safety: node must be a live node of this tree, and is freed here — nothing may read through it afterwards.

NameTypeDescription
node*var TreeNode<K, V>the node to act on

Returns: the entry the node held

Source

Clear

pub func Clear(&var self)

Destroys every entry and gives every node back to the allocator, leaving an empty tree. self is borrowed mutably and stays usable: entries may be attached again afterwards, and nothing has been kept back to make that cheaper, since a tree has no capacity to keep. Cannot fail. Runs in time proportional to the length and allocates nothing.

Source

Destructor

func ~Tree(&var self)

Destroys every entry and gives every node back. Runs when the tree dies. Never called directly; a tree is move-only, so there is exactly one of these per tree however many times it was passed along.

Source

TreeNode<K, V>

pub struct TreeNode<K, V> {
    pub key: K;
    pub value: V;
    pub left: *var TreeNode<K, V>;
    pub right: *var TreeNode<K, V>;
    pub parent: *var TreeNode<K, V>;
    pub red: bool;
}

One node of a red-black tree. Its own allocation, so its address survives any rearrangement of the tree around it.

Source

Type parameters

NameDescription
Kthe key type
Vthe value type

Fields

NameTypeDescription
keyKThe key this node is ordered by.
valueVThe value stored under that key.
left*var TreeNode<K, V>The subtree of smaller keys, or null.
right*var TreeNode<K, V>The subtree of larger keys, or null.
parent*var TreeNode<K, V>The node this one hangs from, null only for the root.
redboolWhether this node is red. A missing child counts as black, which is what makes null a colour and not a gap.