scala.collection.mutable

class PriorityQueue

[source: scala/collection/mutable/PriorityQueue.scala]

@serializable

@cloneable

class PriorityQueue[A](implicit ord : Ordering[A])
extends ResizableArray[A] with Addable[A, PriorityQueue[A]] with Growable[A] with Cloneable[PriorityQueue[A]]
This class implements priority queues using a heap. To prioritize elements of type T there must be an implicit Ordering[T] available at creation.
Author
Matthias Zenger
Version
1.0, 03/05/2004
Values and Variables inherited from ResizableArray
array, size0
Method Summary
def + (elem : A) : PriorityQueue[A]
Creates a new collection with an additional element, unless the element is already present.
override def + (elem1 : A, elem2 : A, elems : A*) : PriorityQueue[A]
Add two or more elements to this set.
def ++ (elems : Traversable[A]) : PriorityQueue[A]
Adds all elements provided by an Iterable object into the priority queue.
override def ++ (iter : Iterator[A]) : PriorityQueue[A]
Adds all elements provided by an iterator into the priority queue.
def += (elem : A) : PriorityQueue[A]
Inserts a single element into the priority queue.
def clear : Unit
Removes all elements from the queue. After this operation is completed, the queue will be empty.
override def clone : PriorityQueue[A]
This method clones the priority queue.
def dequeue : A
Returns the element with the highest priority in the queue, and removes this element from the queue.
def enqueue (elems : A*) : Unit
Adds all elements to the queue.
override def equals (obj : Any) : Boolean
Checks if two queues are structurally identical.
protected def fixDown (as : Array[AnyRef], m : Int, n : Int) : Unit
protected def fixUp (as : Array[AnyRef], m : Int) : Unit
override def foreach [U](f : (A) => U) : Unit
Copy all elements to a buffer
override def hashCode : Int
The hashCode method always yields an error, since it is not safe to use mutable queues as keys in hash tables.
override def isEmpty : Boolean
Does this iterable contain no elements?
override def iterator : Iterator[A]
Returns an iterator which yields all the elements of the priority queue in descending priority order.
override def length : Int
Returns the length of this resizable array.
def max : A
Returns the element with the highest priority in the queue, or throws an error if there is no element contained in the queue.
protected override def thisCollection : PriorityQueue[A]
override def toList : List[A]
Returns a list with all elements of this traversable object.
def toQueue : Queue[A]
Returns a regular queue containing the same elements.
override def toString : java.lang.String
Returns a textual representation of a queue as a string.
Methods inherited from Growable
+=, ++=, ++=
Methods inherited from Addable
++
Methods inherited from ResizableArray
companion, initialSize, apply, update, copyToArray, reduceToSize, ensureSize, swap, copy
Methods inherited from MutableVectorTemplate
view, view
Methods inherited from VectorTemplate
forall, exists, find, foldLeft, foldRight, reduceLeft, reduceRight, zip, zipWithIndex, slice, head, tail, last, init, take, drop, takeRight, dropRight, splitAt, takeWhile, dropWhile, span, sameElements, copyToArray, lengthCompare, segmentLength, indexWhere, lastIndexWhere, reverse, reverseIterator, startsWith, endsWith
Methods inherited from Unhashable
identityHashCode
Methods inherited from SequenceTemplate
size, isDefinedAt, zipAll, prefixLength, indexWhere, findIndexOf, indexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexWhere, reversedElements, startsWith, indexOfSeq, indexOfSeq, lastIndexOfSeq, lastIndexOfSeq, contains, union, diff, intersect, removeDuplicates, patch, padTo, toSequence, indices, sortWith, findLastIndexOf, slice, equalsWith, containsSlice, projection
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from IterableTemplate
elements, toIterable, toStream, first, firstOption, toSeq
Methods inherited from TraversableClass
newBuilder, genericBuilder, unzip, flatten, transpose
Methods inherited from TraversableTemplate
nonEmpty, hasDefiniteSize, ++, ++, map, flatMap, filter, filterMap, filterNot, remove, partition, groupBy, count, /:, :\, reduceLeftOption, reduceRightOption, headOption, lastOption, copyToBuffer, toArray, toSet, mkString, mkString, mkString, addString, addString, addString, stringPrefix
Methods inherited from AnyRef
getClass, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
override def length : Int
Returns the length of this resizable array.
Overrides
ResizableArray.length

override def isEmpty : Boolean
Does this iterable contain no elements?

protected override def thisCollection : PriorityQueue[A]

The proper way to do this would be to make self of type This. But unfortunately this makes this to be of type Traversable[A]. Since Traversable is a subtype of TraversableTemplate, all methods of this are taken from Traversable. In particular the newBuilder method is taken from Traversable, which means it yields a Traversable[A] instead of a This.

The right way out of this is to change Scala's member selection rules, so that always the most specific type will be selected, no matter whether a member is abstract or concrete. I tried to fake this by having a method thisTemplate which returns this at the Template type. But unfortunately that does not work, because we need to call newBuilder on this at the Template type (so that we get back a This) and newBuilder has to be a protected[this] because of variance.
The less appealing alternative is implemented now: Forget the self type and introduce a thisCollection which is this seen as an instance of This. We should go back to this once we have ameliorated Scala's member selection rules.

Overrides
Addable.thisCollection

override def foreach[U](f : (A) => U) : Unit
Copy all elements to a buffer
Parameters
The - buffer to which elements are copied override def copyToBuffer[B >: A](dest: Buffer[B]) { dest ++= (array: Sequence[AnyRef]).asInstanceOf[Sequence[B]] }
Overrides
ResizableArray.foreach

protected def fixUp(as : Array[AnyRef], m : Int) : Unit

protected def fixDown(as : Array[AnyRef], m : Int, n : Int) : Unit

def +=(elem : A) : PriorityQueue[A]
Inserts a single element into the priority queue.
Parameters
elem - the element to insert
Overrides
Growable.+=

def +(elem : A) : PriorityQueue[A]
Creates a new collection with an additional element, unless the element is already present.
Parameters
elem - the element to be added
Returns
a fresh collection
Overrides
Addable.+

override def +(elem1 : A, elem2 : A, elems : A*) : PriorityQueue[A]
Add two or more elements to this set.
Parameters
elem1 - the first element.
kv2 - the second element.
kvs - the remaining elements.
Overrides
Addable.+

def ++(elems : Traversable[A]) : PriorityQueue[A]
Adds all elements provided by an Iterable object into the priority queue.
Parameters
iter - an iterable object

override def ++(iter : Iterator[A]) : PriorityQueue[A]
Adds all elements provided by an iterator into the priority queue.
Parameters
it - an iterator
Overrides
Addable.++

def enqueue(elems : A*) : Unit
Adds all elements to the queue.
Parameters
elems - the elements to add.

def dequeue : A
Returns the element with the highest priority in the queue, and removes this element from the queue.
Throws
Predef.NoSuchElementException -
Returns
the element with the highest priority.

def max : A
Returns the element with the highest priority in the queue, or throws an error if there is no element contained in the queue.
Returns
the element with the highest priority.

def clear : Unit
Removes all elements from the queue. After this operation is completed, the queue will be empty.
Overrides
Growable.clear

override def iterator : Iterator[A]
Returns an iterator which yields all the elements of the priority queue in descending priority order.
Returns
an iterator over all elements sorted in descending order.

override def equals(obj : Any) : Boolean
Checks if two queues are structurally identical.
Returns
true, iff both queues contain the same sequence of elements.

override def hashCode : Int
The hashCode method always yields an error, since it is not safe to use mutable queues as keys in hash tables.
Returns
never.

def toQueue : Queue[A]
Returns a regular queue containing the same elements.

override def toString : java.lang.String
Returns a textual representation of a queue as a string.
Returns
the string representation of this queue.

override def toList : List[A]
Returns a list with all elements of this traversable object.
Notes
Will not terminate for infinite-sized collections.

override def clone : PriorityQueue[A]
This method clones the priority queue.
Returns
a priority queue with the same elements.
Overrides
Cloneable.clone