/** Adds mapping from key to obj and returns the node containing that mapping. */ @Override public Node<K, O> addTraverse(K newKey, O newObj) { if (newKey.compareTo(this.key) < 0) { leftNode = leftNode.addTraverse(newKey, newObj); } else if (newKey.compareTo(this.key) > 0) { rightNode = rightNode.addTraverse(newKey, newObj); } else { obj = newObj; } return this; }
protected V get(NODE node, K key) { if (node == null) return null; int cmp = key.compareTo(node.key); if (cmp < 0) return get(node.left, key); else if (cmp > 0) return get(node.right, key); else return node.value; }
private V get(K key, Node<K, V> node) { if (node == null) return null; int cmp = key.compareTo(node.key); if (cmp == 0) return node.value; else if (cmp < 0) return get(key, node.left); else return get(key, node.right); }
protected int rank(NODE node, K key) { if (node == null) return 0; int cmp = key.compareTo(node.key); if (cmp < 0) { return rank(node.left, key); } else if (cmp > 0) { return rank(node.right, key) + size(node.left) + 1; } else { return size(node.left); } }
@SuppressWarnings("unchecked") @Override public int compareTo(final IDLTerm<I, L, K, R> o) { final int compare = getDLTermOrder().compareTo(o); if (compare == 0) { assert o instanceof IDLClassReference; return _klass.compareTo((K) (((IDLClassReference) o).getElement())); } else { return compare; } }
protected NODE ceiling(NODE node, K key) { if (node == null) return null; int cmp = key.compareTo(node.key); if (cmp < 0) { NODE x = ceiling(node.left, key); return x != null ? x : node; } else if (cmp > 0) { return ceiling(node.right, key); } else { return node; } }
private Node<K, V> put(K key, V val, Node<K, V> node) { if (node == null) return new Node<K, V>(key, val, 1); int cmp = key.compareTo(node.key); if (cmp == 0) { node.value = val; } else if (cmp < 0) { node.left = put(key, val, node.left); } else if (cmp > 0) { node.right = put(key, val, node.right); } node.size = size(node.left) + size(node.right) + 1; return node; }
public void put(K k1, K k2, V value) { if (k1.compareTo(k2) > 0) { K k3 = k1; k1 = k2; k2 = k3; } Map<K, V> l2Map = l1Map.get(k1); if (l2Map == null) { l2Map = new HashMap<K, V>(); l1Map.put(k1, l2Map); } l2Map.put(k2, value); }
V get(K k1, K k2) { if (k1.compareTo(k2) > 0) { K k3 = k1; k1 = k2; k2 = k3; } Map<K, V> l2Map = l1Map.get(k1); if (l2Map == null) { l2Map = new HashMap<K, V>(sizePerDimension); l1Map.put(k1, l2Map); } return l2Map.get(k2); }
protected NODE delete(NODE node, K key) { if (node == null) { // We've reached a leaf. Nothing to delete. return null; } int cmp = key.compareTo(node.key); if (cmp < 0) { // Key is less than the node. Delete in the left subtree. node.left = delete(node.left, key); node.size = size(node.left) + size(node.right) + 1; return node; } else if (cmp > 0) { // Key is greater than the node. Delete in the right subtree. node.right = delete(node.right, key); node.size = size(node.left) + size(node.right) + 1; return node; } else { // Delete the node. if (node.left == null) { // Replace node with its right subtree. return node.right; } else if (node.right == null) { // Replace node with its left subtree. return node.left; } else if (node.right.left == null) { // Replace node with its right subtree. // Move the left subtree to the right node. node.right.left = node.left; node.right.size += size(node.left); return node.right; } else { // Replace node with its successor (Hibbard). NODE s = min(node.right); NODE x = delete(node, s.key); node.key = s.key; node.value = s.value; return x; } } }
protected NODE put(NODE node, K key, V value) { if (node == null) { // We've reached a leaf. Create a new node to store the new value. return createNode(key, value); } int cmp = key.compareTo(node.key); if (cmp < 0) { // Key is less than the node. Update the left subtree. node.left = put(node.left, key, value); node.size = size(node.left) + size(node.right) + 1; return node; } else if (cmp > 0) { // Key is greater than the node. Update the right subtree. node.right = put(node.right, key, value); node.size = size(node.left) + size(node.right) + 1; return node; } else { // Update the value on the node. node.value = value; return node; } }
public int compareTo(Sortable s) { return key.compareTo(s.key); }
public static <K extends Comparable<K>> K max(K x, K y) { return x.compareTo(y) > 0 ? x : y; }
@Override public int compareTo(CountPair<K, V> o) { int vc = o.val.compareTo(val); return (0 != vc ? vc : key.compareTo(o.key)); }
/*@ ensures /result == obj;*/ public O Search(K key) { if (key.compareTo(this.key) < 0) return leftNode.Search(key); else if (key.compareTo(this.key) > 0) return rightNode.Search(key); else return obj; }
public TreeNode<K, V> lookupNode(K k) { TreeNode<K, V> node = null; // cache // if(usecache && !cache.isEmpty() && k.compareTo(cache.firstKey().getMinKey()) <= 0){ // node = cache.get(cache.firstKey()); //TODO: most left cache only possible with double // linked leafs // }else{ if (usecache) { for (Entry<Range<K>, TreeNode<K, V>> e : cache.entrySet()) { if (k.compareTo(e.getKey().getMinKey()) >= 0 && (e.getKey().getMaxKey() == null || k.compareTo(e.getKey().getMaxKey()) < 0)) { node = e.getValue(); break; } } if (node != null) { cachehit++; return node; } } // tree walk treewalk++; node = root; while (true) { if (node.getChildren().isEmpty()) { // fill cache if (usecache && node != root) { Range<K> range; if (node.getNextNode() != null) { range = new Range<K>(node.getMinKey(), node.getNextNode().getMinKey()); } else { range = new Range<K>(node.getMinKey(), null); } cache.put(range, node); } break; // is leaf } else { Iterator<TreeNode<K, V>> i = node.getChildren().iterator(); TreeNode<K, V> n0 = i.next(); if (k.compareTo(n0.getMinKey()) <= 0) { node = n0; // go left continue; } while (i.hasNext()) { TreeNode<K, V> n1 = i .next(); // FIXME: can throw concurrent mod exception (even with fully // synchronized TreeNode) if (k.compareTo(n0.getMinKey()) >= 0 && k.compareTo(n1.getMinKey()) < 0) { node = n0; // inner node break; } n0 = n1; } node = n0; // go right } } return node; }
public int compareTo(final OPair<K, V> o) { return key.compareTo(o.key); }