pqueue

view pqueue.go @ 3:2c5c676d74a4

Split main from pqueue
author David McLeish <dave@mcleish.id.au>
date Sun Jan 23 12:51:36 2011 +1100 (2011-01-23)
parents fbf7947f8ca1
children d072151ea94a
line source
1 package main
3 import (
4 "container/heap"
5 "container/vector"
6 )
8 type pHeap struct {
9 vector.Vector
10 }
12 func (h *pHeap) Less(i, j int) bool {
13 return h.At(i).(int) < h.At(j).(int)
14 }
16 func feedPQ(in, out chan int) {
17 h := new(pHeap)
18 more := true
19 for more {
20 block := false
21 for {
22 var val int
23 var ok bool
24 if block {
25 val, ok = <-in
26 if !ok { break }
27 } else {
28 val = <-in
29 block = true
30 }
31 if closed(in) {
32 more = false
33 break
34 }
35 heap.Push(h, val)
36 }
37 for h.Len() > 0 {
38 ok := out <- h.At(0).(int)
39 if ok {
40 heap.Pop(h)
41 } else {
42 break
43 }
44 }
45 }
46 for h.Len() > 0 {
47 out <- heap.Pop(h).(int)
48 }
49 }