fixed-queue.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict'
  2. // Extracted from node/lib/internal/fixed_queue.js
  3. // Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
  4. const kSize = 2048
  5. const kMask = kSize - 1
  6. // The FixedQueue is implemented as a singly-linked list of fixed-size
  7. // circular buffers. It looks something like this:
  8. //
  9. // head tail
  10. // | |
  11. // v v
  12. // +-----------+ <-----\ +-----------+ <------\ +-----------+
  13. // | [null] | \----- | next | \------- | next |
  14. // +-----------+ +-----------+ +-----------+
  15. // | item | <-- bottom | item | <-- bottom | undefined |
  16. // | item | | item | | undefined |
  17. // | item | | item | | undefined |
  18. // | item | | item | | undefined |
  19. // | item | | item | bottom --> | item |
  20. // | item | | item | | item |
  21. // | ... | | ... | | ... |
  22. // | item | | item | | item |
  23. // | item | | item | | item |
  24. // | undefined | <-- top | item | | item |
  25. // | undefined | | item | | item |
  26. // | undefined | | undefined | <-- top top --> | undefined |
  27. // +-----------+ +-----------+ +-----------+
  28. //
  29. // Or, if there is only one circular buffer, it looks something
  30. // like either of these:
  31. //
  32. // head tail head tail
  33. // | | | |
  34. // v v v v
  35. // +-----------+ +-----------+
  36. // | [null] | | [null] |
  37. // +-----------+ +-----------+
  38. // | undefined | | item |
  39. // | undefined | | item |
  40. // | item | <-- bottom top --> | undefined |
  41. // | item | | undefined |
  42. // | undefined | <-- top bottom --> | item |
  43. // | undefined | | item |
  44. // +-----------+ +-----------+
  45. //
  46. // Adding a value means moving `top` forward by one, removing means
  47. // moving `bottom` forward by one. After reaching the end, the queue
  48. // wraps around.
  49. //
  50. // When `top === bottom` the current queue is empty and when
  51. // `top + 1 === bottom` it's full. This wastes a single space of storage
  52. // but allows much quicker checks.
  53. /**
  54. * @type {FixedCircularBuffer}
  55. * @template T
  56. */
  57. class FixedCircularBuffer {
  58. /** @type {number} */
  59. bottom = 0
  60. /** @type {number} */
  61. top = 0
  62. /** @type {Array<T|undefined>} */
  63. list = new Array(kSize).fill(undefined)
  64. /** @type {T|null} */
  65. next = null
  66. /** @returns {boolean} */
  67. isEmpty () {
  68. return this.top === this.bottom
  69. }
  70. /** @returns {boolean} */
  71. isFull () {
  72. return ((this.top + 1) & kMask) === this.bottom
  73. }
  74. /**
  75. * @param {T} data
  76. * @returns {void}
  77. */
  78. push (data) {
  79. this.list[this.top] = data
  80. this.top = (this.top + 1) & kMask
  81. }
  82. /** @returns {T|null} */
  83. shift () {
  84. const nextItem = this.list[this.bottom]
  85. if (nextItem === undefined) { return null }
  86. this.list[this.bottom] = undefined
  87. this.bottom = (this.bottom + 1) & kMask
  88. return nextItem
  89. }
  90. }
  91. /**
  92. * @template T
  93. */
  94. module.exports = class FixedQueue {
  95. constructor () {
  96. /** @type {FixedCircularBuffer<T>} */
  97. this.head = this.tail = new FixedCircularBuffer()
  98. }
  99. /** @returns {boolean} */
  100. isEmpty () {
  101. return this.head.isEmpty()
  102. }
  103. /** @param {T} data */
  104. push (data) {
  105. if (this.head.isFull()) {
  106. // Head is full: Creates a new queue, sets the old queue's `.next` to it,
  107. // and sets it as the new main queue.
  108. this.head = this.head.next = new FixedCircularBuffer()
  109. }
  110. this.head.push(data)
  111. }
  112. /** @returns {T|null} */
  113. shift () {
  114. const tail = this.tail
  115. const next = tail.shift()
  116. if (tail.isEmpty() && tail.next !== null) {
  117. // If there is another queue, it forms the new tail.
  118. this.tail = tail.next
  119. tail.next = null
  120. }
  121. return next
  122. }
  123. }