github.com/klaytn/klaytn@v1.12.1/common/prque/prque.go (about) 1 // CookieJar - A contestant's algorithm toolbox 2 // Copyright (c) 2013 Peter Szilagyi. All rights reserved. 3 // 4 // CookieJar is dual licensed: use of this source code is governed by a BSD 5 // license that can be found in the LICENSE file. Alternatively, the CookieJar 6 // toolbox may be used in accordance with the terms and conditions contained 7 // in a signed written agreement between you and the author(s). 8 9 // This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque". 10 11 // Package prque implements a priority queue data structure supporting arbitrary 12 // value types and int64 priorities. 13 // 14 // If you would like to use a min-priority queue, simply negate the priorities. 15 // 16 // Internally the queue is based on the standard heap package working on a 17 // sortable version of the block based stack. 18 19 package prque 20 21 import ( 22 "container/heap" 23 ) 24 25 // Priority queue data structure. 26 type Prque struct { 27 cont *sstack 28 } 29 30 // New creates a new priority queue. 31 func New() *Prque { 32 return &Prque{newSstack()} 33 } 34 35 // Pushes a value with a given priority into the queue, expanding if necessary. 36 func (p *Prque) Push(data interface{}, priority int64) { 37 heap.Push(p.cont, &item{data, priority}) 38 } 39 40 // Peek returns the value with the greatest priority but does not pop it off. 41 func (p *Prque) Peek() (interface{}, int64) { 42 item := p.cont.blocks[0][0] 43 return item.value, item.priority 44 } 45 46 // Pops the value with the greates priority off the stack and returns it. 47 // Currently no shrinking is done. 48 func (p *Prque) Pop() (interface{}, int64) { 49 item := heap.Pop(p.cont).(*item) 50 return item.value, item.priority 51 } 52 53 // Pops only the item from the queue, dropping the associated priority value. 54 func (p *Prque) PopItem() interface{} { 55 return heap.Pop(p.cont).(*item).value 56 } 57 58 // Checks whether the priority queue is empty. 59 func (p *Prque) Empty() bool { 60 return p.cont.Len() == 0 61 } 62 63 // Returns the number of element in the priority queue. 64 func (p *Prque) Size() int { 65 return p.cont.Len() 66 } 67 68 // Clears the contents of the priority queue. 69 func (p *Prque) Reset() { 70 *p = *New() 71 }