Package org.kingdoms.utils.internal
Class UnsafeHashMap.TreeNode<K,V>
java.lang.Object
org.kingdoms.utils.internal.UnsafeHashMap.Node<K,V>
org.kingdoms.utils.internal.UnsafeHashMap.Entry<K,V>
org.kingdoms.utils.internal.UnsafeHashMap.TreeNode<K,V>
- All Implemented Interfaces:
Map.Entry<K,V>
- Enclosing class:
- UnsafeHashMap<K,
V>
Entry for Tree bins. Extends LinkedHashMap.Entry (which in turn
extends Node) so can be used as extension of either regular or
linked node.
-
Field Summary
Fields inherited from class org.kingdoms.utils.internal.UnsafeHashMap.Node
hash, key, next, value -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> UnsafeHashMap.TreeNode<K, V> balanceDeletion(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> x) static <K,V> UnsafeHashMap.TreeNode<K, V> balanceInsertion(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> x) static <K,V> boolean Recursive invariant checkfinal UnsafeHashMap.TreeNode<K,V> Finds the node starting at root p with the given hash and key.final UnsafeHashMap.TreeNode<K,V> getTreeNode(int h, Object k) Calls find for root node.static <K,V> void moveRootToFront(UnsafeHashMap.Node<K, V>[] tab, UnsafeHashMap.TreeNode<K, V> root) Ensures that the given root is the first node of its bin.final @Nullable UnsafeHashMap.TreeNode<K,V> putTreeVal(@NonNull UnsafeHashMap<K, V> map, @NonNull UnsafeHashMap.Node<K, V>[] tab, int h, @NonNull K k, @Nullable V v) Tree version of putVal.final voidremoveTreeNode(UnsafeHashMap<K, V> map, UnsafeHashMap.Node<K, V>[] tab, boolean movable) Removes the given node, that must be present before this call.final UnsafeHashMap.TreeNode<K,V> root()Returns root of tree containing this node.static <K,V> UnsafeHashMap.TreeNode<K, V> rotateLeft(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> p) static <K,V> UnsafeHashMap.TreeNode<K, V> rotateRight(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> p) final voidsplit(UnsafeHashMap<K, V> map, UnsafeHashMap.Node<K, V>[] tab, int index, int bit) Splits nodes in a tree bin into lower and upper tree bins, or untreeifies if now too small.static inttieBreakOrder(Object a, Object b) Tie-breaking utility for ordering insertions when equal hashCodes and non-comparable.final voidtreeify(UnsafeHashMap.Node<K, V>[] tab) Forms tree of the nodes linked from this node.final UnsafeHashMap.Node<K,V> untreeify(UnsafeHashMap<K, V> map) Returns a list of non-TreeNodes replacing those linked from this node.
-
Method Details
-
moveRootToFront
public static <K,V> void moveRootToFront(UnsafeHashMap.Node<K, V>[] tab, UnsafeHashMap.TreeNode<K, V> root) Ensures that the given root is the first node of its bin. -
tieBreakOrder
Tie-breaking utility for ordering insertions when equal hashCodes and non-comparable. We don't require a total order, just a consistent insertion rule to maintain equivalence across rebalancings. Tie-breaking further than necessary simplifies testing a bit. -
rotateLeft
public static <K,V> UnsafeHashMap.TreeNode<K,V> rotateLeft(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> p) -
rotateRight
public static <K,V> UnsafeHashMap.TreeNode<K,V> rotateRight(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> p) -
balanceInsertion
public static <K,V> UnsafeHashMap.TreeNode<K,V> balanceInsertion(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> x) -
balanceDeletion
public static <K,V> UnsafeHashMap.TreeNode<K,V> balanceDeletion(UnsafeHashMap.TreeNode<K, V> root, UnsafeHashMap.TreeNode<K, V> x) -
checkInvariants
Recursive invariant check -
root
Returns root of tree containing this node. -
find
Finds the node starting at root p with the given hash and key. The kc argument caches comparableClassFor(key) upon first use comparing keys. -
getTreeNode
Calls find for root node. -
treeify
Forms tree of the nodes linked from this node. return root of tree -
untreeify
Returns a list of non-TreeNodes replacing those linked from this node. -
putTreeVal
public final @Nullable UnsafeHashMap.TreeNode<K,V> putTreeVal(@NonNull UnsafeHashMap<K, V> map, @NonNull UnsafeHashMap.Node<K, V>[] tab, int h, @NonNull K k, @Nullable V v) Tree version of putVal. -
removeTreeNode
public final void removeTreeNode(UnsafeHashMap<K, V> map, UnsafeHashMap.Node<K, V>[] tab, boolean movable) Removes the given node, that must be present before this call. This is messier than typical red-black deletion code because we cannot swap the contents of an interior node with a leaf successor that is pinned by "next" pointers that are accessible independently during traversal. So instead we swap the tree linkages. If the current tree appears to have too few nodes, the bin is converted back to a plain bin. (The test triggers somewhere between 2 and 6 nodes, depending on tree structure). -
split
Splits nodes in a tree bin into lower and upper tree bins, or untreeifies if now too small. Called only from resize; see above discussion about split bits and indices.- Parameters:
map- the maptab- the table for recording bin headsindex- the index of the table being splitbit- the bit of hash to split on
-