github.com/jflude/taocp@v0.0.0-20240210234939-99f2a91af3c2/mix/priority.go (about)

     1  package mix
     2  
     3  type event struct {
     4  	when int64
     5  	loc  int
     6  }
     7  
     8  type priority []event
     9  
    10  func (p priority) Len() int {
    11  	return len(p)
    12  }
    13  
    14  func (p priority) Less(i, j int) bool {
    15  	if p[i].when < p[j].when {
    16  		return true
    17  	}
    18  	if p[i].when > p[j].when {
    19  		return false
    20  	}
    21  	return p[i].loc > p[j].loc
    22  }
    23  
    24  func (p priority) Swap(i, j int) {
    25  	p[i], p[j] = p[j], p[i]
    26  }
    27  
    28  func (p *priority) Push(x interface{}) {
    29  	*p = append(*p, x.(event))
    30  }
    31  
    32  func (p *priority) Pop() interface{} {
    33  	old := *p
    34  	n := len(old)
    35  	x := old[n-1]
    36  	*p = old[:n-1]
    37  	return x
    38  }