/branches/ot/external/gridsim/trunk/source/gridsim/parallel/profile/LinkedTreeMap.java
Java | 1870 lines | 980 code | 201 blank | 689 comment | 306 complexity | 6fe3b054a9a852d953633f37a8e829b7 MD5 | raw file
Possible License(s): AGPL-1.0
Large files files are truncated, but you can click here to view the full file
- /*
- * @(#)TreeMap.java 1.65 04/02/19
- *
- * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package gridsim.parallel.profile;
- import java.util.AbstractCollection;
- import java.util.AbstractMap;
- import java.util.AbstractSet;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.ConcurrentModificationException;
- import java.util.HashMap;
- import java.util.Hashtable;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.NoSuchElementException;
- import java.util.Set;
- import java.util.SortedMap;
- import java.util.SortedSet;
- /**
- * Red-Black tree based implementation of the <tt>SortedMap</tt> interface.
- * This class guarantees that the map will be in ascending key order, sorted
- * according to the <i>natural order</i> for the key's class (see
- * <tt>Comparable</tt>), or by the comparator provided at creation time,
- * depending on which constructor is used.<p>
- *
- * This implementation provides guaranteed log(n) time cost for the
- * <tt>containsKey</tt>, <tt>get</tt>, <tt>put</tt> and <tt>remove</tt>
- * operations. Algorithms are adaptations of those in Cormen, Leiserson, and
- * Rivest's <I>Introduction to Algorithms</I>.<p>
- *
- * Note that the ordering maintained by a sorted map (whether or not an
- * explicit comparator is provided) must be <i>consistent with equals</i> if
- * this sorted map is to correctly implement the <tt>Map</tt> interface. (See
- * <tt>Comparable</tt> or <tt>Comparator</tt> for a precise definition of
- * <i>consistent with equals</i>.) This is so because the <tt>Map</tt>
- * interface is defined in terms of the equals operation, but a map performs
- * all key comparisons using its <tt>compareTo</tt> (or <tt>compare</tt>)
- * method, so two keys that are deemed equal by this method are, from the
- * standpoint of the sorted map, equal. The behavior of a sorted map
- * <i>is</i> well-defined even if its ordering is inconsistent with equals; it
- * just fails to obey the general contract of the <tt>Map</tt> interface.<p>
- *
- * <b>Note that this implementation is not synchronized.</b> If multiple
- * threads access a map concurrently, and at least one of the threads modifies
- * the map structurally, it <i>must</i> be synchronized externally. (A
- * structural modification is any operation that adds or deletes one or more
- * mappings; merely changing the value associated with an existing key is not
- * a structural modification.) This is typically accomplished by
- * synchronizing on some object that naturally encapsulates the map. If no
- * such object exists, the map should be "wrapped" using the
- * <tt>Collections.synchronizedMap</tt> method. This is best done at creation
- * time, to prevent accidental unsynchronized access to the map:
- * <pre>
- * Map m = Collections.synchronizedMap(new TreeMap(...));
- * </pre><p>
- *
- * The iterators returned by all of this class's "collection view methods" are
- * <i>fail-fast</i>: if the map is structurally modified at any time after the
- * iterator is created, in any way except through the iterator's own
- * <tt>remove</tt> or <tt>add</tt> methods, the iterator throws a
- * <tt>ConcurrentModificationException</tt>. Thus, in the face of concurrent
- * modification, the iterator fails quickly and cleanly, rather than risking
- * arbitrary, non-deterministic behavior at an undetermined time in the
- * future.
- *
- * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
- * as it is, generally speaking, impossible to make any hard guarantees in the
- * presence of unsynchronized concurrent modification. Fail-fast iterators
- * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
- * Therefore, it would be wrong to write a program that depended on this
- * exception for its correctness: <i>the fail-fast behavior of iterators
- * should be used only to detect bugs.</i><p>
- *
- * This class is a member of the
- * <a href="{@docRoot}/../guide/collections/index.html">
- * Java Collections Framework</a>.
- *
- * @author Josh Bloch and Doug Lea
- * @version 1.65, 02/19/04
- *
- * @author Marcos Assuncao has modified this class to support a linked list
- * between the elements of the red black tree and speed up iterations.
- * In that sense, an element in the tree has one reference to its predecessor
- * and another to its successor.
- *
- * @see Map
- * @see HashMap
- * @see Hashtable
- * @see Comparable
- * @see Comparator
- * @see Collection
- * @see Collections#synchronizedMap(Map)
- * @since 1.2
- */
- public class LinkedTreeMap<K,V>
- implements SortedMap<K,V>, Cloneable, java.io.Serializable
- {
- /**
- * The Comparator used to maintain order in this TreeMap, or
- * null if this TreeMap uses its elements natural ordering.
- *
- * @serial
- */
- private Comparator<? super K> comparator = null;
- private transient Entry<K,V> root = null;
- /**
- * The number of entries in the tree
- */
- private transient int size = 0;
- /**
- * The number of structural modifications to the tree.
- */
- private transient int modCount = 0;
- private void incrementSize() { modCount++; size++; }
- private void decrementSize() { modCount++; size--; }
- /**
- * Constructs a new, empty map, sorted according to the keys' natural
- * order. All keys inserted into the map must implement the
- * <tt>Comparable</tt> interface. Furthermore, all such keys must be
- * <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw a
- * ClassCastException for any elements <tt>k1</tt> and <tt>k2</tt> in the
- * map. If the user attempts to put a key into the map that violates this
- * constraint (for example, the user attempts to put a string key into a
- * map whose keys are integers), the <tt>put(Object key, Object
- * value)</tt> call will throw a <tt>ClassCastException</tt>.
- *
- * @see Comparable
- */
- public LinkedTreeMap() {
- }
- /**
- * Constructs a new, empty map, sorted according to the given comparator.
- * All keys inserted into the map must be <i>mutually comparable</i> by
- * the given comparator: <tt>comparator.compare(k1, k2)</tt> must not
- * throw a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and
- * <tt>k2</tt> in the map. If the user attempts to put a key into the
- * map that violates this constraint, the <tt>put(Object key, Object
- * value)</tt> call will throw a <tt>ClassCastException</tt>.
- *
- * @param c the comparator that will be used to sort this map. A
- * <tt>null</tt> value indicates that the keys' <i>natural
- * ordering</i> should be used.
- */
- public LinkedTreeMap(Comparator<? super K> c) {
- this.comparator = c;
- }
- /**
- * Constructs a new map containing the same mappings as the given map,
- * sorted according to the keys' <i>natural order</i>. All keys inserted
- * into the new map must implement the <tt>Comparable</tt> interface.
- * Furthermore, all such keys must be <i>mutually comparable</i>:
- * <tt>k1.compareTo(k2)</tt> must not throw a <tt>ClassCastException</tt>
- * for any elements <tt>k1</tt> and <tt>k2</tt> in the map. This method
- * runs in n*log(n) time.
- *
- * @param m the map whose mappings are to be placed in this map.
- * @throws ClassCastException the keys in t are not Comparable, or
- * are not mutually comparable.
- * @throws NullPointerException if the specified map is null.
- */
- public LinkedTreeMap(Map<? extends K, ? extends V> m) {
- putAll(m);
- }
- /**
- * Constructs a new map containing the same mappings as the given
- * <tt>SortedMap</tt>, sorted according to the same ordering. This method
- * runs in linear time.
- *
- * @param m the sorted map whose mappings are to be placed in this map,
- * and whose comparator is to be used to sort this map.
- * @throws NullPointerException if the specified sorted map is null.
- */
- public LinkedTreeMap(SortedMap<K, ? extends V> m) {
- comparator = m.comparator();
- try {
- buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
- } catch (java.io.IOException cannotHappen) {
- } catch (ClassNotFoundException cannotHappen) {
- }
- }
- // Query Operations
- /**
- * Returns the number of key-value mappings in this map.
- *
- * @return the number of key-value mappings in this map.
- */
- public int size() {
- return size;
- }
- /**
- * Returns <tt>true</tt> if this map contains no key-value mappings. <p>
- *
- * This implementation returns <tt>size() == 0</tt>.
- *
- * @return <tt>true</tt> if this map contains no key-value mappings.
- */
- public boolean isEmpty() {
- return size() == 0;
- }
-
- /**
- * Returns <tt>true</tt> if this map contains a mapping for the specified
- * key.
- *
- * @param key key whose presence in this map is to be tested.
- *
- * @return <tt>true</tt> if this map contains a mapping for the
- * specified key.
- * @throws ClassCastException if the key cannot be compared with the keys
- * currently in the map.
- * @throws NullPointerException key is <tt>null</tt> and this map uses
- * natural ordering, or its comparator does not tolerate
- * <tt>null</tt> keys.
- */
- public boolean containsKey(Object key) {
- return getEntry(key) != null;
- }
- /**
- * Returns <tt>true</tt> if this map maps one or more keys to the
- * specified value. More formally, returns <tt>true</tt> if and only if
- * this map contains at least one mapping to a value <tt>v</tt> such
- * that <tt>(value==null ? v==null : value.equals(v))</tt>. This
- * operation will probably require time linear in the Map size for most
- * implementations of Map.
- *
- * @param value value whose presence in this Map is to be tested.
- * @return <tt>true</tt> if a mapping to <tt>value</tt> exists;
- * <tt>false</tt> otherwise.
- * @since 1.2
- */
- public boolean containsValue(Object value) {
- return (root==null ? false :
- (value==null ? valueSearchNull(root)
- : valueSearchNonNull(root, value)));
- }
- private boolean valueSearchNull(Entry n) {
- if (n.value == null)
- return true;
- // Check left and right subtrees for value
- return (n.left != null && valueSearchNull(n.left)) ||
- (n.right != null && valueSearchNull(n.right));
- }
- private boolean valueSearchNonNull(Entry n, Object value) {
- // Check this node for the value
- if (value.equals(n.value))
- return true;
- // Check left and right subtrees for value
- return (n.left != null && valueSearchNonNull(n.left, value)) ||
- (n.right != null && valueSearchNonNull(n.right, value));
- }
- /**
- * Returns the value to which this map maps the specified key. Returns
- * <tt>null</tt> if the map contains no mapping for this key. A return
- * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
- * map contains no mapping for the key; it's also possible that the map
- * explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>
- * operation may be used to distinguish these two cases.
- *
- * @param key key whose associated value is to be returned.
- * @return the value to which this map maps the specified key, or
- * <tt>null</tt> if the map contains no mapping for the key.
- * @throws ClassCastException key cannot be compared with the keys
- * currently in the map.
- * @throws NullPointerException key is <tt>null</tt> and this map uses
- * natural ordering, or its comparator does not tolerate
- * <tt>null</tt> keys.
- *
- * @see #containsKey(Object)
- */
- public V get(Object key) {
- Entry<K,V> p = getEntry(key);
- return (p==null ? null : p.value);
- }
- /**
- * Returns the comparator used to order this map, or <tt>null</tt> if this
- * map uses its keys' natural order.
- *
- * @return the comparator associated with this sorted map, or
- * <tt>null</tt> if it uses its keys' natural sort method.
- */
- public Comparator<? super K> comparator() {
- return comparator;
- }
- /**
- * Returns the first (lowest) key currently in this sorted map.
- *
- * @return the first (lowest) key currently in this sorted map.
- * @throws NoSuchElementException Map is empty.
- */
- public K firstKey() {
- return key(firstEntry());
- }
-
- /**
- * Returns the last (highest) key currently in this sorted map.
- *
- * @return the last (highest) key currently in this sorted map.
- * @throws NoSuchElementException Map is empty.
- */
- public K lastKey() {
- return key(lastEntry());
- }
- /**
- * Copies all of the mappings from the specified map to this map. These
- * mappings replace any mappings that this map had for any of the keys
- * currently in the specified map.
- *
- * @param map mappings to be stored in this map.
- * @throws ClassCastException class of a key or value in the specified
- * map prevents it from being stored in this map.
- *
- * @throws NullPointerException if the given map is <tt>null</tt> or
- * this map does not permit <tt>null</tt> keys and a
- * key in the specified map is <tt>null</tt>.
- */
- public void putAll(Map<? extends K, ? extends V> map) {
- int mapSize = map.size();
- if (size==0 && mapSize!=0 && map instanceof SortedMap) {
- Comparator c = ((SortedMap)map).comparator();
- if (c == comparator || (c != null && c.equals(comparator))) {
- ++modCount;
- try {
- buildFromSorted(mapSize, map.entrySet().iterator(),
- null, null);
- } catch (java.io.IOException cannotHappen) {
- } catch (ClassNotFoundException cannotHappen) {
- }
- return;
- }
- }
- Iterator<? extends Map.Entry<? extends K, ? extends V>> i = map.entrySet().iterator();
- while (i.hasNext()) {
- Map.Entry<? extends K, ? extends V> e = i.next();
- put(e.getKey(), e.getValue());
- }
- }
- /**
- * Returns this map's entry for the given key, or <tt>null</tt> if the map
- * does not contain an entry for the key.
- *
- * @return this map's entry for the given key, or <tt>null</tt> if the map
- * does not contain an entry for the key.
- * @throws ClassCastException if the key cannot be compared with the keys
- * currently in the map.
- * @throws NullPointerException key is <tt>null</tt> and this map uses
- * natural order, or its comparator does not tolerate *
- * <tt>null</tt> keys.
- */
- private Entry<K,V> getEntry(Object key) {
- Entry<K,V> p = root;
- K k = (K) key;
- while (p != null) {
- int cmp = compare(k, p.key);
- if (cmp == 0)
- return p;
- else if (cmp < 0)
- p = p.left;
- else
- p = p.right;
- }
- return null;
- }
-
- /**
- * Gets the entry corresponding to the specified key; if no such entry
- * exists, returns the entry for the least key greater than the specified
- * key; if no such entry exists (i.e., the greatest key in the Tree is less
- * than the specified key), returns <tt>null</tt>.
- */
- private Entry<K,V> getCeilEntry(K key) {
- Entry<K,V> p = root;
- if (p==null)
- return null;
- while (true) {
- int cmp = compare(key, p.key);
- if (cmp == 0) {
- return p;
- } else if (cmp < 0) {
- if (p.left != null)
- p = p.left;
- else
- return p;
- } else {
- if (p.right != null) {
- p = p.right;
- } else {
- Entry<K,V> parent = p.parent;
- Entry<K,V> ch = p;
- while (parent != null && ch == parent.right) {
- ch = parent;
- parent = parent.parent;
- }
- return parent;
- }
- }
- }
- }
- /**
- * Returns the entry for the greatest key less than the specified key; if
- * no such entry exists (i.e., the least key in the Tree is greater than
- * the specified key), returns <tt>null</tt>.
- */
- private Entry<K,V> getPrecedingEntry(K key) {
- Entry<K,V> p = root;
- if (p==null)
- return null;
- while (true) {
- int cmp = compare(key, p.key);
- if (cmp > 0) {
- if (p.right != null)
- p = p.right;
- else
- return p;
- } else {
- if (p.left != null) {
- p = p.left;
- } else {
- Entry<K,V> parent = p.parent;
- Entry<K,V> ch = p;
- while (parent != null && ch == parent.left) {
- ch = parent;
- parent = parent.parent;
- }
- return parent;
- }
- }
- }
- }
-
- /*
- * ----------------- Methods inserted by Marcos --------------------
- */
-
- /**
- * Returns the entry for the greatest key less than the specified key; if
- * no such entry exists or the entry for the specified key if it exists
- * (i.e., if the the least key in the Tree is greater than the specified
- * key), returns <tt>null</tt>.
- */
- private Entry<K,V> getPrecedingEntryInclusive(K key) {
- Entry<K,V> p = root;
- if (p==null)
- return null;
- while (true) {
- int cmp = compare(key, p.key);
- if (cmp > 0) {
- if (p.right != null)
- p = p.right;
- else
- return p;
- }
- else if (cmp == 0) {
- return p;
- }
- else {
- if (p.left != null) {
- p = p.left;
- } else {
- Entry<K,V> parent = p.parent;
- Entry<K,V> ch = p;
- while (parent != null && ch == parent.left) {
- ch = parent;
- parent = parent.parent;
- }
- return parent;
- }
- }
- }
- }
-
- /**
- * Returns a value iterator whose first element is the entry searched for.
- * If the entry does not exist, return an iterator with 0 elements
- * @param key the key from which the iterator starts.
- * @return the value iterator
- */
- public Iterator<V> itValues(K key) {
- Entry<K,V> entry = getEntry(key);
- return new ValueIterator(entry);
- }
-
- /**
- * Returns a value iterator whose first element is the entry next
- * to the provided key. If the key does not exist, the first element is
- * the next element after the preceding key. If none exist, return an
- * iterator with 0 elements.
- * @param key the key to search for.
- * @return the value iterator
- */
- public Iterator<V> itValuesAfter(K key) {
- Entry<K,V> entry = this.getPrecedingEntryInclusive(key);
- return new ValueIterator( entry == null ? null : entry.next);
- }
-
-
- /**
- * Returns the value of entry whose key is preceding the provided key.
- * if inclusive is <tt>true</tt> consider the key provided as well in the
- * search.
- * @param key the key to search for
- * @param inclusive <tt>true</tt> if you want the value for the provided
- * key or, if not found, that value whose key is smaller than the key given.
- * @return the value or <tt>null</tt> if not found
- */
- public V getPrecValue(K key, boolean inclusive) {
- Entry<K,V> entry = inclusive ?
- getPrecedingEntryInclusive(key) :
- getPrecedingEntry(key);
- return entry == null ? null : entry.value;
- }
-
- /**
- * Returns a value iterator whose first entry's key is before
- * the key given, or the key given if it exists.
- * @param key the key to search for.
- * @return the value iterator.
- */
- public Iterator<V> itValuesFromPrec(K key) {
- Entry<K,V> entry = getPrecedingEntryInclusive(key);
- return new ValueIterator(entry);
- }
-
- /*
- * -------------- End of Methods inserted by Marcos --------------------
- */
-
- /**
- * Returns the key corresponding to the specified Entry. Throw
- * NoSuchElementException if the Entry is <tt>null</tt>.
- */
- private static <K> K key(Entry<K,?> e) {
- if (e==null)
- throw new NoSuchElementException();
- return e.key;
- }
- /**
- * Associates the specified value with the specified key in this map.
- * If the map previously contained a mapping for this key, the old
- * value is replaced.
- *
- * @param key key with which the specified value is to be associated.
- * @param value value to be associated with the specified key.
- *
- * @return previous value associated with specified key, or <tt>null</tt>
- * if there was no mapping for key. A <tt>null</tt> return can
- * also indicate that the map previously associated <tt>null</tt>
- * with the specified key.
- * @throws ClassCastException key cannot be compared with the keys
- * currently in the map.
- * @throws NullPointerException key is <tt>null</tt> and this map uses
- * natural order, or its comparator does not tolerate
- * <tt>null</tt> keys.
- */
- public V put(K key, V value) {
- Entry<K,V> t = root;
- if (t == null) {
- incrementSize();
- root = new Entry<K,V>(key, value, null);
- return null;
- }
- while (true) {
- int cmp = compare(key, t.key);
- if (cmp == 0) {
- return t.setValue(value);
- } else if (cmp < 0) {
- if (t.left != null) {
- t = t.left;
- } else {
- incrementSize();
- t.left = new Entry<K,V>(key, value, t);
-
- // -------------------
- t.left.next = t;
-
- // internal node
- if(t.previous != null) {
- t.left.previous = t.previous;
- t.previous.next = t.left;
- }
- t.previous = t.left;
- // ------------------
-
- fixAfterInsertion(t.left);
- return null;
- }
- } else { // cmp > 0
- if (t.right != null) {
- t = t.right;
- } else {
- incrementSize();
- t.right = new Entry<K,V>(key, value, t);
-
- // -------------------
- t.right.next = t.next;
- t.right.previous = t;
-
- // internal node
- if(t.next != null) {
- t.next.previous = t.right;
- }
- t.next = t.right;
- // ------------------
-
- fixAfterInsertion(t.right);
- return null;
- }
- }
- }
- }
- /**
- * Removes the mapping for this key from this TreeMap if present.
- *
- * @param key key for which mapping should be removed
- * @return previous value associated with specified key, or <tt>null</tt>
- * if there was no mapping for key. A <tt>null</tt> return can
- * also indicate that the map previously associated
- * <tt>null</tt> with the specified key.
- *
- * @throws ClassCastException key cannot be compared with the keys
- * currently in the map.
- * @throws NullPointerException key is <tt>null</tt> and this map uses
- * natural order, or its comparator does not tolerate
- * <tt>null</tt> keys.
- */
- public V remove(Object key) {
- Entry<K,V> p = getEntry(key);
- if (p == null)
- return null;
- V oldValue = p.value;
- deleteEntry(p);
- return oldValue;
- }
- /**
- * Removes all mappings from this TreeMap.
- */
- public void clear() {
- modCount++;
- size = 0;
- root = null;
- }
- /**
- * Returns a shallow copy of this <tt>TreeMap</tt> instance. (The keys and
- * values themselves are not cloned.)
- *
- * @return a shallow copy of this Map.
- */
- public Object clone() {
- LinkedTreeMap<K,V> clone = null;
- try {
- clone = (LinkedTreeMap<K,V>) super.clone();
- } catch (CloneNotSupportedException e) {
- throw new InternalError();
- }
- // Put clone into "virgin" state (except for comparator)
- clone.root = null;
- clone.size = 0;
- clone.modCount = 0;
- clone.entrySet = null;
- // Initialize clone with our mappings
- try {
- clone.buildFromSorted(size, entrySet().iterator(), null, null);
- } catch (java.io.IOException cannotHappen) {
- } catch (ClassNotFoundException cannotHappen) {
- }
- return clone;
- }
- // Views
- /**
- * This field is initialized to contain an instance of the entry set
- * view the first time this view is requested. The view is stateless,
- * so there's no reason to create more than one.
- */
- private transient volatile Set<Map.Entry<K,V>> entrySet = null;
-
- /**
- * Each of these fields are initialized to contain an instance of the
- * appropriate view the first time this view is requested. The views are
- * stateless, so there's no reason to create more than one of each.
- */
- transient volatile Set<K> keySet = null;
- transient volatile Collection<V> values = null;
- /**
- * Returns a Set view of the keys contained in this map. The set's
- * iterator will return the keys in ascending order. The map is backed by
- * this <tt>TreeMap</tt> instance, so changes to this map are reflected in
- * the Set, and vice-versa. The Set supports element removal, which
- * removes the corresponding mapping from the map, via the
- * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
- * <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not support
- * the <tt>add</tt> or <tt>addAll</tt> operations.
- *
- * @return a set view of the keys contained in this TreeMap.
- */
- public Set<K> keySet() {
- if (keySet == null) {
- keySet = new AbstractSet<K>() {
- public Iterator<K> iterator() {
- return new KeyIterator();
- }
- public int size() {
- return LinkedTreeMap.this.size();
- }
- public boolean contains(Object o) {
- return containsKey(o);
- }
- public boolean remove(Object o) {
- int oldSize = size;
- LinkedTreeMap.this.remove(o);
- return size != oldSize;
- }
- public void clear() {
- LinkedTreeMap.this.clear();
- }
- };
- }
- return keySet;
- }
- /**
- * Returns a collection view of the values contained in this map. The
- * collection's iterator will return the values in the order that their
- * corresponding keys appear in the tree. The collection is backed by
- * this <tt>TreeMap</tt> instance, so changes to this map are reflected in
- * the collection, and vice-versa. The collection supports element
- * removal, which removes the corresponding mapping from the map through
- * the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
- * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
- * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
- *
- * @return a collection view of the values contained in this map.
- */
- public Collection<V> values() {
- if (values == null) {
- values = new AbstractCollection<V>() {
- public Iterator<V> iterator() {
- return new ValueIterator();
- }
- public int size() {
- return LinkedTreeMap.this.size();
- }
- public boolean contains(Object o) {
- for (Entry<K,V> e = firstEntry(); e != null; e = successor(e))
- if (valEquals(e.getValue(), o))
- return true;
- return false;
- }
- public boolean remove(Object o) {
- for (Entry<K,V> e = firstEntry(); e != null; e = successor(e)) {
- if (valEquals(e.getValue(), o)) {
- deleteEntry(e);
- return true;
- }
- }
- return false;
- }
- public void clear() {
- LinkedTreeMap.this.clear();
- }
- };
- }
- return values;
- }
- /**
- * Returns a set view of the mappings contained in this map. The set's
- * iterator returns the mappings in ascending key order. Each element in
- * the returned set is a <tt>Map.Entry</tt>. The set is backed by this
- * map, so changes to this map are reflected in the set, and vice-versa.
- * The set supports element removal, which removes the corresponding
- * mapping from the TreeMap, through the <tt>Iterator.remove</tt>,
- * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
- * <tt>clear</tt> operations. It does not support the <tt>add</tt> or
- * <tt>addAll</tt> operations.
- *
- * @return a set view of the mappings contained in this map.
- * @see Map.Entry
- */
- public Set<Map.Entry<K,V>> entrySet() {
- if (entrySet == null) {
- entrySet = new AbstractSet<Map.Entry<K,V>>() {
- public Iterator<Map.Entry<K,V>> iterator() {
- return new EntryIterator();
- }
- public boolean contains(Object o) {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
- V value = entry.getValue();
- Entry<K,V> p = getEntry(entry.getKey());
- return p != null && valEquals(p.getValue(), value);
- }
- public boolean remove(Object o) {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
- V value = entry.getValue();
- Entry<K,V> p = getEntry(entry.getKey());
- if (p != null && valEquals(p.getValue(), value)) {
- deleteEntry(p);
- return true;
- }
- return false;
- }
- public int size() {
- return LinkedTreeMap.this.size();
- }
- public void clear() {
- LinkedTreeMap.this.clear();
- }
- };
- }
- return entrySet;
- }
- /**
- * Returns a view of the portion of this map whose keys range from
- * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If
- * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned sorted map
- * is empty.) The returned sorted map is backed by this map, so changes
- * in the returned sorted map are reflected in this map, and vice-versa.
- * The returned sorted map supports all optional map operations.<p>
- *
- * The sorted map returned by this method will throw an
- * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
- * less than <tt>fromKey</tt> or greater than or equal to
- * <tt>toKey</tt>.<p>
- *
- * Note: this method always returns a <i>half-open range</i> (which
- * includes its low endpoint but not its high endpoint). If you need a
- * <i>closed range</i> (which includes both endpoints), and the key type
- * allows for calculation of the successor a given key, merely request the
- * subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
- * For example, suppose that <tt>m</tt> is a sorted map whose keys are
- * strings. The following idiom obtains a view containing all of the
- * key-value mappings in <tt>m</tt> whose keys are between <tt>low</tt>
- * and <tt>high</tt>, inclusive:
- * <pre> SortedMap sub = m.submap(low, high+"\0");</pre>
- * A similar technique can be used to generate an <i>open range</i> (which
- * contains neither endpoint). The following idiom obtains a view
- * containing all of the key-value mappings in <tt>m</tt> whose keys are
- * between <tt>low</tt> and <tt>high</tt>, exclusive:
- * <pre> SortedMap sub = m.subMap(low+"\0", high);</pre>
- *
- * @param fromKey low endpoint (inclusive) of the subMap.
- * @param toKey high endpoint (exclusive) of the subMap.
- *
- * @return a view of the portion of this map whose keys range from
- * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.
- *
- * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
- * cannot be compared to one another using this map's comparator
- * (or, if the map has no comparator, using natural ordering).
- * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
- * <tt>toKey</tt>.
- * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
- * <tt>null</tt> and this map uses natural order, or its
- * comparator does not tolerate <tt>null</tt> keys.
- */
- public SortedMap<K,V> subMap(K fromKey, K toKey) {
- return new SubMap(fromKey, toKey);
- }
- /**
- * Returns a view of the portion of this map whose keys are strictly less
- * than <tt>toKey</tt>. The returned sorted map is backed by this map, so
- * changes in the returned sorted map are reflected in this map, and
- * vice-versa. The returned sorted map supports all optional map
- * operations.<p>
- *
- * The sorted map returned by this method will throw an
- * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
- * greater than or equal to <tt>toKey</tt>.<p>
- *
- * Note: this method always returns a view that does not contain its
- * (high) endpoint. If you need a view that does contain this endpoint,
- * and the key type allows for calculation of the successor a given key,
- * merely request a headMap bounded by <tt>successor(highEndpoint)</tt>.
- * For example, suppose that suppose that <tt>m</tt> is a sorted map whose
- * keys are strings. The following idiom obtains a view containing all of
- * the key-value mappings in <tt>m</tt> whose keys are less than or equal
- * to <tt>high</tt>:
- * <pre>
- * SortedMap head = m.headMap(high+"\0");
- * </pre>
- *
- * @param toKey high endpoint (exclusive) of the headMap.
- * @return a view of the portion of this map whose keys are strictly
- * less than <tt>toKey</tt>.
- *
- * @throws ClassCastException if <tt>toKey</tt> is not compatible
- * with this map's comparator (or, if the map has no comparator,
- * if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
- * @throws IllegalArgumentException if this map is itself a subMap,
- * headMap, or tailMap, and <tt>toKey</tt> is not within the
- * specified range of the subMap, headMap, or tailMap.
- * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
- * this map uses natural order, or its comparator does not
- * tolerate <tt>null</tt> keys.
- */
- public SortedMap<K,V> headMap(K toKey) {
- return new SubMap(toKey, true);
- }
- /**
- * Returns a view of the portion of this map whose keys are greater than
- * or equal to <tt>fromKey</tt>. The returned sorted map is backed by
- * this map, so changes in the returned sorted map are reflected in this
- * map, and vice-versa. The returned sorted map supports all optional map
- * operations.<p>
- *
- * The sorted map returned by this method will throw an
- * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
- * less than <tt>fromKey</tt>.<p>
- *
- * Note: this method always returns a view that contains its (low)
- * endpoint. If you need a view that does not contain this endpoint, and
- * the element type allows for calculation of the successor a given value,
- * merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>.
- * For example, suppose that <tt>m</tt> is a sorted map whose keys
- * are strings. The following idiom obtains a view containing
- * all of the key-value mappings in <tt>m</tt> whose keys are strictly
- * greater than <tt>low</tt>: <pre>
- * SortedMap tail = m.tailMap(low+"\0");
- * </pre>
- *
- * @param fromKey low endpoint (inclusive) of the tailMap.
- * @return a view of the portion of this map whose keys are greater
- * than or equal to <tt>fromKey</tt>.
- * @throws ClassCastException if <tt>fromKey</tt> is not compatible
- * with this map's comparator (or, if the map has no comparator,
- * if <tt>fromKey</tt> does not implement <tt>Comparable</tt>).
- * @throws IllegalArgumentException if this map is itself a subMap,
- * headMap, or tailMap, and <tt>fromKey</tt> is not within the
- * specified range of the subMap, headMap, or tailMap.
- * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
- * this map uses natural order, or its comparator does not
- * tolerate <tt>null</tt> keys.
- */
- public SortedMap<K,V> tailMap(K fromKey) {
- return new SubMap(fromKey, false);
- }
- private class SubMap
- extends AbstractMap<K,V>
- implements SortedMap<K,V>, java.io.Serializable {
- private static final long serialVersionUID = -6520786458950516097L;
- /**
- * fromKey is significant only if fromStart is false. Similarly,
- * toKey is significant only if toStart is false.
- */
- private boolean fromStart = false, toEnd = false;
- private K fromKey, toKey;
- SubMap(K fromKey, K toKey) {
- if (compare(fromKey, toKey) > 0)
- throw new IllegalArgumentException("fromKey > toKey");
- this.fromKey = fromKey;
- this.toKey = toKey;
- }
- SubMap(K key, boolean headMap) {
- compare(key, key); // Type-check key
- if (headMap) {
- fromStart = true;
- toKey = key;
- } else {
- toEnd = true;
- fromKey = key;
- }
- }
- SubMap(boolean fromStart, K fromKey, boolean toEnd, K toKey) {
- this.fromStart = fromStart;
- this.fromKey= fromKey;
- this.toEnd = toEnd;
- this.toKey = toKey;
- }
- public boolean isEmpty() {
- return entrySet.isEmpty();
- }
- public boolean containsKey(Object key) {
- return inRange((K) key) && LinkedTreeMap.this.containsKey(key);
- }
- public V get(Object key) {
- if (!inRange((K) key))
- return null;
- return LinkedTreeMap.this.get(key);
- }
- public V put(K key, V value) {
- if (!inRange(key))
- throw new IllegalArgumentException("key out of range");
- return LinkedTreeMap.this.put(key, value);
- }
- public Comparator<? super K> comparator() {
- return comparator;
- }
- public K firstKey() {
- LinkedTreeMap.Entry<K,V> e = fromStart ? firstEntry() : getCeilEntry(fromKey);
- K first = key(e);
- if (!toEnd && compare(first, toKey) >= 0)
- throw(new NoSuchElementException());
- return first;
- }
- public K lastKey() {
- LinkedTreeMap.Entry<K,V> e = toEnd ? lastEntry() : getPrecedingEntry(toKey);
- K last = key(e);
- if (!fromStart && compare(last, fromKey) < 0)
- throw(new NoSuchElementException());
- return last;
- }
- private transient Set<Map.Entry<K,V>> entrySet = new EntrySetView();
- public Set<Map.Entry<K,V>> entrySet() {
- return entrySet;
- }
- private class EntrySetView extends AbstractSet<Map.Entry<K,V>> {
- private transient int size = -1, sizeModCount;
- public int size() {
- if (size == -1 || sizeModCount != LinkedTreeMap.this.modCount) {
- size = 0; sizeModCount = LinkedTreeMap.this.modCount;
- Iterator i = iterator();
- while (i.hasNext()) {
- size++;
- i.next();
- }
- }
- return size;
- }
- public boolean isEmpty() {
- return !iterator().hasNext();
- }
- public boolean contains(Object o) {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
- K key = entry.getKey();
- if (!inRange(key))
- return false;
- LinkedTreeMap.Entry node = getEntry(key);
- return node != null &&
- valEquals(node.getValue(), entry.getValue());
- }
- public boolean remove(Object o) {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
- K key = entry.getKey();
- if (!inRange(key))
- return false;
- LinkedTreeMap.Entry<K,V> node = getEntry(key);
- if (node!=null && valEquals(node.getValue(),entry.getValue())){
- deleteEntry(node);
- return true;
- }
- return false;
- }
- public Iterator<Map.Entry<K,V>> iterator() {
- return new SubMapEntryIterator(
- (fromStart ? firstEntry() : getCeilEntry(fromKey)),
- (toEnd ? null : getCeilEntry(toKey)));
- }
- }
- public SortedMap<K,V> subMap(K fromKey, K toKey) {
- if (!inRange2(fromKey))
- throw new IllegalArgumentException("fromKey out of range");
- if (!inRange2(toKey))
- throw new IllegalArgumentException("toKey out of range");
- return new SubMap(fromKey, toKey);
- }
- public SortedMap<K,V> headMap(K toKey) {
- if (!inRange2(toKey))
- throw new IllegalArgumentException("toKey out of range");
- return new SubMap(fromStart, fromKey, false, toKey);
- }
- public SortedMap<K,V> tailMap(K fromKey) {
- if (!inRange2(fromKey))
- throw new IllegalArgumentException("fromKey out of range");
- return new SubMap(false, fromKey, toEnd, toKey);
- }
- private boolean inRange(K key) {
- return (fromStart || compare(key, fromKey) >= 0) &&
- (toEnd || compare(key, toKey) < 0);
- }
- // This form allows the high endpoint (as well as all legit keys)
- private boolean inRange2(K key) {
- return (fromStart || compare(key, fromKey) >= 0) &&
- (toEnd || compare(key, toKey) <= 0);
- }
- }
- /**
- * TreeMap Iterator.
- */
- private abstract class PrivateEntryIterator<T> implements Iterator<T> {
- private int expectedModCount = LinkedTreeMap.this.modCount;
- private Entry<K,V> lastReturned = null;
- Entry<K,V> next;
- PrivateEntryIterator() {
- next = firstEntry();
- }
- // Used by SubMapEntryIterator
- PrivateEntryIterator(Entry<K,V> first) {
- next = first;
- }
- public boolean hasNext() {
- return next != null;
- }
- final Entry<K,V> nextEntry() {
- if (next == null)
- throw new NoSuchElementException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- lastReturned = next;
- next = next.next;
- return lastReturned;
- }
- public void remove() {
- if (lastReturned == null)
- throw new IllegalStateException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- if (lastReturned.left != null && lastReturned.right != null)
- next = lastReturned;
-
- deleteEntry(lastReturned);
- expectedModCount++;
- lastReturned = null;
- }
- }
-
-
- private class EntryIterator extends PrivateEntryIterator<Map.Entry<K,V>> {
- public Map.Entry<K,V> next() {
- return nextEntry();
- }
- }
- private class KeyIterator extends PrivateEntryIterator<K> {
- public K next() {
- return nextEntry().key;
- }
- }
-
- private class ValueIterator extends PrivateEntryIterator<V> {
-
- ValueIterator() {
- super();
- }
-
- ValueIterator(Entry<K,V> first) {
- super(first);
- }
-
- public V next() {
- return nextEntry().value;
- }
- }
- private class SubMapEntryIterator extends PrivateEntryIterator<Map.Entry<K,V>> {
- private final K firstExcludedKey;
- SubMapEntryIterator(Entry<K,V> first, Entry<K,V> firstExcluded) {
- super(first);
- firstExcludedKey = (firstExcluded == null
- ? null
- : firstExcluded.key);
- }
- public boolean hasNext() {
- return next != null && next.key != firstExcludedKey;
- }
- public Map.Entry<K,V> next() {
- if (next == null || next.key == firstExcludedKey)
- throw new NoSuchElementException();
- return nextEntry();
- }
- }
- /**
- * Compares two keys using the correct comparison method for this TreeMap.
- */
- private int compare(K k1, K k2) {
- return (comparator==null ? ((Comparable</*-*/K>)k1).compareTo(k2)
- : comparator.compare((K)k1, (K)k2));
- }
- /**
- * Test two values for equality. Differs from o1.equals(o2) only in
- * that it copes with <tt>null</tt> o1 properly.
- */
- private static boolean valEquals(Object o1, Object o2) {
- return (o1==null ? o2==null : o1.equals(o2));
- }
- private static final boolean RED = false;
- private static final boolean BLACK = true;
- /**
- * Node in the Tree. Doubles as a means to pass key-value pairs back to
- * user (see Map.Entry).
- */
- static class Entry<K,V> implements Map.Entry<K,V> {
- K key;
- V value;
- Entry<K,V> left = null;
- Entry<K,V> right = null;
- Entry<K,V> parent;
- Entry<K,V> next = null;
- Entry<K,V> previous = null;
- boolean color = BLACK;
- /**
- * Make a new cell with given key, value, and parent, and with
- * <tt>null</tt> child links, and BLACK color.
- */
- Entry(K key, V value, Entry<K,V> parent) {
- this.key = key;
- this.value = value;
- this.parent = parent;
- }
-
- /**
- * Returns the key.
- *
- * @return the key.
- */
- public K getKey() {
- return key;
- }
- /**
- * Returns the value associated with the key.
- *
- * @return the value associated with the key.
- */
- public V getValue() {
- return value;
- }
- /**
- * Replaces the value currently associated with the key with the given
- * value.
- *
- * @return the value associated with the key before this method was
- * called.
- */
- public V setValue(V value) {
- V oldValue = this.value;
- this.value = value;
- return oldValue;
- }
- public boolean equals(Object o) {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry e = (Map.Entry)o;
- return valE…
Large files files are truncated, but you can click here to view the full file