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