/prio/prio.go

https://code.google.com/p/go-priority-queue/ · Go · 169 lines · 97 code · 13 blank · 59 comment · 14 complexity · e5e8d739fdbb4a5ada31f8f381e88ed7 MD5 · raw file

  1. // Copyright 2012 Stefan Nilsson
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package prio provides a priority queue.
  15. // The queue can hold elements that implement the two methods of prio.Interface.
  16. package prio
  17. /*
  18. A type that implements prio.Interface can be inserted into a priority queue.
  19. The simplest use case looks like this:
  20. type myInt int
  21. func (x myInt) Less(y prio.Interface) bool { return x < y.(myInt) }
  22. func (x myInt) Index(i int) {}
  23. To use the Remove method you need to keep track of the index of elements
  24. in the heap, e.g. like this:
  25. type myType struct {
  26. value int
  27. index int // index in heap
  28. }
  29. func (x *myType) Less(y prio.Interface) bool { return x.value < y.(*myType).value }
  30. func (x *myType) Index(i int) { x.index = i }
  31. */
  32. type Interface interface {
  33. // Less returns whether this element should sort before element x.
  34. Less(x Interface) bool
  35. // Index is called by the priority queue when this element is moved to index i.
  36. Index(i int)
  37. }
  38. // Queue represents a priority queue.
  39. // The zero value for Queue is an empty queue ready to use.
  40. type Queue struct {
  41. h []Interface
  42. }
  43. // New returns an initialized priority queue with the given elements.
  44. // A call of the form New(x...) uses the underlying array of x to implement
  45. // the queue and hence might change the elements of x.
  46. // The complexity is O(n), where n = len(x).
  47. func New(x ...Interface) Queue {
  48. q := Queue{x}
  49. heapify(q.h)
  50. return q
  51. }
  52. // Push pushes the element x onto the queue.
  53. // The complexity is O(log(n)), where n = q.Len().
  54. func (q *Queue) Push(x Interface) {
  55. n := len(q.h)
  56. q.h = append(q.h, x)
  57. up(q.h, n) // x.Index(n) is done by up.
  58. }
  59. // Pop removes a minimum element (according to Less) from the queue and returns it.
  60. // The complexity is O(log(n)), where n = q.Len().
  61. func (q *Queue) Pop() Interface {
  62. h := q.h
  63. n := len(h) - 1
  64. x := h[0]
  65. h[0], h[n] = h[n], nil
  66. h = h[:n]
  67. if n > 0 {
  68. down(h, 0) // h[0].Index(0) is done by down.
  69. }
  70. q.h = h
  71. x.Index(-1) // for safety
  72. return x
  73. }
  74. // Peek returns, but does not remove, a minimum element (according to Less) of the queue.
  75. func (q *Queue) Peek() Interface {
  76. return q.h[0]
  77. }
  78. // Remove removes the element at index i from the queue and returns it.
  79. // The complexity is O(log(n)), where n = q.Len().
  80. func (q *Queue) Remove(i int) Interface {
  81. h := q.h
  82. n := len(h) - 1
  83. x := h[i]
  84. h[i], h[n] = h[n], nil
  85. h = h[:n]
  86. if i < n {
  87. down(h, i) // h[i].Index(i) is done by down.
  88. up(h, i)
  89. }
  90. q.h = h
  91. x.Index(-1) // for safety
  92. return x
  93. }
  94. // Len returns the number of elements in the queue.
  95. func (q *Queue) Len() int {
  96. return len(q.h)
  97. }
  98. // Fix reestablishes the heap ordering after the element at index i has changed its value.
  99. // Changing the value of the element at index i and then calling Fix is equivalent to,
  100. // but less expensive than, calling Remove(i) followed by a Push of the new value.
  101. // The complexity is O(log(n)) where n = q.Len().
  102. func (q *Queue) Fix(i int) {
  103. up(q.h, i)
  104. down(q.h, i)
  105. }
  106. // Establishes the heap invariant in O(n) time.
  107. func heapify(h []Interface) {
  108. n := len(h)
  109. for i := n - 1; i >= n/2; i-- {
  110. h[i].Index(i)
  111. }
  112. for i := n/2 - 1; i >= 0; i-- { // h[i].Index(i) is done by down.
  113. down(h, i)
  114. }
  115. }
  116. // Moves element at position i towards top of heap to restore invariant.
  117. func up(h []Interface, i int) {
  118. for {
  119. parent := (i - 1) / 2
  120. if i == 0 || h[parent].Less(h[i]) {
  121. h[i].Index(i)
  122. break
  123. }
  124. h[parent], h[i] = h[i], h[parent]
  125. h[i].Index(i)
  126. i = parent
  127. }
  128. }
  129. // Moves element at position i towards bottom of heap to restore invariant.
  130. func down(h []Interface, i int) {
  131. for {
  132. n := len(h)
  133. left := 2*i + 1
  134. if left >= n || left < 0 { // left < 0 after int overflow
  135. h[i].Index(i)
  136. break
  137. }
  138. j := left
  139. if right := left + 1; right < n && h[right].Less(h[left]) {
  140. j = right
  141. }
  142. if h[i].Less(h[j]) {
  143. h[i].Index(i)
  144. break
  145. }
  146. h[i], h[j] = h[j], h[i]
  147. h[i].Index(i)
  148. i = j
  149. }
  150. }