1 /* TreeMap.java -- a class providing a basic Red-Black Tree data structure,
2 mapping Object --> Object
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import java
.io
.IOException
;
43 import java
.io
.ObjectInputStream
;
44 import java
.io
.ObjectOutputStream
;
45 import java
.io
.Serializable
;
48 * This class provides a red-black tree implementation of the SortedMap
49 * interface. Elements in the Map will be sorted by either a user-provided
50 * Comparator object, or by the natural ordering of the keys.
52 * The algorithms are adopted from Corman, Leiserson, and Rivest's
53 * <i>Introduction to Algorithms.</i> TreeMap guarantees O(log n)
54 * insertion and deletion of elements. That being said, there is a large
55 * enough constant coefficient in front of that "log n" (overhead involved
56 * in keeping the tree balanced), that TreeMap may not be the best choice
57 * for small collections. If something is already sorted, you may want to
58 * just use a LinkedHashMap to maintain the order while providing O(1) access.
60 * TreeMap is a part of the JDK1.2 Collections API. Null keys are allowed
61 * only if a Comparator is used which can deal with them; natural ordering
62 * cannot cope with null. Null values are always allowed. Note that the
63 * ordering must be <i>consistent with equals</i> to correctly implement
64 * the Map interface. If this condition is violated, the map is still
65 * well-behaved, but you may have suprising results when comparing it to
68 * This implementation is not synchronized. If you need to share this between
69 * multiple threads, do something like:<br>
71 * = Collections.synchronizedSortedMap(new TreeMap(...));</code><p>
73 * The iterators are <i>fail-fast</i>, meaning that any structural
74 * modification, except for <code>remove()</code> called on the iterator
75 * itself, cause the iterator to throw a
76 * <code>ConcurrentModificationException</code> rather than exhibit
77 * non-deterministic behavior.
79 * @author Jon Zeppieri
80 * @author Bryce McKinlay
81 * @author Eric Blake <ebb9@email.byu.edu>
89 * @see Collections#synchronizedSortedMap(SortedMap)
91 * @status updated to 1.4
93 public class TreeMap
extends AbstractMap
94 implements SortedMap
, Cloneable
, Serializable
96 // Implementation note:
97 // A red-black tree is a binary search tree with the additional properties
98 // that all paths to a leaf node visit the same number of black nodes,
99 // and no red node has red children. To avoid some null-pointer checks,
100 // we use the special node nil which is always black, has no relatives,
101 // and has key and value of null (but is not equal to a mapping of null).
104 * Compatible with JDK 1.2.
106 private static final long serialVersionUID
= 919286545866124006L;
109 * Color status of a node. Package visible for use by nested classes.
111 static final int RED
= -1,
115 * Sentinal node, used to avoid null checks for corner cases and make the
116 * delete rebalance code simpler. The rebalance code must never assign
117 * the parent, left, or right of nil, but may safely reassign the color
118 * to be black. This object must never be used as a key in a TreeMap, or
119 * it will break bounds checking of a SubMap.
121 static final Node nil
= new Node(null, null, BLACK
);
124 // Nil is self-referential, so we must initialize it after creation.
131 * The root node of this TreeMap.
133 private transient Node root
;
136 * The size of this TreeMap. Package visible for use by nested classes.
141 * The cache for {@link #entrySet()}.
143 private transient Set entries
;
146 * Counts the number of modifications this TreeMap has undergone, used
147 * by Iterators to know when to throw ConcurrentModificationExceptions.
148 * Package visible for use by nested classes.
150 transient int modCount
;
153 * This TreeMap's comparator, or null for natural ordering.
154 * Package visible for use by nested classes.
155 * @serial the comparator ordering this tree, or null
157 final Comparator comparator
;
160 * Class to represent an entry in the tree. Holds a single key-value pair,
161 * plus pointers to parent and child nodes.
163 * @author Eric Blake <ebb9@email.byu.edu>
165 private static final class Node
extends AbstractMap
.BasicMapEntry
167 // All fields package visible for use by nested classes.
168 /** The color of this node. */
171 /** The left child node. */
173 /** The right child node. */
175 /** The parent node. */
179 * Simple constructor.
181 * @param value the value
183 Node(Object key
, Object value
, int color
)
191 * Instantiate a new TreeMap with no elements, using the keys' natural
192 * ordering to sort. All entries in the map must have a key which implements
193 * Comparable, and which are <i>mutually comparable</i>, otherwise map
194 * operations may throw a {@link ClassCastException}. Attempts to use
195 * a null key will throw a {@link NullPointerException}.
201 this((Comparator
) null);
205 * Instantiate a new TreeMap with no elements, using the provided comparator
206 * to sort. All entries in the map must have keys which are mutually
207 * comparable by the Comparator, otherwise map operations may throw a
208 * {@link ClassCastException}.
210 * @param comparator the sort order for the keys of this map, or null
211 * for the natural order
213 public TreeMap(Comparator c
)
220 * Instantiate a new TreeMap, initializing it with all of the elements in
221 * the provided Map. The elements will be sorted using the natural
222 * ordering of the keys. This algorithm runs in n*log(n) time. All entries
223 * in the map must have keys which implement Comparable and are mutually
224 * comparable, otherwise map operations may throw a
225 * {@link ClassCastException}.
227 * @param map a Map, whose entries will be put into this TreeMap
228 * @throws ClassCastException if the keys in the provided Map are not
230 * @throws NullPointerException if map is null
233 public TreeMap(Map map
)
235 this((Comparator
) null);
240 * Instantiate a new TreeMap, initializing it with all of the elements in
241 * the provided SortedMap. The elements will be sorted using the same
242 * comparator as in the provided SortedMap. This runs in linear time.
244 * @param sm a SortedMap, whose entries will be put into this TreeMap
245 * @throws NullPointerException if sm is null
247 public TreeMap(SortedMap sm
)
249 this(sm
.comparator());
251 Iterator itr
= sm
.entrySet().iterator();
254 Node node
= firstNode();
258 Map
.Entry me
= (Map
.Entry
) itr
.next();
259 node
.key
= me
.getKey();
260 node
.value
= me
.getValue();
261 node
= successor(node
);
266 * Clears the Map so it has no keys. This is O(1).
279 * Returns a shallow clone of this TreeMap. The Map itself is cloned,
280 * but its contents are not.
284 public Object
clone()
289 copy
= (TreeMap
) super.clone();
291 catch (CloneNotSupportedException x
)
295 copy
.fabricateTree(size
);
297 Node node
= firstNode();
298 Node cnode
= copy
.firstNode();
302 cnode
.key
= node
.key
;
303 cnode
.value
= node
.value
;
304 node
= successor(node
);
305 cnode
= copy
.successor(cnode
);
311 * Return the comparator used to sort this map, or null if it is by
314 * @return the map's comparator
316 public Comparator
comparator()
322 * Returns true if the map contains a mapping for the given key.
324 * @param key the key to look for
325 * @return true if the key has a mapping
326 * @throws ClassCastException if key is not comparable to map elements
327 * @throws NullPointerException if key is null and the comparator is not
330 public boolean containsKey(Object key
)
332 return getNode(key
) != nil
;
336 * Returns true if the map contains at least one mapping to the given value.
337 * This requires linear time.
339 * @param value the value to look for
340 * @return true if the value appears in a mapping
342 public boolean containsValue(Object value
)
344 Node node
= firstNode();
347 if (equals(value
, node
.value
))
349 node
= successor(node
);
355 * Returns a "set view" of this TreeMap's entries. The set is backed by
356 * the TreeMap, so changes in one show up in the other. The set supports
357 * element removal, but not element addition.<p>
359 * Note that the iterators for all three views, from keySet(), entrySet(),
360 * and values(), traverse the TreeMap in sorted sequence.
362 * @return a set view of the entries
367 public Set
entrySet()
370 // Create an AbstractSet with custom implementations of those methods
371 // that can be overriden easily and efficiently.
372 entries
= new AbstractSet()
379 public Iterator
iterator()
381 return new TreeIterator(ENTRIES
);
386 TreeMap
.this.clear();
389 public boolean contains(Object o
)
391 if (! (o
instanceof Map
.Entry
))
393 Map
.Entry me
= (Map
.Entry
) o
;
394 Node n
= getNode(me
.getKey());
395 return n
!= nil
&& AbstractSet
.equals(me
.getValue(), n
.value
);
398 public boolean remove(Object o
)
400 if (! (o
instanceof Map
.Entry
))
402 Map
.Entry me
= (Map
.Entry
) o
;
403 Node n
= getNode(me
.getKey());
404 if (n
!= nil
&& AbstractSet
.equals(me
.getValue(), n
.value
))
416 * Returns the first (lowest) key in the map.
418 * @return the first key
419 * @throws NoSuchElementException if the map is empty
421 public Object
firstKey()
424 throw new NoSuchElementException();
425 return firstNode().key
;
429 * Return the value in this TreeMap associated with the supplied key,
430 * or <code>null</code> if the key maps to nothing. NOTE: Since the value
431 * could also be null, you must use containsKey to see if this key
432 * actually maps to something.
434 * @param key the key for which to fetch an associated value
435 * @return what the key maps to, if present
436 * @throws ClassCastException if key is not comparable to elements in the map
437 * @throws NullPointerException if key is null but the comparator does not
439 * @see #put(Object, Object)
440 * @see #containsKey(Object)
442 public Object
get(Object key
)
444 // Exploit fact that nil.value == null.
445 return getNode(key
).value
;
449 * Returns a view of this Map including all entries with keys less than
450 * <code>toKey</code>. The returned map is backed by the original, so changes
451 * in one appear in the other. The submap will throw an
452 * {@link IllegalArgumentException} for any attempt to access or add an
453 * element beyond the specified cutoff. The returned map does not include
454 * the endpoint; if you want inclusion, pass the successor element.
456 * @param toKey the (exclusive) cutoff point
457 * @return a view of the map less than the cutoff
458 * @throws ClassCastException if <code>toKey</code> is not compatible with
459 * the comparator (or is not Comparable, for natural ordering)
460 * @throws NullPointerException if toKey is null, but the comparator does not
461 * tolerate null elements
463 public SortedMap
headMap(Object toKey
)
465 return new SubMap(nil
, toKey
);
469 * Returns a "set view" of this TreeMap's keys. The set is backed by the
470 * TreeMap, so changes in one show up in the other. The set supports
471 * element removal, but not element addition.
473 * @return a set view of the keys
480 // Create an AbstractSet with custom implementations of those methods
481 // that can be overriden easily and efficiently.
482 keys
= new AbstractSet()
489 public Iterator
iterator()
491 return new TreeIterator(KEYS
);
496 TreeMap
.this.clear();
499 public boolean contains(Object o
)
501 return containsKey(o
);
504 public boolean remove(Object key
)
506 Node n
= getNode(key
);
517 * Returns the last (highest) key in the map.
519 * @return the last key
520 * @throws NoSuchElementException if the map is empty
522 public Object
lastKey()
525 throw new NoSuchElementException("empty");
526 return lastNode().key
;
530 * Puts the supplied value into the Map, mapped by the supplied key.
531 * The value may be retrieved by any object which <code>equals()</code>
532 * this key. NOTE: Since the prior value could also be null, you must
533 * first use containsKey if you want to see if you are replacing the
536 * @param key the key used to locate the value
537 * @param value the value to be stored in the Map
538 * @return the prior mapping of the key, or null if there was none
539 * @throws ClassCastException if key is not comparable to current map keys
540 * @throws NullPointerException if key is null, but the comparator does
543 * @see Object#equals(Object)
545 public Object
put(Object key
, Object value
)
551 // Find new node's parent.
552 while (current
!= nil
)
555 comparison
= compare(key
, current
.key
);
557 current
= current
.right
;
558 else if (comparison
< 0)
559 current
= current
.left
;
560 else // Key already in tree.
561 return current
.setValue(value
);
565 Node n
= new Node(key
, value
, RED
);
568 // Insert node in tree.
573 // Special case inserting into an empty tree.
582 // Rebalance after insert.
588 * Copies all elements of the given map into this TreeMap. If this map
589 * already has a mapping for a key, the new mapping replaces the current
592 * @param m the map to be added
593 * @throws ClassCastException if a key in m is not comparable with keys
595 * @throws NullPointerException if a key in m is null, and the comparator
596 * does not tolerate nulls
598 public void putAll(Map m
)
600 Iterator itr
= m
.entrySet().iterator();
604 Map
.Entry e
= (Map
.Entry
) itr
.next();
605 put(e
.getKey(), e
.getValue());
610 * Removes from the TreeMap and returns the value which is mapped by the
611 * supplied key. If the key maps to nothing, then the TreeMap remains
612 * unchanged, and <code>null</code> is returned. NOTE: Since the value
613 * could also be null, you must use containsKey to see if you are
614 * actually removing a mapping.
616 * @param key the key used to locate the value to remove
617 * @return whatever the key mapped to, if present
618 * @throws ClassCastException if key is not comparable to current map keys
619 * @throws NullPointerException if key is null, but the comparator does
622 public Object
remove(Object key
)
624 Node n
= getNode(key
);
627 // Note: removeNode can alter the contents of n, so save value now.
628 Object result
= n
.value
;
634 * Returns the number of key-value mappings currently in this Map.
644 * Returns a view of this Map including all entries with keys greater or
645 * equal to <code>fromKey</code> and less than <code>toKey</code> (a
646 * half-open interval). The returned map is backed by the original, so
647 * changes in one appear in the other. The submap will throw an
648 * {@link IllegalArgumentException} for any attempt to access or add an
649 * element beyond the specified cutoffs. The returned map includes the low
650 * endpoint but not the high; if you want to reverse this behavior on
651 * either end, pass in the successor element.
653 * @param fromKey the (inclusive) low cutoff point
654 * @param toKey the (exclusive) high cutoff point
655 * @return a view of the map between the cutoffs
656 * @throws ClassCastException if either cutoff is not compatible with
657 * the comparator (or is not Comparable, for natural ordering)
658 * @throws NullPointerException if fromKey or toKey is null, but the
659 * comparator does not tolerate null elements
660 * @throws IllegalArgumentException if fromKey is greater than toKey
662 public SortedMap
subMap(Object fromKey
, Object toKey
)
664 return new SubMap(fromKey
, toKey
);
668 * Returns a view of this Map including all entries with keys greater or
669 * equal to <code>fromKey</code>. The returned map is backed by the
670 * original, so changes in one appear in the other. The submap will throw an
671 * {@link IllegalArgumentException} for any attempt to access or add an
672 * element beyond the specified cutoff. The returned map includes the
673 * endpoint; if you want to exclude it, pass in the successor element.
675 * @param fromKey the (inclusive) low cutoff point
676 * @return a view of the map above the cutoff
677 * @throws ClassCastException if <code>fromKey</code> is not compatible with
678 * the comparator (or is not Comparable, for natural ordering)
679 * @throws NullPointerException if fromKey is null, but the comparator
680 * does not tolerate null elements
682 public SortedMap
tailMap(Object fromKey
)
684 return new SubMap(fromKey
, nil
);
688 * Returns a "collection view" (or "bag view") of this TreeMap's values.
689 * The collection is backed by the TreeMap, so changes in one show up
690 * in the other. The collection supports element removal, but not element
693 * @return a bag view of the values
697 public Collection
values()
700 // We don't bother overriding many of the optional methods, as doing so
701 // wouldn't provide any significant performance advantage.
702 values
= new AbstractCollection()
709 public Iterator
iterator()
711 return new TreeIterator(VALUES
);
716 TreeMap
.this.clear();
723 * Compares two elements by the set comparator, or by natural ordering.
724 * Package visible for use by nested classes.
726 * @param o1 the first object
727 * @param o2 the second object
728 * @throws ClassCastException if o1 and o2 are not mutually comparable,
729 * or are not Comparable with natural ordering
730 * @throws NullPointerException if o1 or o2 is null with natural ordering
732 final int compare(Object o1
, Object o2
)
734 return (comparator
== null
735 ?
((Comparable
) o1
).compareTo(o2
)
736 : comparator
.compare(o1
, o2
));
740 * Maintain red-black balance after deleting a node.
742 * @param node the child of the node just deleted, possibly nil
743 * @param parent the parent of the node just deleted, never nil
745 private void deleteFixup(Node node
, Node parent
)
747 // if (parent == nil)
748 // throw new InternalError();
749 // If a black node has been removed, we need to rebalance to avoid
750 // violating the "same number of black nodes on any path" rule. If
751 // node is red, we can simply recolor it black and all is well.
752 while (node
!= root
&& node
.color
== BLACK
)
754 if (node
== parent
.left
)
756 // Rebalance left side.
757 Node sibling
= parent
.right
;
758 // if (sibling == nil)
759 // throw new InternalError();
760 if (sibling
.color
== RED
)
762 // Case 1: Sibling is red.
763 // Recolor sibling and parent, and rotate parent left.
764 sibling
.color
= BLACK
;
767 sibling
= parent
.right
;
770 if (sibling
.left
.color
== BLACK
&& sibling
.right
.color
== BLACK
)
772 // Case 2: Sibling has no red children.
773 // Recolor sibling, and move to parent.
776 parent
= parent
.parent
;
780 if (sibling
.right
.color
== BLACK
)
782 // Case 3: Sibling has red left child.
783 // Recolor sibling and left child, rotate sibling right.
784 sibling
.left
.color
= BLACK
;
786 rotateRight(sibling
);
787 sibling
= parent
.right
;
789 // Case 4: Sibling has red right child. Recolor sibling,
790 // right child, and parent, and rotate parent left.
791 sibling
.color
= parent
.color
;
792 parent
.color
= BLACK
;
793 sibling
.right
.color
= BLACK
;
795 node
= root
; // Finished.
800 // Symmetric "mirror" of left-side case.
801 Node sibling
= parent
.left
;
802 // if (sibling == nil)
803 // throw new InternalError();
804 if (sibling
.color
== RED
)
806 // Case 1: Sibling is red.
807 // Recolor sibling and parent, and rotate parent right.
808 sibling
.color
= BLACK
;
811 sibling
= parent
.left
;
814 if (sibling
.right
.color
== BLACK
&& sibling
.left
.color
== BLACK
)
816 // Case 2: Sibling has no red children.
817 // Recolor sibling, and move to parent.
820 parent
= parent
.parent
;
824 if (sibling
.left
.color
== BLACK
)
826 // Case 3: Sibling has red right child.
827 // Recolor sibling and right child, rotate sibling left.
828 sibling
.right
.color
= BLACK
;
831 sibling
= parent
.left
;
833 // Case 4: Sibling has red left child. Recolor sibling,
834 // left child, and parent, and rotate parent right.
835 sibling
.color
= parent
.color
;
836 parent
.color
= BLACK
;
837 sibling
.left
.color
= BLACK
;
839 node
= root
; // Finished.
847 * Construct a perfectly balanced tree consisting of n "blank" nodes. This
848 * permits a tree to be generated from pre-sorted input in linear time.
850 * @param count the number of blank nodes, non-negative
852 private void fabricateTree(final int count
)
861 // We color every row of nodes black, except for the overflow nodes.
862 // I believe that this is the optimal arrangement. We construct the tree
863 // in place by temporarily linking each node to the next node in the row,
864 // then updating those links to the children when working on the next row.
866 // Make the root node.
867 root
= new Node(null, null, BLACK
);
872 // Fill each row that is completely full of nodes.
873 for (rowsize
= 2; rowsize
+ rowsize
<= count
; rowsize
<<= 1)
877 for (int i
= 0; i
< rowsize
; i
+= 2)
879 Node left
= new Node(null, null, BLACK
);
880 Node right
= new Node(null, null, BLACK
);
881 left
.parent
= parent
;
883 right
.parent
= parent
;
885 Node next
= parent
.right
;
886 parent
.right
= right
;
895 // Now do the partial final row in red.
896 int overflow
= count
- rowsize
;
899 for (i
= 0; i
< overflow
; i
+= 2)
901 Node left
= new Node(null, null, RED
);
902 Node right
= new Node(null, null, RED
);
903 left
.parent
= parent
;
904 right
.parent
= parent
;
906 Node next
= parent
.right
;
907 parent
.right
= right
;
910 // Add a lone left node if necessary.
911 if (i
- overflow
== 0)
913 Node left
= new Node(null, null, RED
);
914 left
.parent
= parent
;
916 parent
= parent
.right
;
917 left
.parent
.right
= nil
;
919 // Unlink the remaining nodes of the previous row.
920 while (parent
!= nil
)
922 Node next
= parent
.right
;
929 * Returns the first sorted node in the map, or nil if empty. Package
930 * visible for use by nested classes.
932 * @return the first node
934 final Node
firstNode()
936 // Exploit fact that nil.left == nil.
938 while (node
.left
!= nil
)
944 * Return the TreeMap.Node associated with key, or the nil node if no such
945 * node exists in the tree. Package visible for use by nested classes.
947 * @param key the key to search for
948 * @return the node where the key is found, or nil
950 final Node
getNode(Object key
)
953 while (current
!= nil
)
955 int comparison
= compare(key
, current
.key
);
957 current
= current
.right
;
958 else if (comparison
< 0)
959 current
= current
.left
;
967 * Find the "highest" node which is < key. If key is nil, return last
968 * node. Package visible for use by nested classes.
970 * @param key the upper bound, exclusive
971 * @return the previous node
973 final Node
highestLessThan(Object key
)
982 while (current
!= nil
)
985 comparison
= compare(key
, current
.key
);
987 current
= current
.right
;
988 else if (comparison
< 0)
989 current
= current
.left
;
991 return predecessor(last
);
993 return comparison
<= 0 ?
predecessor(last
) : last
;
997 * Maintain red-black balance after inserting a new node.
999 * @param n the newly inserted node
1001 private void insertFixup(Node n
)
1003 // Only need to rebalance when parent is a RED node, and while at least
1004 // 2 levels deep into the tree (ie: node has a grandparent). Remember
1005 // that nil.color == BLACK.
1006 while (n
.parent
.color
== RED
&& n
.parent
.parent
!= nil
)
1008 if (n
.parent
== n
.parent
.parent
.left
)
1010 Node uncle
= n
.parent
.parent
.right
;
1011 // Uncle may be nil, in which case it is BLACK.
1012 if (uncle
.color
== RED
)
1014 // Case 1. Uncle is RED: Change colors of parent, uncle,
1015 // and grandparent, and move n to grandparent.
1016 n
.parent
.color
= BLACK
;
1017 uncle
.color
= BLACK
;
1018 uncle
.parent
.color
= RED
;
1023 if (n
== n
.parent
.right
)
1025 // Case 2. Uncle is BLACK and x is right child.
1026 // Move n to parent, and rotate n left.
1030 // Case 3. Uncle is BLACK and x is left child.
1031 // Recolor parent, grandparent, and rotate grandparent right.
1032 n
.parent
.color
= BLACK
;
1033 n
.parent
.parent
.color
= RED
;
1034 rotateRight(n
.parent
.parent
);
1039 // Mirror image of above code.
1040 Node uncle
= n
.parent
.parent
.left
;
1041 // Uncle may be nil, in which case it is BLACK.
1042 if (uncle
.color
== RED
)
1044 // Case 1. Uncle is RED: Change colors of parent, uncle,
1045 // and grandparent, and move n to grandparent.
1046 n
.parent
.color
= BLACK
;
1047 uncle
.color
= BLACK
;
1048 uncle
.parent
.color
= RED
;
1053 if (n
== n
.parent
.left
)
1055 // Case 2. Uncle is BLACK and x is left child.
1056 // Move n to parent, and rotate n right.
1060 // Case 3. Uncle is BLACK and x is right child.
1061 // Recolor parent, grandparent, and rotate grandparent left.
1062 n
.parent
.color
= BLACK
;
1063 n
.parent
.parent
.color
= RED
;
1064 rotateLeft(n
.parent
.parent
);
1072 * Returns the last sorted node in the map, or nil if empty.
1074 * @return the last node
1076 private Node
lastNode()
1078 // Exploit fact that nil.right == nil.
1080 while (node
.right
!= nil
)
1086 * Find the "lowest" node which is >= key. If key is nil, return either
1087 * nil or the first node, depending on the parameter first.
1088 * Package visible for use by nested classes.
1090 * @param key the lower bound, inclusive
1091 * @param first true to return the first element instead of nil for nil key
1092 * @return the next node
1094 final Node
lowestGreaterThan(Object key
, boolean first
)
1097 return first ?
firstNode() : nil
;
1100 Node current
= root
;
1103 while (current
!= nil
)
1106 comparison
= compare(key
, current
.key
);
1108 current
= current
.right
;
1109 else if (comparison
< 0)
1110 current
= current
.left
;
1114 return comparison
> 0 ?
successor(last
) : last
;
1118 * Return the node preceding the given one, or nil if there isn't one.
1120 * @param node the current node, not nil
1121 * @return the prior node in sorted order
1123 private Node
predecessor(Node node
)
1125 if (node
.left
!= nil
)
1128 while (node
.right
!= nil
)
1133 Node parent
= node
.parent
;
1134 // Exploit fact that nil.left == nil and node is non-nil.
1135 while (node
== parent
.left
)
1138 parent
= node
.parent
;
1144 * Construct a tree from sorted keys in linear time. Package visible for
1147 * @param s the stream to read from
1148 * @param count the number of keys to read
1149 * @param readValue true to read values, false to insert "" as the value
1150 * @throws ClassNotFoundException if the underlying stream fails
1151 * @throws IOException if the underlying stream fails
1152 * @see #readObject(ObjectInputStream)
1153 * @see TreeSet#readObject(ObjectInputStream)
1155 final void putFromObjStream(ObjectInputStream s
, int count
,
1157 throws IOException
, ClassNotFoundException
1159 fabricateTree(count
);
1160 Node node
= firstNode();
1162 while (--count
>= 0)
1164 node
.key
= s
.readObject();
1165 node
.value
= readValues ? s
.readObject() : "";
1166 node
= successor(node
);
1171 * Construct a tree from sorted keys in linear time, with values of "".
1172 * Package visible for use by TreeSet.
1174 * @param keys the iterator over the sorted keys
1175 * @param count the number of nodes to insert
1176 * @see TreeSet#TreeSet(SortedSet)
1178 final void putKeysLinear(Iterator keys
, int count
)
1180 fabricateTree(count
);
1181 Node node
= firstNode();
1183 while (--count
>= 0)
1185 node
.key
= keys
.next();
1187 node
= successor(node
);
1192 * Deserializes this object from the given stream.
1194 * @param s the stream to read from
1195 * @throws ClassNotFoundException if the underlying stream fails
1196 * @throws IOException if the underlying stream fails
1197 * @serialData the <i>size</i> (int), followed by key (Object) and value
1198 * (Object) pairs in sorted order
1200 private void readObject(ObjectInputStream s
)
1201 throws IOException
, ClassNotFoundException
1203 s
.defaultReadObject();
1204 int size
= s
.readInt();
1205 putFromObjStream(s
, size
, true);
1209 * Remove node from tree. This will increment modCount and decrement size.
1210 * Node must exist in the tree. Package visible for use by nested classes.
1212 * @param node the node to remove
1214 final void removeNode(Node node
)
1222 // Find splice, the node at the position to actually remove from the tree.
1223 if (node
.left
== nil
)
1225 // Node to be deleted has 0 or 1 children.
1229 else if (node
.right
== nil
)
1231 // Node to be deleted has 1 child.
1237 // Node has 2 children. Splice is node's predecessor, and we swap
1238 // its contents into node.
1240 while (splice
.right
!= nil
)
1241 splice
= splice
.right
;
1242 child
= splice
.left
;
1243 node
.key
= splice
.key
;
1244 node
.value
= splice
.value
;
1247 // Unlink splice from the tree.
1248 Node parent
= splice
.parent
;
1250 child
.parent
= parent
;
1253 // Special case for 0 or 1 node remaining.
1257 if (splice
== parent
.left
)
1258 parent
.left
= child
;
1260 parent
.right
= child
;
1262 if (splice
.color
== BLACK
)
1263 deleteFixup(child
, parent
);
1267 * Rotate node n to the left.
1269 * @param node the node to rotate
1271 private void rotateLeft(Node node
)
1273 Node child
= node
.right
;
1274 // if (node == nil || child == nil)
1275 // throw new InternalError();
1277 // Establish node.right link.
1278 node
.right
= child
.left
;
1279 if (child
.left
!= nil
)
1280 child
.left
.parent
= node
;
1282 // Establish child->parent link.
1283 child
.parent
= node
.parent
;
1284 if (node
.parent
!= nil
)
1286 if (node
== node
.parent
.left
)
1287 node
.parent
.left
= child
;
1289 node
.parent
.right
= child
;
1294 // Link n and child.
1296 node
.parent
= child
;
1300 * Rotate node n to the right.
1302 * @param node the node to rotate
1304 private void rotateRight(Node node
)
1306 Node child
= node
.left
;
1307 // if (node == nil || child == nil)
1308 // throw new InternalError();
1310 // Establish node.left link.
1311 node
.left
= child
.right
;
1312 if (child
.right
!= nil
)
1313 child
.right
.parent
= node
;
1315 // Establish child->parent link.
1316 child
.parent
= node
.parent
;
1317 if (node
.parent
!= nil
)
1319 if (node
== node
.parent
.right
)
1320 node
.parent
.right
= child
;
1322 node
.parent
.left
= child
;
1327 // Link n and child.
1329 node
.parent
= child
;
1333 * Return the node following the given one, or nil if there isn't one.
1334 * Package visible for use by nested classes.
1336 * @param node the current node, not nil
1337 * @return the next node in sorted order
1339 final Node
successor(Node node
)
1341 if (node
.right
!= nil
)
1344 while (node
.left
!= nil
)
1349 Node parent
= node
.parent
;
1350 // Exploit fact that nil.right == nil and node is non-nil.
1351 while (node
== parent
.right
)
1354 parent
= parent
.parent
;
1360 * Serializes this object to the given stream.
1362 * @param s the stream to write to
1363 * @throws IOException if the underlying stream fails
1364 * @serialData the <i>size</i> (int), followed by key (Object) and value
1365 * (Object) pairs in sorted order
1367 private void writeObject(ObjectOutputStream s
) throws IOException
1369 s
.defaultWriteObject();
1371 Node node
= firstNode();
1375 s
.writeObject(node
.key
);
1376 s
.writeObject(node
.value
);
1377 node
= successor(node
);
1382 * Iterate over TreeMap's entries. This implementation is parameterized
1383 * to give a sequential view of keys, values, or entries.
1385 * @author Eric Blake <ebb9@email.byu.edu>
1387 private final class TreeIterator
implements Iterator
1390 * The type of this Iterator: {@link #KEYS}, {@link #VALUES},
1391 * or {@link #ENTRIES}.
1393 private final int type
;
1394 /** The number of modifications to the backing Map that we know about. */
1395 private int knownMod
= modCount
;
1396 /** The last Entry returned by a next() call. */
1398 /** The next entry that should be returned by next(). */
1401 * The last node visible to this iterator. This is used when iterating
1404 private final Node max
;
1407 * Construct a new TreeIterator with the supplied type.
1408 * @param type {@link #KEYS}, {@link #VALUES}, or {@link #ENTRIES}
1410 TreeIterator(int type
)
1412 // FIXME gcj cannot handle this. Bug java/4695
1413 // this(type, firstNode(), nil);
1415 this.next
= firstNode();
1420 * Construct a new TreeIterator with the supplied type. Iteration will
1421 * be from "first" (inclusive) to "max" (exclusive).
1423 * @param type {@link #KEYS}, {@link #VALUES}, or {@link #ENTRIES}
1424 * @param first where to start iteration, nil for empty iterator
1425 * @param max the cutoff for iteration, nil for all remaining nodes
1427 TreeIterator(int type
, Node first
, Node max
)
1435 * Returns true if the Iterator has more elements.
1436 * @return true if there are more elements
1437 * @throws ConcurrentModificationException if the TreeMap was modified
1439 public boolean hasNext()
1441 if (knownMod
!= modCount
)
1442 throw new ConcurrentModificationException();
1447 * Returns the next element in the Iterator's sequential view.
1448 * @return the next element
1449 * @throws ConcurrentModificationException if the TreeMap was modified
1450 * @throws NoSuchElementException if there is none
1452 public Object
next()
1454 if (knownMod
!= modCount
)
1455 throw new ConcurrentModificationException();
1457 throw new NoSuchElementException();
1459 next
= successor(last
);
1463 else if (type
== KEYS
)
1469 * Removes from the backing TreeMap the last element which was fetched
1470 * with the <code>next()</code> method.
1471 * @throws ConcurrentModificationException if the TreeMap was modified
1472 * @throws IllegalStateException if called when there is no last element
1474 public void remove()
1477 throw new IllegalStateException();
1478 if (knownMod
!= modCount
)
1479 throw new ConcurrentModificationException();
1485 } // class TreeIterator
1488 * Implementation of {@link #subMap(Object, Object)} and other map
1489 * ranges. This class provides a view of a portion of the original backing
1490 * map, and throws {@link IllegalArgumentException} for attempts to
1491 * access beyond that range.
1493 * @author Eric Blake <ebb9@email.byu.edu>
1495 private final class SubMap
extends AbstractMap
implements SortedMap
1498 * The lower range of this view, inclusive, or nil for unbounded.
1499 * Package visible for use by nested classes.
1501 final Object minKey
;
1504 * The upper range of this view, exclusive, or nil for unbounded.
1505 * Package visible for use by nested classes.
1507 final Object maxKey
;
1510 * The cache for {@link #entrySet()}.
1512 private Set entries
;
1515 * Create a SubMap representing the elements between minKey (inclusive)
1516 * and maxKey (exclusive). If minKey is nil, SubMap has no lower bound
1517 * (headMap). If maxKey is nil, the SubMap has no upper bound (tailMap).
1519 * @param minKey the lower bound
1520 * @param maxKey the upper bound
1521 * @throws IllegalArgumentException if minKey > maxKey
1523 SubMap(Object minKey
, Object maxKey
)
1525 if (minKey
!= nil
&& maxKey
!= nil
&& compare(minKey
, maxKey
) > 0)
1526 throw new IllegalArgumentException("fromKey > toKey");
1527 this.minKey
= minKey
;
1528 this.maxKey
= maxKey
;
1532 * Check if "key" is in within the range bounds for this SubMap. The
1533 * lower ("from") SubMap range is inclusive, and the upper ("to") bound
1534 * is exclusive. Package visible for use by nested classes.
1536 * @param key the key to check
1537 * @return true if the key is in range
1539 boolean keyInRange(Object key
)
1541 return ((minKey
== nil
|| compare(key
, minKey
) >= 0)
1542 && (maxKey
== nil
|| compare(key
, maxKey
) < 0));
1547 Node next
= lowestGreaterThan(minKey
, true);
1548 Node max
= lowestGreaterThan(maxKey
, false);
1551 Node current
= next
;
1552 next
= successor(current
);
1553 removeNode(current
);
1557 public Comparator
comparator()
1562 public boolean containsKey(Object key
)
1564 return keyInRange(key
) && TreeMap
.this.containsKey(key
);
1567 public boolean containsValue(Object value
)
1569 Node node
= lowestGreaterThan(minKey
, true);
1570 Node max
= lowestGreaterThan(maxKey
, false);
1573 if (equals(value
, node
.getValue()))
1575 node
= successor(node
);
1580 public Set
entrySet()
1582 if (entries
== null)
1583 // Create an AbstractSet with custom implementations of those methods
1584 // that can be overriden easily and efficiently.
1585 entries
= new AbstractSet()
1589 return SubMap
.this.size();
1592 public Iterator
iterator()
1594 Node first
= lowestGreaterThan(minKey
, true);
1595 Node max
= lowestGreaterThan(maxKey
, false);
1596 return new TreeIterator(ENTRIES
, first
, max
);
1601 SubMap
.this.clear();
1604 public boolean contains(Object o
)
1606 if (! (o
instanceof Map
.Entry
))
1608 Map
.Entry me
= (Map
.Entry
) o
;
1609 Object key
= me
.getKey();
1610 if (! keyInRange(key
))
1612 Node n
= getNode(key
);
1613 return n
!= nil
&& AbstractSet
.equals(me
.getValue(), n
.value
);
1616 public boolean remove(Object o
)
1618 if (! (o
instanceof Map
.Entry
))
1620 Map
.Entry me
= (Map
.Entry
) o
;
1621 Object key
= me
.getKey();
1622 if (! keyInRange(key
))
1624 Node n
= getNode(key
);
1625 if (n
!= nil
&& AbstractSet
.equals(me
.getValue(), n
.value
))
1636 public Object
firstKey()
1638 Node node
= lowestGreaterThan(minKey
, true);
1639 if (node
== nil
|| ! keyInRange(node
.key
))
1640 throw new NoSuchElementException();
1644 public Object
get(Object key
)
1646 if (keyInRange(key
))
1647 return TreeMap
.this.get(key
);
1651 public SortedMap
headMap(Object toKey
)
1653 if (! keyInRange(toKey
))
1654 throw new IllegalArgumentException("key outside range");
1655 return new SubMap(minKey
, toKey
);
1660 if (this.keys
== null)
1661 // Create an AbstractSet with custom implementations of those methods
1662 // that can be overriden easily and efficiently.
1663 this.keys
= new AbstractSet()
1667 return SubMap
.this.size();
1670 public Iterator
iterator()
1672 Node first
= lowestGreaterThan(minKey
, true);
1673 Node max
= lowestGreaterThan(maxKey
, false);
1674 return new TreeIterator(KEYS
, first
, max
);
1679 SubMap
.this.clear();
1682 public boolean contains(Object o
)
1684 if (! keyInRange(o
))
1686 return getNode(o
) != nil
;
1689 public boolean remove(Object o
)
1691 if (! keyInRange(o
))
1693 Node n
= getNode(o
);
1705 public Object
lastKey()
1707 Node node
= highestLessThan(maxKey
);
1708 if (node
== nil
|| ! keyInRange(node
.key
))
1709 throw new NoSuchElementException();
1713 public Object
put(Object key
, Object value
)
1715 if (! keyInRange(key
))
1716 throw new IllegalArgumentException("Key outside range");
1717 return TreeMap
.this.put(key
, value
);
1720 public Object
remove(Object key
)
1722 if (keyInRange(key
))
1723 return TreeMap
.this.remove(key
);
1729 Node node
= lowestGreaterThan(minKey
, true);
1730 Node max
= lowestGreaterThan(maxKey
, false);
1735 node
= successor(node
);
1740 public SortedMap
subMap(Object fromKey
, Object toKey
)
1742 if (! keyInRange(fromKey
) || ! keyInRange(toKey
))
1743 throw new IllegalArgumentException("key outside range");
1744 return new SubMap(fromKey
, toKey
);
1747 public SortedMap
tailMap(Object fromKey
)
1749 if (! keyInRange(fromKey
))
1750 throw new IllegalArgumentException("key outside range");
1751 return new SubMap(fromKey
, maxKey
);
1754 public Collection
values()
1756 if (this.values
== null)
1757 // Create an AbstractCollection with custom implementations of those
1758 // methods that can be overriden easily and efficiently.
1759 this.values
= new AbstractCollection()
1763 return SubMap
.this.size();
1766 public Iterator
iterator()
1768 Node first
= lowestGreaterThan(minKey
, true);
1769 Node max
= lowestGreaterThan(maxKey
, false);
1770 return new TreeIterator(VALUES
, first
, max
);
1775 SubMap
.this.clear();