scoop:scoop.std:0.1.0
src/collections/priority_queue.scoop
Selected production source · src/collections/priority_queue.scoop
1
package scoop.collections3
import scoop.comparisons.Comparator5
/**6
* A binary min-heap ordered by its comparator. Removal returns the smallest element; iteration7
* exposes heap order, not sorted order. Structural changes invalidate iterators.8
*/9
public class PriorityQueue<T> public constructor(10
/**11
* The comparator used for ordering and, for ordered maps/sets, equivalence.12
*/13
public val comparator: Comparator<T>) : Collection<T> {14
private var storage: ElementBuffer<T> = ElementBuffer<T>(8L)15
private var elementCount: Long = 0L16
private val structure: ModificationCounter = ModificationCounter()17
/**18
* The number of queued elements, including duplicates.19
*/20
public override val size: Long get() = elementCount22
internal fun elementAt(index: Long): T = storage.read(index)24
private fun compare(left: T, right: T, stamp: RevisionStamp): Long {25
val result = comparator.compare(left, right)26
structure.check(stamp)27
return result28
}30
private fun ensureRoom() {31
if (elementCount < storage.capacity) return32
val replacement = ElementBuffer<T>(expandedCollectionCapacity(storage.capacity))33
var index = 0L34
while (index < elementCount) { replacement.write(index, storage.read(index)); index += 1L }35
storage = replacement36
}38
/**39
* Inserts an element while preserving the heap ordering.40
*/41
public fun add(element: T) {42
val stamp = structure.snapshot()43
var destination = elementCount44
while (destination > 0L) {45
val parent = (destination - 1L) / 2L46
if (compare(element, storage.read(parent), stamp) >= 0L) break47
destination = parent48
}49
ensureRoom()50
var index = elementCount51
while (index > destination) {52
val parent = (index - 1L) / 2L53
storage.write(index, storage.read(parent))54
index = parent55
}56
storage.write(destination, element)57
elementCount += 1L58
structure.advance()59
}61
/**62
* Returns the smallest element without removal; an empty queue throws NoSuchElementException.63
*/64
public fun peek(): T {65
if (elementCount == 0L) throw NoSuchElementException("empty priority queue")66
return storage.read(0L)67
}69
/**70
* Returns the smallest element without removal, or None when empty.71
*/72
public fun peekOrNone(): T? = if (elementCount == 0L) None else Some(storage.read(0L))73
/**74
* Removes the smallest element, or returns None when empty.75
*/76
public fun removeFirstOrNone(): T? = if (elementCount == 0L) None else Some(removeFirst())78
/**79
* Removes the smallest element; an empty queue throws NoSuchElementException.80
*/81
public fun removeFirst(): T {82
if (elementCount == 0L) throw NoSuchElementException("empty priority queue")83
val result = storage.read(0L)84
val remaining = elementCount - 1L85
if (remaining > 0L) {86
val last = storage.read(remaining)87
val stamp = structure.snapshot()88
var destination = 0L89
var mask = 1L90
while (destination < remaining / 2L) {91
var child = destination * 2L + 1L92
if (child + 1L < remaining && compare(storage.read(child + 1L), storage.read(child), stamp) < 0L) child += 1L93
if (compare(last, storage.read(child), stamp) <= 0L) break94
destination = child95
mask *= 2L96
}97
// The bits of destination + 1 encode the root-to-node path.98
var index = 0L99
mask /= 2L100
while (mask > 0L) {101
val child = index * 2L + 1L + ((destination + 1L) / mask) % 2L102
storage.write(index, storage.read(child))103
index = child104
mask /= 2L105
}106
storage.write(destination, last)107
}108
storage.erase(remaining)109
elementCount = remaining110
structure.advance()111
return result112
}114
/**115
* Removes all elements.116
*/117
public fun clear() {118
if (elementCount == 0L) return119
var index = 0L120
while (index < elementCount) { storage.erase(index); index += 1L }121
elementCount = 0L122
structure.advance()123
}125
/**126
* Iterates in heap storage order, which is not sorted; structural changes invalidate the127
* iterator.128
*/129
public override operator fun iterator(): Iterator<T> = HeapIterator<T>(this, structure)131
/**132
* Serialization factories using explicit element or key/value codecs.133
*/134
public companion object {135
/**136
* Encodes entries or elements in iteration order using the supplied component codecs.137
*/138
public fun encoder(element: ValueEncoder<T>): ValueEncoder<PriorityQueue<T>> =139
ValueEncoderFunction<PriorityQueue<T>> { values, encoder -> encodeElements(values, element, encoder) }140
/**141
* Decodes entries or elements into a new collection using the supplied component codecs142
* and comparator where required.143
*/144
public fun decoder(element: ValueDecoder<T>, comparator: Comparator<T>): ValueDecoder<PriorityQueue<T>> = ValueDecoderFunction<PriorityQueue<T>> { decoder ->145
val result = PriorityQueue<T>(comparator)146
decodeElements(decoder, element) { value -> result.add(value) }147
result148
}149
}150
}