aboutsummaryrefslogtreecommitdiff
path: root/test/chan
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2011-08-22 13:29:17 +1000
committerRob Pike <r@golang.org>2011-08-22 13:29:17 +1000
commit97eb06233fef967a39688fff3ea907c88c329160 (patch)
tree327dab5660d72b66e840a4e3e358222b35e025dc /test/chan
parentab44a814c2bcc54975d145ee437a9adf54475d18 (diff)
downloadgo-97eb06233fef967a39688fff3ea907c88c329160.tar.gz
go-97eb06233fef967a39688fff3ea907c88c329160.zip
test/chan/sieve2.go: remove container/vector.
R=golang-dev, dsymonds, r CC=golang-dev https://golang.org/cl/4918043
Diffstat (limited to 'test/chan')
-rw-r--r--test/chan/sieve2.go38
1 files changed, 26 insertions, 12 deletions
diff --git a/test/chan/sieve2.go b/test/chan/sieve2.go
index 7f2ed91579..9a7ab15406 100644
--- a/test/chan/sieve2.go
+++ b/test/chan/sieve2.go
@@ -13,7 +13,6 @@ package main
import (
"container/heap"
"container/ring"
- "container/vector"
)
// Return a chan of odd numbers, starting from 5.
@@ -47,13 +46,28 @@ type PeekCh struct {
ch chan int
}
-// Heap of PeekCh, sorting by head values.
-type PeekChHeap struct {
- *vector.Vector
-}
+// Heap of PeekCh, sorting by head values, satisfies Heap interface.
+type PeekChHeap []*PeekCh
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'.
@@ -108,26 +122,26 @@ func Sieve() chan int {
// Merge channels of multiples of 'primes' into 'composites'.
go func() {
- h := &PeekChHeap{new(vector.Vector)}
+ var h PeekChHeap
min := 15
for {
m := multiples(<-primes)
head := <-m
for min < head {
composites <- min
- minchan := heap.Pop(h).(*PeekCh)
+ minchan := heap.Pop(&h).(*PeekCh)
min = minchan.head
minchan.head = <-minchan.ch
- heap.Push(h, minchan)
+ heap.Push(&h, minchan)
}
for min == head {
- minchan := heap.Pop(h).(*PeekCh)
+ minchan := heap.Pop(&h).(*PeekCh)
min = minchan.head
minchan.head = <-minchan.ch
- heap.Push(h, minchan)
+ heap.Push(&h, minchan)
}
composites <- head
- heap.Push(h, &PeekCh{<-m, m})
+ heap.Push(&h, &PeekCh{<-m, m})
}
}()