queue.go 4.4 KB

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