github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/prque/prque.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
    19  
    20  // Package prque implements a priority queue data structure supporting arbitrary
    21  // value types and int64 priorities.
    22  //
    23  // If you would like to use a min-priority queue, simply negate the priorities.
    24  //
    25  // Internally the queue is based on the standard heap package working on a
    26  // sortable version of the block based stack.
    27  package prque
    28  
    29  import (
    30  	"container/heap"
    31  )
    32  
    33  // Priority queue data structure.
    34  type Prque struct {
    35  	cont *sstack
    36  }
    37  
    38  // New creates a new priority queue.
    39  func New(setIndex SetIndexCallback) *Prque {
    40  	return &Prque{newSstack(setIndex)}
    41  }
    42  
    43  // Pushes a value with a given priority into the queue, expanding if necessary.
    44  func (p *Prque) Push(data interface{}, priority int64) {
    45  	heap.Push(p.cont, &item{data, priority})
    46  }
    47  
    48  // Peek returns the value with the greates priority but does not pop it off.
    49  func (p *Prque) Peek() (interface{}, int64) {
    50  	item := p.cont.blocks[0][0]
    51  	return item.value, item.priority
    52  }
    53  
    54  // Pops the value with the greates priority off the stack and returns it.
    55  // Currently no shrinking is done.
    56  func (p *Prque) Pop() (interface{}, int64) {
    57  	item := heap.Pop(p.cont).(*item)
    58  	return item.value, item.priority
    59  }
    60  
    61  // Pops only the item from the queue, dropping the associated priority value.
    62  func (p *Prque) PopItem() interface{} {
    63  	return heap.Pop(p.cont).(*item).value
    64  }
    65  
    66  // Remove removes the element with the given index.
    67  func (p *Prque) Remove(i int) interface{} {
    68  	if i < 0 {
    69  		return nil
    70  	}
    71  	return heap.Remove(p.cont, i)
    72  }
    73  
    74  // Checks whether the priority queue is empty.
    75  func (p *Prque) Empty() bool {
    76  	return p.cont.Len() == 0
    77  }
    78  
    79  // Returns the number of element in the priority queue.
    80  func (p *Prque) Size() int {
    81  	return p.cont.Len()
    82  }
    83  
    84  // Clears the contents of the priority queue.
    85  func (p *Prque) Reset() {
    86  	*p = *New(p.cont.setIndex)
    87  }