circularbuffer.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  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. /**
  15. * @fileoverview Datastructure: Circular Buffer.
  16. *
  17. * Implements a buffer with a maximum size. New entries override the oldest
  18. * entries when the maximum size has been reached.
  19. *
  20. */
  21. goog.provide('goog.structs.CircularBuffer');
  22. /**
  23. * Class for CircularBuffer.
  24. * @param {number=} opt_maxSize The maximum size of the buffer.
  25. * @constructor
  26. * @template T
  27. */
  28. goog.structs.CircularBuffer = function(opt_maxSize) {
  29. /**
  30. * Index of the next element in the circular array structure.
  31. * @private {number}
  32. */
  33. this.nextPtr_ = 0;
  34. /**
  35. * Maximum size of the the circular array structure.
  36. * @private {number}
  37. */
  38. this.maxSize_ = opt_maxSize || 100;
  39. /**
  40. * Underlying array for the CircularBuffer.
  41. * @private {!Array<T>}
  42. */
  43. this.buff_ = [];
  44. };
  45. /**
  46. * Adds an item to the buffer. May remove the oldest item if the buffer is at
  47. * max size.
  48. * @param {T} item The item to add.
  49. * @return {T|undefined} The removed old item, if the buffer is at max size.
  50. * Return undefined, otherwise.
  51. */
  52. goog.structs.CircularBuffer.prototype.add = function(item) {
  53. var previousItem = this.buff_[this.nextPtr_];
  54. this.buff_[this.nextPtr_] = item;
  55. this.nextPtr_ = (this.nextPtr_ + 1) % this.maxSize_;
  56. return previousItem;
  57. };
  58. /**
  59. * Returns the item at the specified index.
  60. * @param {number} index The index of the item. The index of an item can change
  61. * after calls to {@code add()} if the buffer is at maximum size.
  62. * @return {T} The item at the specified index.
  63. */
  64. goog.structs.CircularBuffer.prototype.get = function(index) {
  65. index = this.normalizeIndex_(index);
  66. return this.buff_[index];
  67. };
  68. /**
  69. * Sets the item at the specified index.
  70. * @param {number} index The index of the item. The index of an item can change
  71. * after calls to {@code add()} if the buffer is at maximum size.
  72. * @param {T} item The item to add.
  73. */
  74. goog.structs.CircularBuffer.prototype.set = function(index, item) {
  75. index = this.normalizeIndex_(index);
  76. this.buff_[index] = item;
  77. };
  78. /**
  79. * Returns the current number of items in the buffer.
  80. * @return {number} The current number of items in the buffer.
  81. */
  82. goog.structs.CircularBuffer.prototype.getCount = function() {
  83. return this.buff_.length;
  84. };
  85. /**
  86. * @return {boolean} Whether the buffer is empty.
  87. */
  88. goog.structs.CircularBuffer.prototype.isEmpty = function() {
  89. return this.buff_.length == 0;
  90. };
  91. /**
  92. * Empties the current buffer.
  93. */
  94. goog.structs.CircularBuffer.prototype.clear = function() {
  95. this.buff_.length = 0;
  96. this.nextPtr_ = 0;
  97. };
  98. /** @return {!Array<T>} The values in the buffer. */
  99. goog.structs.CircularBuffer.prototype.getValues = function() {
  100. // getNewestValues returns all the values if the maxCount parameter is the
  101. // count
  102. return this.getNewestValues(this.getCount());
  103. };
  104. /**
  105. * Returns the newest values in the buffer up to {@code count}.
  106. * @param {number} maxCount The maximum number of values to get. Should be a
  107. * positive number.
  108. * @return {!Array<T>} The newest values in the buffer up to {@code count}.
  109. */
  110. goog.structs.CircularBuffer.prototype.getNewestValues = function(maxCount) {
  111. var l = this.getCount();
  112. var start = this.getCount() - maxCount;
  113. var rv = [];
  114. for (var i = start; i < l; i++) {
  115. rv.push(this.get(i));
  116. }
  117. return rv;
  118. };
  119. /** @return {!Array<number>} The indexes in the buffer. */
  120. goog.structs.CircularBuffer.prototype.getKeys = function() {
  121. var rv = [];
  122. var l = this.getCount();
  123. for (var i = 0; i < l; i++) {
  124. rv[i] = i;
  125. }
  126. return rv;
  127. };
  128. /**
  129. * Whether the buffer contains the key/index.
  130. * @param {number} key The key/index to check for.
  131. * @return {boolean} Whether the buffer contains the key/index.
  132. */
  133. goog.structs.CircularBuffer.prototype.containsKey = function(key) {
  134. return key < this.getCount();
  135. };
  136. /**
  137. * Whether the buffer contains the given value.
  138. * @param {T} value The value to check for.
  139. * @return {boolean} Whether the buffer contains the given value.
  140. */
  141. goog.structs.CircularBuffer.prototype.containsValue = function(value) {
  142. var l = this.getCount();
  143. for (var i = 0; i < l; i++) {
  144. if (this.get(i) == value) {
  145. return true;
  146. }
  147. }
  148. return false;
  149. };
  150. /**
  151. * Returns the last item inserted into the buffer.
  152. * @return {T|null} The last item inserted into the buffer,
  153. * or null if the buffer is empty.
  154. */
  155. goog.structs.CircularBuffer.prototype.getLast = function() {
  156. if (this.getCount() == 0) {
  157. return null;
  158. }
  159. return this.get(this.getCount() - 1);
  160. };
  161. /**
  162. * Helper function to convert an index in the number space of oldest to
  163. * newest items in the array to the position that the element will be at in the
  164. * underlying array.
  165. * @param {number} index The index of the item in a list ordered from oldest to
  166. * newest.
  167. * @return {number} The index of the item in the CircularBuffer's underlying
  168. * array.
  169. * @private
  170. */
  171. goog.structs.CircularBuffer.prototype.normalizeIndex_ = function(index) {
  172. if (index >= this.buff_.length) {
  173. throw Error('Out of bounds exception');
  174. }
  175. if (this.buff_.length < this.maxSize_) {
  176. return index;
  177. }
  178. return (this.nextPtr_ + Number(index)) % this.maxSize_;
  179. };