public class BTree
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
BTree.Builder<V> |
static class |
BTree.Dir
Represents the direction of iteration.
|
static class |
BTree.FastBuilder<V>
A pooled builder for constructing a tree in-order, and without needing any reconciliation.
|
Modifier and Type | Field and Description |
---|---|
static int |
BRANCH_SHIFT
The
BRANCH_FACTOR is defined as the maximum number of children of each branch, with between
BRANCH_FACTOR/2-1 and BRANCH_FACTOR-1 keys being stored in every node. |
static int |
MAX_KEYS |
static int |
MIN_KEYS |
Constructor and Description |
---|
BTree() |
Modifier and Type | Method and Description |
---|---|
static <V,A> long |
accumulate(java.lang.Object[] btree,
BiLongAccumulator<A,V> accumulator,
A arg,
java.util.Comparator<V> comparator,
V from,
long initialValue)
Walk the btree and accumulate a long value using the supplied accumulator function.
|
static <V,A> long |
accumulate(java.lang.Object[] btree,
BiLongAccumulator<A,V> accumulator,
A arg,
long initialValue) |
static <V> long |
accumulate(java.lang.Object[] btree,
LongAccumulator<V> accumulator,
java.util.Comparator<V> comparator,
V from,
long initialValue) |
static <V> long |
accumulate(java.lang.Object[] btree,
LongAccumulator<V> accumulator,
long initialValue) |
static <V,A> void |
apply(java.lang.Object[] btree,
java.util.function.BiConsumer<A,V> function,
A argument)
Simple method to walk the btree forwards and apply a function till a stop condition is reached
|
static <V> void |
apply(java.lang.Object[] btree,
java.util.function.Consumer<V> function)
Simple method to walk the btree forwards and apply a function till a stop condition is reached
|
static <V,A> void |
applyLeaf(java.lang.Object[] btree,
java.util.function.BiConsumer<A,V> function,
A argument) |
static <C,I extends C,O extends C> |
build(BulkIterator<I> source,
int size,
UpdateFunction<I,O> updateF) |
static <C,K extends C,V extends C> |
build(java.util.Collection<K> source)
Deprecated.
|
static <C,K extends C,V extends C> |
build(java.util.Collection<K> source,
UpdateFunction<K,V> updateF)
Deprecated.
|
static <V> BTree.Builder<V> |
builder(java.util.Comparator<? super V> comparator) |
static <V> BTree.Builder<V> |
builder(java.util.Comparator<? super V> comparator,
int initialCapacity) |
static <V> V |
ceil(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static <V> int |
ceilIndex(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static int |
depth(java.lang.Object[] tree) |
static java.lang.Object[] |
empty()
Returns an empty BTree
|
static boolean |
equals(java.lang.Object[] a,
java.lang.Object[] b) |
static <V> BTree.FastBuilder<V> |
fastBuilder()
Build a tree of unknown size, in order.
|
static <V> V |
find(java.lang.Object[] node,
java.util.Comparator<? super V> comparator,
V find) |
static <V> V |
findByIndex(java.lang.Object[] tree,
int index) |
static <V> int |
findIndex(java.lang.Object[] node,
java.util.Comparator<? super V> comparator,
V find)
Honours result semantics of
Arrays.binarySearch(long[], long) , as though it were performed on the tree flattened into an array |
static <V> V |
floor(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static <V> int |
floorIndex(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static int |
hashCode(java.lang.Object[] btree) |
static int |
height(java.lang.Object[] tree) |
static <V> V |
higher(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static <V> int |
higherIndex(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static boolean |
isEmpty(java.lang.Object[] tree) |
static boolean |
isLeaf(java.lang.Object[] node)
Checks is the node is a leaf.
|
static boolean |
isWellFormed(java.lang.Object[] btree,
java.util.Comparator<?> cmp) |
static <V> java.lang.Iterable<V> |
iterable(java.lang.Object[] btree) |
static <V> java.lang.Iterable<V> |
iterable(java.lang.Object[] btree,
BTree.Dir dir) |
static <V> java.lang.Iterable<V> |
iterable(java.lang.Object[] btree,
int lb,
int ub,
BTree.Dir dir) |
static <V> java.util.Iterator<V> |
iterator(java.lang.Object[] btree) |
static <V> java.util.Iterator<V> |
iterator(java.lang.Object[] btree,
BTree.Dir dir) |
static <V> java.util.Iterator<V> |
iterator(java.lang.Object[] btree,
int lb,
int ub,
BTree.Dir dir) |
static <V> V |
lower(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static <V> int |
lowerIndex(java.lang.Object[] btree,
java.util.Comparator<? super V> comparator,
V find) |
static <V> void |
replaceInSitu(java.lang.Object[] node,
java.util.Comparator<? super V> comparator,
V find,
V replace)
Modifies the provided btree directly.
|
static <V> void |
replaceInSitu(java.lang.Object[] tree,
int index,
V replace)
Modifies the provided btree directly.
|
static void |
reverseInSitu(java.lang.Object[] tree) |
static java.lang.Object[] |
singleton(java.lang.Object value)
Create a BTree containing only the specified object
|
static int |
size(java.lang.Object[] tree) |
static long |
sizeOfStructureOnHeap(java.lang.Object[] tree) |
static long |
sizeOnHeapOf(java.lang.Object[] tree) |
static <K,V> BTreeSearchIterator<K,V> |
slice(java.lang.Object[] btree,
java.util.Comparator<? super K> comparator,
BTree.Dir dir)
Returns an Iterator over the entire tree
|
static <K,V extends K> |
slice(java.lang.Object[] btree,
java.util.Comparator<? super K> comparator,
int startIndex,
int endIndex,
BTree.Dir dir) |
static <K,V extends K> |
slice(java.lang.Object[] btree,
java.util.Comparator<? super K> comparator,
K start,
boolean startInclusive,
K end,
boolean endInclusive,
BTree.Dir dir) |
static <K,V extends K> |
slice(java.lang.Object[] btree,
java.util.Comparator<? super K> comparator,
K start,
K end,
BTree.Dir dir) |
static int |
toArray(java.lang.Object[] tree,
int treeStart,
int treeEnd,
java.lang.Object[] target,
int targetOffset) |
static int |
toArray(java.lang.Object[] tree,
java.lang.Object[] target,
int targetOffset)
Fill the target array with the contents of the provided subtree, in ascending order, starting at targetOffset
|
static java.lang.String |
toString(java.lang.Object[] btree) |
static <I,O> java.lang.Object[] |
transform(java.lang.Object[] tree,
java.util.function.Function<? super I,? extends O> function)
Takes a tree and transforms it using the provided function.
|
static <I,I2,O> java.lang.Object[] |
transformAndFilter(java.lang.Object[] tree,
java.util.function.BiFunction<? super I,? super I2,? extends O> apply,
I2 param)
Takes a tree and transforms it using the provided function, filtering out any null results.
|
static <I,O> java.lang.Object[] |
transformAndFilter(java.lang.Object[] tree,
java.util.function.Function<? super I,? extends O> apply)
Takes a tree and transforms it using the provided function, filtering out any null results.
|
static int |
treeIndexOfBranchKey(java.lang.Object[] root,
int keyIndex) |
static int |
treeIndexOffsetOfChild(java.lang.Object[] root,
int childIndex) |
static int |
treeIndexOfKey(java.lang.Object[] root,
int keyIndex)
tree index => index of key wrt all items in the tree laid out serially
|
static int |
treeIndexOfLeafKey(int keyIndex) |
static <Compare> java.lang.Object[] |
update(java.lang.Object[] toUpdate,
java.lang.Object[] insert,
java.util.Comparator<? super Compare> comparator) |
static <Compare,Existing extends Compare,Insert extends Compare> |
update(java.lang.Object[] toUpdate,
java.lang.Object[] insert,
java.util.Comparator<? super Compare> comparator,
UpdateFunction<Insert,Existing> updateF)
Inserts
insert into update , applying updateF to each new item in insert ,
as well as any matched items in update . |
static <Compare,Existing extends Compare,Insert extends Compare> |
updateLeaves(java.lang.Object[] unode,
java.lang.Object[] inode,
java.util.Comparator<? super Compare> comparator,
UpdateFunction<Insert,Existing> updateF)
A fast tight-loop variant of updating one btree with another, when both are leaves.
|
public static final int BRANCH_SHIFT
BRANCH_FACTOR
is defined as the maximum number of children of each branch, with between
BRANCH_FACTOR/2-1 and BRANCH_FACTOR-1 keys being stored in every node. This yields a minimum tree size of
(BRANCH_FACTOR/2)^height - 1
and a maximum tree size of BRANCH_FACTOR^height - 1
.
Branches differ from leaves only in that they contain a suffix region containing the child nodes that occur either side of the keys, and a sizeMap in the last position, permitting seeking by index within the tree. Nodes are disambiguated by the length of the array that represents them: an even number is a branch, odd a leaf.
Leaf Nodes are represented by an odd-length array of keys, with the final element possibly null, i.e. Object[V1, V2, ...,null?]
Branch nodes: Object[V1, V2, ..., child[<V1.key], child[<V2.key], ..., child[< Inf], sizeMap] Each child is either a branch or leaf, i.e., always an Object[]. The key elements in a branch node occupy the first half of the array (minus one)
BTrees are immutable; updating one returns a new tree that reuses unmodified nodes.
There are no references back to a parent node from its children (this would make it impossible to re-use subtrees when modifying the tree, since the modified tree would need new parent references). Instead, we store these references in a Path as needed when navigating the tree.
public static final int MIN_KEYS
public static final int MAX_KEYS
public static java.lang.Object[] empty()
public static java.lang.Object[] singleton(java.lang.Object value)
@Deprecated public static <C,K extends C,V extends C> java.lang.Object[] build(java.util.Collection<K> source)
@Deprecated public static <C,K extends C,V extends C> java.lang.Object[] build(java.util.Collection<K> source, UpdateFunction<K,V> updateF)
public static <C,I extends C,O extends C> java.lang.Object[] build(BulkIterator<I> source, int size, UpdateFunction<I,O> updateF)
public static <Compare> java.lang.Object[] update(java.lang.Object[] toUpdate, java.lang.Object[] insert, java.util.Comparator<? super Compare> comparator)
public static <Compare,Existing extends Compare,Insert extends Compare> java.lang.Object[] update(java.lang.Object[] toUpdate, java.lang.Object[] insert, java.util.Comparator<? super Compare> comparator, UpdateFunction<Insert,Existing> updateF)
insert
into update
, applying updateF
to each new item in insert
,
as well as any matched items in update
.
Note that UpdateFunction.noOp
is assumed to indicate a lack of interest in which value survives.
public static <Compare,Existing extends Compare,Insert extends Compare> java.lang.Object[] updateLeaves(java.lang.Object[] unode, java.lang.Object[] inode, java.util.Comparator<? super Compare> comparator, UpdateFunction<Insert,Existing> updateF)
public static void reverseInSitu(java.lang.Object[] tree)
public static <V> java.util.Iterator<V> iterator(java.lang.Object[] btree)
public static <V> java.util.Iterator<V> iterator(java.lang.Object[] btree, BTree.Dir dir)
public static <V> java.util.Iterator<V> iterator(java.lang.Object[] btree, int lb, int ub, BTree.Dir dir)
public static <V> java.lang.Iterable<V> iterable(java.lang.Object[] btree)
public static <V> java.lang.Iterable<V> iterable(java.lang.Object[] btree, BTree.Dir dir)
public static <V> java.lang.Iterable<V> iterable(java.lang.Object[] btree, int lb, int ub, BTree.Dir dir)
public static <K,V> BTreeSearchIterator<K,V> slice(java.lang.Object[] btree, java.util.Comparator<? super K> comparator, BTree.Dir dir)
V
- btree
- the tree to iterate overdir
- direction of iterationpublic static <K,V extends K> BTreeSearchIterator<K,V> slice(java.lang.Object[] btree, java.util.Comparator<? super K> comparator, K start, K end, BTree.Dir dir)
btree
- the tree to iterate overcomparator
- the comparator that defines the ordering over the items in the treestart
- the beginning of the range to return, inclusive (in ascending order)end
- the end of the range to return, exclusive (in ascending order)dir
- if false, the iterator will start at the last item and move backwardspublic static <K,V extends K> BTreeSearchIterator<K,V> slice(java.lang.Object[] btree, java.util.Comparator<? super K> comparator, int startIndex, int endIndex, BTree.Dir dir)
btree
- the tree to iterate overcomparator
- the comparator that defines the ordering over the items in the treestartIndex
- the start index of the range to return, inclusiveendIndex
- the end index of the range to return, inclusivedir
- if false, the iterator will start at the last item and move backwardspublic static <K,V extends K> BTreeSearchIterator<K,V> slice(java.lang.Object[] btree, java.util.Comparator<? super K> comparator, K start, boolean startInclusive, K end, boolean endInclusive, BTree.Dir dir)
btree
- the tree to iterate overcomparator
- the comparator that defines the ordering over the items in the treestart
- low bound of the rangestartInclusive
- inclusivity of lower boundend
- high bound of the rangeendInclusive
- inclusivity of higher bounddir
- direction of iterationpublic static <V> V find(java.lang.Object[] node, java.util.Comparator<? super V> comparator, V find)
public static <V> void replaceInSitu(java.lang.Object[] tree, int index, V replace)
public static <V> void replaceInSitu(java.lang.Object[] node, java.util.Comparator<? super V> comparator, V find, V replace)
public static <V> int findIndex(java.lang.Object[] node, java.util.Comparator<? super V> comparator, V find)
Arrays.binarySearch(long[], long)
, as though it were performed on the tree flattened into an arraypublic static <V> V findByIndex(java.lang.Object[] tree, int index)
public static <V> int lowerIndex(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static <V> V lower(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static <V> int floorIndex(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static <V> V floor(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static <V> int higherIndex(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static <V> V higher(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static <V> int ceilIndex(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static <V> V ceil(java.lang.Object[] btree, java.util.Comparator<? super V> comparator, V find)
public static int size(java.lang.Object[] tree)
public static long sizeOfStructureOnHeap(java.lang.Object[] tree)
public static boolean isLeaf(java.lang.Object[] node)
true
if the provided node is a leaf, false
if it is a branch.public static boolean isEmpty(java.lang.Object[] tree)
public static int depth(java.lang.Object[] tree)
public static int toArray(java.lang.Object[] tree, java.lang.Object[] target, int targetOffset)
tree
- sourcetarget
- arraytargetOffset
- offset in target arraypublic static int toArray(java.lang.Object[] tree, int treeStart, int treeEnd, java.lang.Object[] target, int targetOffset)
public static <I,I2,O> java.lang.Object[] transformAndFilter(java.lang.Object[] tree, java.util.function.BiFunction<? super I,? super I2,? extends O> apply, I2 param)
If no modifications are made, the original is returned.
NOTE: codewise *identical* to transformAndFilter(Object[], Function)
public static <I,O> java.lang.Object[] transformAndFilter(java.lang.Object[] tree, java.util.function.Function<? super I,? extends O> apply)
If no modifications are made, the original is returned.
An efficient transformAndFilter implementation suitable for a tree consisting of a single leaf root
NOTE: codewise *identical* to transformAndFilter(Object[], BiFunction, Object)
public static <I,O> java.lang.Object[] transform(java.lang.Object[] tree, java.util.function.Function<? super I,? extends O> function)
If no modifications are made, the original is returned.
public static boolean equals(java.lang.Object[] a, java.lang.Object[] b)
public static int hashCode(java.lang.Object[] btree)
public static java.lang.String toString(java.lang.Object[] btree)
public static int treeIndexOfKey(java.lang.Object[] root, int keyIndex)
This version of the method permits requesting out-of-bounds indexes, -1 and size
root
- to calculate tree index withinkeyIndex
- root-local index of key to calculate tree-indexpublic static int treeIndexOfLeafKey(int keyIndex)
keyIndex
- node-local index of the key to calculate index ofpublic static int treeIndexOfBranchKey(java.lang.Object[] root, int keyIndex)
root
- to calculate tree-index withinkeyIndex
- root-local index of key to calculate tree-index ofpublic static int treeIndexOffsetOfChild(java.lang.Object[] root, int childIndex)
root
- to calculate tree-index withinchildIndex
- root-local index of *child* to calculate tree-index ofpublic static <V> BTree.Builder<V> builder(java.util.Comparator<? super V> comparator)
public static <V> BTree.Builder<V> builder(java.util.Comparator<? super V> comparator, int initialCapacity)
public static <V,A> void applyLeaf(java.lang.Object[] btree, java.util.function.BiConsumer<A,V> function, A argument)
public static <V,A> void apply(java.lang.Object[] btree, java.util.function.BiConsumer<A,V> function, A argument)
Private method
btree
- function
- public static <V> void apply(java.lang.Object[] btree, java.util.function.Consumer<V> function)
Private method
btree
- function
- public static <V,A> long accumulate(java.lang.Object[] btree, BiLongAccumulator<A,V> accumulator, A arg, java.util.Comparator<V> comparator, V from, long initialValue)
If the optional from argument is not null, iteration will start from that value (or the one after it's insertion point if an exact match isn't found)
public static <V> long accumulate(java.lang.Object[] btree, LongAccumulator<V> accumulator, java.util.Comparator<V> comparator, V from, long initialValue)
public static <V> long accumulate(java.lang.Object[] btree, LongAccumulator<V> accumulator, long initialValue)
public static <V,A> long accumulate(java.lang.Object[] btree, BiLongAccumulator<A,V> accumulator, A arg, long initialValue)
public static int height(java.lang.Object[] tree)
tree
public static long sizeOnHeapOf(java.lang.Object[] tree)
public static boolean isWellFormed(java.lang.Object[] btree, java.util.Comparator<?> cmp)
public static <V> BTree.FastBuilder<V> fastBuilder()
Can be used with reverseInSitu(java.lang.Object[])
to build a tree in reverse.
Copyright © 2009- The Apache Software Foundation