2019-05-27 15:48:30 +03:00
|
|
|
// CookieJar - A contestant's algorithm toolbox
|
|
|
|
// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
|
|
|
|
//
|
|
|
|
// CookieJar is dual licensed: use of this source code is governed by a BSD
|
|
|
|
// license that can be found in the LICENSE file. Alternatively, the CookieJar
|
|
|
|
// toolbox may be used in accordance with the terms and conditions contained
|
|
|
|
// in a signed written agreement between you and the author(s).
|
|
|
|
|
2018-08-14 22:44:46 +02:00
|
|
|
// This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
|
|
|
|
|
2019-05-27 15:48:30 +03:00
|
|
|
// Package prque implements a priority queue data structure supporting arbitrary
|
|
|
|
// value types and int64 priorities.
|
|
|
|
//
|
|
|
|
// If you would like to use a min-priority queue, simply negate the priorities.
|
|
|
|
//
|
|
|
|
// Internally the queue is based on the standard heap package working on a
|
|
|
|
// sortable version of the block based stack.
|
2018-08-14 22:44:46 +02:00
|
|
|
package prque
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2023-02-09 13:03:54 +02:00
|
|
|
|
|
|
|
"golang.org/x/exp/constraints"
|
2018-08-14 22:44:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Priority queue data structure.
|
2023-02-09 13:03:54 +02:00
|
|
|
type Prque[P constraints.Ordered, V any] struct {
|
|
|
|
cont *sstack[P, V]
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 15:48:30 +03:00
|
|
|
// New creates a new priority queue.
|
2023-02-09 13:03:54 +02:00
|
|
|
func New[P constraints.Ordered, V any](setIndex SetIndexCallback[V]) *Prque[P, V] {
|
|
|
|
return &Prque[P, V]{newSstack[P, V](setIndex)}
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pushes a value with a given priority into the queue, expanding if necessary.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) Push(data V, priority P) {
|
|
|
|
heap.Push(p.cont, &item[P, V]{data, priority})
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 01:00:21 -05:00
|
|
|
// Peek returns the value with the greatest priority but does not pop it off.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) Peek() (V, P) {
|
2019-05-27 15:48:30 +03:00
|
|
|
item := p.cont.blocks[0][0]
|
|
|
|
return item.value, item.priority
|
|
|
|
}
|
|
|
|
|
2022-08-19 01:00:21 -05:00
|
|
|
// Pops the value with the greatest priority off the stack and returns it.
|
2018-08-14 22:44:46 +02:00
|
|
|
// Currently no shrinking is done.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) Pop() (V, P) {
|
|
|
|
item := heap.Pop(p.cont).(*item[P, V])
|
2018-08-14 22:44:46 +02:00
|
|
|
return item.value, item.priority
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pops only the item from the queue, dropping the associated priority value.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) PopItem() V {
|
|
|
|
return heap.Pop(p.cont).(*item[P, V]).value
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the element with the given index.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) Remove(i int) V {
|
|
|
|
return heap.Remove(p.cont, i).(*item[P, V]).value
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether the priority queue is empty.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) Empty() bool {
|
2018-08-14 22:44:46 +02:00
|
|
|
return p.cont.Len() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of element in the priority queue.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) Size() int {
|
2018-08-14 22:44:46 +02:00
|
|
|
return p.cont.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clears the contents of the priority queue.
|
2023-02-09 13:03:54 +02:00
|
|
|
func (p *Prque[P, V]) Reset() {
|
|
|
|
*p = *New[P, V](p.cont.setIndex)
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|