1
0
mirror of https://github.com/golang/go synced 2024-11-25 03:07:56 -07:00

test/chan/sieve2.go: remove container/vector.

R=golang-dev, dsymonds, r
CC=golang-dev
https://golang.org/cl/4918043
This commit is contained in:
Rob Pike 2011-08-22 13:29:17 +10:00
parent ab44a814c2
commit 97eb06233f

View File

@ -13,7 +13,6 @@ package main
import ( import (
"container/heap" "container/heap"
"container/ring" "container/ring"
"container/vector"
) )
// Return a chan of odd numbers, starting from 5. // Return a chan of odd numbers, starting from 5.
@ -47,13 +46,28 @@ type PeekCh struct {
ch chan int ch chan int
} }
// Heap of PeekCh, sorting by head values. // Heap of PeekCh, sorting by head values, satisfies Heap interface.
type PeekChHeap struct { type PeekChHeap []*PeekCh
*vector.Vector
}
func (h *PeekChHeap) Less(i, j int) bool { func (h *PeekChHeap) Less(i, j int) bool {
return h.At(i).(*PeekCh).head < h.At(j).(*PeekCh).head return (*h)[i].head < (*h)[j].head
}
func (h *PeekChHeap) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}
func (h *PeekChHeap) Len() int {
return len(*h)
}
func (h *PeekChHeap) Pop() (v interface{}) {
*h, v = (*h)[:h.Len()-1], (*h)[h.Len()-1]
return
}
func (h *PeekChHeap) Push(v interface{}) {
*h = append(*h, v.(*PeekCh))
} }
// Return a channel to serve as a sending proxy to 'out'. // Return a channel to serve as a sending proxy to 'out'.
@ -108,26 +122,26 @@ func Sieve() chan int {
// Merge channels of multiples of 'primes' into 'composites'. // Merge channels of multiples of 'primes' into 'composites'.
go func() { go func() {
h := &PeekChHeap{new(vector.Vector)} var h PeekChHeap
min := 15 min := 15
for { for {
m := multiples(<-primes) m := multiples(<-primes)
head := <-m head := <-m
for min < head { for min < head {
composites <- min composites <- min
minchan := heap.Pop(h).(*PeekCh) minchan := heap.Pop(&h).(*PeekCh)
min = minchan.head min = minchan.head
minchan.head = <-minchan.ch minchan.head = <-minchan.ch
heap.Push(h, minchan) heap.Push(&h, minchan)
} }
for min == head { for min == head {
minchan := heap.Pop(h).(*PeekCh) minchan := heap.Pop(&h).(*PeekCh)
min = minchan.head min = minchan.head
minchan.head = <-minchan.ch minchan.head = <-minchan.ch
heap.Push(h, minchan) heap.Push(&h, minchan)
} }
composites <- head composites <- head
heap.Push(h, &PeekCh{<-m, m}) heap.Push(&h, &PeekCh{<-m, m})
} }
}() }()