jdk/src/share/classes/java/util/TreeMap.java
author mduigou
Thu, 11 Nov 2010 11:01:25 -0800
changeset 7180 71cc1d6d7c4d
parent 5506 202f599c92aa
child 7518 0282db800fe1
permissions -rw-r--r--
6465367: (coll) Typo in TreeMap documentation Reviewed-by: alanb, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2428
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2428
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2428
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2428
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2428
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2428
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A Red-Black tree based {@link NavigableMap} implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The map is sorted according to the {@linkplain Comparable natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * ordering} of its keys, or by a {@link Comparator} provided at map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * creation time, depending on which constructor is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>This implementation provides guaranteed log(n) time cost for the
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    35
 * {@code containsKey}, {@code get}, {@code put} and {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * operations.  Algorithms are adaptations of those in Cormen, Leiserson, and
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    37
 * Rivest's <em>Introduction to Algorithms</em>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    39
 * <p>Note that the ordering maintained by a tree map, like any sorted map, and
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    40
 * whether or not an explicit comparator is provided, must be <em>consistent
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    41
 * with {@code equals}</em> if this sorted map is to correctly implement the
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    42
 * {@code Map} interface.  (See {@code Comparable} or {@code Comparator} for a
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    43
 * precise definition of <em>consistent with equals</em>.)  This is so because
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    44
 * the {@code Map} interface is defined in terms of the {@code equals}
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    45
 * operation, but a sorted map performs all key comparisons using its {@code
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    46
 * compareTo} (or {@code compare}) method, so two keys that are deemed equal by
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    47
 * this method are, from the standpoint of the sorted map, equal.  The behavior
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    48
 * of a sorted map <em>is</em> well-defined even if its ordering is
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    49
 * inconsistent with {@code equals}; it just fails to obey the general contract
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    50
 * of the {@code Map} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * If multiple threads access a map concurrently, and at least one of the
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    54
 * threads modifies the map structurally, it <em>must</em> be synchronized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * externally.  (A structural modification is any operation that adds or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * deletes one or more mappings; merely changing the value associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * with an existing key is not a structural modification.)  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * typically accomplished by synchronizing on some object that naturally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * unsynchronized access to the map: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    66
 * <p>The iterators returned by the {@code iterator} method of the collections
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * returned by all of this class's "collection view methods" are
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    68
 * <em>fail-fast</em>: if the map is structurally modified at any time after
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    69
 * the iterator is created, in any way except through the iterator's own
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    70
 * {@code remove} method, the iterator will throw a {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * arbitrary, non-deterministic behavior at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    78
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Therefore, it would be wrong to write a program that depended on this
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    80
 * exception for its correctness:   <em>the fail-fast behavior of iterators
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    81
 * should be used only to detect bugs.</em>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    83
 * <p>All {@code Map.Entry} pairs returned by methods in this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * and its views represent snapshots of mappings at the time they were
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    85
 * produced. They do <strong>not</strong> support the {@code Entry.setValue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * method. (Note however that it is possible to change mappings in the
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
    87
 * associated map using {@code put}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @author  Josh Bloch and Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @see Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @see HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @see Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @see Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
public class TreeMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    implements NavigableMap<K,V>, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The comparator used to maintain order in this tree map, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * null if it uses the natural ordering of its keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private final Comparator<? super K> comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private transient Entry<K,V> root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The number of entries in the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private transient int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The number of structural modifications to the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private transient int modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Constructs a new, empty tree map, using the natural ordering of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * keys.  All keys inserted into the map must implement the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Comparable} interface.  Furthermore, all such keys must be
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   134
     * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   135
     * a {@code ClassCastException} for any keys {@code k1} and
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   136
     * {@code k2} in the map.  If the user attempts to put a key into the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * map that violates this constraint (for example, the user attempts to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * put a string key into a map whose keys are integers), the
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   139
     * {@code put(Object key, Object value)} call will throw a
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   140
     * {@code ClassCastException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public TreeMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        comparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Constructs a new, empty tree map, ordered according to the given
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   148
     * comparator.  All keys inserted into the map must be <em>mutually
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   149
     * comparable</em> by the given comparator: {@code comparator.compare(k1,
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   150
     * k2)} must not throw a {@code ClassCastException} for any keys
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   151
     * {@code k1} and {@code k2} in the map.  If the user attempts to put
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   152
     * a key into the map that violates this constraint, the {@code put(Object
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   153
     * key, Object value)} call will throw a
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   154
     * {@code ClassCastException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param comparator the comparator that will be used to order this map.
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   157
     *        If {@code null}, the {@linkplain Comparable natural
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *        ordering} of the keys will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public TreeMap(Comparator<? super K> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.comparator = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Constructs a new tree map containing the same mappings as the given
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   166
     * map, ordered according to the <em>natural ordering</em> of its keys.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * All keys inserted into the new map must implement the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Comparable} interface.  Furthermore, all such keys must be
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   169
     * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   170
     * a {@code ClassCastException} for any keys {@code k1} and
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   171
     * {@code k2} in the map.  This method runs in n*log(n) time.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param  m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @throws ClassCastException if the keys in m are not {@link Comparable},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *         or are not mutually comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public TreeMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        comparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Constructs a new tree map containing the same mappings and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * using the same ordering as the specified sorted map.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * method runs in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param  m the sorted map whose mappings are to be placed in this map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *         and whose comparator is to be used to sort this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public TreeMap(SortedMap<K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        comparator = m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } catch (java.io.IOException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // Query Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   214
     * Returns {@code true} if this map contains a mapping for the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param key key whose presence in this map is to be tested
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   218
     * @return {@code true} if this map contains a mapping for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *         specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return getEntry(key) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   231
     * Returns {@code true} if this map maps one or more keys to the
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   232
     * specified value.  More formally, returns {@code true} if and only if
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   233
     * this map contains at least one mapping to a value {@code v} such
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   234
     * that {@code (value==null ? v==null : value.equals(v))}.  This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * operation will probably require time linear in the map size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * most implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param value value whose presence in this map is to be tested
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   239
     * @return {@code true} if a mapping to {@code value} exists;
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   240
     *         {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (valEquals(value, e.value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * {@code k} to a value {@code v} such that {@code key} compares
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * equal to {@code k} according to the map's ordering, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * method returns {@code v}; otherwise it returns {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   260
     * <p>A return value of {@code null} does not <em>necessarily</em>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        Entry<K,V> p = getEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return (p==null ? null : p.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public Comparator<? super K> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public K firstKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return key(getFirstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public K lastKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return key(getLastEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * These mappings replace any mappings that this map had for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param  map mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws ClassCastException if the class of a key or value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *         the specified map prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @throws NullPointerException if the specified map is null or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *         the specified map contains a null key and this map does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *         permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void putAll(Map<? extends K, ? extends V> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int mapSize = map.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (size==0 && mapSize!=0 && map instanceof SortedMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            Comparator c = ((SortedMap)map).comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (c == comparator || (c != null && c.equals(comparator))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                ++modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    buildFromSorted(mapSize, map.entrySet().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                    null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                } catch (java.io.IOException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        super.putAll(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   326
     * Returns this map's entry for the given key, or {@code null} if the map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * does not contain an entry for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   329
     * @return this map's entry for the given key, or {@code null} if the map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *         does not contain an entry for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    final Entry<K,V> getEntry(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // Offload comparator-based version for sake of performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (comparator != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return getEntryUsingComparator(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Comparable<? super K> k = (Comparable<? super K>) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            int cmp = k.compareTo(p.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Version of getEntry using comparator. Split off from getEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * for performance. (This is not worth doing for most methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * that are less dependent on comparator performance, but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * worthwhile here.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    final Entry<K,V> getEntryUsingComparator(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        K k = (K) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        Comparator<? super K> cpr = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (cpr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                int cmp = cpr.compare(k, p.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Gets the entry corresponding to the specified key; if no such entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * exists, returns the entry for the least key greater than the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * key; if no such entry exists (i.e., the greatest key in the Tree is less
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   385
     * than the specified key), returns {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    final Entry<K,V> getCeilingEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            int cmp = compare(key, p.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (cmp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                if (p.left != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } else if (cmp > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                if (p.right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    Entry<K,V> parent = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    Entry<K,V> ch = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    while (parent != null && ch == parent.right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        ch = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        parent = parent.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Gets the entry corresponding to the specified key; if no such entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * exists, returns the entry for the greatest key less than the specified
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   417
     * key; if no such entry exists, returns {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    final Entry<K,V> getFloorEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            int cmp = compare(key, p.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (cmp > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                if (p.right != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            } else if (cmp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                if (p.left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    Entry<K,V> parent = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    Entry<K,V> ch = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    while (parent != null && ch == parent.left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        ch = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        parent = parent.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Gets the entry for the least key greater than the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * key; if no such entry exists, returns the entry for the least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * key greater than the specified key; if no such entry exists
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   451
     * returns {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    final Entry<K,V> getHigherEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            int cmp = compare(key, p.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (cmp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (p.left != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (p.right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    Entry<K,V> parent = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    Entry<K,V> ch = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    while (parent != null && ch == parent.right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        ch = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        parent = parent.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Returns the entry for the greatest key less than the specified key; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * no such entry exists (i.e., the least key in the Tree is greater than
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   482
     * the specified key), returns {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    final Entry<K,V> getLowerEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            int cmp = compare(key, p.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (cmp > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                if (p.right != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if (p.left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    Entry<K,V> parent = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    Entry<K,V> ch = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    while (parent != null && ch == parent.left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        ch = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        parent = parent.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * If the map previously contained a mapping for the key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   518
     * @return the previous value associated with {@code key}, or
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   519
     *         {@code null} if there was no mapping for {@code key}.
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   520
     *         (A {@code null} return can also indicate that the map
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   521
     *         previously associated {@code null} with {@code key}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        Entry<K,V> t = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (t == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // TBD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            // 5045147: (coll) Adding null to an empty TreeSet should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            // throw NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            // compare(key, key); // type check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            root = new Entry<K,V>(key, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        int cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        Entry<K,V> parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        // split comparator and comparable paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        Comparator<? super K> cpr = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (cpr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                parent = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                cmp = cpr.compare(key, t.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    t = t.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    t = t.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    return t.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            } while (t != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            Comparable<? super K> k = (Comparable<? super K>) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                parent = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                cmp = k.compareTo(t.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    t = t.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    t = t.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    return t.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            } while (t != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        Entry<K,V> e = new Entry<K,V>(key, value, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            parent.left = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            parent.right = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        fixAfterInsertion(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Removes the mapping for this key from this TreeMap if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param  key key for which mapping should be removed
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   587
     * @return the previous value associated with {@code key}, or
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   588
     *         {@code null} if there was no mapping for {@code key}.
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   589
     *         (A {@code null} return can also indicate that the map
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   590
     *         previously associated {@code null} with {@code key}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        Entry<K,V> p = getEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        V oldValue = p.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        deleteEntry(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   618
     * Returns a shallow copy of this {@code TreeMap} instance. (The keys and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * values themselves are not cloned.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        TreeMap<K,V> clone = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            clone = (TreeMap<K,V>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // Put clone into "virgin" state (except for comparator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        clone.root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        clone.size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        clone.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        clone.entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        clone.navigableKeySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        clone.descendingMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        // Initialize clone with our mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            clone.buildFromSorted(size, entrySet().iterator(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        } catch (java.io.IOException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    // NavigableMap API methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public Map.Entry<K,V> firstEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return exportEntry(getFirstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public Map.Entry<K,V> lastEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return exportEntry(getLastEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    public Map.Entry<K,V> pollFirstEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        Entry<K,V> p = getFirstEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        Map.Entry<K,V> result = exportEntry(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            deleteEntry(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public Map.Entry<K,V> pollLastEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        Entry<K,V> p = getLastEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        Map.Entry<K,V> result = exportEntry(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            deleteEntry(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public Map.Entry<K,V> lowerEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        return exportEntry(getLowerEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public K lowerKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        return keyOrNull(getLowerEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    public Map.Entry<K,V> floorEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        return exportEntry(getFloorEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public K floorKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        return keyOrNull(getFloorEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    public Map.Entry<K,V> ceilingEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return exportEntry(getCeilingEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public K ceilingKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        return keyOrNull(getCeilingEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    public Map.Entry<K,V> higherEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        return exportEntry(getHigherEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public K higherKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return keyOrNull(getHigherEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Fields initialized to contain an instance of the entry set view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * the first time this view is requested.  Views are stateless, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * there's no reason to create more than one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    private transient EntrySet entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    private transient KeySet<K> navigableKeySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    private transient NavigableMap<K,V> descendingMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * The set's iterator returns the keys in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * while an iteration over the set is in progress (except through
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   792
     * the iterator's own {@code remove} operation), the results of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * which removes the corresponding mapping from the map, via the
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   795
     * {@code Iterator.remove}, {@code Set.remove},
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   796
     * {@code removeAll}, {@code retainAll}, and {@code clear}
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   797
     * operations.  It does not support the {@code add} or {@code addAll}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        return navigableKeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    public NavigableSet<K> navigableKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        KeySet<K> nks = navigableKeySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        return (nks != null) ? nks : (navigableKeySet = new KeySet(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public NavigableSet<K> descendingKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        return descendingMap().navigableKeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * The collection's iterator returns the values in ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * of the corresponding keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * modified while an iteration over the collection is in progress
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   826
     * (except through the iterator's own {@code remove} operation),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * supports element removal, which removes the corresponding
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   829
     * mapping from the map, via the {@code Iterator.remove},
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   830
     * {@code Collection.remove}, {@code removeAll},
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   831
     * {@code retainAll} and {@code clear} operations.  It does not
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   832
     * support the {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        return (vs != null) ? vs : (values = new Values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * The set's iterator returns the entries in ascending key order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * while an iteration over the set is in progress (except through
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   845
     * the iterator's own {@code remove} operation, or through the
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   846
     * {@code setValue} operation on a map entry returned by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * supports element removal, which removes the corresponding
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   849
     * mapping from the map, via the {@code Iterator.remove},
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   850
     * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   851
     * {@code clear} operations.  It does not support the
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   852
     * {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        EntrySet es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        return (es != null) ? es : (entrySet = new EntrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    public NavigableMap<K, V> descendingMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        NavigableMap<K, V> km = descendingMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        return (km != null) ? km :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            (descendingMap = new DescendingSubMap(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                                                  true, null, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                                  true, null, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @throws ClassCastException       {@inheritDoc}
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   872
     * @throws NullPointerException if {@code fromKey} or {@code toKey} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *         null and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                                    K toKey,   boolean toInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        return new AscendingSubMap(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                   false, fromKey, fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                   false, toKey,   toInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @throws ClassCastException       {@inheritDoc}
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   887
     * @throws NullPointerException if {@code toKey} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        return new AscendingSubMap(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                                   true,  null,  true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                   false, toKey, inclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @throws ClassCastException       {@inheritDoc}
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   901
     * @throws NullPointerException if {@code fromKey} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        return new AscendingSubMap(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                                   false, fromKey, inclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                                   true,  null,    true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @throws ClassCastException       {@inheritDoc}
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   915
     * @throws NullPointerException if {@code fromKey} or {@code toKey} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *         null and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    public SortedMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        return subMap(fromKey, true, toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @throws ClassCastException       {@inheritDoc}
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   926
     * @throws NullPointerException if {@code toKey} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public SortedMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return headMap(toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @throws ClassCastException       {@inheritDoc}
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
   937
     * @throws NullPointerException if {@code fromKey} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *         and this map uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *         does not permit null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    public SortedMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return tailMap(fromKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    // View class support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            return new ValueIterator(getFirstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return TreeMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            return TreeMap.this.containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                if (valEquals(e.getValue(), o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    deleteEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            TreeMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            return new EntryIterator(getFirstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            V value = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            Entry<K,V> p = getEntry(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            return p != null && valEquals(p.getValue(), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            V value = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            Entry<K,V> p = getEntry(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            if (p != null && valEquals(p.getValue(), value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                deleteEntry(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            return TreeMap.this.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            TreeMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Unlike Values and EntrySet, the KeySet class is static,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * delegating to a NavigableMap to allow use by SubMaps, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * outweighs the ugliness of needing type-tests for the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * Iterator methods that are defined appropriately in main versus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * submap classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    Iterator<K> keyIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return new KeyIterator(getFirstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    Iterator<K> descendingKeyIterator() {
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1025
        return new DescendingKeyIterator(getLastEntry());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        private final NavigableMap<E, Object> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        KeySet(NavigableMap<E,Object> map) { m = map; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            if (m instanceof TreeMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                return ((TreeMap<E,Object>)m).keyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                return (Iterator<E>)(((TreeMap.NavigableSubMap)m).keyIterator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            if (m instanceof TreeMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                return ((TreeMap<E,Object>)m).descendingKeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                return (Iterator<E>)(((TreeMap.NavigableSubMap)m).descendingKeyIterator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        public int size() { return m.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        public boolean isEmpty() { return m.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        public boolean contains(Object o) { return m.containsKey(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        public void clear() { m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        public E lower(E e) { return m.lowerKey(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        public E floor(E e) { return m.floorKey(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        public E ceiling(E e) { return m.ceilingKey(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        public E higher(E e) { return m.higherKey(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        public E first() { return m.firstKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        public E last() { return m.lastKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        public Comparator<? super E> comparator() { return m.comparator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        public E pollFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            Map.Entry<E,Object> e = m.pollFirstEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            return e == null? null : e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        public E pollLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            Map.Entry<E,Object> e = m.pollLastEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            return e == null? null : e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            int oldSize = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            m.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            return size() != oldSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                                      E toElement,   boolean toInclusive) {
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 493
diff changeset
  1072
            return new KeySet<E>(m.subMap(fromElement, fromInclusive,
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 493
diff changeset
  1073
                                          toElement,   toInclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        public NavigableSet<E> headSet(E toElement, boolean inclusive) {
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 493
diff changeset
  1076
            return new KeySet<E>(m.headMap(toElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 493
diff changeset
  1079
            return new KeySet<E>(m.tailMap(fromElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        public SortedSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            return subSet(fromElement, true, toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        public SortedSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            return headSet(toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        public SortedSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            return tailSet(fromElement, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        public NavigableSet<E> descendingSet() {
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 493
diff changeset
  1091
            return new KeySet(m.descendingMap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * Base class for TreeMap Iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    abstract class PrivateEntryIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        Entry<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        Entry<K,V> lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        int expectedModCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        PrivateEntryIterator(Entry<K,V> first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            next = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        public final boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            return next != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        final Entry<K,V> nextEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            Entry<K,V> e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            next = successor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            lastReturned = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        final Entry<K,V> prevEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            Entry<K,V> e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            next = predecessor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            lastReturned = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            // deleted entries are replaced by their successors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            if (lastReturned.left != null && lastReturned.right != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                next = lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            deleteEntry(lastReturned);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    final class EntryIterator extends PrivateEntryIterator<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        EntryIterator(Entry<K,V> first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            super(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            return nextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    final class ValueIterator extends PrivateEntryIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        ValueIterator(Entry<K,V> first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            super(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return nextEntry().value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    final class KeyIterator extends PrivateEntryIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        KeyIterator(Entry<K,V> first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            super(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            return nextEntry().key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    final class DescendingKeyIterator extends PrivateEntryIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        DescendingKeyIterator(Entry<K,V> first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            super(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return prevEntry().key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    // Little utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * Compares two keys using the correct comparison method for this TreeMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    final int compare(Object k1, Object k2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            : comparator.compare((K)k1, (K)k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Test two values for equality.  Differs from o1.equals(o2) only in
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
  1197
     * that it copes with {@code null} o1 properly.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    final static boolean valEquals(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        return (o1==null ? o2==null : o1.equals(o2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * Return SimpleImmutableEntry for entry, or null if null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    static <K,V> Map.Entry<K,V> exportEntry(TreeMap.Entry<K,V> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        return e == null? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            new AbstractMap.SimpleImmutableEntry<K,V>(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * Return key for entry, or null if null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    static <K,V> K keyOrNull(TreeMap.Entry<K,V> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        return e == null? null : e.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * Returns the key corresponding to the specified Entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @throws NoSuchElementException if the Entry is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    static <K> K key(Entry<K,?> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        if (e==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        return e.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    // SubMaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * Dummy value serving as unmatchable fence key for unbounded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * SubMapIterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    private static final Object UNBOUNDED = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    static abstract class NavigableSubMap<K,V> extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        implements NavigableMap<K,V>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
         * The backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        final TreeMap<K,V> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
         * Endpoints are represented as triples (fromStart, lo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
         * loInclusive) and (toEnd, hi, hiInclusive). If fromStart is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
         * true, then the low (absolute) bound is the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
         * backing map, and the other values are ignored. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
         * if loInclusive is true, lo is the inclusive bound, else lo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         * is the exclusive bound. Similarly for the upper bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        final K lo, hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        final boolean fromStart, toEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        final boolean loInclusive, hiInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        NavigableSubMap(TreeMap<K,V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                        boolean fromStart, K lo, boolean loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                        boolean toEnd,     K hi, boolean hiInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            if (!fromStart && !toEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                if (m.compare(lo, hi) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                    throw new IllegalArgumentException("fromKey > toKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                if (!fromStart) // type check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                    m.compare(lo, lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                if (!toEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                    m.compare(hi, hi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            this.fromStart = fromStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            this.lo = lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            this.loInclusive = loInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            this.toEnd = toEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            this.hi = hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            this.hiInclusive = hiInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        // internal utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        final boolean tooLow(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            if (!fromStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                int c = m.compare(key, lo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                if (c < 0 || (c == 0 && !loInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        final boolean tooHigh(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            if (!toEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                int c = m.compare(key, hi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                if (c > 0 || (c == 0 && !hiInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        final boolean inRange(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            return !tooLow(key) && !tooHigh(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        final boolean inClosedRange(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            return (fromStart || m.compare(key, lo) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                && (toEnd || m.compare(hi, key) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        final boolean inRange(Object key, boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            return inclusive ? inRange(key) : inClosedRange(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
         * Absolute versions of relation operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
         * Subclasses map to these using like-named "sub"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
         * versions that invert senses for descending maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        final TreeMap.Entry<K,V> absLowest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            TreeMap.Entry<K,V> e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                (fromStart ?  m.getFirstEntry() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                 (loInclusive ? m.getCeilingEntry(lo) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                                m.getHigherEntry(lo)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            return (e == null || tooHigh(e.key)) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        final TreeMap.Entry<K,V> absHighest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            TreeMap.Entry<K,V> e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                (toEnd ?  m.getLastEntry() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                 (hiInclusive ?  m.getFloorEntry(hi) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                                 m.getLowerEntry(hi)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            return (e == null || tooLow(e.key)) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        final TreeMap.Entry<K,V> absCeiling(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            if (tooLow(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                return absLowest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            TreeMap.Entry<K,V> e = m.getCeilingEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            return (e == null || tooHigh(e.key)) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        final TreeMap.Entry<K,V> absHigher(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            if (tooLow(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                return absLowest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            TreeMap.Entry<K,V> e = m.getHigherEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            return (e == null || tooHigh(e.key)) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        final TreeMap.Entry<K,V> absFloor(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            if (tooHigh(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                return absHighest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            TreeMap.Entry<K,V> e = m.getFloorEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            return (e == null || tooLow(e.key)) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        final TreeMap.Entry<K,V> absLower(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            if (tooHigh(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                return absHighest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            TreeMap.Entry<K,V> e = m.getLowerEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            return (e == null || tooLow(e.key)) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        /** Returns the absolute high fence for ascending traversal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        final TreeMap.Entry<K,V> absHighFence() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            return (toEnd ? null : (hiInclusive ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                                    m.getHigherEntry(hi) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                                    m.getCeilingEntry(hi)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        /** Return the absolute low fence for descending traversal  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        final TreeMap.Entry<K,V> absLowFence() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            return (fromStart ? null : (loInclusive ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                                        m.getLowerEntry(lo) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                                        m.getFloorEntry(lo)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        // Abstract methods defined in ascending vs descending classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        // These relay to the appropriate absolute versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        abstract TreeMap.Entry<K,V> subLowest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        abstract TreeMap.Entry<K,V> subHighest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        abstract TreeMap.Entry<K,V> subCeiling(K key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        abstract TreeMap.Entry<K,V> subHigher(K key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        abstract TreeMap.Entry<K,V> subFloor(K key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        abstract TreeMap.Entry<K,V> subLower(K key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        /** Returns ascending iterator from the perspective of this submap */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        abstract Iterator<K> keyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        /** Returns descending iterator from the perspective of this submap */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        abstract Iterator<K> descendingKeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        // public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            return (fromStart && toEnd) ? m.size() : entrySet().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        public final boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            return inRange(key) && m.containsKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        public final V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            if (!inRange(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                throw new IllegalArgumentException("key out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            return m.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        public final V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            return !inRange(key)? null :  m.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        public final V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            return !inRange(key)? null  : m.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        public final Map.Entry<K,V> ceilingEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            return exportEntry(subCeiling(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        public final K ceilingKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            return keyOrNull(subCeiling(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        public final Map.Entry<K,V> higherEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            return exportEntry(subHigher(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        public final K higherKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            return keyOrNull(subHigher(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        public final Map.Entry<K,V> floorEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            return exportEntry(subFloor(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        public final K floorKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            return keyOrNull(subFloor(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        public final Map.Entry<K,V> lowerEntry(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            return exportEntry(subLower(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        public final K lowerKey(K key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            return keyOrNull(subLower(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        public final K firstKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            return key(subLowest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        public final K lastKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            return key(subHighest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        public final Map.Entry<K,V> firstEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            return exportEntry(subLowest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        public final Map.Entry<K,V> lastEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            return exportEntry(subHighest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        public final Map.Entry<K,V> pollFirstEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            TreeMap.Entry<K,V> e = subLowest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            Map.Entry<K,V> result = exportEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                m.deleteEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        public final Map.Entry<K,V> pollLastEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            TreeMap.Entry<K,V> e = subHighest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            Map.Entry<K,V> result = exportEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                m.deleteEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        transient NavigableMap<K,V> descendingMapView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        transient EntrySetView entrySetView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        transient KeySet<K> navigableKeySetView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        public final NavigableSet<K> navigableKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            KeySet<K> nksv = navigableKeySetView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            return (nksv != null) ? nksv :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                (navigableKeySetView = new TreeMap.KeySet(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        public final Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            return navigableKeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        public NavigableSet<K> descendingKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            return descendingMap().navigableKeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        public final SortedMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            return subMap(fromKey, true, toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        public final SortedMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            return headMap(toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        public final SortedMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            return tailMap(fromKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        // View classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            private transient int size = -1, sizeModCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                if (fromStart && toEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                    return m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                if (size == -1 || sizeModCount != m.modCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                    sizeModCount = m.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                    Iterator i = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                        i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                TreeMap.Entry<K,V> n = absLowest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                return n == null || tooHigh(n.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                K key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                if (!inRange(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                TreeMap.Entry node = m.getEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                return node != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                    valEquals(node.getValue(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                K key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                if (!inRange(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                TreeMap.Entry<K,V> node = m.getEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                if (node!=null && valEquals(node.getValue(),entry.getValue())){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                    m.deleteEntry(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
         * Iterators for SubMaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        abstract class SubMapIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            TreeMap.Entry<K,V> lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            TreeMap.Entry<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            final Object fenceKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            int expectedModCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            SubMapIterator(TreeMap.Entry<K,V> first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                           TreeMap.Entry<K,V> fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                expectedModCount = m.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                next = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                fenceKey = fence == null ? UNBOUNDED : fence.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            public final boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                return next != null && next.key != fenceKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            final TreeMap.Entry<K,V> nextEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                TreeMap.Entry<K,V> e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                if (e == null || e.key == fenceKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                if (m.modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                    throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                next = successor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                lastReturned = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            final TreeMap.Entry<K,V> prevEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                TreeMap.Entry<K,V> e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                if (e == null || e.key == fenceKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                if (m.modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                    throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                next = predecessor(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                lastReturned = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            final void removeAscending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                    throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                if (m.modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                    throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                // deleted entries are replaced by their successors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                if (lastReturned.left != null && lastReturned.right != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                    next = lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                m.deleteEntry(lastReturned);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                expectedModCount = m.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            final void removeDescending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                    throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                if (m.modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                    throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                m.deleteEntry(lastReturned);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                expectedModCount = m.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            SubMapEntryIterator(TreeMap.Entry<K,V> first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                                TreeMap.Entry<K,V> fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                super(first, fence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                return nextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                removeAscending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        final class SubMapKeyIterator extends SubMapIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            SubMapKeyIterator(TreeMap.Entry<K,V> first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                              TreeMap.Entry<K,V> fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                super(first, fence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                return nextEntry().key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                removeAscending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        final class DescendingSubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            DescendingSubMapEntryIterator(TreeMap.Entry<K,V> last,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                                          TreeMap.Entry<K,V> fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                super(last, fence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                return prevEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                removeDescending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        final class DescendingSubMapKeyIterator extends SubMapIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            DescendingSubMapKeyIterator(TreeMap.Entry<K,V> last,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                                        TreeMap.Entry<K,V> fence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                super(last, fence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                return prevEntry().key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                removeDescending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    static final class AscendingSubMap<K,V> extends NavigableSubMap<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        private static final long serialVersionUID = 912986545866124060L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        AscendingSubMap(TreeMap<K,V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                        boolean fromStart, K lo, boolean loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                        boolean toEnd,     K hi, boolean hiInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        public Comparator<? super K> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            return m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                                        K toKey,   boolean toInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            if (!inRange(fromKey, fromInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                throw new IllegalArgumentException("fromKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            if (!inRange(toKey, toInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                throw new IllegalArgumentException("toKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            return new AscendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                                       false, fromKey, fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                                       false, toKey,   toInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            if (!inRange(toKey, inclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                throw new IllegalArgumentException("toKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            return new AscendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                                       fromStart, lo,    loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                                       false,     toKey, inclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            if (!inRange(fromKey, inclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                throw new IllegalArgumentException("fromKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            return new AscendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                                       false, fromKey, inclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                                       toEnd, hi,      hiInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        public NavigableMap<K,V> descendingMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            NavigableMap<K,V> mv = descendingMapView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            return (mv != null) ? mv :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                (descendingMapView =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                 new DescendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                                      fromStart, lo, loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                                      toEnd,     hi, hiInclusive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        Iterator<K> keyIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            return new SubMapKeyIterator(absLowest(), absHighFence());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        Iterator<K> descendingKeyIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        final class AscendingEntrySetView extends EntrySetView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                return new SubMapEntryIterator(absLowest(), absHighFence());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            EntrySetView es = entrySetView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            return (es != null) ? es : new AscendingEntrySetView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        TreeMap.Entry<K,V> subLowest()       { return absLowest(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        TreeMap.Entry<K,V> subHighest()      { return absHighest(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        TreeMap.Entry<K,V> subCeiling(K key) { return absCeiling(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        TreeMap.Entry<K,V> subHigher(K key)  { return absHigher(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        TreeMap.Entry<K,V> subFloor(K key)   { return absFloor(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        TreeMap.Entry<K,V> subLower(K key)   { return absLower(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    static final class DescendingSubMap<K,V>  extends NavigableSubMap<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        private static final long serialVersionUID = 912986545866120460L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        DescendingSubMap(TreeMap<K,V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                        boolean fromStart, K lo, boolean loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                        boolean toEnd,     K hi, boolean hiInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        private final Comparator<? super K> reverseComparator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            Collections.reverseOrder(m.comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        public Comparator<? super K> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            return reverseComparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                                        K toKey,   boolean toInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            if (!inRange(fromKey, fromInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                throw new IllegalArgumentException("fromKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            if (!inRange(toKey, toInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                throw new IllegalArgumentException("toKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            return new DescendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                                        false, toKey,   toInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                                        false, fromKey, fromInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            if (!inRange(toKey, inclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                throw new IllegalArgumentException("toKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            return new DescendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                                        false, toKey, inclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                                        toEnd, hi,    hiInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            if (!inRange(fromKey, inclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                throw new IllegalArgumentException("fromKey out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            return new DescendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                                        fromStart, lo, loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                                        false, fromKey, inclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        public NavigableMap<K,V> descendingMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            NavigableMap<K,V> mv = descendingMapView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            return (mv != null) ? mv :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                (descendingMapView =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                 new AscendingSubMap(m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                                     fromStart, lo, loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                     toEnd,     hi, hiInclusive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        Iterator<K> keyIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        Iterator<K> descendingKeyIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            return new SubMapKeyIterator(absLowest(), absHighFence());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        final class DescendingEntrySetView extends EntrySetView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                return new DescendingSubMapEntryIterator(absHighest(), absLowFence());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            EntrySetView es = entrySetView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            return (es != null) ? es : new DescendingEntrySetView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        TreeMap.Entry<K,V> subLowest()       { return absHighest(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        TreeMap.Entry<K,V> subHighest()      { return absLowest(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        TreeMap.Entry<K,V> subCeiling(K key) { return absFloor(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        TreeMap.Entry<K,V> subHigher(K key)  { return absLower(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        TreeMap.Entry<K,V> subFloor(K key)   { return absCeiling(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        TreeMap.Entry<K,V> subLower(K key)   { return absHigher(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * This class exists solely for the sake of serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * compatibility with previous releases of TreeMap that did not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * support NavigableMap.  It translates an old-version SubMap into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * a new-version AscendingSubMap. This class is never otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    private class SubMap extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        implements SortedMap<K,V>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        private static final long serialVersionUID = -6520786458950516097L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        private boolean fromStart = false, toEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        private K fromKey, toKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            return new AscendingSubMap(TreeMap.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                                       fromStart, fromKey, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                                       toEnd, toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        public Set<Map.Entry<K,V>> entrySet() { throw new InternalError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        public K lastKey() { throw new InternalError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        public K firstKey() { throw new InternalError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        public SortedMap<K,V> subMap(K fromKey, K toKey) { throw new InternalError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        public SortedMap<K,V> headMap(K toKey) { throw new InternalError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        public SortedMap<K,V> tailMap(K fromKey) { throw new InternalError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        public Comparator<? super K> comparator() { throw new InternalError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    // Red-black mechanics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    private static final boolean RED   = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    private static final boolean BLACK = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * Node in the Tree.  Doubles as a means to pass key-value pairs back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * user (see Map.Entry).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    static final class Entry<K,V> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        Entry<K,V> left = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        Entry<K,V> right = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        Entry<K,V> parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        boolean color = BLACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
         * Make a new cell with given key, value, and parent, and with
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
  1901
         * {@code null} child links, and BLACK color.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        Entry(K key, V value, Entry<K,V> parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         * Returns the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         * @return the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        public K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
         * Returns the value associated with the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
         * @return the value associated with the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        public V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
         * Replaces the value currently associated with the key with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
         * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         * @return the value associated with the key before this method was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
         *         called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            V oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            return valEquals(key,e.getKey()) && valEquals(value,e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            int keyHash = (key==null ? 0 : key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            int valueHash = (value==null ? 0 : value.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            return keyHash ^ valueHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            return key + "=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * Returns the first Entry in the TreeMap (according to the TreeMap's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * key-sort function).  Returns null if the TreeMap is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    final Entry<K,V> getFirstEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            while (p.left != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * Returns the last Entry in the TreeMap (according to the TreeMap's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * key-sort function).  Returns null if the TreeMap is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    final Entry<K,V> getLastEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        Entry<K,V> p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            while (p.right != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * Returns the successor of the specified Entry, or null if no such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        if (t == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        else if (t.right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            Entry<K,V> p = t.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            while (p.left != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                p = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            Entry<K,V> p = t.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            Entry<K,V> ch = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            while (p != null && ch == p.right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                ch = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                p = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * Returns the predecessor of the specified Entry, or null if no such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    static <K,V> Entry<K,V> predecessor(Entry<K,V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        if (t == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        else if (t.left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            Entry<K,V> p = t.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            while (p.right != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                p = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            Entry<K,V> p = t.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            Entry<K,V> ch = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            while (p != null && ch == p.left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                ch = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                p = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * Balancing operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * Implementations of rebalancings during insertion and deletion are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * slightly different than the CLR version.  Rather than using dummy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * nilnodes, we use a set of accessors that deal properly with null.  They
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * are used to avoid messiness surrounding nullness checks in the main
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    private static <K,V> boolean colorOf(Entry<K,V> p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        return (p == null ? BLACK : p.color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
    private static <K,V> Entry<K,V> parentOf(Entry<K,V> p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        return (p == null ? null: p.parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
    private static <K,V> void setColor(Entry<K,V> p, boolean c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            p.color = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    private static <K,V> Entry<K,V> leftOf(Entry<K,V> p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        return (p == null) ? null: p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
    private static <K,V> Entry<K,V> rightOf(Entry<K,V> p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        return (p == null) ? null: p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
    /** From CLR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    private void rotateLeft(Entry<K,V> p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
            Entry<K,V> r = p.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            p.right = r.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            if (r.left != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                r.left.parent = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            r.parent = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            if (p.parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                root = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
            else if (p.parent.left == p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                p.parent.left = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                p.parent.right = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            r.left = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            p.parent = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    /** From CLR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    private void rotateRight(Entry<K,V> p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            Entry<K,V> l = p.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            p.left = l.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            if (l.right != null) l.right.parent = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            l.parent = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            if (p.parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                root = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            else if (p.parent.right == p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                p.parent.right = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            else p.parent.left = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            l.right = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            p.parent = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    /** From CLR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    private void fixAfterInsertion(Entry<K,V> x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        x.color = RED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        while (x != null && x != root && x.parent.color == RED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                Entry<K,V> y = rightOf(parentOf(parentOf(x)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                if (colorOf(y) == RED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                    setColor(parentOf(x), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                    setColor(y, BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                    setColor(parentOf(parentOf(x)), RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    x = parentOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                    if (x == rightOf(parentOf(x))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        x = parentOf(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                        rotateLeft(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                    setColor(parentOf(x), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                    setColor(parentOf(parentOf(x)), RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                    rotateRight(parentOf(parentOf(x)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                Entry<K,V> y = leftOf(parentOf(parentOf(x)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                if (colorOf(y) == RED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                    setColor(parentOf(x), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                    setColor(y, BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                    setColor(parentOf(parentOf(x)), RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                    x = parentOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                    if (x == leftOf(parentOf(x))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                        x = parentOf(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                        rotateRight(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                    setColor(parentOf(x), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                    setColor(parentOf(parentOf(x)), RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                    rotateLeft(parentOf(parentOf(x)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        root.color = BLACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * Delete node p, and then rebalance the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
    private void deleteEntry(Entry<K,V> p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        // If strictly internal, copy successor's element to p and then make p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        // point to successor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        if (p.left != null && p.right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            Entry<K,V> s = successor (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            p.key = s.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            p.value = s.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            p = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        } // p has 2 children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        // Start fixup at replacement node, if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
        Entry<K,V> replacement = (p.left != null ? p.left : p.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        if (replacement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            // Link replacement to parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            replacement.parent = p.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            if (p.parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                root = replacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            else if (p == p.parent.left)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                p.parent.left  = replacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                p.parent.right = replacement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            // Null out links so they are OK to use by fixAfterDeletion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            p.left = p.right = p.parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            // Fix replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            if (p.color == BLACK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                fixAfterDeletion(replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        } else if (p.parent == null) { // return if we are the only node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        } else { //  No children. Use self as phantom replacement and unlink.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            if (p.color == BLACK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                fixAfterDeletion(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            if (p.parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                if (p == p.parent.left)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                    p.parent.left = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                else if (p == p.parent.right)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                    p.parent.right = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                p.parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    /** From CLR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    private void fixAfterDeletion(Entry<K,V> x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        while (x != root && colorOf(x) == BLACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            if (x == leftOf(parentOf(x))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                Entry<K,V> sib = rightOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                if (colorOf(sib) == RED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                    setColor(sib, BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                    setColor(parentOf(x), RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                    rotateLeft(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                    sib = rightOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                if (colorOf(leftOf(sib))  == BLACK &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    colorOf(rightOf(sib)) == BLACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    setColor(sib, RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                    x = parentOf(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                    if (colorOf(rightOf(sib)) == BLACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                        setColor(leftOf(sib), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                        setColor(sib, RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                        rotateRight(sib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                        sib = rightOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                    setColor(sib, colorOf(parentOf(x)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    setColor(parentOf(x), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                    setColor(rightOf(sib), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                    rotateLeft(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                    x = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            } else { // symmetric
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                Entry<K,V> sib = leftOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                if (colorOf(sib) == RED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                    setColor(sib, BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                    setColor(parentOf(x), RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                    rotateRight(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                    sib = leftOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                if (colorOf(rightOf(sib)) == BLACK &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                    colorOf(leftOf(sib)) == BLACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                    setColor(sib, RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                    x = parentOf(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                    if (colorOf(leftOf(sib)) == BLACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                        setColor(rightOf(sib), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                        setColor(sib, RED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                        rotateLeft(sib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                        sib = leftOf(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    setColor(sib, colorOf(parentOf(x)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                    setColor(parentOf(x), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                    setColor(leftOf(sib), BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                    rotateRight(parentOf(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                    x = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        setColor(x, BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
    private static final long serialVersionUID = 919286545866124006L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    /**
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
  2253
     * Save the state of the {@code TreeMap} instance to a stream (i.e.,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     *
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
  2256
     * @serialData The <em>size</em> of the TreeMap (the number of key-value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     *             mappings) is emitted (int), followed by the key (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     *             and value (Object) for each key-value mapping represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     *             by the TreeMap. The key-value mappings are emitted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     *             key-order (as determined by the TreeMap's Comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     *             or by the keys' natural ordering if the TreeMap has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     *             Comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        // Write out the Comparator and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        // Write out keys and values (alternating)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        for (Iterator<Map.Entry<K,V>> i = entrySet().iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            Map.Entry<K,V> e = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            s.writeObject(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            s.writeObject(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    /**
7180
71cc1d6d7c4d 6465367: (coll) Typo in TreeMap documentation
mduigou
parents: 5506
diff changeset
  2281
     * Reconstitute the {@code TreeMap} instance from a stream (i.e.,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
    private void readObject(final java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        // Read in the Comparator and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        // Read in size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
        buildFromSorted(size, null, s, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
    /** Intended to be called only from TreeSet.readObject */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
    void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        buildFromSorted(size, null, s, defaultVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
    /** Intended to be called only from TreeSet.addAll */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
    void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            buildFromSorted(set.size(), set.iterator(), null, defaultVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        } catch (java.io.IOException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        } catch (ClassNotFoundException cannotHappen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * Linear time tree building algorithm from sorted data.  Can accept keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * and/or values from iterator or stream. This leads to too many
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * parameters, but seems better than alternatives.  The four formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * that this method accepts are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     *    1) An iterator of Map.Entries.  (it != null, defaultVal == null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     *    2) An iterator of keys.         (it != null, defaultVal != null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     *    3) A stream of alternating serialized keys and values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     *                                   (it == null, defaultVal == null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     *    4) A stream of serialized keys. (it == null, defaultVal != null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * It is assumed that the comparator of the TreeMap is already set prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * to calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * @param size the number of keys (or key-value pairs) to be read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     *        the iterator or stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * @param it If non-null, new entries are created from entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     *        or keys read from this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * @param str If non-null, new entries are created from keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     *        possibly values read from this stream in serialized form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     *        Exactly one of it and str should be non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @param defaultVal if non-null, this default value is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     *        each value in the map.  If null, each value is read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     *        iterator or stream, as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     * @throws IOException propagated from stream reads. This cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
     *         occur if str is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     * @throws ClassNotFoundException propagated from readObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     *         This cannot occur if str is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    private void buildFromSorted(int size, Iterator it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                                 java.io.ObjectInputStream str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                                 V defaultVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        throws  java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        root = buildFromSorted(0, 0, size-1, computeRedLevel(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                               it, str, defaultVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * Recursive "helper method" that does the real work of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * previous method.  Identically named parameters have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * identical definitions.  Additional parameters are documented below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * It is assumed that the comparator and size fields of the TreeMap are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * already set prior to calling this method.  (It ignores both fields.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * @param level the current level of tree. Initial call should be 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * @param lo the first element index of this subtree. Initial should be 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * @param hi the last element index of this subtree.  Initial should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     *        size-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * @param redLevel the level at which nodes should be red.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     *        Must be equal to computeRedLevel for tree of this size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
    private final Entry<K,V> buildFromSorted(int level, int lo, int hi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                                             int redLevel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                                             Iterator it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                                             java.io.ObjectInputStream str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                                             V defaultVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        throws  java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
         * Strategy: The root is the middlemost element. To get to it, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
         * have to first recursively construct the entire left subtree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
         * so as to grab all of its elements. We can then proceed with right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
         * subtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
         * The lo and hi arguments are the minimum and maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
         * indices to pull out of the iterator or stream for current subtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
         * They are not actually indexed, we just proceed sequentially,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
         * ensuring that items are extracted in corresponding order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        if (hi < lo) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        int mid = (lo + hi) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        Entry<K,V> left  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        if (lo < mid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            left = buildFromSorted(level+1, lo, mid - 1, redLevel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                                   it, str, defaultVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        // extract key and/or value from iterator or stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        if (it != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            if (defaultVal==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                Map.Entry<K,V> entry = (Map.Entry<K,V>)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                key = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                value = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                key = (K)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                value = defaultVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        } else { // use stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            key = (K) str.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            value = (defaultVal != null ? defaultVal : (V) str.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        Entry<K,V> middle =  new Entry<K,V>(key, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        // color nodes in non-full bottommost level red
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        if (level == redLevel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            middle.color = RED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        if (left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            middle.left = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            left.parent = middle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        if (mid < hi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
            Entry<K,V> right = buildFromSorted(level+1, mid+1, hi, redLevel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                                               it, str, defaultVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
            middle.right = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            right.parent = middle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        return middle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * Find the level down to which to assign all nodes BLACK.  This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * last `full' level of the complete binary tree produced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * buildTree. The remaining nodes are colored RED. (This makes a `nice'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * set of color assignments wrt future insertions.) This level number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * computed by finding the number of splits needed to reach the zeroeth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * node.  (The answer is ~lg(N), but in any case must be computed by same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * quick O(lg(N)) loop.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
    private static int computeRedLevel(int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        int level = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        for (int m = sz - 1; m >= 0; m = m / 2 - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            level++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        return level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
}