Skip to content

Commit

Permalink
Javadoc fixes, add Overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
manning authored and Stanford NLP committed May 10, 2018
1 parent 349c497 commit 0f59f52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
15 changes: 14 additions & 1 deletion src/edu/stanford/nlp/util/FixedPrioritiesPriorityQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class FixedPrioritiesPriorityQueue<E>
private static final long serialVersionUID = 1L;
private int size;
private int capacity;
@SuppressWarnings("serial")
private List<E> elements;
private double[] priorities;

Expand All @@ -51,6 +52,7 @@ public FixedPrioritiesPriorityQueue(int capacity) {
/**
* Returns true if the priority queue is non-empty
*/
@Override
public boolean hasNext() {
return ! isEmpty();
}
Expand All @@ -59,13 +61,15 @@ public boolean hasNext() {
* Returns the element in the queue with highest priority, and pops it from
* the queue.
*/
@Override
public E next() throws NoSuchElementException {
return removeFirst();
}

/**
* Not supported -- next() already removes the head of the queue.
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
Expand All @@ -78,6 +82,7 @@ public void remove() {
* the queue, it will be added an additional time, NOT promoted/demoted.
*
*/
@Override
public boolean add(E key, double priority) {
if (size == capacity) {
grow(2 * capacity + 1);
Expand All @@ -92,6 +97,7 @@ public boolean add(E key, double priority) {
/**
* Not supported in this implementation.
*/
@Override
public boolean changePriority(E key, double priority) {
throw new UnsupportedOperationException();
}
Expand All @@ -100,6 +106,7 @@ public boolean changePriority(E key, double priority) {
* Returns the highest-priority element without removing it from the
* queue.
*/
@Override
public E getFirst() {
if (size() > 0)
return elements.get(0);
Expand All @@ -110,6 +117,7 @@ public E getFirst() {
* Note that this method will be linear (not constant) time in this
* implementation! Better not to use it.
*/
@Override
public double getPriority(Object key) {
for (int i = 0, sz = elements.size(); i < sz; i++) {
if (elements.get(i).equals(key)) {
Expand All @@ -122,6 +130,7 @@ public double getPriority(Object key) {
/**
* Gets the priority of the highest-priority element of the queue.
*/
@Override
public double getPriority() {
// check empty other way around
if (size() > 0)
Expand All @@ -132,13 +141,15 @@ public double getPriority() {
/**
* Not supported in this implementation.
*/
@Override
public boolean relaxPriority(E key, double priority) {
throw new UnsupportedOperationException();
}

/**
* Returns the highest-priority element and removes it from the queue.
*/
@Override
public E removeFirst() throws NoSuchElementException {
E first = getFirst();
swap(0, size - 1);
Expand All @@ -148,6 +159,7 @@ public E removeFirst() throws NoSuchElementException {
return first;
}

@Override
public List<E> toSortedList() {
// initialize with size
List<E> list = new ArrayList<>();
Expand Down Expand Up @@ -257,6 +269,7 @@ public String toString() {
}

/** {@inheritDoc} */
@Override
public String toString(int maxKeysToPrint) {
return toString(maxKeysToPrint, "%.3f");
}
Expand Down Expand Up @@ -288,7 +301,7 @@ public String toString(int maxKeysToPrint, String dblFmt) {
if (numKeysPrinted < size()) {
sb.append("...");
}
sb.append("]");
sb.append(']');
return sb.toString();
}

Expand Down
26 changes: 14 additions & 12 deletions src/edu/stanford/nlp/util/PriorityQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@
/**
* A Set that also represents an ordering of its elements, and responds
* quickly to {@code add()}, {@code changePriority()},
* {@code removeFirst()}, and {@code getFirst()} method calls. <p/>
*
* {@code removeFirst()}, and {@code getFirst()} method calls.
* <p>
* There are several important differences between this interface and
* the JDK {@link java.util.PriorityQueue}: <p/>
* the JDK {@link java.util.PriorityQueue}:
* <p>
* <ol>
* <li> This interface uses explicitly-assigned <code>double</code> values
* <li> This interface uses explicitly-assigned {@code double} values
* as priorities for queue elements, while
* <code>java.util.PriorityQueue</code> uses either the elements'
* {@code java.util.PriorityQueue} uses either the elements'
* <i>natural order</i> (see {@link java.lang.Comparable}) or a {@link
* java.util.Comparator}.</li>
*
* <li> In this interface, larger <code>double</code>s represent higher
* priorities; in <code>java.util.PriorityQueue</code>, <i>lesser</i>
* <li> In this interface, larger {@code double}s represent higher
* priorities; in {@code java.util.PriorityQueue}, <i>lesser</i>
* elements (with respect to the specified ordering) have higher
* priorities.</li>
*
* <li> This interface enables you to <i>change</i> the priority of an
* element <i>after</i> it has entered the queue. With
* <code>java.util.PriorityQueue</code>, that's not possible.</li>
* {@code java.util.PriorityQueue}, that's not possible.</li>
*
* <li> However, there is a price to pay for this flexibility. The primary
* implementation of this interface, {@link
* edu.stanford.nlp.util.BinaryHeapPriorityQueue}, is roughly 2x slower
* than <code>java.util.PriorityQueue</code> in informal benchmark
* than {@code java.util.PriorityQueue} in informal benchmark
* testing.</li>
*
* <li> So, there's another implementation of this interface,
Expand All @@ -40,7 +41,8 @@
* </ol>
* <p>
* On the other hand, this interface and {@link java.util.PriorityQueue}
* also have some characteristics in common: <p/>
* also have some characteristics in common:
* <p>
* <ol>
* <li> Both make no guarantee about the order in which elements with equal
* priority are returned from the queue. This does <i>not</i> mean that
Expand Down Expand Up @@ -100,7 +102,7 @@ public interface PriorityQueue<E> extends Set<E> {
* a lower priority, but that wasn't the historical behavior, and it seemed like
* we'd need to do a lot of archeology before changing the behavior.
*
* @return <tt>true</tt> if this set did not already contain the specified
* @return {@code true} if this set did not already contain the specified
* element.
*/
public boolean add(E key, double priority);
Expand All @@ -109,7 +111,7 @@ public interface PriorityQueue<E> extends Set<E> {
/**
* Changes a priority, either up or down, adding the key it if it wasn't there already.
*
* @param key an <code>E</code> value
* @param key an {@code E} value
* @return whether the priority actually changed.
*/
public boolean changePriority(E key, double priority);
Expand Down

0 comments on commit 0f59f52

Please sign in to comment.