[cvs] / jsr166 / src / main / java / util / PriorityQueue.java Repository:
ViewVC logotype

Diff of /jsr166/src/main/java/util/PriorityQueue.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.69, Sun May 18 23:59:57 2008 UTC revision 1.70, Mon May 10 20:11:01 2010 UTC
# Line 170  Line 170 
170       * @throws NullPointerException if the specified collection or any       * @throws NullPointerException if the specified collection or any
171       *         of its elements are null       *         of its elements are null
172       */       */
173        @SuppressWarnings("unchecked")
174      public PriorityQueue(Collection<? extends E> c) {      public PriorityQueue(Collection<? extends E> c) {
175          initFromCollection(c);          if (c instanceof SortedSet<?>) {
176          if (c instanceof SortedSet)              SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
177              comparator = (Comparator<? super E>)              this.comparator = (Comparator<? super E>) ss.comparator();
178                  ((SortedSet<? extends E>)c).comparator();              initElementsFromCollection(ss);
179          else if (c instanceof PriorityQueue)          }
180              comparator = (Comparator<? super E>)          else if (c instanceof PriorityQueue<?>) {
181                  ((PriorityQueue<? extends E>)c).comparator();              PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
182                this.comparator = (Comparator<? super E>) pq.comparator();
183                initFromPriorityQueue(pq);
184            }
185          else {          else {
186              comparator = null;              this.comparator = null;
187              heapify();              initFromCollection(c);
188          }          }
189      }      }
190    
# Line 198  Line 202 
202       * @throws NullPointerException if the specified priority queue or any       * @throws NullPointerException if the specified priority queue or any
203       *         of its elements are null       *         of its elements are null
204       */       */
205        @SuppressWarnings("unchecked")
206      public PriorityQueue(PriorityQueue<? extends E> c) {      public PriorityQueue(PriorityQueue<? extends E> c) {
207          comparator = (Comparator<? super E>)c.comparator();          this.comparator = (Comparator<? super E>) c.comparator();
208          initFromCollection(c);          initFromPriorityQueue(c);
209      }      }
210    
211      /**      /**
# Line 216  Line 221 
221       * @throws NullPointerException if the specified sorted set or any       * @throws NullPointerException if the specified sorted set or any
222       *         of its elements are null       *         of its elements are null
223       */       */
224        @SuppressWarnings("unchecked")
225      public PriorityQueue(SortedSet<? extends E> c) {      public PriorityQueue(SortedSet<? extends E> c) {
226          comparator = (Comparator<? super E>)c.comparator();          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);          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      /**      /**
254       * Initializes queue array with elements from the given Collection.       * Initializes queue array with elements from the given Collection.
# Line 227  Line 256 
256       * @param c the collection       * @param c the collection
257       */       */
258      private void initFromCollection(Collection<? extends E> c) {      private void initFromCollection(Collection<? extends E> c) {
259          Object[] a = c.toArray();          initElementsFromCollection(c);
260          // If c.toArray incorrectly doesn't return Object[], copy it.          heapify();
         if (a.getClass() != Object[].class)  
             a = Arrays.copyOf(a, a.length, Object[].class);  
         queue = a;  
         size = a.length;  
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.       * Increases the capacity of the array.
273       *       *
274       * @param minCapacity the desired minimum capacity       * @param minCapacity the desired minimum capacity
275       */       */
276      private void grow(int minCapacity) {      private void grow(int minCapacity) {
         if (minCapacity < 0) // overflow  
             throw new OutOfMemoryError();  
277          int oldCapacity = queue.length;          int oldCapacity = queue.length;
278          // Double size if small; else grow by 50%          // Double size if small; else grow by 50%
279          int newCapacity = ((oldCapacity < 64)?          int newCapacity = oldCapacity + ((oldCapacity < 64) ?
280                             ((oldCapacity + 1) * 2):                                           (oldCapacity + 2) :
281                             ((oldCapacity / 2) * 3));                                           (oldCapacity >> 1));
282          if (newCapacity < 0) // overflow          // overflow-conscious code
283              newCapacity = Integer.MAX_VALUE;          if (newCapacity - MAX_ARRAY_SIZE > 0)
284          if (newCapacity < minCapacity)              newCapacity = hugeCapacity(minCapacity);
             newCapacity = minCapacity;  
285          queue = Arrays.copyOf(queue, newCapacity);          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.       * Inserts the specified element into this priority queue.
298       *       *

Legend:
Removed from v.1.69  
changed lines
  Added in v.1.70

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8