1 7 8 package java.util; 9 10 66 public class PriorityQueue<E> extends AbstractQueue <E> 67 implements java.io.Serializable { 68 69 private static final long serialVersionUID = -7720805057305804111L; 70 71 private static final int DEFAULT_INITIAL_CAPACITY = 11; 72 73 86 private transient Object [] queue; 87 88 91 private int size = 0; 92 93 97 private final Comparator <? super E> comparator; 98 99 103 private transient int modCount = 0; 104 105 110 public PriorityQueue() { 111 this(DEFAULT_INITIAL_CAPACITY, null); 112 } 113 114 123 public PriorityQueue(int initialCapacity) { 124 this(initialCapacity, null); 125 } 126 127 138 public PriorityQueue(int initialCapacity, 139 Comparator <? super E> comparator) { 140 if (initialCapacity < 1) 141 throw new IllegalArgumentException (); 142 this.queue = new Object [initialCapacity + 1]; 143 this.comparator = comparator; 144 } 145 146 150 private void initializeArray(Collection <? extends E> c) { 151 int sz = c.size(); 152 int initialCapacity = (int)Math.min((sz * 110L) / 100, 153 Integer.MAX_VALUE - 1); 154 if (initialCapacity < 1) 155 initialCapacity = 1; 156 157 this.queue = new Object [initialCapacity + 1]; 158 } 159 160 165 private void fillFromSorted(Collection <? extends E> c) { 166 for (Iterator <? extends E> i = c.iterator(); i.hasNext(); ) 167 queue[++size] = i.next(); 168 } 169 170 175 private void fillFromUnsorted(Collection <? extends E> c) { 176 for (Iterator <? extends E> i = c.iterator(); i.hasNext(); ) 177 queue[++size] = i.next(); 178 heapify(); 179 } 180 181 201 public PriorityQueue(Collection <? extends E> c) { 202 initializeArray(c); 203 if (c instanceof SortedSet ) { 204 SortedSet <? extends E> s = (SortedSet <? extends E>)c; 205 comparator = (Comparator <? super E>)s.comparator(); 206 fillFromSorted(s); 207 } else if (c instanceof PriorityQueue ) { 208 PriorityQueue <? extends E> s = (PriorityQueue <? extends E>) c; 209 comparator = (Comparator <? super E>)s.comparator(); 210 fillFromSorted(s); 211 } else { 212 comparator = null; 213 fillFromUnsorted(c); 214 } 215 } 216 217 234 public PriorityQueue(PriorityQueue <? extends E> c) { 235 initializeArray(c); 236 comparator = (Comparator <? super E>)c.comparator(); 237 fillFromSorted(c); 238 } 239 240 257 public PriorityQueue(SortedSet <? extends E> c) { 258 initializeArray(c); 259 comparator = (Comparator <? super E>)c.comparator(); 260 fillFromSorted(c); 261 } 262 263 266 private void grow(int index) { 267 int newlen = queue.length; 268 if (index < newlen) return; 270 if (index == Integer.MAX_VALUE) 271 throw new OutOfMemoryError (); 272 while (newlen <= index) { 273 if (newlen >= Integer.MAX_VALUE / 2) newlen = Integer.MAX_VALUE; 275 else 276 newlen <<= 2; 277 } 278 Object [] newQueue = new Object [newlen]; 279 System.arraycopy(queue, 0, newQueue, 0, queue.length); 280 queue = newQueue; 281 } 282 283 284 293 public boolean offer(E o) { 294 if (o == null) 295 throw new NullPointerException (); 296 modCount++; 297 ++size; 298 299 if (size >= queue.length) 301 grow(size); 302 303 queue[size] = o; 304 fixUp(size); 305 return true; 306 } 307 308 public E peek() { 309 if (size == 0) 310 return null; 311 return (E) queue[1]; 312 } 313 314 316 326 public boolean add(E o) { 327 return offer(o); 328 } 329 330 334 public boolean remove(Object o) { 335 if (o == null) 336 return false; 337 338 if (comparator == null) { 339 for (int i = 1; i <= size; i++) { 340 if (((Comparable <E>)queue[i]).compareTo((E)o) == 0) { 341 removeAt(i); 342 return true; 343 } 344 } 345 } else { 346 for (int i = 1; i <= size; i++) { 347 if (comparator.compare((E)queue[i], (E)o) == 0) { 348 removeAt(i); 349 return true; 350 } 351 } 352 } 353 return false; 354 } 355 356 362 public Iterator <E> iterator() { 363 return new Itr(); 364 } 365 366 private class Itr implements Iterator <E> { 367 368 372 private int cursor = 1; 373 374 379 private int lastRet = 0; 380 381 386 private int expectedModCount = modCount; 387 388 399 private ArrayList <E> forgetMeNot = null; 400 401 405 private Object lastRetElt = null; 406 407 public boolean hasNext() { 408 return cursor <= size || forgetMeNot != null; 409 } 410 411 public E next() { 412 checkForComodification(); 413 E result; 414 if (cursor <= size) { 415 result = (E) queue[cursor]; 416 lastRet = cursor++; 417 } 418 else if (forgetMeNot == null) 419 throw new NoSuchElementException (); 420 else { 421 int remaining = forgetMeNot.size(); 422 result = forgetMeNot.remove(remaining - 1); 423 if (remaining == 1) 424 forgetMeNot = null; 425 lastRet = 0; 426 lastRetElt = result; 427 } 428 return result; 429 } 430 431 public void remove() { 432 checkForComodification(); 433 434 if (lastRet != 0) { 435 E moved = PriorityQueue.this.removeAt(lastRet); 436 lastRet = 0; 437 if (moved == null) { 438 cursor--; 439 } else { 440 if (forgetMeNot == null) 441 forgetMeNot = new ArrayList <E>(); 442 forgetMeNot.add(moved); 443 } 444 } else if (lastRetElt != null) { 445 PriorityQueue.this.remove(lastRetElt); 446 lastRetElt = null; 447 } else { 448 throw new IllegalStateException (); 449 } 450 451 expectedModCount = modCount; 452 } 453 454 final void checkForComodification() { 455 if (modCount != expectedModCount) 456 throw new ConcurrentModificationException (); 457 } 458 } 459 460 public int size() { 461 return size; 462 } 463 464 468 public void clear() { 469 modCount++; 470 471 for (int i=1; i<=size; i++) 473 queue[i] = null; 474 475 size = 0; 476 } 477 478 public E poll() { 479 if (size == 0) 480 return null; 481 modCount++; 482 483 E result = (E) queue[1]; 484 queue[1] = queue[size]; 485 queue[size--] = null; if (size > 1) 487 fixDown(1); 488 489 return result; 490 } 491 492 505 private E removeAt(int i) { 506 assert i > 0 && i <= size; 507 modCount++; 508 509 E moved = (E) queue[size]; 510 queue[i] = moved; 511 queue[size--] = null; if (i <= size) { 513 fixDown(i); 514 if (queue[i] == moved) { 515 fixUp(i); 516 if (queue[i] != moved) 517 return moved; 518 } 519 } 520 return null; 521 } 522 523 532 private void fixUp(int k) { 533 if (comparator == null) { 534 while (k > 1) { 535 int j = k >> 1; 536 if (((Comparable <E>)queue[j]).compareTo((E)queue[k]) <= 0) 537 break; 538 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; 539 k = j; 540 } 541 } else { 542 while (k > 1) { 543 int j = k >>> 1; 544 if (comparator.compare((E)queue[j], (E)queue[k]) <= 0) 545 break; 546 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; 547 k = j; 548 } 549 } 550 } 551 552 561 private void fixDown(int k) { 562 int j; 563 if (comparator == null) { 564 while ((j = k << 1) <= size && (j > 0)) { 565 if (j<size && 566 ((Comparable <E>)queue[j]).compareTo((E)queue[j+1]) > 0) 567 j++; 569 if (((Comparable <E>)queue[k]).compareTo((E)queue[j]) <= 0) 570 break; 571 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; 572 k = j; 573 } 574 } else { 575 while ((j = k << 1) <= size && (j > 0)) { 576 if (j<size && 577 comparator.compare((E)queue[j], (E)queue[j+1]) > 0) 578 j++; if (comparator.compare((E)queue[k], (E)queue[j]) <= 0) 580 break; 581 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; 582 k = j; 583 } 584 } 585 } 586 587 591 private void heapify() { 592 for (int i = size/2; i >= 1; i--) 593 fixDown(i); 594 } 595 596 604 public Comparator <? super E> comparator() { 605 return comparator; 606 } 607 608 617 private void writeObject(java.io.ObjectOutputStream s) 618 throws java.io.IOException { 619 s.defaultWriteObject(); 621 622 s.writeInt(queue.length); 624 625 for (int i=1; i<=size; i++) 627 s.writeObject(queue[i]); 628 } 629 630 635 private void readObject(java.io.ObjectInputStream s) 636 throws java.io.IOException , ClassNotFoundException { 637 s.defaultReadObject(); 639 640 int arrayLength = s.readInt(); 642 queue = new Object [arrayLength]; 643 644 for (int i=1; i<=size; i++) 646 queue[i] = (E) s.readObject(); 647 } 648 649 } 650 | Popular Tags |