github.com/chainopen/ethchaincode@v0.0.0-20190924072703-d975acdaa1c6/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  package prque
    19  
    20  import (
    21  	"container/heap"
    22  )
    23  
    24  // Priority queue data structure.
    25  type Prque struct {
    26  	cont *sstack
    27  }
    28  
    29  // New creates a new priority queue.
    30  func New(setIndex SetIndexCallback) *Prque {
    31  	return &Prque{newSstack(setIndex)}
    32  }
    33  
    34  // Pushes a value with a given priority into the queue, expanding if necessary.
    35  func (p *Prque) Push(data interface{}, priority int64) {
    36  	heap.Push(p.cont, &item{data, priority})
    37  }
    38  
    39  // Peek returns the value with the greates priority but does not pop it off.
    40  func (p *Prque) Peek() (interface{}, int64) {
    41  	item := p.cont.blocks[0][0]
    42  	return item.value, item.priority
    43  }
    44  
    45  // Pops the value with the greates priority off the stack and returns it.
    46  // Currently no shrinking is done.
    47  func (p *Prque) Pop() (interface{}, int64) {
    48  	item := heap.Pop(p.cont).(*item)
    49  	return item.value, item.priority
    50  }
    51  
    52  // Pops only the item from the queue, dropping the associated priority value.
    53  func (p *Prque) PopItem() interface{} {
    54  	return heap.Pop(p.cont).(*item).value
    55  }
    56  
    57  // Remove removes the element with the given index.
    58  func (p *Prque) Remove(i int) interface{} {
    59  	if i < 0 {
    60  		return nil
    61  	}
    62  	return heap.Remove(p.cont, i)
    63  }
    64  
    65  // Checks whether the priority queue is empty.
    66  func (p *Prque) Empty() bool {
    67  	return p.cont.Len() == 0
    68  }
    69  
    70  // Returns the number of element in the priority queue.
    71  func (p *Prque) Size() int {
    72  	return p.cont.Len()
    73  }
    74  
    75  // Clears the contents of the priority queue.
    76  func (p *Prque) Reset() {
    77  	*p = *New(p.cont.setIndex)
    78  }