in protonj2/src/main/java/org/apache/qpid/protonj2/engine/util/SplayMap.java [630:727]
private static <E> SplayedEntry<E> splay(SplayedEntry<E> root, int key) {
if (root == null || root.key == key) {
return root;
}
SplayedEntry<E> lessThanKeyRoot = null;
SplayedEntry<E> lessThanKeyNode = null;
SplayedEntry<E> greaterThanKeyRoot = null;
SplayedEntry<E> greaterThanKeyNode = null;
while (true) {
if (compare(key, root.key) < 0) {
// Entry must be to the left of the current node so we bring that up
// and then work from there to see if we can find the key
if (root.left != null && compare(key, root.left.key) < 0) {
root = rightRotate(root);
}
// Is there nowhere else to go, if so we are done.
if (root.left == null) {
break;
}
// Haven't found it yet but we now know the current element is greater
// than the element we are looking for so it goes to the right tree.
if (greaterThanKeyRoot == null) {
greaterThanKeyRoot = greaterThanKeyNode = root;
} else {
greaterThanKeyNode.left = root;
greaterThanKeyNode.left.parent = greaterThanKeyNode;
greaterThanKeyNode = root;
}
root = root.left;
root.parent = null;
} else if (compare(key, root.key) > 0) {
// Entry must be to the right of the current node so we bring that up
// and then work from there to see if we can find the key
if (root.right != null && compare(key, root.right.key) > 0) {
root = leftRotate(root);
}
// Is there nowhere else to go, if so we are done.
if (root.right == null) {
break;
}
// Haven't found it yet but we now know the current element is less
// than the element we are looking for so it goes to the left tree.
if (lessThanKeyRoot == null) {
lessThanKeyRoot = lessThanKeyNode = root;
} else {
lessThanKeyNode.right = root;
lessThanKeyNode.right.parent = lessThanKeyNode;
lessThanKeyNode = root;
}
root = root.right;
root.parent = null;
} else {
break; // Found it
}
}
// Reassemble the tree from the left, right and middle the assembled nodes in the
// left and right should have their last element either nulled out or linked to the
// remaining items middle tree
if (lessThanKeyRoot == null) {
lessThanKeyRoot = root.left;
} else {
lessThanKeyNode.right = root.left;
if (lessThanKeyNode.right != null) {
lessThanKeyNode.right.parent = lessThanKeyNode;
}
}
if (greaterThanKeyRoot == null) {
greaterThanKeyRoot = root.right;
} else {
greaterThanKeyNode.left = root.right;
if (greaterThanKeyNode.left != null) {
greaterThanKeyNode.left.parent = greaterThanKeyNode;
}
}
// The found or last accessed element is now rooted to the splayed
// left and right trees and returned as the new tree.
root.left = lessThanKeyRoot;
if (root.left != null) {
root.left.parent = root;
}
root.right = greaterThanKeyRoot;
if (root.right != null) {
root.right.parent = root;
}
return root;
}