prque.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
  9. // Package prque implements a priority queue data structure supporting arbitrary
  10. // value types and int64 priorities.
  11. //
  12. // If you would like to use a min-priority queue, simply negate the priorities.
  13. //
  14. // Internally the queue is based on the standard heap package working on a
  15. // sortable version of the block based stack.
  16. package prque
  17. import (
  18. "container/heap"
  19. )
  20. // Priority queue data structure.
  21. type Prque struct {
  22. cont *sstack
  23. }
  24. // New creates a new priority queue.
  25. func New(setIndex SetIndexCallback) *Prque {
  26. return &Prque{newSstack(setIndex, false)}
  27. }
  28. // NewWrapAround creates a new priority queue with wrap-around priority handling.
  29. func NewWrapAround(setIndex SetIndexCallback) *Prque {
  30. return &Prque{newSstack(setIndex, true)}
  31. }
  32. // Pushes a value with a given priority into the queue, expanding if necessary.
  33. func (p *Prque) Push(data interface{}, priority int64) {
  34. heap.Push(p.cont, &item{data, priority})
  35. }
  36. // Peek returns the value with the greates priority but does not pop it off.
  37. func (p *Prque) Peek() (interface{}, int64) {
  38. item := p.cont.blocks[0][0]
  39. return item.value, item.priority
  40. }
  41. // Pops the value with the greates priority off the stack and returns it.
  42. // Currently no shrinking is done.
  43. func (p *Prque) Pop() (interface{}, int64) {
  44. item := heap.Pop(p.cont).(*item)
  45. return item.value, item.priority
  46. }
  47. // Pops only the item from the queue, dropping the associated priority value.
  48. func (p *Prque) PopItem() interface{} {
  49. return heap.Pop(p.cont).(*item).value
  50. }
  51. // Remove removes the element with the given index.
  52. func (p *Prque) Remove(i int) interface{} {
  53. if i < 0 {
  54. return nil
  55. }
  56. return heap.Remove(p.cont, i)
  57. }
  58. // Checks whether the priority queue is empty.
  59. func (p *Prque) Empty() bool {
  60. return p.cont.Len() == 0
  61. }
  62. // Returns the number of element in the priority queue.
  63. func (p *Prque) Size() int {
  64. return p.cont.Len()
  65. }
  66. // Clears the contents of the priority queue.
  67. func (p *Prque) Reset() {
  68. *p = *New(p.cont.setIndex)
  69. }