github.com/fholzer/ordered-concurrently/v3@v3.0.0-20221001131746-406a6eece748/heap.go (about)

     1  package orderedconcurrently
     2  
     3  type processInput struct {
     4  	input interface{}
     5  	order uint64
     6  	value interface{}
     7  }
     8  
     9  type processInputHeap []*processInput
    10  
    11  func (h processInputHeap) Len() int {
    12  	return len(h)
    13  }
    14  
    15  func (h processInputHeap) Less(i, j int) bool {
    16  	return h[i].order < h[j].order
    17  }
    18  
    19  func (h processInputHeap) Swap(i, j int) {
    20  	h[i], h[j] = h[j], h[i]
    21  }
    22  
    23  func (h *processInputHeap) Push(x interface{}) {
    24  	*h = append(*h, x.(*processInput))
    25  }
    26  
    27  func (s processInputHeap) Peek() (*processInput, bool) {
    28  	if len(s) > 0 {
    29  		return s[0], true
    30  	}
    31  	return nil, false
    32  }
    33  
    34  func (h *processInputHeap) Pop() interface{} {
    35  	old := *h
    36  	n := len(old)
    37  	x := old[n-1]
    38  	*h = old[0 : n-1]
    39  	return x
    40  }