Ejemplo n.º 1
0
 @Override
 public TSortedMap<K, V> subMap(K fromKey, K toKey) {
   if (comparator.compare(fromKey, toKey) > 0) {
     throw new TIllegalArgumentException();
   }
   return new MapView<>(this, fromKey, true, true, toKey, false, true, false);
 }
Ejemplo n.º 2
0
 public TTreeMap(TMap<? extends K, ? extends V> m) {
   this((TComparator<? super K>) null);
   @SuppressWarnings("unchecked")
   Entry<K, V>[] entries = (Entry<K, V>[]) new Entry<?, ?>[m.size()];
   entries = m.entrySet().toArray(entries);
   TArrays.sort(entries, (o1, o2) -> comparator.compare(o1.getKey(), o2.getKey()));
   fillMap(entries);
 }
Ejemplo n.º 3
0
 private TreeNode<K, V> getOrCreateNode(TreeNode<K, V> root, K key) {
   if (root == null) {
     return new TreeNode<>(key);
   }
   int cmp = comparator.compare(key, root.getKey());
   if (cmp == 0) {
     return root;
   } else if (cmp < 0) {
     root.left = getOrCreateNode(root.left, key);
   } else {
     root.right = getOrCreateNode(root.right, key);
   }
   root.fix();
   return root.balance();
 }
Ejemplo n.º 4
0
 TreeNode<?, V> findExact(Object key) {
   TreeNode<K, V> node = root;
   while (node != null) {
     @SuppressWarnings("unchecked")
     int cmp = comparator.compare((K) key, node.getKey());
     if (cmp == 0) {
       return node;
     } else if (cmp < 0) {
       node = node.left;
     } else {
       node = node.right;
     }
   }
   return null;
 }
Ejemplo n.º 5
0
 TreeNode<K, V> findNext(Object key, boolean reverse) {
   TreeNode<K, V> node = root;
   TreeNode<K, V> lastForward = null;
   while (node != null) {
     @SuppressWarnings("unchecked")
     int cmp = comparator.compare((K) key, node.getKey());
     if (reverse) {
       cmp = -cmp;
     }
     if (cmp < 0) {
       lastForward = node;
       node = node.forward(reverse);
     } else {
       node = node.down(reverse);
     }
   }
   return lastForward;
 }
Ejemplo n.º 6
0
 TreeNode<K, V>[] pathToNext(Object key, boolean reverse) {
   @SuppressWarnings("unchecked")
   TreeNode<K, V>[] path = (TreeNode<K, V>[]) new TreeNode<?, ?>[height()];
   int depth = 0;
   TreeNode<K, V> node = root;
   while (node != null) {
     @SuppressWarnings("unchecked")
     int cmp = comparator.compare((K) key, node.getKey());
     if (reverse) {
       cmp = -cmp;
     }
     if (cmp < 0) {
       path[depth++] = node;
       node = node.forward(reverse);
     } else {
       node = node.down(reverse);
     }
   }
   return TArrays.copyOf(path, depth);
 }
Ejemplo n.º 7
0
 private TreeNode<K, V> deleteNode(TreeNode<K, V> root, Object key) {
   if (root == null) {
     return null;
   }
   @SuppressWarnings("unchecked")
   int cmp = comparator.compare((K) key, root.getKey());
   if (cmp < 0) {
     root.left = deleteNode(root.left, key);
   } else if (cmp > 0) {
     root.right = deleteNode(root.right, key);
   } else if (root.right == null) {
     return root.left;
   } else {
     TreeNode<K, V> left = root.left;
     TreeNode<K, V> right = root.right;
     TreeNode<K, V> min = right;
     @SuppressWarnings("unchecked")
     TreeNode<K, V>[] pathToMin = (TreeNode<K, V>[]) new TreeNode<?, ?>[right.height];
     int minDepth = 0;
     while (min.left != null) {
       pathToMin[minDepth++] = min;
       min = min.left;
     }
     right = min.right;
     while (minDepth > 0) {
       TreeNode<K, V> node = pathToMin[--minDepth];
       node.left = right;
       node.fix();
       node = node.balance();
       right = node;
     }
     min.right = right;
     min.left = left;
     root = min;
     root.fix();
   }
   root.fix();
   return root.balance();
 }
Ejemplo n.º 8
0
 private void ensureRevertedComparator() {
   if (revertedComparator == null) {
     revertedComparator = (o1, o2) -> -originalComparator.compare(o1, o2);
   }
 }