Fold over the structure of the tree.
Fold over the structure of the tree. The given functions correspond to the three possible variations of the finger tree.
if the tree is empty, convert the measure to a B
if the tree contains a single element, convert the measure and this element to a B
otherwise, convert the measure, the two fingers, and the sub tree to a B
.
Prepends an element to the left of the tree.
Prepends an element to the left of the tree. O(1).
Appends an element to the right of the tree.
Appends an element to the right of the tree. O(1).
Replace the last element of the tree with the given value.
Replace the last element of the tree with the given value. O(1)
Appends the given finger tree to the right of this 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.
the tree is empty
Selects a subtree containing all elements except the last
Selects a subtree containing all elements except the last
the tree is empty
An iterator that visits each element in the tree.
Selects the last element in the tree.
Selects the last element in the tree.
the tree is empty
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.
Splits this tree into a pair of subtrees at the point where the given predicate, based on the measure,
changes from true
to false
.
Splits this tree into a pair of subtrees at the point where the given predicate, based on the measure,
changes from true
to false
. O(log(min(i,n-i)))
(as, bs)
as
: the subtree containing elements before the point where pred
first holds
fs
the subtree containing element at and after the point where pred
first holds. Empty if pred
never holds.
Like split
, but returns the element where pred
first holds separately
Like split
, but returns the element where pred
first holds separately
the tree is empty.
Selects a subtree containing all elements except the first
Selects a subtree containing all elements except the first
the tree is empty
Convert the leaves of the tree to a scala.List
Convert the leaves of the tree to a scala.Stream
Convert the tree to a String
.
Convert the tree to a String
. Unsafe: this uses Any#toString
for types V
and A
Like traverse, but with a more constraint type: we need the additional measure to construct the new tree.
Replace the first element of the tree with the given value.
Replace the first element of the tree with the given value. O(1)
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:
O(1)
O(1)
(O lg min(l1, l2))
wherel1
andl2
are their sizesn
isO(lg min(n, l - n))
, wherel
is the size of the tree.The type of the annotations of the nodes (the measure)
The type of the elements stored at the leaves
http://apfelmus.nfshost.com/articles/monoid-fingertree.html
Finger trees: a simple general-purpose data structure