Finger trees with leaves of type A and Nodes that are annotated with type V.
Finger Trees provide a base for implementations of various collection types, as described in "Finger trees: a simple general-purpose data structure", by Ralf Hinze and Ross Paterson. A gentle introduction is presented in the blog post "Monoids and Finger Trees" by Heinrich Apfelmus.
This is done by choosing a suitable type to annotate the nodes. For example, a binary tree can be implemented by annotating each node with the size of its subtree, while a priority queue can be implemented by labelling the nodes by the minimum priority of its children.
The operations on FingerTree enforce the constraint measured (in the form of a Reducer instance).
Finger Trees have excellent (amortized) asymptotic performance:
- Access to the first and last elements is
O(1)
- Appending/prepending a single value is
O(1)
- Concatenating two trees is
(O lg min(l1, l2))
wherel1
andl2
are their sizes - Random access to an element at
n
isO(lg min(n, l - n))
, wherel
is the size of the tree. - Constructing a tree with n copies of a value is O(lg n).
- Type Params
- A
The type of the elements stored at the leaves
- V
The type of the annotations of the nodes (the '''measure''')
- See also
- Companion
- object
Value members
Abstract methods
Fold over the structure of the tree. The given functions correspond to the three possible variations of the finger tree.
Fold over the structure of the tree. The given functions correspond to the three possible variations of the finger tree.
- Value Params
- deep
otherwise, convert the measure, the two fingers, and the sub tree to a
B
.- empty
value to return if the tree is empty
- single
if the tree contains a single element, convert the measure and this element to a
B
Concrete methods
Prepends an element to the left of the tree. O(1).
Prepends an element to the left of the tree. O(1).
Appends an element to the right of the tree. O(1).
Appends an element to the right of the tree. O(1).
Replace the last element of the tree with the given value. O(1)
Replace the last element of the tree with the given value. O(1)
Appends the given finger tree to the right of this tree.
Appends the given finger tree to the right of this tree.
Execute the provided side effect for each element in the tree.
Execute the provided side effect for each element in the tree.
Selects the first element in the tree.
Selects the first element in the tree.
- Throws
- if
the tree is empty
Selects a subtree containing all elements except the last
Selects a subtree containing all elements except the last
- Throws
- if
the tree is empty
Selects the last element in the tree.
Selects the last element in the tree.
- Throws
- if
the tree is empty
Maps the given function across the tree, annotating nodes in the resulting tree according to the provided Reducer
.
Maps the given function across the tree, annotating nodes in the resulting tree according to the provided Reducer
.
An iterator that visits each element in the tree in reverse order.
An iterator that visits each element in the tree in reverse order.
Splits this tree into a pair of subtrees at the point where the given predicate, based on the measure,
changes from false
to true
. O(log(min(i,n-i)))
Splits this tree into a pair of subtrees at the point where the given predicate, based on the measure,
changes from false
to true
. O(log(min(i,n-i)))
- Value Params
- pred
predicate on node measures. Must be a semigroup homomorphism from the semigroup
V
of node measures to the semigroup ofBoolean
s with||
as the semigroup operation. Namely, it must hold thatpred(v1 |+| v2) = pred(v1) || pred(v2)
.
- Returns
(as, bs)
, where -as
: the subtree containing elements before the point wherepred
first holds -bs
: the subtree containing elements at and after the point wherepred
first holds. Empty ifpred
never holds.
Like split
, but returns the element where pred
first holds separately
Like split
, but returns the element where pred
first holds separately
- Throws
- if
the tree is empty.
Selects a subtree containing all elements except the first
Selects a subtree containing all elements except the first
- Throws
- if
the tree is empty
Convert the tree to a String
. Unsafe: this uses Any#toString
for types V
and A
Convert the tree to a String
. Unsafe: this uses Any#toString
for types V
and A
- Definition Classes
- Any
Like traverse, but with a more constraint type: we need the additional measure to construct the new tree.
Like traverse, but with a more constraint type: we need the additional measure to construct the new tree.