ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/PriorityQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/PriorityQueue.java (file contents):
Revision 1.67 by jsr166, Sun May 20 07:54:01 2007 UTC vs.
Revision 1.70 by jsr166, Mon May 10 20:11:01 2010 UTC

# Line 74 | Line 74 | package java.util;
74   * Java Collections Framework</a>.
75   *
76   * @since 1.5
77 * @version %I%, %G%
77   * @author Josh Bloch, Doug Lea
78   * @param <E> the type of elements held in this collection
79   */
# Line 171 | Line 170 | public class PriorityQueue<E> extends Ab
170       * @throws NullPointerException if the specified collection or any
171       *         of its elements are null
172       */
173 +    @SuppressWarnings("unchecked")
174      public PriorityQueue(Collection<? extends E> c) {
175 <        initFromCollection(c);
176 <        if (c instanceof SortedSet)
177 <            comparator = (Comparator<? super E>)
178 <                ((SortedSet<? extends E>)c).comparator();
179 <        else if (c instanceof PriorityQueue)
180 <            comparator = (Comparator<? super E>)
181 <                ((PriorityQueue<? extends E>)c).comparator();
175 >        if (c instanceof SortedSet<?>) {
176 >            SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
177 >            this.comparator = (Comparator<? super E>) ss.comparator();
178 >            initElementsFromCollection(ss);
179 >        }
180 >        else if (c instanceof PriorityQueue<?>) {
181 >            PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
182 >            this.comparator = (Comparator<? super E>) pq.comparator();
183 >            initFromPriorityQueue(pq);
184 >        }
185          else {
186 <            comparator = null;
187 <            heapify();
186 >            this.comparator = null;
187 >            initFromCollection(c);
188          }
189      }
190  
# Line 199 | Line 202 | public class PriorityQueue<E> extends Ab
202       * @throws NullPointerException if the specified priority queue or any
203       *         of its elements are null
204       */
205 +    @SuppressWarnings("unchecked")
206      public PriorityQueue(PriorityQueue<? extends E> c) {
207 <        comparator = (Comparator<? super E>)c.comparator();
208 <        initFromCollection(c);
207 >        this.comparator = (Comparator<? super E>) c.comparator();
208 >        initFromPriorityQueue(c);
209      }
210  
211      /**
# Line 217 | Line 221 | public class PriorityQueue<E> extends Ab
221       * @throws NullPointerException if the specified sorted set or any
222       *         of its elements are null
223       */
224 +    @SuppressWarnings("unchecked")
225      public PriorityQueue(SortedSet<? extends E> c) {
226 <        comparator = (Comparator<? super E>)c.comparator();
227 <        initFromCollection(c);
226 >        this.comparator = (Comparator<? super E>) c.comparator();
227 >        initElementsFromCollection(c);
228 >    }
229 >
230 >    private void initFromPriorityQueue(PriorityQueue<? extends E> c) {
231 >        if (c.getClass() == PriorityQueue.class) {
232 >            this.queue = c.toArray();
233 >            this.size = c.size();
234 >        } else {
235 >            initFromCollection(c);
236 >        }
237 >    }
238 >
239 >    private void initElementsFromCollection(Collection<? extends E> c) {
240 >        Object[] a = c.toArray();
241 >        // If c.toArray incorrectly doesn't return Object[], copy it.
242 >        if (a.getClass() != Object[].class)
243 >            a = Arrays.copyOf(a, a.length, Object[].class);
244 >        int len = a.length;
245 >        if (len == 1 || this.comparator != null)
246 >            for (int i = 0; i < len; i++)
247 >                if (a[i] == null)
248 >                    throw new NullPointerException();
249 >        this.queue = a;
250 >        this.size = a.length;
251      }
252  
253      /**
# Line 228 | Line 256 | public class PriorityQueue<E> extends Ab
256       * @param c the collection
257       */
258      private void initFromCollection(Collection<? extends E> c) {
259 <        Object[] a = c.toArray();
260 <        // If c.toArray incorrectly doesn't return Object[], copy it.
233 <        if (a.getClass() != Object[].class)
234 <            a = Arrays.copyOf(a, a.length, Object[].class);
235 <        queue = a;
236 <        size = a.length;
259 >        initElementsFromCollection(c);
260 >        heapify();
261      }
262  
263      /**
264 +     * The maximum size of array to allocate.
265 +     * Some VMs reserve some header words in an array.
266 +     * Attempts to allocate larger arrays may result in
267 +     * OutOfMemoryError: Requested array size exceeds VM limit
268 +     */
269 +    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
270 +
271 +    /**
272       * Increases the capacity of the array.
273       *
274       * @param minCapacity the desired minimum capacity
275       */
276      private void grow(int minCapacity) {
277 <        if (minCapacity < 0) // overflow
246 <            throw new OutOfMemoryError();
247 <        int oldCapacity = queue.length;
277 >        int oldCapacity = queue.length;
278          // Double size if small; else grow by 50%
279 <        int newCapacity = ((oldCapacity < 64)?
280 <                           ((oldCapacity + 1) * 2):
281 <                           ((oldCapacity / 2) * 3));
282 <        if (newCapacity < 0) // overflow
283 <            newCapacity = Integer.MAX_VALUE;
284 <        if (newCapacity < minCapacity)
255 <            newCapacity = minCapacity;
279 >        int newCapacity = oldCapacity + ((oldCapacity < 64) ?
280 >                                         (oldCapacity + 2) :
281 >                                         (oldCapacity >> 1));
282 >        // overflow-conscious code
283 >        if (newCapacity - MAX_ARRAY_SIZE > 0)
284 >            newCapacity = hugeCapacity(minCapacity);
285          queue = Arrays.copyOf(queue, newCapacity);
286      }
287  
288 +    private static int hugeCapacity(int minCapacity) {
289 +        if (minCapacity < 0) // overflow
290 +            throw new OutOfMemoryError();
291 +        return (minCapacity > MAX_ARRAY_SIZE) ?
292 +            Integer.MAX_VALUE :
293 +            MAX_ARRAY_SIZE;
294 +    }
295 +
296      /**
297       * Inserts the specified element into this priority queue.
298       *
# Line 300 | Line 337 | public class PriorityQueue<E> extends Ab
337      }
338  
339      private int indexOf(Object o) {
340 <        if (o != null) {
340 >        if (o != null) {
341              for (int i = 0; i < size; i++)
342                  if (o.equals(queue[i]))
343                      return i;
# Line 320 | Line 357 | public class PriorityQueue<E> extends Ab
357       * @return {@code true} if this queue changed as a result of the call
358       */
359      public boolean remove(Object o) {
360 <        int i = indexOf(o);
361 <        if (i == -1)
362 <            return false;
363 <        else {
364 <            removeAt(i);
365 <            return true;
366 <        }
360 >        int i = indexOf(o);
361 >        if (i == -1)
362 >            return false;
363 >        else {
364 >            removeAt(i);
365 >            return true;
366 >        }
367      }
368  
369      /**
# Line 337 | Line 374 | public class PriorityQueue<E> extends Ab
374       * @return {@code true} if removed
375       */
376      boolean removeEq(Object o) {
377 <        for (int i = 0; i < size; i++) {
378 <            if (o == queue[i]) {
377 >        for (int i = 0; i < size; i++) {
378 >            if (o == queue[i]) {
379                  removeAt(i);
380                  return true;
381              }
# Line 355 | Line 392 | public class PriorityQueue<E> extends Ab
392       * @return {@code true} if this queue contains the specified element
393       */
394      public boolean contains(Object o) {
395 <        return indexOf(o) != -1;
395 >        return indexOf(o) != -1;
396      }
397  
398      /**
# Line 416 | Line 453 | public class PriorityQueue<E> extends Ab
453          if (a.length < size)
454              // Make a new array of a's runtime type, but my contents:
455              return (T[]) Arrays.copyOf(queue, size, a.getClass());
456 <        System.arraycopy(queue, 0, a, 0, size);
456 >        System.arraycopy(queue, 0, a, 0, size);
457          if (a.length > size)
458              a[size] = null;
459          return a;
# Line 509 | Line 546 | public class PriorityQueue<E> extends Ab
546                  lastRetElt = null;
547              } else {
548                  throw new IllegalStateException();
549 <            }
549 >            }
550              expectedModCount = modCount;
551          }
552      }
# Line 725 | Line 762 | public class PriorityQueue<E> extends Ab
762          // Read in (and discard) array length
763          s.readInt();
764  
765 <        queue = new Object[size];
765 >        queue = new Object[size];
766  
767          // Read in all elements.
768          for (int i = 0; i < size; i++)
769              queue[i] = s.readObject();
770  
771 <        // Elements are guaranteed to be in "proper order", but the
772 <        // spec has never explained what that might be.
773 <        heapify();
771 >        // Elements are guaranteed to be in "proper order", but the
772 >        // spec has never explained what that might be.
773 >        heapify();
774      }
775   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines