queue.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package dag
  2. /* 用slice实现队列
  3. */
  4. // minQueueLen is smallest capacity that queue may have.
  5. // Must be power of 2 for bitwise modulus: x % n == x & (n - 1).
  6. const minQueueLen = 16
  7. // Queue represents a single instance of the queue data structure.
  8. type Queue struct {
  9. buf []interface{}
  10. head, tail, count int
  11. }
  12. // New constructs and returns a new Queue.
  13. func NewQueue() *Queue {
  14. return &Queue{
  15. buf: make([]interface{}, minQueueLen),
  16. }
  17. }
  18. // Length returns the number of elements currently stored in the queue.
  19. func (q *Queue) Length() int {
  20. return q.count
  21. }
  22. // resizes the queue to fit exactly twice its current contents
  23. // this can result in shrinking if the queue is less than half-full
  24. func (q *Queue) resize() {
  25. newBuf := make([]interface{}, q.count<<1)
  26. if q.tail > q.head {
  27. copy(newBuf, q.buf[q.head:q.tail])
  28. } else {
  29. n := copy(newBuf, q.buf[q.head:])
  30. copy(newBuf[n:], q.buf[:q.tail])
  31. }
  32. q.head = 0
  33. q.tail = q.count
  34. q.buf = newBuf
  35. }
  36. // Add puts an element on the end of the queue.
  37. func (q *Queue) Add(elem interface{}) {
  38. if q.count == len(q.buf) {
  39. q.resize()
  40. }
  41. q.buf[q.tail] = elem
  42. // bitwise modulus
  43. q.tail = (q.tail + 1) & (len(q.buf) - 1)
  44. q.count++
  45. }
  46. // Peek returns the element at the head of the queue. This call panics
  47. // if the queue is empty.
  48. func (q *Queue) Peek() interface{} {
  49. if q.count <= 0 {
  50. panic("queue: Peek() called on empty queue")
  51. }
  52. return q.buf[q.head]
  53. }
  54. // Get returns the element at index i in the queue. If the index is
  55. // invalid, the call will panic. This method accepts both positive and
  56. // negative index values. Index 0 refers to the first element, and
  57. // index -1 refers to the last.
  58. func (q *Queue) Get(i int) interface{} {
  59. // If indexing backwards, convert to positive index.
  60. if i < 0 {
  61. i += q.count
  62. }
  63. if i < 0 || i >= q.count {
  64. panic("queue: Get() called with index out of range")
  65. }
  66. // bitwise modulus
  67. return q.buf[(q.head+i)&(len(q.buf)-1)]
  68. }
  69. // Remove removes and returns the element from the front of the queue. If the
  70. // queue is empty, the call will panic.
  71. func (q *Queue) Remove() interface{} {
  72. if q.count <= 0 {
  73. panic("queue: Remove() called on empty queue")
  74. }
  75. ret := q.buf[q.head]
  76. q.buf[q.head] = nil
  77. // bitwise modulus
  78. q.head = (q.head + 1) & (len(q.buf) - 1)
  79. q.count--
  80. // Resize down if buffer 1/4 full.
  81. if len(q.buf) > minQueueLen && (q.count<<2) == len(q.buf) {
  82. q.resize()
  83. }
  84. return ret
  85. }