--- jsr166/src/main/java/util/TreeMap.java 2006/04/20 20:34:37 1.29 +++ jsr166/src/main/java/util/TreeMap.java 2010/09/01 07:48:47 1.49 @@ -1,8 +1,26 @@ /* - * %W% %E% + * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. - * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. */ package java.util; @@ -68,14 +86,13 @@ package java.util; * associated map using put.) * *

This class is a member of the - * + * * Java Collections Framework. * * @param the type of keys maintained by this map * @param the type of mapped values * * @author Josh Bloch and Doug Lea - * @version %I%, %G% * @see Map * @see HashMap * @see Hashtable @@ -95,7 +112,7 @@ public class TreeMap * * @serial */ - private Comparator comparator = null; + private final Comparator comparator; private transient Entry root = null; @@ -109,9 +126,6 @@ public class TreeMap */ private transient int modCount = 0; - private void incrementSize() { modCount++; size++; } - private void decrementSize() { modCount++; size--; } - /** * Constructs a new, empty tree map, using the natural ordering of its * keys. All keys inserted into the map must implement the {@link @@ -125,6 +139,7 @@ public class TreeMap * ClassCastException. */ public TreeMap() { + comparator = null; } /** @@ -160,6 +175,7 @@ public class TreeMap * @throws NullPointerException if the specified map is null */ public TreeMap(Map m) { + comparator = null; putAll(m); } @@ -220,32 +236,14 @@ public class TreeMap * * @param value value whose presence in this map is to be tested * @return true if a mapping to value exists; - * false otherwise + * false otherwise * @since 1.2 */ public boolean containsValue(Object value) { - return (root==null ? false : - (value==null ? valueSearchNull(root) - : valueSearchNonNull(root, value))); - } - - private boolean valueSearchNull(Entry n) { - if (n.value == null) - return true; - - // Check left and right subtrees for value - return (n.left != null && valueSearchNull(n.left)) || - (n.right != null && valueSearchNull(n.right)); - } - - private boolean valueSearchNonNull(Entry n, Object value) { - // Check this node for the value - if (value.equals(n.value)) - return true; - - // Check left and right subtrees for value - return (n.left != null && valueSearchNonNull(n.left, value)) || - (n.right != null && valueSearchNonNull(n.right, value)); + for (Entry e = getFirstEntry(); e != null; e = successor(e)) + if (valEquals(value, e.value)) + return true; + return false; } /** @@ -310,14 +308,14 @@ public class TreeMap if (size==0 && mapSize!=0 && map instanceof SortedMap) { Comparator c = ((SortedMap)map).comparator(); if (c == comparator || (c != null && c.equals(comparator))) { - ++modCount; - try { - buildFromSorted(mapSize, map.entrySet().iterator(), - null, null); - } catch (java.io.IOException cannotHappen) { - } catch (ClassNotFoundException cannotHappen) { - } - return; + ++modCount; + try { + buildFromSorted(mapSize, map.entrySet().iterator(), + null, null); + } catch (java.io.IOException cannotHappen) { + } catch (ClassNotFoundException cannotHappen) { + } + return; } } super.putAll(map); @@ -341,7 +339,7 @@ public class TreeMap return getEntryUsingComparator(key); if (key == null) throw new NullPointerException(); - Comparable k = (Comparable) key; + Comparable k = (Comparable) key; Entry p = root; while (p != null) { int cmp = k.compareTo(p.key); @@ -362,17 +360,19 @@ public class TreeMap * worthwhile here.) */ final Entry getEntryUsingComparator(Object key) { - K k = (K) key; + K k = (K) key; Comparator cpr = comparator; - Entry p = root; - while (p != null) { - int cmp = cpr.compare(k, p.key); - if (cmp < 0) - p = p.left; - else if (cmp > 0) - p = p.right; - else - return p; + if (cpr != null) { + Entry p = root; + while (p != null) { + int cmp = cpr.compare(k, p.key); + if (cmp < 0) + p = p.left; + else if (cmp > 0) + p = p.right; + else + return p; + } } return null; } @@ -385,10 +385,7 @@ public class TreeMap */ final Entry getCeilingEntry(K key) { Entry p = root; - if (p==null) - return null; - - while (true) { + while (p != null) { int cmp = compare(key, p.key); if (cmp < 0) { if (p.left != null) @@ -410,6 +407,7 @@ public class TreeMap } else return p; } + return null; } /** @@ -419,10 +417,7 @@ public class TreeMap */ final Entry getFloorEntry(K key) { Entry p = root; - if (p==null) - return null; - - while (true) { + while (p != null) { int cmp = compare(key, p.key); if (cmp > 0) { if (p.right != null) @@ -445,6 +440,7 @@ public class TreeMap return p; } + return null; } /** @@ -455,10 +451,7 @@ public class TreeMap */ final Entry getHigherEntry(K key) { Entry p = root; - if (p==null) - return null; - - while (true) { + while (p != null) { int cmp = compare(key, p.key); if (cmp < 0) { if (p.left != null) @@ -479,6 +472,7 @@ public class TreeMap } } } + return null; } /** @@ -488,10 +482,7 @@ public class TreeMap */ final Entry getLowerEntry(K key) { Entry p = root; - if (p==null) - return null; - - while (true) { + while (p != null) { int cmp = compare(key, p.key); if (cmp > 0) { if (p.right != null) @@ -512,16 +503,7 @@ public class TreeMap } } } - } - - /** - * Returns the key corresponding to the specified Entry. - * @throws NoSuchElementException if the Entry is null - */ - static K key(Entry e) { - if (e==null) - throw new NoSuchElementException(); - return e.key; + return null; } /** @@ -544,43 +526,57 @@ public class TreeMap */ public V put(K key, V value) { Entry t = root; - if (t == null) { - // TBD - // if (key == null) { - // if (comparator == null) - // throw new NullPointerException(); - // comparator.compare(key, key); - // } - incrementSize(); + // TBD: + // 5045147: (coll) Adding null to an empty TreeSet should + // throw NullPointerException + // + // compare(key, key); // type check root = new Entry(key, value, null); + size = 1; + modCount++; return null; } - - while (true) { - int cmp = compare(key, t.key); - if (cmp == 0) { - return t.setValue(value); - } else if (cmp < 0) { - if (t.left != null) { + int cmp; + Entry parent; + // split comparator and comparable paths + Comparator cpr = comparator; + if (cpr != null) { + do { + parent = t; + cmp = cpr.compare(key, t.key); + if (cmp < 0) t = t.left; - } else { - incrementSize(); - t.left = new Entry(key, value, t); - fixAfterInsertion(t.left); - return null; - } - } else { // cmp > 0 - if (t.right != null) { + else if (cmp > 0) t = t.right; - } else { - incrementSize(); - t.right = new Entry(key, value, t); - fixAfterInsertion(t.right); - return null; - } - } + else + return t.setValue(value); + } while (t != null); + } + else { + if (key == null) + throw new NullPointerException(); + Comparable k = (Comparable) key; + do { + parent = t; + cmp = k.compareTo(t.key); + if (cmp < 0) + t = t.left; + else if (cmp > 0) + t = t.right; + else + return t.setValue(value); + } while (t != null); } + Entry e = new Entry(key, value, parent); + if (cmp < 0) + parent.left = e; + else + parent.right = e; + fixAfterInsertion(e); + size++; + modCount++; + return null; } /** @@ -655,16 +651,14 @@ public class TreeMap * @since 1.6 */ public Map.Entry firstEntry() { - Entry e = getFirstEntry(); - return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); + return exportEntry(getFirstEntry()); } /** * @since 1.6 */ public Map.Entry lastEntry() { - Entry e = getLastEntry(); - return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); + return exportEntry(getLastEntry()); } /** @@ -672,10 +666,9 @@ public class TreeMap */ public Map.Entry pollFirstEntry() { Entry p = getFirstEntry(); - if (p == null) - return null; - Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); - deleteEntry(p); + Map.Entry result = exportEntry(p); + if (p != null) + deleteEntry(p); return result; } @@ -684,10 +677,9 @@ public class TreeMap */ public Map.Entry pollLastEntry() { Entry p = getLastEntry(); - if (p == null) - return null; - Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); - deleteEntry(p); + Map.Entry result = exportEntry(p); + if (p != null) + deleteEntry(p); return result; } @@ -699,8 +691,7 @@ public class TreeMap * @since 1.6 */ public Map.Entry lowerEntry(K key) { - Entry e = getLowerEntry(key); - return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); + return exportEntry(getLowerEntry(key)); } /** @@ -711,8 +702,7 @@ public class TreeMap * @since 1.6 */ public K lowerKey(K key) { - Entry e = getLowerEntry(key); - return (e == null)? null : e.key; + return keyOrNull(getLowerEntry(key)); } /** @@ -723,8 +713,7 @@ public class TreeMap * @since 1.6 */ public Map.Entry floorEntry(K key) { - Entry e = getFloorEntry(key); - return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); + return exportEntry(getFloorEntry(key)); } /** @@ -735,8 +724,7 @@ public class TreeMap * @since 1.6 */ public K floorKey(K key) { - Entry e = getFloorEntry(key); - return (e == null)? null : e.key; + return keyOrNull(getFloorEntry(key)); } /** @@ -747,8 +735,7 @@ public class TreeMap * @since 1.6 */ public Map.Entry ceilingEntry(K key) { - Entry e = getCeilingEntry(key); - return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); + return exportEntry(getCeilingEntry(key)); } /** @@ -759,8 +746,7 @@ public class TreeMap * @since 1.6 */ public K ceilingKey(K key) { - Entry e = getCeilingEntry(key); - return (e == null)? null : e.key; + return keyOrNull(getCeilingEntry(key)); } /** @@ -771,8 +757,7 @@ public class TreeMap * @since 1.6 */ public Map.Entry higherEntry(K key) { - Entry e = getHigherEntry(key); - return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); + return exportEntry(getHigherEntry(key)); } /** @@ -783,8 +768,7 @@ public class TreeMap * @since 1.6 */ public K higherKey(K key) { - Entry e = getHigherEntry(key); - return (e == null)? null : e.key; + return keyOrNull(getHigherEntry(key)); } // Views @@ -877,9 +861,9 @@ public class TreeMap public NavigableMap descendingMap() { NavigableMap km = descendingMap; return (km != null) ? km : - (descendingMap = new DescendingSubMap(this, - true, null, 0, - true, null, 0)); + (descendingMap = new DescendingSubMap(this, + true, null, true, + true, null, true)); } /** @@ -892,9 +876,9 @@ public class TreeMap */ public NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { - return new AscendingSubMap(this, - false, fromKey, excluded(fromInclusive), - false, toKey, excluded(toInclusive)); + return new AscendingSubMap(this, + false, fromKey, fromInclusive, + false, toKey, toInclusive); } /** @@ -906,9 +890,9 @@ public class TreeMap * @since 1.6 */ public NavigableMap headMap(K toKey, boolean inclusive) { - return new AscendingSubMap(this, - true, null, 0, - false, toKey, excluded(inclusive)); + return new AscendingSubMap(this, + true, null, true, + false, toKey, inclusive); } /** @@ -920,17 +904,9 @@ public class TreeMap * @since 1.6 */ public NavigableMap tailMap(K fromKey, boolean inclusive) { - return new AscendingSubMap(this, - false, fromKey, excluded(inclusive), - true, null, 0); - } - - /** - * Translates a boolean "inclusive" value to the correct int value - * for the loExcluded or hiExcluded field. - */ - static int excluded(boolean inclusive) { - return inclusive ? 0 : 1; + return new AscendingSubMap(this, + false, fromKey, inclusive, + true, null, true); } /** @@ -978,10 +954,7 @@ public class TreeMap } public boolean contains(Object o) { - for (Entry e = getFirstEntry(); e != null; e = successor(e)) - if (valEquals(e.getValue(), o)) - return true; - return false; + return TreeMap.this.containsValue(o); } public boolean remove(Object o) { @@ -1048,7 +1021,7 @@ public class TreeMap } Iterator descendingKeyIterator() { - return new DescendingKeyIterator(getFirstEntry()); + return new DescendingKeyIterator(getLastEntry()); } static final class KeySet extends AbstractSet implements NavigableSet { @@ -1093,19 +1066,16 @@ public class TreeMap m.remove(o); return size() != oldSize; } - public NavigableSet subSet(E fromElement, - boolean fromInclusive, - E toElement, - boolean toInclusive) { - return new TreeSet - (m.subMap(fromElement, fromInclusive, - toElement, toInclusive)); + public NavigableSet subSet(E fromElement, boolean fromInclusive, + E toElement, boolean toInclusive) { + return new KeySet(m.subMap(fromElement, fromInclusive, + toElement, toInclusive)); } public NavigableSet headSet(E toElement, boolean inclusive) { - return new TreeSet(m.headMap(toElement, inclusive)); + return new KeySet(m.headMap(toElement, inclusive)); } public NavigableSet tailSet(E fromElement, boolean inclusive) { - return new TreeSet(m.tailMap(fromElement, inclusive)); + return new KeySet(m.tailMap(fromElement, inclusive)); } public SortedSet subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); @@ -1117,7 +1087,7 @@ public class TreeMap return tailSet(fromElement, true); } public NavigableSet descendingSet() { - return new TreeSet(m.descendingMap()); + return new KeySet(m.descendingMap()); } } @@ -1125,11 +1095,13 @@ public class TreeMap * Base class for TreeMap Iterators */ abstract class PrivateEntryIterator implements Iterator { - int expectedModCount = TreeMap.this.modCount; - Entry lastReturned = null; Entry next; + Entry lastReturned; + int expectedModCount; PrivateEntryIterator(Entry first) { + expectedModCount = modCount; + lastReturned = null; next = first; } @@ -1137,24 +1109,26 @@ public class TreeMap return next != null; } - final Entry nextEntry() { - if (next == null) + final Entry nextEntry() { + Entry e = next; + if (e == null) throw new NoSuchElementException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); - lastReturned = next; - next = successor(next); - return lastReturned; + next = successor(e); + lastReturned = e; + return e; } final Entry prevEntry() { - if (next == null) + Entry e = next; + if (e == null) throw new NoSuchElementException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); - lastReturned = next; - next = predecessor(next); - return lastReturned; + next = predecessor(e); + lastReturned = e; + return e; } public void remove() { @@ -1162,10 +1136,11 @@ public class TreeMap throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); + // deleted entries are replaced by their successors if (lastReturned.left != null && lastReturned.right != null) next = lastReturned; deleteEntry(lastReturned); - expectedModCount++; + expectedModCount = modCount; lastReturned = null; } } @@ -1206,65 +1181,124 @@ public class TreeMap } } - // SubMaps + // Little utilities - static abstract class NavigableSubMap extends AbstractMap - implements NavigableMap, java.io.Serializable { + /** + * Compares two keys using the correct comparison method for this TreeMap. + */ + final int compare(Object k1, Object k2) { + return comparator==null ? ((Comparable)k1).compareTo((K)k2) + : comparator.compare((K)k1, (K)k2); + } - /* - * The backing map. - */ - final TreeMap m; + /** + * Test two values for equality. Differs from o1.equals(o2) only in + * that it copes with null o1 properly. + */ + final static boolean valEquals(Object o1, Object o2) { + return (o1==null ? o2==null : o1.equals(o2)); + } - /** True if low point is from start of backing map */ - boolean fromStart; + /** + * Return SimpleImmutableEntry for entry, or null if null + */ + static Map.Entry exportEntry(TreeMap.Entry e) { + return e == null? null : + new AbstractMap.SimpleImmutableEntry(e); + } - /** - * The low endpoint of this submap in absolute terms, or null - * if fromStart. - */ - K lo; + /** + * Return key for entry, or null if null + */ + static K keyOrNull(TreeMap.Entry e) { + return e == null? null : e.key; + } - /** - * Zero if the low endpoint is excluded from this submap, one if - * it's included. This field is unused if fromStart. - */ - int loExcluded; + /** + * Returns the key corresponding to the specified Entry. + * @throws NoSuchElementException if the Entry is null + */ + static K key(Entry e) { + if (e==null) + throw new NoSuchElementException(); + return e.key; + } - /** True if high point is to End of backing map */ - boolean toEnd; - /** - * The high endpoint of this submap in absolute terms, or null - * if toEnd. + // SubMaps + + /** + * Dummy value serving as unmatchable fence key for unbounded + * SubMapIterators + */ + private static final Object UNBOUNDED = new Object(); + + /** + * @serial include + */ + static abstract class NavigableSubMap extends AbstractMap + implements NavigableMap, java.io.Serializable { + /** + * The backing map. */ - K hi; + final TreeMap m; /** - * Zero if the high endpoint is excluded from this submap, one if - * it's included. This field is unused if toEnd. + * Endpoints are represented as triples (fromStart, lo, + * loInclusive) and (toEnd, hi, hiInclusive). If fromStart is + * true, then the low (absolute) bound is the start of the + * backing map, and the other values are ignored. Otherwise, + * if loInclusive is true, lo is the inclusive bound, else lo + * is the exclusive bound. Similarly for the upper bound. */ - int hiExcluded; + final K lo, hi; + final boolean fromStart, toEnd; + final boolean loInclusive, hiInclusive; + + NavigableSubMap(TreeMap m, + boolean fromStart, K lo, boolean loInclusive, + boolean toEnd, K hi, boolean hiInclusive) { + if (!fromStart && !toEnd) { + if (m.compare(lo, hi) > 0) + throw new IllegalArgumentException("fromKey > toKey"); + } else { + if (!fromStart) // type check + m.compare(lo, lo); + if (!toEnd) + m.compare(hi, hi); + } - NavigableSubMap(TreeMap m, - boolean fromStart, K lo, int loExcluded, - boolean toEnd, K hi, int hiExcluded) { - if (!fromStart && !toEnd && m.compare(lo, hi) > 0) - throw new IllegalArgumentException("fromKey > toKey"); this.m = m; this.fromStart = fromStart; this.lo = lo; - this.loExcluded = loExcluded; + this.loInclusive = loInclusive; this.toEnd = toEnd; this.hi = hi; - this.hiExcluded = hiExcluded; + this.hiInclusive = hiInclusive; } // internal utilities + final boolean tooLow(Object key) { + if (!fromStart) { + int c = m.compare(key, lo); + if (c < 0 || (c == 0 && !loInclusive)) + return true; + } + return false; + } + + final boolean tooHigh(Object key) { + if (!toEnd) { + int c = m.compare(key, hi); + if (c > 0 || (c == 0 && !hiInclusive)) + return true; + } + return false; + } + final boolean inRange(Object key) { - return (fromStart || m.compare(key, lo) >= loExcluded) - && (toEnd || m.compare(hi, key) >= hiExcluded); + return !tooLow(key) && !tooHigh(key); } final boolean inClosedRange(Object key) { @@ -1276,149 +1310,176 @@ public class TreeMap return inclusive ? inRange(key) : inClosedRange(key); } - final boolean tooLow(K key) { - return !fromStart && m.compare(key, lo) < loExcluded; - } - - final boolean tooHigh(K key) { - return !toEnd && m.compare(hi, key) < hiExcluded; - } - + /* + * Absolute versions of relation operations. + * Subclasses map to these using like-named "sub" + * versions that invert senses for descending maps + */ - /** Returns the lowest entry in this submap (absolute ordering) */ - final TreeMap.Entry loEntry() { - TreeMap.Entry result = + final TreeMap.Entry absLowest() { + TreeMap.Entry e = (fromStart ? m.getFirstEntry() : - (loExcluded == 0 ? m.getCeilingEntry(lo) : - m.getHigherEntry(lo))); - return (result == null || tooHigh(result.key)) ? null : result; + (loInclusive ? m.getCeilingEntry(lo) : + m.getHigherEntry(lo))); + return (e == null || tooHigh(e.key)) ? null : e; } - /** Returns the highest key in this submap (absolute ordering) */ - final TreeMap.Entry hiEntry() { - TreeMap.Entry result = + final TreeMap.Entry absHighest() { + TreeMap.Entry e = (toEnd ? m.getLastEntry() : - (hiExcluded == 0 ? m.getFloorEntry(hi) : - m.getLowerEntry(hi))); - return (result == null || tooLow(result.key)) ? null : result; + (hiInclusive ? m.getFloorEntry(hi) : + m.getLowerEntry(hi))); + return (e == null || tooLow(e.key)) ? null : e; } - /** Polls the lowest entry in this submap (absolute ordering) */ - final Map.Entry pollLoEntry() { - TreeMap.Entry e = loEntry(); - if (e == null) - return null; - Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); - m.deleteEntry(e); - return result; + final TreeMap.Entry absCeiling(K key) { + if (tooLow(key)) + return absLowest(); + TreeMap.Entry e = m.getCeilingEntry(key); + return (e == null || tooHigh(e.key)) ? null : e; } - /** Polls the highest key in this submap (absolute ordering) */ - final Map.Entry pollHiEntry() { - TreeMap.Entry e = hiEntry(); - if (e == null) - return null; - Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); - m.deleteEntry(e); - return result; + final TreeMap.Entry absHigher(K key) { + if (tooLow(key)) + return absLowest(); + TreeMap.Entry e = m.getHigherEntry(key); + return (e == null || tooHigh(e.key)) ? null : e; } - /** - * Return the absolute high fence for ascending traversal - */ - final TreeMap.Entry hiFence() { - if (toEnd) - return null; - else if (hiExcluded == 0) - return m.getHigherEntry(hi); - else - return m.getCeilingEntry(hi); + final TreeMap.Entry absFloor(K key) { + if (tooHigh(key)) + return absHighest(); + TreeMap.Entry e = m.getFloorEntry(key); + return (e == null || tooLow(e.key)) ? null : e; } - /** - * Return the absolute low fence for descending traversal - */ - final TreeMap.Entry loFence() { - if (fromStart) - return null; - else if (loExcluded == 0) - return m.getLowerEntry(lo); - else - return m.getFloorEntry(lo); + final TreeMap.Entry absLower(K key) { + if (tooHigh(key)) + return absHighest(); + TreeMap.Entry e = m.getLowerEntry(key); + return (e == null || tooLow(e.key)) ? null : e; } + /** Returns the absolute high fence for ascending traversal */ + final TreeMap.Entry absHighFence() { + return (toEnd ? null : (hiInclusive ? + m.getHigherEntry(hi) : + m.getCeilingEntry(hi))); + } + + /** Return the absolute low fence for descending traversal */ + final TreeMap.Entry absLowFence() { + return (fromStart ? null : (loInclusive ? + m.getLowerEntry(lo) : + m.getFloorEntry(lo))); + } + + // Abstract methods defined in ascending vs descending classes + // These relay to the appropriate absolute versions + + abstract TreeMap.Entry subLowest(); + abstract TreeMap.Entry subHighest(); + abstract TreeMap.Entry subCeiling(K key); + abstract TreeMap.Entry subHigher(K key); + abstract TreeMap.Entry subFloor(K key); + abstract TreeMap.Entry subLower(K key); + + /** Returns ascending iterator from the perspective of this submap */ + abstract Iterator keyIterator(); + + /** Returns descending iterator from the perspective of this submap */ + abstract Iterator descendingKeyIterator(); + + // public methods public boolean isEmpty() { - return entrySet().isEmpty(); + return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty(); } - public boolean containsKey(Object key) { - return inRange(key) && m.containsKey(key); + public int size() { + return (fromStart && toEnd) ? m.size() : entrySet().size(); } - public V get(Object key) { - if (!inRange(key)) - return null; - return m.get(key); + public final boolean containsKey(Object key) { + return inRange(key) && m.containsKey(key); } - public V put(K key, V value) { + public final V put(K key, V value) { if (!inRange(key)) throw new IllegalArgumentException("key out of range"); return m.put(key, value); } - public V remove(Object key) { - if (!inRange(key)) - return null; - return m.remove(key); + public final V get(Object key) { + return !inRange(key)? null : m.get(key); } - public Map.Entry ceilingEntry(K key) { - TreeMap.Entry e = subCeiling(key); - return e == null? null : new AbstractMap.SimpleImmutableEntry(e); + public final V remove(Object key) { + return !inRange(key)? null : m.remove(key); } - public K ceilingKey(K key) { - TreeMap.Entry e = subCeiling(key); - return e == null? null : e.key; + public final Map.Entry ceilingEntry(K key) { + return exportEntry(subCeiling(key)); } - public Map.Entry higherEntry(K key) { - TreeMap.Entry e = subHigher(key); - return e == null? null : new AbstractMap.SimpleImmutableEntry(e); + public final K ceilingKey(K key) { + return keyOrNull(subCeiling(key)); } - public K higherKey(K key) { - TreeMap.Entry e = subHigher(key); - return e == null? null : e.key; + public final Map.Entry higherEntry(K key) { + return exportEntry(subHigher(key)); } - public Map.Entry floorEntry(K key) { - TreeMap.Entry e = subFloor(key); - return e == null? null : new AbstractMap.SimpleImmutableEntry(e); + public final K higherKey(K key) { + return keyOrNull(subHigher(key)); } - public K floorKey(K key) { - TreeMap.Entry e = subFloor(key); - return e == null? null : e.key; + public final Map.Entry floorEntry(K key) { + return exportEntry(subFloor(key)); } - public Map.Entry lowerEntry(K key) { - TreeMap.Entry e = subLower(key); - return e == null? null : new AbstractMap.SimpleImmutableEntry(e); + public final K floorKey(K key) { + return keyOrNull(subFloor(key)); } - public K lowerKey(K key) { - TreeMap.Entry e = subLower(key); - return e == null? null : e.key; + public final Map.Entry lowerEntry(K key) { + return exportEntry(subLower(key)); } - abstract Iterator keyIterator(); - abstract Iterator descendingKeyIterator(); + public final K lowerKey(K key) { + return keyOrNull(subLower(key)); + } - public NavigableSet descendingKeySet() { - return descendingMap().navigableKeySet(); + public final K firstKey() { + return key(subLowest()); + } + + public final K lastKey() { + return key(subHighest()); + } + + public final Map.Entry firstEntry() { + return exportEntry(subLowest()); + } + + public final Map.Entry lastEntry() { + return exportEntry(subHighest()); + } + + public final Map.Entry pollFirstEntry() { + TreeMap.Entry e = subLowest(); + Map.Entry result = exportEntry(e); + if (e != null) + m.deleteEntry(e); + return result; + } + + public final Map.Entry pollLastEntry() { + TreeMap.Entry e = subHighest(); + Map.Entry result = exportEntry(e); + if (e != null) + m.deleteEntry(e); + return result; } // Views @@ -1426,6 +1487,34 @@ public class TreeMap transient EntrySetView entrySetView = null; transient KeySet navigableKeySetView = null; + public final NavigableSet navigableKeySet() { + KeySet nksv = navigableKeySetView; + return (nksv != null) ? nksv : + (navigableKeySetView = new TreeMap.KeySet(this)); + } + + public final Set keySet() { + return navigableKeySet(); + } + + public NavigableSet descendingKeySet() { + return descendingMap().navigableKeySet(); + } + + public final SortedMap subMap(K fromKey, K toKey) { + return subMap(fromKey, true, toKey, false); + } + + public final SortedMap headMap(K toKey) { + return headMap(toKey, false); + } + + public final SortedMap tailMap(K fromKey) { + return tailMap(fromKey, true); + } + + // View classes + abstract class EntrySetView extends AbstractSet> { private transient int size = -1, sizeModCount; @@ -1434,7 +1523,7 @@ public class TreeMap return m.size(); if (size == -1 || sizeModCount != m.modCount) { sizeModCount = m.modCount; - size = 0; + size = 0; Iterator i = iterator(); while (i.hasNext()) { size++; @@ -1445,7 +1534,7 @@ public class TreeMap } public boolean isEmpty() { - TreeMap.Entry n = loEntry(); + TreeMap.Entry n = absLowest(); return n == null || tooHigh(n.key); } @@ -1477,228 +1566,191 @@ public class TreeMap } } - public NavigableSet navigableKeySet() { - KeySet nksv = navigableKeySetView; - return (nksv != null) ? nksv : - (navigableKeySetView = new TreeMap.KeySet(this)); - } - - public Set keySet() { - return navigableKeySet(); - } - - public SortedMap subMap(K fromKey, K toKey) { - return subMap(fromKey, true, toKey, false); - } - - public SortedMap headMap(K toKey) { - return headMap(toKey, false); - } - - public SortedMap tailMap(K fromKey) { - return tailMap(fromKey, true); - } - - - // The following four definitions are correct only for - // ascending submaps. They are overridden in DescendingSubMap. - // They are defined in the base class because the definitions - // in DescendingSubMap rely on those for AscendingSubMap. - - /** - * Returns the entry corresponding to the ceiling of the specified - * key from the perspective of this submap, or null if the submap - * contains no such entry. - */ - TreeMap.Entry subCeiling(K key) { - if (tooLow(key)) - return loEntry(); - TreeMap.Entry e = m.getCeilingEntry(key); - return (e == null || tooHigh(e.key)) ? null : e; - } - - /** - * Returns the entry corresponding to the higher of the specified - * key from the perspective of this submap, or null if the submap - * contains no such entry. - */ - TreeMap.Entry subHigher(K key) { - if (tooLow(key)) - return loEntry(); - TreeMap.Entry e = m.getHigherEntry(key); - return (e == null || tooHigh(e.key)) ? null : e; - } - - /** - * Returns the entry corresponding to the floor of the specified - * key from the perspective of this submap, or null if the submap - * contains no such entry. - */ - TreeMap.Entry subFloor(K key) { - if (tooHigh(key)) - return hiEntry(); - TreeMap.Entry e = m.getFloorEntry(key); - return (e == null || tooLow(e.key)) ? null : e; - } - - /** - * Returns the entry corresponding to the lower of the specified - * key from the perspective of this submap, or null if the submap - * contains no such entry. - */ - TreeMap.Entry subLower(K key) { - if (tooHigh(key)) - return hiEntry(); - TreeMap.Entry e = m.getLowerEntry(key); - return (e == null || tooLow(e.key)) ? null : e; - } - /** * Iterators for SubMaps */ abstract class SubMapIterator implements Iterator { - int expectedModCount = m.modCount; - TreeMap.Entry lastReturned = null; + TreeMap.Entry lastReturned; TreeMap.Entry next; - final K firstExcludedKey; + final Object fenceKey; + int expectedModCount; - SubMapIterator(TreeMap.Entry first, - TreeMap.Entry firstExcluded) { + SubMapIterator(TreeMap.Entry first, + TreeMap.Entry fence) { + expectedModCount = m.modCount; + lastReturned = null; next = first; - firstExcludedKey = (firstExcluded == null ? null - : firstExcluded.key); + fenceKey = fence == null ? UNBOUNDED : fence.key; } public final boolean hasNext() { - return next != null && next.key != firstExcludedKey; + return next != null && next.key != fenceKey; } final TreeMap.Entry nextEntry() { - if (next == null || next.key == firstExcludedKey) + TreeMap.Entry e = next; + if (e == null || e.key == fenceKey) throw new NoSuchElementException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); - lastReturned = next; - next = m.successor(next); - return lastReturned; + next = successor(e); + lastReturned = e; + return e; } final TreeMap.Entry prevEntry() { - if (next == null || next.key == firstExcludedKey) + TreeMap.Entry e = next; + if (e == null || e.key == fenceKey) throw new NoSuchElementException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); - lastReturned = next; - next = m.predecessor(next); - return lastReturned; + next = predecessor(e); + lastReturned = e; + return e; } - public void remove() { + final void removeAscending() { if (lastReturned == null) throw new IllegalStateException(); if (m.modCount != expectedModCount) throw new ConcurrentModificationException(); + // deleted entries are replaced by their successors if (lastReturned.left != null && lastReturned.right != null) next = lastReturned; m.deleteEntry(lastReturned); - expectedModCount++; lastReturned = null; + expectedModCount = m.modCount; } + + final void removeDescending() { + if (lastReturned == null) + throw new IllegalStateException(); + if (m.modCount != expectedModCount) + throw new ConcurrentModificationException(); + m.deleteEntry(lastReturned); + lastReturned = null; + expectedModCount = m.modCount; + } + } final class SubMapEntryIterator extends SubMapIterator> { - SubMapEntryIterator(TreeMap.Entry first, - TreeMap.Entry firstExcluded) { - super(first, firstExcluded); + SubMapEntryIterator(TreeMap.Entry first, + TreeMap.Entry fence) { + super(first, fence); } public Map.Entry next() { return nextEntry(); } + public void remove() { + removeAscending(); + } } final class SubMapKeyIterator extends SubMapIterator { - SubMapKeyIterator(TreeMap.Entry first, - TreeMap.Entry firstExcluded) { - super(first, firstExcluded); + SubMapKeyIterator(TreeMap.Entry first, + TreeMap.Entry fence) { + super(first, fence); } public K next() { return nextEntry().key; } + public void remove() { + removeAscending(); + } } final class DescendingSubMapEntryIterator extends SubMapIterator> { - DescendingSubMapEntryIterator(TreeMap.Entry last, - TreeMap.Entry lastExcluded) { - super(last, lastExcluded); + DescendingSubMapEntryIterator(TreeMap.Entry last, + TreeMap.Entry fence) { + super(last, fence); } public Map.Entry next() { return prevEntry(); } + public void remove() { + removeDescending(); + } } final class DescendingSubMapKeyIterator extends SubMapIterator { - DescendingSubMapKeyIterator(TreeMap.Entry last, - TreeMap.Entry lastExcluded) { - super(last, lastExcluded); + DescendingSubMapKeyIterator(TreeMap.Entry last, + TreeMap.Entry fence) { + super(last, fence); } public K next() { return prevEntry().key; } + public void remove() { + removeDescending(); + } } } - static class AscendingSubMap extends NavigableSubMap { + /** + * @serial include + */ + static final class AscendingSubMap extends NavigableSubMap { private static final long serialVersionUID = 912986545866124060L; - AscendingSubMap(TreeMap m, - boolean fromStart, K lo, int loExcluded, - boolean toEnd, K hi, int hiExcluded) { - super(m, fromStart, lo, loExcluded, toEnd, hi, hiExcluded); + AscendingSubMap(TreeMap m, + boolean fromStart, K lo, boolean loInclusive, + boolean toEnd, K hi, boolean hiInclusive) { + super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); } public Comparator comparator() { return m.comparator(); } - public NavigableMap subMap(K fromKey, boolean fromInclusive, - K toKey, boolean toInclusive) { + public NavigableMap subMap(K fromKey, boolean fromInclusive, + K toKey, boolean toInclusive) { if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); - return new AscendingSubMap(m, - false, fromKey, excluded(fromInclusive), - false, toKey, excluded(toInclusive)); + return new AscendingSubMap(m, + false, fromKey, fromInclusive, + false, toKey, toInclusive); } public NavigableMap headMap(K toKey, boolean inclusive) { - if (!inClosedRange(toKey)) + if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); - return new AscendingSubMap(m, - fromStart, lo, loExcluded, - false, toKey, excluded(inclusive)); + return new AscendingSubMap(m, + fromStart, lo, loInclusive, + false, toKey, inclusive); } public NavigableMap tailMap(K fromKey, boolean inclusive){ if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); - return new AscendingSubMap(m, - false, fromKey, excluded(inclusive), - toEnd, hi, hiExcluded); + return new AscendingSubMap(m, + false, fromKey, inclusive, + toEnd, hi, hiInclusive); + } + + public NavigableMap descendingMap() { + NavigableMap mv = descendingMapView; + return (mv != null) ? mv : + (descendingMapView = + new DescendingSubMap(m, + fromStart, lo, loInclusive, + toEnd, hi, hiInclusive)); } Iterator keyIterator() { - return new SubMapKeyIterator(loEntry(), hiFence()); + return new SubMapKeyIterator(absLowest(), absHighFence()); } Iterator descendingKeyIterator() { - return new DescendingSubMapKeyIterator(hiEntry(), loFence()); + return new DescendingSubMapKeyIterator(absHighest(), absLowFence()); } - class AscendingEntrySetView extends NavigableSubMap.EntrySetView { + final class AscendingEntrySetView extends EntrySetView { public Iterator> iterator() { - return new SubMapEntryIterator(loEntry(), hiFence()); + return new SubMapEntryIterator(absLowest(), absHighFence()); } } @@ -1707,46 +1759,23 @@ public class TreeMap return (es != null) ? es : new AscendingEntrySetView(); } - public K firstKey() { - return key(loEntry()); - } - - public K lastKey() { - return key(hiEntry()); - } - - public Map.Entry firstEntry() { - return loEntry(); - } - - public Map.Entry lastEntry() { - return hiEntry(); - } - - public Map.Entry pollFirstEntry() { - return pollLoEntry(); - } - - public Map.Entry pollLastEntry() { - return pollHiEntry(); - } - - public NavigableMap descendingMap() { - NavigableMap mv = descendingMapView; - return (mv != null) ? mv : - (descendingMapView = - new DescendingSubMap(m, - fromStart, lo, loExcluded, - toEnd, hi, hiExcluded)); - } + TreeMap.Entry subLowest() { return absLowest(); } + TreeMap.Entry subHighest() { return absHighest(); } + TreeMap.Entry subCeiling(K key) { return absCeiling(key); } + TreeMap.Entry subHigher(K key) { return absHigher(key); } + TreeMap.Entry subFloor(K key) { return absFloor(key); } + TreeMap.Entry subLower(K key) { return absLower(key); } } - static class DescendingSubMap extends NavigableSubMap { + /** + * @serial include + */ + static final class DescendingSubMap extends NavigableSubMap { private static final long serialVersionUID = 912986545866120460L; - DescendingSubMap(TreeMap m, - boolean fromStart, K lo, int loExcluded, - boolean toEnd, K hi, int hiExcluded) { - super(m, fromStart, lo, loExcluded, toEnd, hi, hiExcluded); + DescendingSubMap(TreeMap m, + boolean fromStart, K lo, boolean loInclusive, + boolean toEnd, K hi, boolean hiInclusive) { + super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); } private final Comparator reverseComparator = @@ -1756,44 +1785,53 @@ public class TreeMap return reverseComparator; } - public NavigableMap subMap(K fromKey, boolean fromInclusive, - K toKey, boolean toInclusive) { + public NavigableMap subMap(K fromKey, boolean fromInclusive, + K toKey, boolean toInclusive) { if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); - return new DescendingSubMap(m, - false, toKey, excluded(toInclusive), - false, fromKey, excluded(fromInclusive)); + return new DescendingSubMap(m, + false, toKey, toInclusive, + false, fromKey, fromInclusive); } public NavigableMap headMap(K toKey, boolean inclusive) { if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); - return new DescendingSubMap(m, - false, toKey, excluded(inclusive), - toEnd, hi, hiExcluded); + return new DescendingSubMap(m, + false, toKey, inclusive, + toEnd, hi, hiInclusive); } public NavigableMap tailMap(K fromKey, boolean inclusive){ if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); - return new DescendingSubMap(m, - fromStart, lo, loExcluded, - false, fromKey, excluded(inclusive)); + return new DescendingSubMap(m, + fromStart, lo, loInclusive, + false, fromKey, inclusive); + } + + public NavigableMap descendingMap() { + NavigableMap mv = descendingMapView; + return (mv != null) ? mv : + (descendingMapView = + new AscendingSubMap(m, + fromStart, lo, loInclusive, + toEnd, hi, hiInclusive)); } Iterator keyIterator() { - return new DescendingSubMapKeyIterator(hiEntry(), loFence()); + return new DescendingSubMapKeyIterator(absHighest(), absLowFence()); } Iterator descendingKeyIterator() { - return new SubMapKeyIterator(loEntry(), hiFence()); + return new SubMapKeyIterator(absLowest(), absHighFence()); } - class DescendingEntrySetView extends NavigableSubMap.EntrySetView { + final class DescendingEntrySetView extends EntrySetView { public Iterator> iterator() { - return new DescendingSubMapEntryIterator(hiEntry(), loFence()); + return new DescendingSubMapEntryIterator(absHighest(), absLowFence()); } } @@ -1802,70 +1840,12 @@ public class TreeMap return (es != null) ? es : new DescendingEntrySetView(); } - public K firstKey() { - return key(hiEntry()); - } - - public K lastKey() { - return key(loEntry()); - } - - public Map.Entry firstEntry() { - return hiEntry(); - } - - public Map.Entry lastEntry() { - return loEntry(); - } - - public Map.Entry pollFirstEntry() { - return pollHiEntry(); - } - - public Map.Entry pollLastEntry() { - return pollLoEntry(); - } - - public NavigableMap descendingMap() { - NavigableMap mv = descendingMapView; - return (mv != null) ? mv : - (descendingMapView = - new AscendingSubMap(m, - fromStart, lo, loExcluded, - toEnd, hi, hiExcluded)); - } - - @Override TreeMap.Entry subCeiling(K key) { - return super.subFloor(key); - } - - @Override TreeMap.Entry subHigher(K key) { - return super.subLower(key); - } - - @Override TreeMap.Entry subFloor(K key) { - return super.subCeiling(key); - } - - @Override TreeMap.Entry subLower(K key) { - return super.subHigher(key); - } - } - - /** - * Compares two keys using the correct comparison method for this TreeMap. - */ - final int compare(Object k1, Object k2) { - return comparator==null ? ((Comparable)k1).compareTo((K)k2) - : comparator.compare((K)k1, (K)k2); - } - - /** - * Test two values for equality. Differs from o1.equals(o2) only in - * that it copes with null o1 properly. - */ - final static boolean valEquals(Object o1, Object o2) { - return (o1==null ? o2==null : o1.equals(o2)); + TreeMap.Entry subLowest() { return absHighest(); } + TreeMap.Entry subHighest() { return absLowest(); } + TreeMap.Entry subCeiling(K key) { return absFloor(key); } + TreeMap.Entry subHigher(K key) { return absLower(key); } + TreeMap.Entry subFloor(K key) { return absCeiling(key); } + TreeMap.Entry subLower(K key) { return absHigher(key); } } /** @@ -1874,16 +1854,18 @@ public class TreeMap * support NavigableMap. It translates an old-version SubMap into * a new-version AscendingSubMap. This class is never otherwise * used. + * + * @serial include */ private class SubMap extends AbstractMap - implements SortedMap, java.io.Serializable { + implements SortedMap, java.io.Serializable { private static final long serialVersionUID = -6520786458950516097L; private boolean fromStart = false, toEnd = false; private K fromKey, toKey; private Object readResolve() { - return new AscendingSubMap(TreeMap.this, - fromStart, fromKey, 0, - toEnd, toKey, 1); + return new AscendingSubMap(TreeMap.this, + fromStart, fromKey, true, + toEnd, toKey, false); } public Set> entrySet() { throw new InternalError(); } public K lastKey() { throw new InternalError(); } @@ -1895,6 +1877,8 @@ public class TreeMap } + // Red-black mechanics + private static final boolean RED = false; private static final boolean BLACK = true; @@ -1904,7 +1888,7 @@ public class TreeMap */ static final class Entry implements Map.Entry { - K key; + K key; V value; Entry left = null; Entry right = null; @@ -1955,7 +1939,7 @@ public class TreeMap public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; - Map.Entry e = (Map.Entry)o; + Map.Entry e = (Map.Entry)o; return valEquals(key,e.getKey()) && valEquals(value,e.getValue()); } @@ -1998,7 +1982,7 @@ public class TreeMap /** * Returns the successor of the specified Entry, or null if no such. */ - final Entry successor(Entry t) { + static TreeMap.Entry successor(Entry t) { if (t == null) return null; else if (t.right != null) { @@ -2020,7 +2004,7 @@ public class TreeMap /** * Returns the predecessor of the specified Entry, or null if no such. */ - final Entry predecessor(Entry t) { + static Entry predecessor(Entry t) { if (t == null) return null; else if (t.left != null) { @@ -2059,7 +2043,7 @@ public class TreeMap private static void setColor(Entry p, boolean c) { if (p != null) - p.color = c; + p.color = c; } private static Entry leftOf(Entry p) { @@ -2070,40 +2054,43 @@ public class TreeMap return (p == null) ? null: p.right; } - /** From CLR **/ + /** From CLR */ private void rotateLeft(Entry p) { - Entry r = p.right; - p.right = r.left; - if (r.left != null) - r.left.parent = p; - r.parent = p.parent; - if (p.parent == null) - root = r; - else if (p.parent.left == p) - p.parent.left = r; - else - p.parent.right = r; - r.left = p; - p.parent = r; + if (p != null) { + Entry r = p.right; + p.right = r.left; + if (r.left != null) + r.left.parent = p; + r.parent = p.parent; + if (p.parent == null) + root = r; + else if (p.parent.left == p) + p.parent.left = r; + else + p.parent.right = r; + r.left = p; + p.parent = r; + } } - /** From CLR **/ + /** From CLR */ private void rotateRight(Entry p) { - Entry l = p.left; - p.left = l.right; - if (l.right != null) l.right.parent = p; - l.parent = p.parent; - if (p.parent == null) - root = l; - else if (p.parent.right == p) - p.parent.right = l; - else p.parent.left = l; - l.right = p; - p.parent = l; + if (p != null) { + Entry l = p.left; + p.left = l.right; + if (l.right != null) l.right.parent = p; + l.parent = p.parent; + if (p.parent == null) + root = l; + else if (p.parent.right == p) + p.parent.right = l; + else p.parent.left = l; + l.right = p; + p.parent = l; + } } - - /** From CLR **/ + /** From CLR */ private void fixAfterInsertion(Entry x) { x.color = RED; @@ -2122,8 +2109,7 @@ public class TreeMap } setColor(parentOf(x), BLACK); setColor(parentOf(parentOf(x)), RED); - if (parentOf(parentOf(x)) != null) - rotateRight(parentOf(parentOf(x))); + rotateRight(parentOf(parentOf(x))); } } else { Entry y = leftOf(parentOf(parentOf(x))); @@ -2137,10 +2123,9 @@ public class TreeMap x = parentOf(x); rotateRight(x); } - setColor(parentOf(x), BLACK); + setColor(parentOf(x), BLACK); setColor(parentOf(parentOf(x)), RED); - if (parentOf(parentOf(x)) != null) - rotateLeft(parentOf(parentOf(x))); + rotateLeft(parentOf(parentOf(x))); } } } @@ -2150,14 +2135,14 @@ public class TreeMap /** * Delete node p, and then rebalance the tree. */ - private void deleteEntry(Entry p) { - decrementSize(); + modCount++; + size--; // If strictly internal, copy successor's element to p and then make p // point to successor. if (p.left != null && p.right != null) { - Entry s = successor (p); + Entry s = successor(p); p.key = s.key; p.value = s.value; p = s; @@ -2198,7 +2183,7 @@ public class TreeMap } } - /** From CLR **/ + /** From CLR */ private void fixAfterDeletion(Entry x) { while (x != root && colorOf(x) == BLACK) { if (x == leftOf(parentOf(x))) { @@ -2213,7 +2198,7 @@ public class TreeMap if (colorOf(leftOf(sib)) == BLACK && colorOf(rightOf(sib)) == BLACK) { - setColor(sib, RED); + setColor(sib, RED); x = parentOf(x); } else { if (colorOf(rightOf(sib)) == BLACK) { @@ -2240,7 +2225,7 @@ public class TreeMap if (colorOf(rightOf(sib)) == BLACK && colorOf(leftOf(sib)) == BLACK) { - setColor(sib, RED); + setColor(sib, RED); x = parentOf(x); } else { if (colorOf(leftOf(sib)) == BLACK) { @@ -2306,19 +2291,19 @@ public class TreeMap buildFromSorted(size, null, s, null); } - /** Intended to be called only from TreeSet.readObject **/ + /** Intended to be called only from TreeSet.readObject */ void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal) throws java.io.IOException, ClassNotFoundException { buildFromSorted(size, null, s, defaultVal); } - /** Intended to be called only from TreeSet.addAll **/ + /** Intended to be called only from TreeSet.addAll */ void addAllForTreeSet(SortedSet set, V defaultVal) { - try { - buildFromSorted(set.size(), set.iterator(), null, defaultVal); - } catch (java.io.IOException cannotHappen) { - } catch (ClassNotFoundException cannotHappen) { - } + try { + buildFromSorted(set.size(), set.iterator(), null, defaultVal); + } catch (java.io.IOException cannotHappen) { + } catch (ClassNotFoundException cannotHappen) { + } } @@ -2353,12 +2338,12 @@ public class TreeMap * This cannot occur if str is null. */ private void buildFromSorted(int size, Iterator it, - java.io.ObjectInputStream str, - V defaultVal) + java.io.ObjectInputStream str, + V defaultVal) throws java.io.IOException, ClassNotFoundException { this.size = size; root = buildFromSorted(0, 0, size-1, computeRedLevel(size), - it, str, defaultVal); + it, str, defaultVal); } /** @@ -2376,10 +2361,10 @@ public class TreeMap * Must be equal to computeRedLevel for tree of this size. */ private final Entry buildFromSorted(int level, int lo, int hi, - int redLevel, - Iterator it, - java.io.ObjectInputStream str, - V defaultVal) + int redLevel, + Iterator it, + java.io.ObjectInputStream str, + V defaultVal) throws java.io.IOException, ClassNotFoundException { /* * Strategy: The root is the middlemost element. To get to it, we @@ -2395,12 +2380,12 @@ public class TreeMap if (hi < lo) return null; - int mid = (lo + hi) / 2; + int mid = (lo + hi) >>> 1; Entry left = null; if (lo < mid) left = buildFromSorted(level+1, lo, mid - 1, redLevel, - it, str, defaultVal); + it, str, defaultVal); // extract key and/or value from iterator or stream K key; @@ -2432,7 +2417,7 @@ public class TreeMap if (mid < hi) { Entry right = buildFromSorted(level+1, mid+1, hi, redLevel, - it, str, defaultVal); + it, str, defaultVal); middle.right = right; right.parent = middle; }