github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/common/prque/prque.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:34</date> 10 //</624450074163810304> 11 12 //这是“gopkg.in/karalabe/cookiejar.v2/collections/prque”的一个复制和稍加修改的版本。 13 14 package prque 15 16 import ( 17 "container/heap" 18 ) 19 20 //优先级队列数据结构。 21 type Prque struct { 22 cont *sstack 23 } 24 25 //创建新的优先级队列。 26 func New(setIndex setIndexCallback) *Prque { 27 return &Prque{newSstack(setIndex)} 28 } 29 30 //将具有给定优先级的值推入队列,必要时展开。 31 func (p *Prque) Push(data interface{}, priority int64) { 32 heap.Push(p.cont, &item{data, priority}) 33 } 34 35 //从堆栈中弹出优先级为greates的值并返回该值。 36 //目前还没有收缩。 37 func (p *Prque) Pop() (interface{}, int64) { 38 item := heap.Pop(p.cont).(*item) 39 return item.value, item.priority 40 } 41 42 //只从队列中弹出项目,删除关联的优先级值。 43 func (p *Prque) PopItem() interface{} { 44 return heap.Pop(p.cont).(*item).value 45 } 46 47 //移除移除具有给定索引的元素。 48 func (p *Prque) Remove(i int) interface{} { 49 if i < 0 { 50 return nil 51 } 52 return heap.Remove(p.cont, i) 53 } 54 55 //检查优先级队列是否为空。 56 func (p *Prque) Empty() bool { 57 return p.cont.Len() == 0 58 } 59 60 //返回优先级队列中的元素数。 61 func (p *Prque) Size() int { 62 return p.cont.Len() 63 } 64 65 //清除优先级队列的内容。 66 func (p *Prque) Reset() { 67 *p = *New(p.cont.setIndex) 68 } 69