I don't think that the implementation here performs correctly...
For example, the following:
q := NewPrioq()
q.In <- timestamp(20)
q.In <- timestamp(10)
q.In <- timestamp(30)
o := <-q.Out
fmt.Println("Read", o)
Prints "Read 20" rather than "Read 30".Furthermore, this input:
q := NewPrioq()
for i := 0; i < 202; i++ {
q.In <- timestamp(i)
}
o := <-q.Out
fmt.Println("Read", o)
will result in a deadlock.As far as I can tell, this code behaves like a FIFO queue with capacity 201. Each iteration of the loop in Prioq.run() will read once from the input channel, insert into the empty tree, remove the "max" element from the single item tree, and then insert into the output channel.
Reading from the input channel and writing to the output channel are performed in lockstep rather than as needed. A correct implementation of this pattern would need to use a select block and to avoid the deadlock and would need a way for writes to the output channel to be signaled as needed so to avoid the stale max value problem.