github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/common/prque/prque.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //这是“gopkg.in/karalabe/cookiejar.v2/collections/prque”的一个复制和稍加修改的版本。
    10  
    11  package prque
    12  
    13  import (
    14  	"container/heap"
    15  )
    16  
    17  //优先级队列数据结构。
    18  type Prque struct {
    19  	cont *sstack
    20  }
    21  
    22  //创建新的优先级队列。
    23  func New(setIndex setIndexCallback) *Prque {
    24  	return &Prque{newSstack(setIndex)}
    25  }
    26  
    27  //将具有给定优先级的值推入队列,必要时展开。
    28  func (p *Prque) Push(data interface{}, priority int64) {
    29  	heap.Push(p.cont, &item{data, priority})
    30  }
    31  
    32  //从堆栈中弹出优先级为greates的值并返回该值。
    33  //目前还没有收缩。
    34  func (p *Prque) Pop() (interface{}, int64) {
    35  	item := heap.Pop(p.cont).(*item)
    36  	return item.value, item.priority
    37  }
    38  
    39  //只从队列中弹出项目,删除关联的优先级值。
    40  func (p *Prque) PopItem() interface{} {
    41  	return heap.Pop(p.cont).(*item).value
    42  }
    43  
    44  //移除移除具有给定索引的元素。
    45  func (p *Prque) Remove(i int) interface{} {
    46  	if i < 0 {
    47  		return nil
    48  	}
    49  	return heap.Remove(p.cont, i)
    50  }
    51  
    52  //检查优先级队列是否为空。
    53  func (p *Prque) Empty() bool {
    54  	return p.cont.Len() == 0
    55  }
    56  
    57  //返回优先级队列中的元素数。
    58  func (p *Prque) Size() int {
    59  	return p.cont.Len()
    60  }
    61  
    62  //清除优先级队列的内容。
    63  func (p *Prque) Reset() {
    64  	*p = *New(p.cont.setIndex)
    65  }