Created
July 13, 2013 10:04
-
-
Save lichenbo/5990225 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PriorityQueue<E> extends AbstractQueue<E> | |
implements java.io.Serializable { | |
private static final long serialVersionUID = -7720805057305804111L; | |
private static final int DEFAULT_INITIAL_CAPACITY = 11; | |
private transient Object[] queue; | |
private int size = 0; | |
private final Comparator<? super E> comparator; | |
private transient int modCount = 0; | |
public PriorityQueue() | |
public PriorityQueue(int initialCapacity) | |
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) | |
public PriorityQueue(Collection<? extends E> c) | |
public PriorityQueue(SortedSet<? extends E> c) | |
private void initFromPriorityQueue(PriorityQueue<? extends E> c) | |
private void initElementsFromCollection(Collection<? extends E> c) | |
private void initFromCollection(Collection<? extends E> c) { | |
initElementsFromCollection(c); | |
heapify(); | |
} | |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; | |
private void grow(int minCapacity) { | |
int oldCapacity = queue.length; | |
// Double size if small; else grow by 50% | |
int newCapacity = oldCapacity + ((oldCapacity < 64) ? | |
(oldCapacity + 2) : | |
(oldCapacity >> 1)); | |
// overflow-conscious code | |
if (newCapacity - MAX_ARRAY_SIZE > 0) | |
newCapacity = hugeCapacity(minCapacity); | |
queue = Arrays.copyOf(queue, newCapacity); | |
} | |
private static int hugeCapacity(int minCapacity) | |
public boolean add(E e) | |
public boolean offer(E e) => grow之后调用siftup | |
public E peek() | |
private int indexOf(Object o) | |
public boolean remove(Object o) | |
boolean removeEq(Object o) | |
public boolean contains(Object o) | |
public Object[] toArray() | |
public <T> T[] toArray(T[] a) | |
public Iterator<E> iterator() | |
private final class Itr implements Iterator<E> { | |
... | |
} | |
public int size() | |
public void clear() | |
public E poll() | |
private E removeAt(int i) | |
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 siftDown(int k, E x) | |
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; | |
} | |
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; | |
} | |
private void heapify() { | |
for (int i = (size >>> 1) - 1; i >= 0; i--) | |
siftDown(i, (E) queue[i]); | |
} | |
public Comparator<? super E> comparator() { | |
return comparator; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment