github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/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  	"cmp"
    22  	"container/heap"
    23  )
    24  
    25  // Prque is a priority queue data structure.
    26  type Prque[P cmp.Ordered, V any] struct {
    27  	cont *sstack[P, V]
    28  }
    29  
    30  // New creates a new priority queue.
    31  func New[P cmp.Ordered, V any](setIndex SetIndexCallback[V]) *Prque[P, V] {
    32  	return &Prque[P, V]{newSstack[P, V](setIndex)}
    33  }
    34  
    35  // Push a value with a given priority into the queue, expanding if necessary.
    36  func (p *Prque[P, V]) Push(data V, priority P) {
    37  	heap.Push(p.cont, &item[P, V]{data, priority})
    38  }
    39  
    40  // Peek returns the value with the greatest priority but does not pop it off.
    41  func (p *Prque[P, V]) Peek() (V, P) {
    42  	item := p.cont.blocks[0][0]
    43  	return item.value, item.priority
    44  }
    45  
    46  // Pop the value with the greatest priority off the stack and returns it.
    47  // Currently no shrinking is done.
    48  func (p *Prque[P, V]) Pop() (V, P) {
    49  	item := heap.Pop(p.cont).(*item[P, V])
    50  	return item.value, item.priority
    51  }
    52  
    53  // PopItem pops only the item from the queue, dropping the associated priority value.
    54  func (p *Prque[P, V]) PopItem() V {
    55  	return heap.Pop(p.cont).(*item[P, V]).value
    56  }
    57  
    58  // Remove removes the element with the given index.
    59  func (p *Prque[P, V]) Remove(i int) V {
    60  	return heap.Remove(p.cont, i).(*item[P, V]).value
    61  }
    62  
    63  // Empty checks whether the priority queue is empty.
    64  func (p *Prque[P, V]) Empty() bool {
    65  	return p.cont.Len() == 0
    66  }
    67  
    68  // Size returns the number of element in the priority queue.
    69  func (p *Prque[P, V]) Size() int {
    70  	return p.cont.Len()
    71  }
    72  
    73  // Reset clears the contents of the priority queue.
    74  func (p *Prque[P, V]) Reset() {
    75  	*p = *New[P, V](p.cont.setIndex)
    76  }