[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.14, Mon Jul 28 16:00:19 2003 UTC revision 1.15, Thu Jul 31 07:18:02 2003 UTC
# Line 102  Line 102 
102       * @param comparator the comparator used to order this priority queue.       * @param comparator the comparator used to order this priority queue.
103       * If <tt>null</tt> then the order depends on the elements' natural       * If <tt>null</tt> then the order depends on the elements' natural
104       * ordering.       * ordering.
105         * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
106         * than 1
107       */       */
108      public PriorityQueue(int initialCapacity, Comparator<E> comparator) {      public PriorityQueue(int initialCapacity, Comparator<E> comparator) {
109          if (initialCapacity < 1)          if (initialCapacity < 1)
110              initialCapacity = 1;              throw new IllegalArgumentException();
111          queue = (E[]) new Object[initialCapacity + 1];          queue = (E[]) new Object[initialCapacity + 1];
112          this.comparator = comparator;          this.comparator = comparator;
113      }      }
# Line 113  Line 115 
115      /**      /**
116       * Create a <tt>PriorityQueue</tt> containing the elements in the specified       * Create a <tt>PriorityQueue</tt> containing the elements in the specified
117       * collection.  The priority queue has an initial capacity of 110% of the       * collection.  The priority queue has an initial capacity of 110% of the
118       * size of the specified collection. If the specified collection       * size of the specified collection; or 1 if the collection is empty.
119         * If the specified collection
120       * implements the {@link Sorted} interface, the priority queue will be       * implements the {@link Sorted} interface, the priority queue will be
121       * sorted according to the same comparator, or according to its elements'       * sorted according to the same comparator, or according to its elements'
122       * natural order if the collection is sorted according to its elements'       * natural order if the collection is sorted according to its elements'
# Line 121  Line 124 
124       * <tt>Sorted</tt>, the priority queue is ordered according to       * <tt>Sorted</tt>, the priority queue is ordered according to
125       * its elements' natural order.       * its elements' natural order.
126       *       *
127       * @param initialElements the collection whose elements are to be placed       * @param c the collection whose elements are to be placed
128       *        into this priority queue.       *        into this priority queue.
129       * @throws ClassCastException if elements of the specified collection       * @throws ClassCastException if elements of the specified collection
130       *         cannot be compared to one another according to the priority       *         cannot be compared to one another according to the priority
131       *         queue's ordering.       *         queue's ordering.
132       * @throws NullPointerException if the specified collection or an       * @throws NullPointerException if <tt>c</tt> or any element within it
133       *         element of the specified collection is <tt>null</tt>.       * is <tt>null</tt>
134       */       */
135      public PriorityQueue(Collection<E> initialElements) {      public PriorityQueue(Collection<E> c) {
136          int sz = initialElements.size();          int sz = c.size();
137          int initialCapacity = (int)Math.min((sz * 110L) / 100,          int initialCapacity = (int)Math.min((sz * 110L) / 100,
138                                              Integer.MAX_VALUE - 1);                                              Integer.MAX_VALUE - 1);
139          if (initialCapacity < 1)          if (initialCapacity < 1)
140              initialCapacity = 1;              initialCapacity = 1;
141    
142          queue = (E[]) new Object[initialCapacity + 1];          queue = (E[]) new Object[initialCapacity + 1];
143    
144          if (initialElements instanceof Sorted) {          if (c instanceof Sorted) {
145              comparator = ((Sorted)initialElements).comparator();              // FIXME: this code assumes too much
146              for (Iterator<E> i = initialElements.iterator(); i.hasNext(); )              comparator = ((Sorted)c).comparator();
147                for (Iterator<E> i = c.iterator(); i.hasNext(); )
148                  queue[++size] = i.next();                  queue[++size] = i.next();
149          } else {          } else {
150              comparator = null;              comparator = null;
151              for (Iterator<E> i = initialElements.iterator(); i.hasNext(); )              for (Iterator<E> i = c.iterator(); i.hasNext(); )
152                  add(i.next());                  add(i.next());
153          }          }
154      }      }
# Line 194  Line 199 
199    
200      /**      /**
201       * @throws NullPointerException if the specified element is <tt>null</tt>.       * @throws NullPointerException if the specified element is <tt>null</tt>.
202         * @throws ClassCastException if the specified element cannot be compared
203         * with elements currently in the priority queue according
204         * to the priority queue's ordering.
205       */       */
206      public boolean add(E element) {      public boolean add(E element) {
207          return super.add(element);          return super.add(element);
# Line 201  Line 209 
209    
210      /**      /**
211       * @throws NullPointerException if any element is <tt>null</tt>.       * @throws NullPointerException if any element is <tt>null</tt>.
212         * @throws ClassCastException if any element cannot be compared
213         * with elements currently in the priority queue according
214         * to the priority queue's ordering.
215       */       */
216      public boolean addAll(Collection<? extends E> c) {      public boolean addAll(Collection<? extends E> c) {
217          return super.addAll(c);          return super.addAll(c);
218      }      }
219    
     /**  
      * @throws NullPointerException if the specified element is <tt>null</tt>.  
      */  
220      public boolean remove(Object o) {      public boolean remove(Object o) {
221          if (o == null)          if (o == null)
222              throw new NullPointerException();              return false;
223    
224          if (comparator == null) {          if (comparator == null) {
225              for (int i = 1; i <= size; i++) {              for (int i = 1; i <= size; i++) {
# Line 231  Line 239 
239          return false;          return false;
240      }      }
241    
     /**  
      * Returns an iterator over the elements in this priority queue.  The  
      * elements of the priority queue will be returned by this iterator in the  
      * order specified by the queue, which is to say the order they would be  
      * returned by repeated calls to <tt>poll</tt>.  
      *  
      * @return an <tt>Iterator</tt> over the elements in this priority queue.  
      */  
242      public Iterator<E> iterator() {      public Iterator<E> iterator() {
243          return new Itr();          return new Itr();
244      }      }

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8