Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heap.toIterator #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions core/src/main/scala/cats/collections/Heap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ sealed abstract class Heap[A] {
def heapify(a: List[A])(implicit order: Order[A]): Heap[A] =
Heap.heapify(a)

def toIterator(implicit order: Order[A]): Iterator[A] = {
@scala.annotation.tailrec
def build(heap: Heap[A], acc: Stream[A]): Stream[A] = heap match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is using a Stream better than just building the List below? Since it is tailrec you are building the whole Stream no? If we do that, isn’t a List cheaper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in list you have to reverse it at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this particular case the following does not apply, but in the other implementations, the stream based removed the need for mutations, and adds stateless build of the iterator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are making a performance argument, can we show a win with benchmarks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man, you are killing me... I haven't written one of those in a while. Let me see

case Leaf() => acc
case b@Branch(_, _, _) => b.pop match {
case Some((m, h)) => build(h, acc.append(Seq(m)))
case None => acc
}
}

build(this, Stream.empty).toIterator
}

/**
* Remove the min element from the heap (the root) and return it along with the updated heap.
* Order O(log n)
Expand Down Expand Up @@ -171,16 +184,7 @@ sealed abstract class Heap[A] {
/**
* Returns a sorted list of the elements within the heap.
*/
def toList(implicit order: Order[A]): List[A] = {
@tailrec
def loop(h: Heap[A], acc: List[A]): List[A] =
h match {
case Branch(m, _, _) => loop(h.remove, m :: acc)
case Leaf() => acc.reverse
}

loop(this, Nil)
}
def toList(implicit order: Order[A]): List[A] = toIterator.toList

/**
* do a foldLeft in the same order as toList.
Expand Down
31 changes: 31 additions & 0 deletions tests/src/test/scala/cats/collections/HeapSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,35 @@ class HeapSpec extends CatsSuite {
law(h, h)
}
}

test("toIterator maintains order")(forAll { heap: Heap[Int] =>
val numbers = heap.toIterator.toList

numbers should contain inOrderElementsOf (numbers.sorted)
})

test("toIterator maintains the inverse order")(forAll { xs: List[Int] =>
whenever(xs.size > 1) {
val order = Order.from[Int]((a, b) => a.compareTo(b) * -1)
val inverseOrder = Order.from[Int]((a, b) => a.compareTo(b))

val heap = Heap.fromIterable(xs)(order)

val it = heap.toIterator(order)
var min = it.next()

while (it.hasNext) {
val current = it.next()

order.gteqv(current, min) should be (true)
inverseOrder.lteqv(current, min) should be (true)

min = current
}
}
})

test("toIterator should match with toList")(forAll {heap: Heap[Int] =>
heap.toIterator.toList === heap.toList
})
}