IndexedPrioQueue

com.phasmidsoftware.visitor.core.IndexedPrioQueue
See theIndexedPrioQueue companion object
case class IndexedPrioQueue[T]

An immutable indexed priority queue ADT backed by BinaryHeap.

Extends the PrioQueue contract with O(log n) decreaseKey and O(1) contains, enabled by a Map[T, Int] index that tracks each element's current position in the heap array.

Unlike PrioQueue, duplicates are not permitted: offer is a no-op if the element is already present. Use decreaseKey to improve an existing entry's priority. This one-entry-per-key invariant is what makes the index meaningful and decreaseKey correct.

The index is rebuilt from heap.data (O(n)) after each structural operation (offer and take). This is acceptable because:

  • the O(n) rebuild cost is dominated by the O(E log V) traversal cost
  • the implementation remains simple, correct, and easy to reason about

Minimum element is dequeued first (or maximum via IndexedPrioQueue.emptyMax).

Type parameters

T

the element type

Value parameters

heap

the underlying binary heap

index

a map from element to its current 0-based position in heap.data

Attributes

Companion
object
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def contains(t: T): Boolean

Returns true if t is currently in the queue. O(1).

Returns true if t is currently in the queue. O(1).

Attributes

def decreaseKey(oldT: T, newT: T): IndexedPrioQueue[T]

Replace oldT with newT if newT has strictly lower priority. If oldT is not present, this is a no-op.

Replace oldT with newT if newT has strictly lower priority. If oldT is not present, this is a no-op.

Implemented by removing oldT from the heap array, inserting newT, and rebuilding the heap from scratch. O(n log n) in the worst case, but correct and simple. The index is rebuilt after the operation. O(n).

Attributes

def head: T
def isEmpty: Boolean
def offer(t: T): IndexedPrioQueue[T]

Add an element. No-op if the element is already present — use decreaseKey to improve its priority. O(n) due to index rebuild.

Add an element. No-op if the element is already present — use decreaseKey to improve its priority. O(n) due to index rebuild.

Attributes

def size: Int
def take: (T, IndexedPrioQueue[T])

Remove and return the minimum element. O(n) due to index rebuild.

Remove and return the minimum element. O(n) due to index rebuild.

Attributes

Inherited methods

def productElementNames: Iterator[String]

An iterator over the names of all the elements of this product.

An iterator over the names of all the elements of this product.

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

An iterator over all the elements of this product.

An iterator over all the elements of this product.

Attributes

Returns

in the default implementation, an Iterator[Any]

Inherited from:
Product