Codebase list golang-github-vbauerster-mpb / 2edd069
bHeap as value Vladimir Bauer 7 years ago
2 changed file(s) with 16 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
1717 }
1818
1919 func (pq *priorityQueue) Push(x interface{}) {
20 n := len(*pq)
20 s := *pq
2121 bar := x.(*Bar)
22 bar.index = n
23 *pq = append(*pq, bar)
22 bar.index = len(s)
23 s = append(s, bar)
24 *pq = s
2425 }
2526
2627 func (pq *priorityQueue) Pop() interface{} {
27 old := *pq
28 n := len(old)
29 bar := old[n-1]
28 s := *pq
29 *pq = s[0 : len(s)-1]
30 bar := s[len(s)-1]
3031 bar.index = -1 // for safety
31 *pq = old[0 : n-1]
3232 return bar
3333 }
3434
3333 }
3434
3535 type pState struct {
36 bHeap *priorityQueue
36 bHeap priorityQueue
3737 heapUpdated bool
3838 pMatrix map[int][]chan int
3939 aMatrix map[int][]chan int
6161 // context. It's not possible to reuse instance after *Progress.Wait()
6262 // method has been called.
6363 func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress {
64 pq := make(priorityQueue, 0)
65 heap.Init(&pq)
6664
6765 s := &pState{
68 bHeap: &pq,
66 bHeap: priorityQueue{},
6967 width: pwidth,
7068 rr: prr,
7169 parkedBars: make(map[*Bar]*Bar),
138136 }
139137 ps.parkedBars[bs.runningBar] = bar
140138 } else {
141 heap.Push(ps.bHeap, bar)
139 heap.Push(&ps.bHeap, bar)
142140 ps.heapUpdated = true
143141 }
144142 ps.idCount++
157155 if b.index < 0 {
158156 return
159157 }
160 s.heapUpdated = heap.Remove(s.bHeap, b.index) != nil
158 s.heapUpdated = heap.Remove(&s.bHeap, b.index) != nil
161159 }:
162160 case <-p.done:
163161 }
247245 tw = s.width
248246 }
249247 for i := 0; i < s.bHeap.Len(); i++ {
250 bar := (*s.bHeap)[i]
248 bar := s.bHeap[i]
251249 go bar.render(tw)
252250 }
253251
257255 func (s *pState) flush(cw *cwriter.Writer) error {
258256 var lineCount int
259257 for s.bHeap.Len() > 0 {
260 bar := heap.Pop(s.bHeap).(*Bar)
258 bar := heap.Pop(&s.bHeap).(*Bar)
261259 defer func() {
262260 if bar.toShutdown {
263261 // shutdown at next flush, in other words decrement underlying WaitGroup
265263 // ensures no bar ends up with less than 100% rendered.
266264 s.barShutdownQueue = append(s.barShutdownQueue, bar.cancel)
267265 if parkedBar := s.parkedBars[bar]; parkedBar != nil {
268 heap.Push(s.bHeap, parkedBar)
266 heap.Push(&s.bHeap, parkedBar)
269267 s.heapUpdated = true
270268 delete(s.parkedBars, bar)
271269 }
274272 return
275273 }
276274 }
277 heap.Push(s.bHeap, bar)
275 heap.Push(&s.bHeap, bar)
278276 }()
279277 cw.ReadFrom(<-bar.frameCh)
280278 lineCount += bar.extendedLines + 1
300298 s.pMatrix = make(map[int][]chan int)
301299 s.aMatrix = make(map[int][]chan int)
302300 for i := 0; i < s.bHeap.Len(); i++ {
303 bar := (*s.bHeap)[i]
301 bar := s.bHeap[i]
304302 table := bar.wSyncTable()
305303 pRow, aRow := table[0], table[1]
306304