Skip to content

Commit 2aa21b2

Browse files
committed
Resolve TODO in mutable.PriorityQueue: explain why array(0) is not used.
1 parent 45396a1 commit 2aa21b2

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

library/src/scala/collection/mutable/PriorityQueue.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ sealed class PriorityQueue[A](implicit val ord: Ordering[A])
9999

100100
private val resarr = new ResizableArrayAccess[A]
101101

102-
resarr.p_size0 += 1 // we do not use array(0) TODO: explain -- what is the first element even for?
102+
// we do not use array(0)
103+
// storing the root of the heap at array(1) simplifies the calculations for
104+
// parent and child indices: for a given index k, the parent of k is k / 2,
105+
// the left child is k * 2, and the right child is k * 2 + 1
106+
resarr.p_size0 += 1
103107
def length: Int = resarr.length - 1 // adjust length accordingly
104108
override def size: Int = length
105109
override def knownSize: Int = length

0 commit comments

Comments
 (0)