@@ -31,28 +31,28 @@ import scala.collection.mutable.PriorityQueue
3131 * return the average of the two top values of heaps. Otherwise we return the top of the
3232 * heap which has one more element.
3333 */
34-
3534private [spark] class MedianHeap (implicit val ord : Ordering [Double ]) {
3635
37- // Stores all the numbers less than the current median in a smallerHalf,
38- // i.e median is the maximum, at the root
36+ /**
37+ * Stores all the numbers less than the current median in a smallerHalf,
38+ * i.e median is the maximum, at the root.
39+ */
3940 private [this ] var smallerHalf = PriorityQueue .empty[Double ](ord)
4041
41- // Stores all the numbers greater than the current median in a largerHalf,
42- // i.e median is the minimum, at the root
42+ /**
43+ * Stores all the numbers greater than the current median in a largerHalf,
44+ * i.e median is the minimum, at the root.
45+ */
4346 private [this ] var largerHalf = PriorityQueue .empty[Double ](ord.reverse)
4447
45- // Returns if there is no element in MedianHeap.
4648 def isEmpty (): Boolean = {
4749 smallerHalf.isEmpty && largerHalf.isEmpty
4850 }
4951
50- // Size of MedianHeap.
5152 def size (): Int = {
5253 smallerHalf.size + largerHalf.size
5354 }
5455
55- // Insert a new number into MedianHeap.
5656 def insert (x : Double ): Unit = {
5757 // If both heaps are empty, we arbitrarily insert it into a heap, let's say, the largerHalf.
5858 if (isEmpty) {
@@ -69,7 +69,6 @@ private[spark] class MedianHeap(implicit val ord: Ordering[Double]) {
6969 rebalance()
7070 }
7171
72- // Re-balance the heaps.
7372 private [this ] def rebalance (): Unit = {
7473 if (largerHalf.size - smallerHalf.size > 1 ) {
7574 smallerHalf.enqueue(largerHalf.dequeue())
@@ -79,7 +78,6 @@ private[spark] class MedianHeap(implicit val ord: Ordering[Double]) {
7978 }
8079 }
8180
82- // Returns the median of the numbers.
8381 def median : Double = {
8482 if (isEmpty) {
8583 throw new NoSuchElementException (" MedianHeap is empty." )
0 commit comments