Top Description Inners Fields Constructors Methods
org.pcollections

pack-priv final Class KVTree<K, V>

extends Object
implements Map.Entry<K, V>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Map.Entry
Type Parameters
<K>
the key type; serves as the key type for TreePMap, and as the element type for TreePSet. This class provides various methods that maintain the ordering and distinctness of keys based on a client-provided instance of Comparator<? super K>.
<V>
the value type; serves as the value type for TreePMap. (Is ignored by TreePSet.)
Imports
java.io.Serializable, java.util.ArrayList, .Comparator, .Iterator, .Map, .NoSuchElementException, .Objects

A persistent (immutable, purely functional) balanced-binary-tree implementation, with such functionality as is needed to support implementations of PSortedMap and PSortedSet, namely TreePMap and TreePSet. Each instance of this class is both a (sub)tree (with a host of methods for examining and manipulating that tree) and the node at the root of that (sub)tree (implementing V> and providing methods getKey() and getValue() to retrieve the mapping at the node). Method Javadoc refers to 'this node' or 'this tree' as appropriate.

All operations are guaranteed to complete within O(log n) time. A complete iteration pass over entryIterator(boolean) completes in O(n) time. A few operations -- namely getKey, getValue, isEmpty, orNullIfEmpty, and size -- complete in O(1) time.

Author
Ran Ari-Gur
Since
3.2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
KVTree.EntryIterator<K, V>

An iterator over the mappings of a KVTree.

private static enum
KVTree.IteratorType

Whether an iterator returns entries or just keys.

pack-priv static enum

Field Summary

Modifier and TypeField and Description
private static final KVTree<?, ?>
EMPTY

The empty tree / leaf node.

private final int
height

The height of this tree: 0 if this tree is empty, otherwise 1 + max(left.height, right.height).

private final K
private final KVTree<K, V>
private final KVTree<K, V>
private static final long
private final int
private final V

Constructor Summary

AccessConstructor and Description
private
KVTree()

Constructor for the empty tree / leaf node EMPTY.

private
KVTree(final KVTree<K, V> left, final K key, final V value, final KVTree<K, V> right)

Constructor for a non-empty/non-leaf node.

Method Summary

Modifier and TypeMethod and Description
private void
private static <K, V> KVTree<K, V>
concat(final KVTree<K, V> left, final KVTree<K, V> right)

pack-priv static <K, V> KVTree<K, V>
pack-priv Iterator<Map.Entry<K, V>>

Returns:

An iterator over the mappings in this tree in the specified direction.
entryIterator
(final boolean
- True if to iterate from left to right; false for right to left.
isLeftToRight
)

Creates an iterator over the mappings in this tree.

public boolean
equals(final Object
object to be compared for equality with this map entry
o
)

Overrides java.lang.Object.equals.

Implements java.util.Map.Entry.equals.

Implements equals(...) as specified by Map.Entry.

pack-priv static <K, V> KVTree<K, V>
fromEntryIterator(final Iterator<? extends Map.Entry<? extends K, ? extends V>> iterator)

private static <K, V> KVTree<K, V>
fromIterator(final Iterator<?> iterator, final KVTree.IteratorType iteratorType, final int maxHeight)

pack-priv static <K, V> KVTree<K, V>
fromKeyIterator(final Iterator<? extends K> iterator)

public K

Returns:

This node's key, or null if this node is the root of the empty tree.
getKey
()

Implements java.util.Map.Entry.getKey.

Returns the key corresponding to this entry.

pack-priv KVTree<K, V>

Returns:

The leftmost non-empty node in this tree.
getLeftmost
()

pack-priv KVTree<K, V>

Returns:

The rightmost non-empty node in this tree.
getRightmost
()

public V

Returns:

This node's value (which may be null), or null if this node is the root of the empty tree.
getValue
()

Implements java.util.Map.Entry.getValue.

Returns the value corresponding to this entry.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.Entry.hashCode.

implements hashCode() as specified by Map.Entry

pack-priv boolean

Returns:

Whether this tree contains any mappings (i.e., whether its size is 0).
isEmpty
()

private static <K, V> KVTree<K, V>

Returns:

A tree containing the specified mappings in the specified order.
join
(final KVTree<K, V> left, final K key, final V value, final KVTree<K, V> right)

Creates a tree consisting of all the mappings of 'left', in order, followed by a mapping from 'key' to 'value', followed by all the mappings of 'right', in order.

pack-priv KVTree<K, V>
minus(final K key, final Comparator<? super K> comparator)

pack-priv KVTree<K, V>

Returns:

A tree with the same mappings as this one, minus the leftmost.
minusLeftmost
()

pack-priv KVTree<K, V>

Returns:

A tree with the same mappings as this one, minus the rightmost.
minusRightmost
()

pack-priv Map.Entry<K, V>

Returns:

This node, or null if this node is the root of the empty tree.
orNullIfEmpty
()

pack-priv KVTree<K, V>
plus(final K key, final V value, final Comparator<? super K> comparator)

pack-priv KVTree<K, V>
range(final K leftBound, final boolean isLeftBoundInclusive, final K rightBound, final boolean isRightBoundInclusive, final Comparator<? super K> comparator)

pack-priv KVTree<K, V>
rangeToLeft(final K rightBound, final boolean isRightBoundInclusive, final Comparator<? super K> comparator)

pack-priv KVTree<K, V>
rangeToRight(final K leftBound, final boolean isleftBoundInclusive, final Comparator<? super K> comparator)

private Object
private KVTree<K, V>
replaceLeft(final KVTree<K, V> newLeft)

private KVTree<K, V>
replaceRight(final KVTree<K, V> newRight)

private KVTree<K, V>
replaceValue(final V newValue)

pack-priv KVTree<K, V>
search(final K key, final Comparator<? super K> comparator, final KVTree.SearchType searchType)

public V
setValue(final V
new value to be stored in this entry
value
)

Implements java.util.Map.Entry.setValue.

Deprecated Unsupported operation.

Replaces the value corresponding to this entry with the specified value (optional operation).

pack-priv int

Returns:

The number of mappings in this tree.
size
()

public String
toString()

Overrides java.lang.Object.toString.

implements toString() in a form expected for an implementation of Map.Entry, namely "KEY=VALUE" (with no information about the presence or absence of child nodes).

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

EMPTYback to summary
private static final KVTree<?, ?> EMPTY

The empty tree / leaf node. Access via empty().

heightback to summary
private final int height

The height of this tree: 0 if this tree is empty, otherwise 1 + max(left.height, right.height).

keyback to summary
private final K key
leftback to summary
private final KVTree<K, V> left
serialVersionUIDback to summary
private static final long serialVersionUID
sizeback to summary
private final int size
valueback to summary
private final V value

Constructor Detail

KVTreeback to summary
private KVTree()

Constructor for the empty tree / leaf node EMPTY.

KVTreeback to summary
private KVTree(final KVTree<K, V> left, final K key, final V value, final KVTree<K, V> right)

Constructor for a non-empty/non-leaf node. Only intended to be called via join(), which takes the same parameters but also handles rebalancing if needed. (This constructor just throws an exception if 'left' and 'right' have mismatched heights.)

Method Detail

checkNotEmptyback to summary
private void checkNotEmpty()
concatback to summary
private static <K, V> KVTree<K, V> concat(final KVTree<K, V> left, final KVTree<K, V> right)
emptyback to summary
pack-priv static <K, V> KVTree<K, V> empty()
Annotations
@SuppressWarnings:unchecked
entryIteratorback to summary
pack-priv Iterator<Map.Entry<K, V>> entryIterator(final boolean isLeftToRight)

Creates an iterator over the mappings in this tree.

Parameters
isLeftToRight:boolean

- True if to iterate from left to right; false for right to left.

Returns:Iterator<Map.Entry<K, V>>

An iterator over the mappings in this tree in the specified direction.

equalsback to summary
public boolean equals(final Object o)

Overrides java.lang.Object.equals.

Implements java.util.Map.Entry.equals.

Implements equals(...) as specified by Map.Entry.

Parameters
o:Object

Doc from java.util.Map.Entry.equals.

object to be compared for equality with this map entry

Returns:boolean

Doc from java.util.Map.Entry.equals.

true if the specified object is equal to this map entry

Annotations
@Override
fromEntryIteratorback to summary
pack-priv static <K, V> KVTree<K, V> fromEntryIterator(final Iterator<? extends Map.Entry<? extends K, ? extends V>> iterator)
fromIteratorback to summary
private static <K, V> KVTree<K, V> fromIterator(final Iterator<?> iterator, final KVTree.IteratorType iteratorType, final int maxHeight)
fromKeyIteratorback to summary
pack-priv static <K, V> KVTree<K, V> fromKeyIterator(final Iterator<? extends K> iterator)
getKeyback to summary
public K getKey()

Implements java.util.Map.Entry.getKey.

Doc from java.util.Map.Entry.getKey.

Returns the key corresponding to this entry.

Returns:K

This node's key, or null if this node is the root of the empty tree.

Annotations
@Override
getLeftmostback to summary
pack-priv KVTree<K, V> getLeftmost()
Returns:KVTree<K, V>

The leftmost non-empty node in this tree.

Exceptions
NoSuchElementException:
if this tree is empty.
getRightmostback to summary
pack-priv KVTree<K, V> getRightmost()
Returns:KVTree<K, V>

The rightmost non-empty node in this tree.

Exceptions
NoSuchElementException:
if this tree is empty.
getValueback to summary
public V getValue()

Implements java.util.Map.Entry.getValue.

Doc from java.util.Map.Entry.getValue.

Returns the value corresponding to this entry. If the mapping has been removed from the backing map (by the iterator's remove operation), the results of this call are undefined.

Returns:V

This node's value (which may be null), or null if this node is the root of the empty tree.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.Entry.hashCode.

implements hashCode() as specified by Map.Entry

Returns:int

Doc from java.util.Map.Entry.hashCode.

the hash code value for this map entry

Annotations
@Override
isEmptyback to summary
pack-priv boolean isEmpty()
Returns:boolean

Whether this tree contains any mappings (i.e., whether its size is 0).

joinback to summary
private static <K, V> KVTree<K, V> join(final KVTree<K, V> left, final K key, final V value, final KVTree<K, V> right)

Creates a tree consisting of all the mappings of 'left', in order, followed by a mapping from 'key' to 'value', followed by all the mappings of 'right', in order. Handles any necessary rebalancing if 'left' and 'right' have mismatched heights. (The intention is that this method be the only code that calls KVTree(KVTree, K, V, KVTree) directly; all other methods should delegate to this one.)

Requires time proportional to the difference in heights between 'left' and 'right', which is in O(log(max(left.size, right.size))) = O(log(left.size + right.size)).

The height of the returned tree is guaranteed to be max(left.height, right.height) plus either zero or one. (This is important in ensuring the time guarantees of this method and of methods that call it.)

Returns:KVTree<K, V>

A tree containing the specified mappings in the specified order.

minusback to summary
pack-priv KVTree<K, V> minus(final K key, final Comparator<? super K> comparator)
minusLeftmostback to summary
pack-priv KVTree<K, V> minusLeftmost()
Returns:KVTree<K, V>

A tree with the same mappings as this one, minus the leftmost.

Exceptions
NoSuchElementException:
if this tree is empty.
minusRightmostback to summary
pack-priv KVTree<K, V> minusRightmost()
Returns:KVTree<K, V>

A tree with the same mappings as this one, minus the rightmost.

Exceptions
NoSuchElementException:
if this tree is empty.
orNullIfEmptyback to summary
pack-priv Map.Entry<K, V> orNullIfEmpty()
Returns:Map.Entry<K, V>

This node, or null if this node is the root of the empty tree.

plusback to summary
pack-priv KVTree<K, V> plus(final K key, final V value, final Comparator<? super K> comparator)
rangeback to summary
pack-priv KVTree<K, V> range(final K leftBound, final boolean isLeftBoundInclusive, final K rightBound, final boolean isRightBoundInclusive, final Comparator<? super K> comparator)
rangeToLeftback to summary
pack-priv KVTree<K, V> rangeToLeft(final K rightBound, final boolean isRightBoundInclusive, final Comparator<? super K> comparator)
rangeToRightback to summary
pack-priv KVTree<K, V> rangeToRight(final K leftBound, final boolean isleftBoundInclusive, final Comparator<? super K> comparator)
readResolveback to summary
private Object readResolve()
replaceLeftback to summary
private KVTree<K, V> replaceLeft(final KVTree<K, V> newLeft)
replaceRightback to summary
private KVTree<K, V> replaceRight(final KVTree<K, V> newRight)
replaceValueback to summary
private KVTree<K, V> replaceValue(final V newValue)
searchback to summary
pack-priv KVTree<K, V> search(final K key, final Comparator<? super K> comparator, final KVTree.SearchType searchType)
setValueback to summary
public V setValue(final V value)

Implements java.util.Map.Entry.setValue.

Doc from java.util.Map.Entry.setValue.

Deprecated

Unsupported operation.

Replaces the value corresponding to this entry with the specified value (optional operation). (Writes through to the map.) The behavior of this call is undefined if the mapping has already been removed from the map (by the iterator's remove operation).

Parameters
value:V

new value to be stored in this entry

Returns:V

old value corresponding to the entry

Annotations
@Deprecated
@Override
Exceptions
UnsupportedOperationException:
always
sizeback to summary
pack-priv int size()
Returns:int

The number of mappings in this tree.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

implements toString() in a form expected for an implementation of Map.Entry, namely "KEY=VALUE" (with no information about the presence or absence of child nodes).

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
org.pcollections back to summary

private Class KVTree.EntryIterator<K, V>

extends Object
implements Iterator<Map.Entry<K, V>>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

An iterator over the mappings of a KVTree.

Field Summary

Modifier and TypeField and Description
private final boolean
isLeftToRight

whether we're iterating from left to right (vs. right to left)

private KVTree<K, V>
nextSubtree

node that we need to enter

private final ArrayList<KVTree<K, V>>
stack

nodes that we've recursed into, but not yet returned

Constructor Summary

AccessConstructor and Description
pack-priv
EntryIterator(final KVTree<K, V> tree, final boolean isLeftToRight)

Method Summary

Modifier and TypeMethod and Description
private KVTree<K, V>
firstChild(final KVTree<K, V> node)

public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public Map.Entry<K, V>
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

private KVTree<K, V>
secondChild(final KVTree<K, V> node)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

isLeftToRightback to summary
private final boolean isLeftToRight

whether we're iterating from left to right (vs. right to left)

nextSubtreeback to summary
private KVTree<K, V> nextSubtree

node that we need to enter

stackback to summary
private final ArrayList<KVTree<K, V>> stack

nodes that we've recursed into, but not yet returned

Constructor Detail

EntryIteratorback to summary
pack-priv EntryIterator(final KVTree<K, V> tree, final boolean isLeftToRight)

Method Detail

firstChildback to summary
private KVTree<K, V> firstChild(final KVTree<K, V> node)
hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public Map.Entry<K, V> next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:Map.Entry<K, V>

the next element in the iteration

Annotations
@Override
secondChildback to summary
private KVTree<K, V> secondChild(final KVTree<K, V> node)
org.pcollections back to summary

private final Enum KVTree.IteratorType

extends Enum<KVTree.IteratorType>
Class Inheritance

Whether an iterator returns entries or just keys. (This is a bit of a hack to let fromEntryIterator(Iterator) and fromKeyIterator(Iterator) share code. A cleaner alternative would be to wrap the key iterator in an entry iterator; but that would create lots of unnecessary Map.Entry instances.)

Field Summary

Modifier and TypeField and Description
public static final KVTree.IteratorType
public static final KVTree.IteratorType

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static KVTree.IteratorType
public static KVTree.IteratorType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ENTRYback to summary
public static final KVTree.IteratorType ENTRY
KEYback to summary
public static final KVTree.IteratorType KEY

Constructor Detail

IteratorTypeback to summary
private IteratorType()

Method Detail

valueOfback to summary
public static KVTree.IteratorType valueOf(String name)
valuesback to summary
public static KVTree.IteratorType[] values()
org.pcollections back to summary

pack-priv final Enum KVTree.SearchType

extends Enum<KVTree.SearchType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final KVTree.SearchType
public static final KVTree.SearchType
public static final KVTree.SearchType
public static final KVTree.SearchType
public static final KVTree.SearchType

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static KVTree.SearchType
public static KVTree.SearchType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

EQback to summary
public static final KVTree.SearchType EQ
GEback to summary
public static final KVTree.SearchType GE
GTback to summary
public static final KVTree.SearchType GT
LEback to summary
public static final KVTree.SearchType LE
LTback to summary
public static final KVTree.SearchType LT

Constructor Detail

SearchTypeback to summary
private SearchType()

Method Detail

valueOfback to summary
public static KVTree.SearchType valueOf(String name)
valuesback to summary
public static KVTree.SearchType[] values()