1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/util/TreeSet.java Sat Sep 07 13:51:24 2013 +0200
1.3 @@ -0,0 +1,539 @@
1.4 +/*
1.5 + * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.util;
1.30 +
1.31 +/**
1.32 + * A {@link NavigableSet} implementation based on a {@link TreeMap}.
1.33 + * The elements are ordered using their {@linkplain Comparable natural
1.34 + * ordering}, or by a {@link Comparator} provided at set creation
1.35 + * time, depending on which constructor is used.
1.36 + *
1.37 + * <p>This implementation provides guaranteed log(n) time cost for the basic
1.38 + * operations ({@code add}, {@code remove} and {@code contains}).
1.39 + *
1.40 + * <p>Note that the ordering maintained by a set (whether or not an explicit
1.41 + * comparator is provided) must be <i>consistent with equals</i> if it is to
1.42 + * correctly implement the {@code Set} interface. (See {@code Comparable}
1.43 + * or {@code Comparator} for a precise definition of <i>consistent with
1.44 + * equals</i>.) This is so because the {@code Set} interface is defined in
1.45 + * terms of the {@code equals} operation, but a {@code TreeSet} instance
1.46 + * performs all element comparisons using its {@code compareTo} (or
1.47 + * {@code compare}) method, so two elements that are deemed equal by this method
1.48 + * are, from the standpoint of the set, equal. The behavior of a set
1.49 + * <i>is</i> well-defined even if its ordering is inconsistent with equals; it
1.50 + * just fails to obey the general contract of the {@code Set} interface.
1.51 + *
1.52 + * <p><strong>Note that this implementation is not synchronized.</strong>
1.53 + * If multiple threads access a tree set concurrently, and at least one
1.54 + * of the threads modifies the set, it <i>must</i> be synchronized
1.55 + * externally. This is typically accomplished by synchronizing on some
1.56 + * object that naturally encapsulates the set.
1.57 + * If no such object exists, the set should be "wrapped" using the
1.58 + * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
1.59 + * method. This is best done at creation time, to prevent accidental
1.60 + * unsynchronized access to the set: <pre>
1.61 + * SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));</pre>
1.62 + *
1.63 + * <p>The iterators returned by this class's {@code iterator} method are
1.64 + * <i>fail-fast</i>: if the set is modified at any time after the iterator is
1.65 + * created, in any way except through the iterator's own {@code remove}
1.66 + * method, the iterator will throw a {@link ConcurrentModificationException}.
1.67 + * Thus, in the face of concurrent modification, the iterator fails quickly
1.68 + * and cleanly, rather than risking arbitrary, non-deterministic behavior at
1.69 + * an undetermined time in the future.
1.70 + *
1.71 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
1.72 + * as it is, generally speaking, impossible to make any hard guarantees in the
1.73 + * presence of unsynchronized concurrent modification. Fail-fast iterators
1.74 + * throw {@code ConcurrentModificationException} on a best-effort basis.
1.75 + * Therefore, it would be wrong to write a program that depended on this
1.76 + * exception for its correctness: <i>the fail-fast behavior of iterators
1.77 + * should be used only to detect bugs.</i>
1.78 + *
1.79 + * <p>This class is a member of the
1.80 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.81 + * Java Collections Framework</a>.
1.82 + *
1.83 + * @param <E> the type of elements maintained by this set
1.84 + *
1.85 + * @author Josh Bloch
1.86 + * @see Collection
1.87 + * @see Set
1.88 + * @see HashSet
1.89 + * @see Comparable
1.90 + * @see Comparator
1.91 + * @see TreeMap
1.92 + * @since 1.2
1.93 + */
1.94 +
1.95 +public class TreeSet<E> extends AbstractSet<E>
1.96 + implements NavigableSet<E>, Cloneable, java.io.Serializable
1.97 +{
1.98 + /**
1.99 + * The backing map.
1.100 + */
1.101 + private transient NavigableMap<E,Object> m;
1.102 +
1.103 + // Dummy value to associate with an Object in the backing Map
1.104 + private static final Object PRESENT = new Object();
1.105 +
1.106 + /**
1.107 + * Constructs a set backed by the specified navigable map.
1.108 + */
1.109 + TreeSet(NavigableMap<E,Object> m) {
1.110 + this.m = m;
1.111 + }
1.112 +
1.113 + /**
1.114 + * Constructs a new, empty tree set, sorted according to the
1.115 + * natural ordering of its elements. All elements inserted into
1.116 + * the set must implement the {@link Comparable} interface.
1.117 + * Furthermore, all such elements must be <i>mutually
1.118 + * comparable</i>: {@code e1.compareTo(e2)} must not throw a
1.119 + * {@code ClassCastException} for any elements {@code e1} and
1.120 + * {@code e2} in the set. If the user attempts to add an element
1.121 + * to the set that violates this constraint (for example, the user
1.122 + * attempts to add a string element to a set whose elements are
1.123 + * integers), the {@code add} call will throw a
1.124 + * {@code ClassCastException}.
1.125 + */
1.126 + public TreeSet() {
1.127 + this(new TreeMap<E,Object>());
1.128 + }
1.129 +
1.130 + /**
1.131 + * Constructs a new, empty tree set, sorted according to the specified
1.132 + * comparator. All elements inserted into the set must be <i>mutually
1.133 + * comparable</i> by the specified comparator: {@code comparator.compare(e1,
1.134 + * e2)} must not throw a {@code ClassCastException} for any elements
1.135 + * {@code e1} and {@code e2} in the set. If the user attempts to add
1.136 + * an element to the set that violates this constraint, the
1.137 + * {@code add} call will throw a {@code ClassCastException}.
1.138 + *
1.139 + * @param comparator the comparator that will be used to order this set.
1.140 + * If {@code null}, the {@linkplain Comparable natural
1.141 + * ordering} of the elements will be used.
1.142 + */
1.143 + public TreeSet(Comparator<? super E> comparator) {
1.144 + this(new TreeMap<>(comparator));
1.145 + }
1.146 +
1.147 + /**
1.148 + * Constructs a new tree set containing the elements in the specified
1.149 + * collection, sorted according to the <i>natural ordering</i> of its
1.150 + * elements. All elements inserted into the set must implement the
1.151 + * {@link Comparable} interface. Furthermore, all such elements must be
1.152 + * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
1.153 + * {@code ClassCastException} for any elements {@code e1} and
1.154 + * {@code e2} in the set.
1.155 + *
1.156 + * @param c collection whose elements will comprise the new set
1.157 + * @throws ClassCastException if the elements in {@code c} are
1.158 + * not {@link Comparable}, or are not mutually comparable
1.159 + * @throws NullPointerException if the specified collection is null
1.160 + */
1.161 + public TreeSet(Collection<? extends E> c) {
1.162 + this();
1.163 + addAll(c);
1.164 + }
1.165 +
1.166 + /**
1.167 + * Constructs a new tree set containing the same elements and
1.168 + * using the same ordering as the specified sorted set.
1.169 + *
1.170 + * @param s sorted set whose elements will comprise the new set
1.171 + * @throws NullPointerException if the specified sorted set is null
1.172 + */
1.173 + public TreeSet(SortedSet<E> s) {
1.174 + this(s.comparator());
1.175 + addAll(s);
1.176 + }
1.177 +
1.178 + /**
1.179 + * Returns an iterator over the elements in this set in ascending order.
1.180 + *
1.181 + * @return an iterator over the elements in this set in ascending order
1.182 + */
1.183 + public Iterator<E> iterator() {
1.184 + return m.navigableKeySet().iterator();
1.185 + }
1.186 +
1.187 + /**
1.188 + * Returns an iterator over the elements in this set in descending order.
1.189 + *
1.190 + * @return an iterator over the elements in this set in descending order
1.191 + * @since 1.6
1.192 + */
1.193 + public Iterator<E> descendingIterator() {
1.194 + return m.descendingKeySet().iterator();
1.195 + }
1.196 +
1.197 + /**
1.198 + * @since 1.6
1.199 + */
1.200 + public NavigableSet<E> descendingSet() {
1.201 + return new TreeSet<>(m.descendingMap());
1.202 + }
1.203 +
1.204 + /**
1.205 + * Returns the number of elements in this set (its cardinality).
1.206 + *
1.207 + * @return the number of elements in this set (its cardinality)
1.208 + */
1.209 + public int size() {
1.210 + return m.size();
1.211 + }
1.212 +
1.213 + /**
1.214 + * Returns {@code true} if this set contains no elements.
1.215 + *
1.216 + * @return {@code true} if this set contains no elements
1.217 + */
1.218 + public boolean isEmpty() {
1.219 + return m.isEmpty();
1.220 + }
1.221 +
1.222 + /**
1.223 + * Returns {@code true} if this set contains the specified element.
1.224 + * More formally, returns {@code true} if and only if this set
1.225 + * contains an element {@code e} such that
1.226 + * <tt>(o==null ? e==null : o.equals(e))</tt>.
1.227 + *
1.228 + * @param o object to be checked for containment in this set
1.229 + * @return {@code true} if this set contains the specified element
1.230 + * @throws ClassCastException if the specified object cannot be compared
1.231 + * with the elements currently in the set
1.232 + * @throws NullPointerException if the specified element is null
1.233 + * and this set uses natural ordering, or its comparator
1.234 + * does not permit null elements
1.235 + */
1.236 + public boolean contains(Object o) {
1.237 + return m.containsKey(o);
1.238 + }
1.239 +
1.240 + /**
1.241 + * Adds the specified element to this set if it is not already present.
1.242 + * More formally, adds the specified element {@code e} to this set if
1.243 + * the set contains no element {@code e2} such that
1.244 + * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
1.245 + * If this set already contains the element, the call leaves the set
1.246 + * unchanged and returns {@code false}.
1.247 + *
1.248 + * @param e element to be added to this set
1.249 + * @return {@code true} if this set did not already contain the specified
1.250 + * element
1.251 + * @throws ClassCastException if the specified object cannot be compared
1.252 + * with the elements currently in this set
1.253 + * @throws NullPointerException if the specified element is null
1.254 + * and this set uses natural ordering, or its comparator
1.255 + * does not permit null elements
1.256 + */
1.257 + public boolean add(E e) {
1.258 + return m.put(e, PRESENT)==null;
1.259 + }
1.260 +
1.261 + /**
1.262 + * Removes the specified element from this set if it is present.
1.263 + * More formally, removes an element {@code e} such that
1.264 + * <tt>(o==null ? e==null : o.equals(e))</tt>,
1.265 + * if this set contains such an element. Returns {@code true} if
1.266 + * this set contained the element (or equivalently, if this set
1.267 + * changed as a result of the call). (This set will not contain the
1.268 + * element once the call returns.)
1.269 + *
1.270 + * @param o object to be removed from this set, if present
1.271 + * @return {@code true} if this set contained the specified element
1.272 + * @throws ClassCastException if the specified object cannot be compared
1.273 + * with the elements currently in this set
1.274 + * @throws NullPointerException if the specified element is null
1.275 + * and this set uses natural ordering, or its comparator
1.276 + * does not permit null elements
1.277 + */
1.278 + public boolean remove(Object o) {
1.279 + return m.remove(o)==PRESENT;
1.280 + }
1.281 +
1.282 + /**
1.283 + * Removes all of the elements from this set.
1.284 + * The set will be empty after this call returns.
1.285 + */
1.286 + public void clear() {
1.287 + m.clear();
1.288 + }
1.289 +
1.290 + /**
1.291 + * Adds all of the elements in the specified collection to this set.
1.292 + *
1.293 + * @param c collection containing elements to be added to this set
1.294 + * @return {@code true} if this set changed as a result of the call
1.295 + * @throws ClassCastException if the elements provided cannot be compared
1.296 + * with the elements currently in the set
1.297 + * @throws NullPointerException if the specified collection is null or
1.298 + * if any element is null and this set uses natural ordering, or
1.299 + * its comparator does not permit null elements
1.300 + */
1.301 + public boolean addAll(Collection<? extends E> c) {
1.302 + // Use linear-time version if applicable
1.303 + if (m.size()==0 && c.size() > 0 &&
1.304 + c instanceof SortedSet &&
1.305 + m instanceof TreeMap) {
1.306 + SortedSet<? extends E> set = (SortedSet<? extends E>) c;
1.307 + TreeMap<E,Object> map = (TreeMap<E, Object>) m;
1.308 + Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
1.309 + Comparator<? super E> mc = map.comparator();
1.310 + if (cc==mc || (cc != null && cc.equals(mc))) {
1.311 + map.addAllForTreeSet(set, PRESENT);
1.312 + return true;
1.313 + }
1.314 + }
1.315 + return super.addAll(c);
1.316 + }
1.317 +
1.318 + /**
1.319 + * @throws ClassCastException {@inheritDoc}
1.320 + * @throws NullPointerException if {@code fromElement} or {@code toElement}
1.321 + * is null and this set uses natural ordering, or its comparator
1.322 + * does not permit null elements
1.323 + * @throws IllegalArgumentException {@inheritDoc}
1.324 + * @since 1.6
1.325 + */
1.326 + public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
1.327 + E toElement, boolean toInclusive) {
1.328 + return new TreeSet<>(m.subMap(fromElement, fromInclusive,
1.329 + toElement, toInclusive));
1.330 + }
1.331 +
1.332 + /**
1.333 + * @throws ClassCastException {@inheritDoc}
1.334 + * @throws NullPointerException if {@code toElement} is null and
1.335 + * this set uses natural ordering, or its comparator does
1.336 + * not permit null elements
1.337 + * @throws IllegalArgumentException {@inheritDoc}
1.338 + * @since 1.6
1.339 + */
1.340 + public NavigableSet<E> headSet(E toElement, boolean inclusive) {
1.341 + return new TreeSet<>(m.headMap(toElement, inclusive));
1.342 + }
1.343 +
1.344 + /**
1.345 + * @throws ClassCastException {@inheritDoc}
1.346 + * @throws NullPointerException if {@code fromElement} is null and
1.347 + * this set uses natural ordering, or its comparator does
1.348 + * not permit null elements
1.349 + * @throws IllegalArgumentException {@inheritDoc}
1.350 + * @since 1.6
1.351 + */
1.352 + public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
1.353 + return new TreeSet<>(m.tailMap(fromElement, inclusive));
1.354 + }
1.355 +
1.356 + /**
1.357 + * @throws ClassCastException {@inheritDoc}
1.358 + * @throws NullPointerException if {@code fromElement} or
1.359 + * {@code toElement} is null and this set uses natural ordering,
1.360 + * or its comparator does not permit null elements
1.361 + * @throws IllegalArgumentException {@inheritDoc}
1.362 + */
1.363 + public SortedSet<E> subSet(E fromElement, E toElement) {
1.364 + return subSet(fromElement, true, toElement, false);
1.365 + }
1.366 +
1.367 + /**
1.368 + * @throws ClassCastException {@inheritDoc}
1.369 + * @throws NullPointerException if {@code toElement} is null
1.370 + * and this set uses natural ordering, or its comparator does
1.371 + * not permit null elements
1.372 + * @throws IllegalArgumentException {@inheritDoc}
1.373 + */
1.374 + public SortedSet<E> headSet(E toElement) {
1.375 + return headSet(toElement, false);
1.376 + }
1.377 +
1.378 + /**
1.379 + * @throws ClassCastException {@inheritDoc}
1.380 + * @throws NullPointerException if {@code fromElement} is null
1.381 + * and this set uses natural ordering, or its comparator does
1.382 + * not permit null elements
1.383 + * @throws IllegalArgumentException {@inheritDoc}
1.384 + */
1.385 + public SortedSet<E> tailSet(E fromElement) {
1.386 + return tailSet(fromElement, true);
1.387 + }
1.388 +
1.389 + public Comparator<? super E> comparator() {
1.390 + return m.comparator();
1.391 + }
1.392 +
1.393 + /**
1.394 + * @throws NoSuchElementException {@inheritDoc}
1.395 + */
1.396 + public E first() {
1.397 + return m.firstKey();
1.398 + }
1.399 +
1.400 + /**
1.401 + * @throws NoSuchElementException {@inheritDoc}
1.402 + */
1.403 + public E last() {
1.404 + return m.lastKey();
1.405 + }
1.406 +
1.407 + // NavigableSet API methods
1.408 +
1.409 + /**
1.410 + * @throws ClassCastException {@inheritDoc}
1.411 + * @throws NullPointerException if the specified element is null
1.412 + * and this set uses natural ordering, or its comparator
1.413 + * does not permit null elements
1.414 + * @since 1.6
1.415 + */
1.416 + public E lower(E e) {
1.417 + return m.lowerKey(e);
1.418 + }
1.419 +
1.420 + /**
1.421 + * @throws ClassCastException {@inheritDoc}
1.422 + * @throws NullPointerException if the specified element is null
1.423 + * and this set uses natural ordering, or its comparator
1.424 + * does not permit null elements
1.425 + * @since 1.6
1.426 + */
1.427 + public E floor(E e) {
1.428 + return m.floorKey(e);
1.429 + }
1.430 +
1.431 + /**
1.432 + * @throws ClassCastException {@inheritDoc}
1.433 + * @throws NullPointerException if the specified element is null
1.434 + * and this set uses natural ordering, or its comparator
1.435 + * does not permit null elements
1.436 + * @since 1.6
1.437 + */
1.438 + public E ceiling(E e) {
1.439 + return m.ceilingKey(e);
1.440 + }
1.441 +
1.442 + /**
1.443 + * @throws ClassCastException {@inheritDoc}
1.444 + * @throws NullPointerException if the specified element is null
1.445 + * and this set uses natural ordering, or its comparator
1.446 + * does not permit null elements
1.447 + * @since 1.6
1.448 + */
1.449 + public E higher(E e) {
1.450 + return m.higherKey(e);
1.451 + }
1.452 +
1.453 + /**
1.454 + * @since 1.6
1.455 + */
1.456 + public E pollFirst() {
1.457 + Map.Entry<E,?> e = m.pollFirstEntry();
1.458 + return (e == null) ? null : e.getKey();
1.459 + }
1.460 +
1.461 + /**
1.462 + * @since 1.6
1.463 + */
1.464 + public E pollLast() {
1.465 + Map.Entry<E,?> e = m.pollLastEntry();
1.466 + return (e == null) ? null : e.getKey();
1.467 + }
1.468 +
1.469 + /**
1.470 + * Returns a shallow copy of this {@code TreeSet} instance. (The elements
1.471 + * themselves are not cloned.)
1.472 + *
1.473 + * @return a shallow copy of this set
1.474 + */
1.475 + public Object clone() {
1.476 + TreeSet<E> clone = null;
1.477 + try {
1.478 + clone = (TreeSet<E>) super.clone();
1.479 + } catch (CloneNotSupportedException e) {
1.480 + throw new InternalError();
1.481 + }
1.482 +
1.483 + clone.m = new TreeMap<>(m);
1.484 + return clone;
1.485 + }
1.486 +
1.487 + /**
1.488 + * Save the state of the {@code TreeSet} instance to a stream (that is,
1.489 + * serialize it).
1.490 + *
1.491 + * @serialData Emits the comparator used to order this set, or
1.492 + * {@code null} if it obeys its elements' natural ordering
1.493 + * (Object), followed by the size of the set (the number of
1.494 + * elements it contains) (int), followed by all of its
1.495 + * elements (each an Object) in order (as determined by the
1.496 + * set's Comparator, or by the elements' natural ordering if
1.497 + * the set has no Comparator).
1.498 + */
1.499 + private void writeObject(java.io.ObjectOutputStream s)
1.500 + throws java.io.IOException {
1.501 + // Write out any hidden stuff
1.502 + s.defaultWriteObject();
1.503 +
1.504 + // Write out Comparator
1.505 + s.writeObject(m.comparator());
1.506 +
1.507 + // Write out size
1.508 + s.writeInt(m.size());
1.509 +
1.510 + // Write out all elements in the proper order.
1.511 + for (E e : m.keySet())
1.512 + s.writeObject(e);
1.513 + }
1.514 +
1.515 + /**
1.516 + * Reconstitute the {@code TreeSet} instance from a stream (that is,
1.517 + * deserialize it).
1.518 + */
1.519 + private void readObject(java.io.ObjectInputStream s)
1.520 + throws java.io.IOException, ClassNotFoundException {
1.521 + // Read in any hidden stuff
1.522 + s.defaultReadObject();
1.523 +
1.524 + // Read in Comparator
1.525 + Comparator<? super E> c = (Comparator<? super E>) s.readObject();
1.526 +
1.527 + // Create backing TreeMap
1.528 + TreeMap<E,Object> tm;
1.529 + if (c==null)
1.530 + tm = new TreeMap<>();
1.531 + else
1.532 + tm = new TreeMap<>(c);
1.533 + m = tm;
1.534 +
1.535 + // Read in size
1.536 + int size = s.readInt();
1.537 +
1.538 + tm.readTreeSet(size, s, PRESENT);
1.539 + }
1.540 +
1.541 + private static final long serialVersionUID = -2479143000061671589L;
1.542 +}