clarifying naming

This commit is contained in:
Stephen McQuay 2018-05-05 09:32:00 -07:00
parent c41e28fdd5
commit 5ec66c2c1e
Signed by: sm
GPG Key ID: C383C74875475AC8

32
main.go
View File

@ -56,7 +56,7 @@ func merge(cs ...<-chan int) <-chan int {
out := make(chan int) out := make(chan int)
go func() { go func() {
h := &IntHeap{} h := &items{}
heap.Init(h) heap.Init(h)
// prime the pumps // prime the pumps
@ -89,28 +89,30 @@ func merge(cs ...<-chan int) <-chan int {
return out return out
} }
// An IntHeap is a min-heap of ints. // item keeps track of an integer value and the index of its source.
type IntHeap []item //
// the index is used to chose the next source to use when a value has been
// pulled.
type item struct {
val int
src int
}
func (h IntHeap) Len() int { return len(h) } // items is a min-heap of item.
func (h IntHeap) Less(i, j int) bool { return h[i].val < h[j].val } type items []item
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { func (h items) Len() int { return len(h) }
// Push and Pop use pointer receivers because they modify the slice's length, func (h items) Less(i, j int) bool { return h[i].val < h[j].val }
// not just its contents. func (h items) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *items) Push(x interface{}) {
*h = append(*h, x.(item)) *h = append(*h, x.(item))
} }
func (h *IntHeap) Pop() interface{} { func (h *items) Pop() interface{} {
old := *h old := *h
n := len(old) n := len(old)
x := old[n-1] x := old[n-1]
*h = old[0 : n-1] *h = old[0 : n-1]
return x return x
} }
type item struct {
val int
src int
}