queue.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (c) 2013, Peter H. Froehlich. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license
  3. // that can be found in the LICENSE file.
  4. // Package queue implements a double-ended queue abstraction on
  5. // top of a slice/array. All operations are constant time except
  6. // for PushFront and PushBack which are amortized constant time.
  7. package queue
  8. import "fmt"
  9. // Queue represents a double-ended queue.
  10. // The zero value for Queue is an empty queue ready to use.
  11. type Queue struct {
  12. // PushBack writes to rep[back] and then increments
  13. // back; PushFront decrements front and then writes
  14. // to rep[front]; gotta love those invariants.
  15. rep []interface{}
  16. front int
  17. back int
  18. length int
  19. }
  20. // New returns an initialized empty queue.
  21. func New() *Queue {
  22. return new(Queue).Init()
  23. }
  24. // Init initializes or clears queue q.
  25. func (q *Queue) Init() *Queue {
  26. // start with a slice of length 2 even if that "wastes"
  27. // some memory; we do front/back arithmetic modulo the
  28. // length, so starting at 1 requires special cases
  29. q.rep = make([]interface{}, 2)
  30. // for some time I considered reusing the existing slice
  31. // if all a client does is re-initialize the queue; the
  32. // big problem with that is that the previous queue might
  33. // have been huge while the current queue doesn't grow
  34. // much at all; if that were to happen we'd hold on to a
  35. // huge chunk of memory for just a few elements and nobody
  36. // could do anything about it; so instead I decided to
  37. // just allocate a new slice and let the GC take care of
  38. // the previous one; seems a better tradeoff all around
  39. q.front, q.back, q.length = 0, 0, 0
  40. return q
  41. }
  42. // lazyInit lazily initializes a zero Queue value.
  43. //
  44. // I am mostly doing this because container/list does the same thing.
  45. // Personally I think it's a little wasteful because every single
  46. // PushFront/PushBack is going to pay the overhead of calling this.
  47. // But that's the price for making zero values useful immediately,
  48. // something Go apparently likes a lot.
  49. func (q *Queue) lazyInit() {
  50. if q.rep == nil {
  51. q.Init()
  52. }
  53. }
  54. // Len returns the number of elements of queue q.
  55. func (q *Queue) Len() int {
  56. return q.length
  57. }
  58. // empty returns true if the queue q has no elements.
  59. func (q *Queue) empty() bool {
  60. return q.length == 0
  61. }
  62. // full returns true if the queue q is at capacity.
  63. func (q *Queue) full() bool {
  64. return q.length == len(q.rep)
  65. }
  66. // grow doubles the size of queue q's underlying slice/array.
  67. func (q *Queue) grow() {
  68. big := make([]interface{}, q.length*2)
  69. j := q.front
  70. for i := 0; i < q.length; i++ {
  71. big[i] = q.rep[j]
  72. q.inc(&j)
  73. }
  74. q.rep = big
  75. q.front = 0
  76. q.back = q.length
  77. }
  78. // TODO: leave this in or not?
  79. func (q *Queue) String() string {
  80. // result := fmt.Sprintf("(f: %d b: %d l:%d c:%d)", q.front, q.back, q.length, len(q.rep))
  81. result := ""
  82. result = result + "["
  83. j := q.front
  84. for i := 0; i < q.length; i++ {
  85. if i == q.length-1 {
  86. result = result + fmt.Sprintf("%v", q.rep[j])
  87. } else {
  88. result = result + fmt.Sprintf("%v, ", q.rep[j])
  89. }
  90. q.inc(&j)
  91. }
  92. result = result + "]"
  93. return result
  94. }
  95. // TODO: convert these two back to proper functions? see ugliness in Back() below
  96. func (q *Queue) inc(i *int) {
  97. l := len(q.rep)
  98. *i = (*i + 1 + l) % l
  99. }
  100. func (q *Queue) dec(i *int) {
  101. l := len(q.rep)
  102. *i = (*i - 1 + l) % l
  103. }
  104. // Front returns the first element of queue q or nil.
  105. func (q *Queue) Front() interface{} {
  106. if q.empty() {
  107. return nil
  108. }
  109. return q.rep[q.front]
  110. }
  111. // Back returns the last element of queue q or nil.
  112. func (q *Queue) Back() interface{} {
  113. if q.empty() {
  114. return nil
  115. }
  116. b := q.back
  117. q.dec(&b)
  118. return q.rep[b]
  119. }
  120. // PushFront inserts a new value v at the front of queue q.
  121. func (q *Queue) PushFront(v interface{}) {
  122. q.lazyInit() // TODO: keep?
  123. if q.full() {
  124. q.grow()
  125. }
  126. q.dec(&q.front)
  127. q.rep[q.front] = v
  128. q.length++
  129. }
  130. // PushBack inserts a new value v at the back of queue q.
  131. func (q *Queue) PushBack(v interface{}) {
  132. q.lazyInit() // TODO: keep?
  133. if q.full() {
  134. q.grow()
  135. }
  136. q.rep[q.back] = v
  137. q.inc(&q.back)
  138. q.length++
  139. }
  140. // PopFront removes and returns the first element of queue q or nil.
  141. func (q *Queue) PopFront() interface{} {
  142. if q.empty() {
  143. return nil
  144. }
  145. v := q.rep[q.front]
  146. q.rep[q.front] = nil // nice to GC?
  147. q.inc(&q.front)
  148. q.length--
  149. return v
  150. }
  151. // PopBack removes and returns the last element of queue q or nil.
  152. func (q *Queue) PopBack() interface{} {
  153. if q.empty() {
  154. return nil
  155. }
  156. q.dec(&q.back)
  157. v := q.rep[q.back]
  158. q.rep[q.back] = nil // nice to GC?
  159. q.length--
  160. return v
  161. }