github.com/etclabscore/ethereum.go-ethereum@v1.8.15/common/prque/prque.go (about)

     1  // This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
     2  
     3  package prque
     4  
     5  import (
     6  	"container/heap"
     7  )
     8  
     9  // Priority queue data structure.
    10  type Prque struct {
    11  	cont *sstack
    12  }
    13  
    14  // Creates a new priority queue.
    15  func New(setIndex setIndexCallback) *Prque {
    16  	return &Prque{newSstack(setIndex)}
    17  }
    18  
    19  // Pushes a value with a given priority into the queue, expanding if necessary.
    20  func (p *Prque) Push(data interface{}, priority int64) {
    21  	heap.Push(p.cont, &item{data, priority})
    22  }
    23  
    24  // Pops the value with the greates priority off the stack and returns it.
    25  // Currently no shrinking is done.
    26  func (p *Prque) Pop() (interface{}, int64) {
    27  	item := heap.Pop(p.cont).(*item)
    28  	return item.value, item.priority
    29  }
    30  
    31  // Pops only the item from the queue, dropping the associated priority value.
    32  func (p *Prque) PopItem() interface{} {
    33  	return heap.Pop(p.cont).(*item).value
    34  }
    35  
    36  // Remove removes the element with the given index.
    37  func (p *Prque) Remove(i int) interface{} {
    38  	if i < 0 {
    39  		return nil
    40  	}
    41  	return heap.Remove(p.cont, i)
    42  }
    43  
    44  // Checks whether the priority queue is empty.
    45  func (p *Prque) Empty() bool {
    46  	return p.cont.Len() == 0
    47  }
    48  
    49  // Returns the number of element in the priority queue.
    50  func (p *Prque) Size() int {
    51  	return p.cont.Len()
    52  }
    53  
    54  // Clears the contents of the priority queue.
    55  func (p *Prque) Reset() {
    56  	*p = *New(p.cont.setIndex)
    57  }