Skip to content

Commit

Permalink
fix(heaps): rename PopAll to Drain
Browse files Browse the repository at this point in the history
  • Loading branch information
houz42 committed Dec 25, 2023
1 parent bd4fe07 commit ad5d832
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 27 deletions.
13 changes: 1 addition & 12 deletions heaps/example_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func ExampleHeap_PopAll() {
h := heaps.New(9, 5, 2, 7)
for i, v := range h.PopAll() {
for i, v := range h.Drain() {
fmt.Println(i, v)
}

Expand All @@ -20,14 +20,3 @@ func ExampleHeap_PopAll() {
// 2 7
// 3 9
}

func ExampleHeap_Pop() {
h := heaps.New("hello", "world")
for _, v := range h.All() {
fmt.Println(v)
}

// Unordered Output:
// hello
// world
}
19 changes: 4 additions & 15 deletions heaps/heaps_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ package heaps

import "iter"

// PopAll returns an iterator that pops elements in the heap by the heap order.
func (h *Heap[E]) PopAll() iter.Seq2[int, E] {
// Drain returns an iterator that pops elements from the heap in the order of the heap.
// It is intentionally named "Drain" to distinguish it from other types' "All" methods,
// as it pops out elements with each call to yield.
func (h *Heap[E]) Drain() iter.Seq2[int, E] {
return func(yield func(int, E) bool) {
i := 0
for h.Len() > 0 {
Expand All @@ -16,16 +18,3 @@ func (h *Heap[E]) PopAll() iter.Seq2[int, E] {
}
}
}

// All returns an iterator to access the elements in the heap.
// The elements will not be pop out, and the order is not following the heap order.
func (h *Heap[E]) All() iter.Seq2[int, E] {
return func(yield func(int, E) bool) {
for i, v := range h.impl.values {
if !yield(i, v) {
return
}
i++
}
}
}

0 comments on commit ad5d832

Please sign in to comment.