//Look at the constructor first. public PriorityQueue() { this(DEFAULT_INITIAL_CAPACITY, null); } public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { if (initialCapacity < 1) throw new IllegalArgumentException(); this.queue = new Object[initialCapacity]; this.comparator = comparator; } public PriorityQueue(Collection<? extends E> c) { if (c instanceof SortedSet<?>) { //The incoming is SortedSet SortedSet<? extends E> ss = (SortedSet<? extends E>) c; this.comparator = (Comparator<? super E>) ss.comparator(); initElementsFromCollection(ss); } / / Determine whether it is a subclass of PriorityQueue else if (c instanceof PriorityQueue<?>) { PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c; this.comparator = (Comparator<? super E>) pq.comparator(); initFromPriorityQueue(pq); } else { this.comparator = null; initFromCollection(c); } } private void initElementsFromCollection(Collection<? extends E> c) { //Because the SortedSet has a natural order, copy the array directly Object[] a = c.toArray(); // If c.toArray incorrectly doesn't return Object[], copy it.可能会出BUG。 if (a.getClass() != Object[].class) //recreate an array a = Arrays.copyOf(a, a.length, Object[].class); int len = a.length; //not understand if (len == 1 || this.comparator != null) for (int i = 0; i < len; i++) if (a[i] == null) throw new NullPointerException(); this.queue = a; this.size = a.length; } private void initFromPriorityQueue(PriorityQueue<? extends E> c) { //The actual type is PriorityQueue if (c.getClass() == PriorityQueue.class) { this.queue = c.toArray(); this.size = c.size(); } else { initFromCollection(c); } } private void initFromCollection(Collection<? extends E> c) { initElementsFromCollection(c); heapify(); } private void heapify() { for (int i = (size >>> 1) - 1; i >= 0; i--) siftDown(i, (E) queue[i]); } private void siftDown(int k, E x) { if (comparator != null) siftDownUsingComparator(k, x); else siftDownComparable(k, x); } //sort with comparator private void siftDownUsingComparator(int k, E x) { int half = size >>> 1; while (k < half) { int child = (k << 1) + 1; Object c = queue[child]; int right = child + 1; if (right < size && comparator.compare((E) c, (E) queue[right]) > 0) c = queue[child = right]; if (comparator.compare(x, (E) c) <= 0) break; queue[k] = c; k = child; } queue[k] = x; } //sort without comparator private void siftDownComparable(int k, E x) { Comparable<? super E> key = (Comparable<? super E>)x; int half = size >>> 1; // loop while a non-leaf while (k < half) { int child = (k << 1) + 1; // assume left child is least Object c = queue[child]; int right = child + 1; if (right < size && ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0) c = queue[child = right]; if (key.compareTo((E) c) <= 0) break; queue[k] = c; k = child; } queue[k] = key; } //add method to add elements public boolean add(E e) { return offer(e); } public boolean offer(E e) { if (e == null) throw new NullPointerException(); // add one to the number of modifications modCount++; int i = size; if (i >= queue.length) //If the current capacity is not enough, expand the capacity grow(i + 1); size = i + 1; // insert the first element if (i == 0) queue[0] = and; else // Insert the element at the appropriate position. (move element up) siftUp(i, e); return true; } private void siftUp(int k, E x) { if (comparator != null) siftUpUsingComparator(k, x); else siftUpComparable(k, x); } private void siftUpComparable(int k, E x) { Comparable<? super E> key = (Comparable<? super E>) x; while (k > 0) { int parent = (k - 1) >>> 1; Object e = queue[parent]; if (key.compareTo((E) e) >= 0) break; queue[k] = e; k = parent; } queue[k] = key; } private void siftUpUsingComparator(int k, E x) { while (k > 0) { int parent = (k - 1) >>> 1; Object e = queue[parent]; if (comparator.compare(x, (E) e) >= 0) break; queue[k] = e; k = parent; } queue[k] = x; } private void grow(int minCapacity) { int oldCapacity = queue.length; // If it is less than 64, the expansion is 2 times +2, otherwise the expansion is 50%. int newCapacity = oldCapacity + ((oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1)); // exceeds the maximum value of the array if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); queue = Arrays.copyOf(queue, newCapacity); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } private void siftUp(int k, E x) { if (comparator != null) siftUpUsingComparator(k, x); else siftUpComparable(k, x); } private void siftUpUsingComparator(int k, E x) { while (k > 0) { int parent = (k - 1) >>> 1; Object e = queue[parent]; if (comparator.compare(x, (E) e) >= 0) break; queue[k] = e; k = parent; } queue[k] = x; } //Get the head element but don't remove it and return null if the queue is empty public E peek() { if (size == 0) return null; return (E) queue[0]; } //Remove the head element and return null if the queue is empty (move down) public E poll() { if (size == 0) return null; int s = --size; modCount++; E result = (E) queue[0]; E x = (E) queue[s]; queue[s] = null; if (s != 0) siftDown (0, x); return result; } // delete element public boolean remove(Object o) { int i = indexOf(o); if (i == -1) return false; else { removeAt(i); return true; } } private int indexOf(Object o) { if (o != null) { for (int i = 0; i < size; i++) if (o.equals(queue[i])) return i; } return -1; } private E removeAt(int i) { assert i >= 0 && i < size; modCount++; int s = --size; if (s == i) // removed last element queue[i] = null; else { E moved = (E) queue[s]; queue[s] = null; siftDown(i, moved); if (queue[i] == moved) { siftUp(i, moved); if (queue[i] != moved) return moved; } } return null; } // clear all elements public void clear() { modCount++; for (int i = 0; i < size; i++) queue[i] = null; size = 0; } //returns the array element of the specified type public <T> T[] toArray(T[] a) { //If the incoming array length is less than the queue length, directly create a new queue and load all the elements in the queue if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(queue, size, a.getClass()); // Greater than direct copy System.arraycopy(queue, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } /**Summary: PriorityQueen is implemented using a binary heap. As for what is the binary heap, please refer to this blog. http://www.cnblogs.com/tstd/p/5125949.html. */
Read priorityQueen source code
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326545824&siteId=291194637
Recommended
Ranking