1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-restricted-syntax, no-invalid-this */
'use strict';
// MODULES //
var isCollection = require( '@stdlib/assert/is-collection' );
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var setReadOnly = require( './../../define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( './../../define-nonenumerable-read-only-accessor' );
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
var MAX_ITERATIONS = require( '@stdlib/constants/float64/max' );
// MAIN //
/**
* Circular buffer constructor.
*
* @constructor
* @param {(PositiveInteger|Collection)} buffer - buffer size or an array-like object to use as the underlying buffer
* @throws {TypeError} must provide either a valid buffer size or an array-like object
* @returns {CircularBuffer} circular buffer instance
*
* @example
* var b = new CircularBuffer( 3 );
*
* // Fill the buffer...
* var v = b.push( 'foo' );
* // returns undefined
*
* v = b.push( 'bar' );
* // returns undefined
*
* v = b.push( 'beep' );
* // returns undefined
*
* // Add another value to the buffer and return the removed element:
* v = b.push( 'boop' );
* // returns 'foo'
*/
function CircularBuffer( buffer ) {
var i;
if ( !(this instanceof CircularBuffer) ) {
return new CircularBuffer( buffer );
}
if ( isPositiveInteger( buffer ) ) {
this._buffer = [];
for ( i = 0; i < buffer; i++ ) {
this._buffer.push( 0.0 ); // initialize with zeros, but could be any value (we're just ensuring a contiguous block of memory)
}
} else if ( isCollection( buffer ) ) {
this._buffer = buffer;
} else {
throw new TypeError( 'invalid argument. Must provide either a valid buffer size (positive integer) or an array-like object which can serve as the underlying buffer. Value: `' + buffer + '`.' );
}
this._length = this._buffer.length;
this._count = 0;
this._i = -1;
return this;
}
/**
* Clears the buffer.
*
* @name clear
* @memberof CircularBuffer.prototype
* @type {Function}
* @returns {CircularBuffer} circular buffer instance
*
* @example
* var b = new CircularBuffer( 2 );
*
* // Add values to the buffer:
* b.push( 'foo' );
* b.push( 'bar' );
* b.push( 'beep' );
* b.push( 'boop' );
*
* // Get the number of elements currently in the buffer:
* var n = b.count;
* // returns 2
*
* // Clear the buffer:
* b.clear();
*
* // Get the number of buffer values:
* n = b.count;
* // returns 0
*/
setReadOnly( CircularBuffer.prototype, 'clear', function clear() {
this._count = 0;
this._i = -1; // NOTE: this ensures that we always fill the buffer starting at index `0`.
return this;
});
/**
* Number of elements currently in the buffer.
*
* @name count
* @memberof CircularBuffer.prototype
* @type {NonNegativeInteger}
*
* @example
* var b = new CircularBuffer( 4 );
*
* // Get the value count:
* var n = b.count;
* // returns 0
*
* // Add values to the buffer:
* b.push( 'foo' );
* b.push( 'bar' );
*
* // Get the value count:
* n = b.count;
* // returns 2
*/
setReadOnlyAccessor( CircularBuffer.prototype, 'count', function get() {
return this._count;
});
/**
* Boolean indicating whether a circular buffer is full.
*
* @name full
* @memberof CircularBuffer.prototype
* @type {boolean}
*
* @example
* var b = new CircularBuffer( 3 );
*
* // Determine if the buffer is full:
* var bool = b.full;
* // returns false
*
* // Add values to the buffer:
* b.push( 'foo' );
* b.push( 'bar' );
* b.push( 'beep' );
* b.push( 'boop' );
*
* // Determine if the buffer is full:
* bool = b.full;
* // returns true
*/
setReadOnlyAccessor( CircularBuffer.prototype, 'full', function get() {
return this._count === this._length;
});
/**
* Returns an iterator for iterating over a circular buffer.
*
* ## Notes
*
* - An iterator does not iterate over partially full buffers.
*
* @name iterator
* @memberof CircularBuffer.prototype
* @type {Function}
* @param {NonNegativeInteger} [niters] - number of iterations
* @throws {TypeError} must provide a nonnegative integer
* @returns {Iterator} iterator
*
* @example
* var b = new CircularBuffer( 3 );
*
* // Add values to the buffer:
* b.push( 'foo' );
* b.push( 'bar' );
* b.push( 'beep' );
* b.push( 'boop' );
*
* // Create an iterator:
* var it = b.iterator( b.length );
*
* // Iterate over the buffer...
* var v = it.next().value;
* // returns 'bar'
*
* v = it.next().value;
* // returns 'beep'
*
* v = it.next().value;
* // returns 'boop'
*
* var bool = it.next().done;
* // returns true
*/
setReadOnly( CircularBuffer.prototype, 'iterator', function iterator( niters ) {
var iter;
var self;
var FLG;
var N;
var n;
var i;
if ( arguments.length ) {
if ( !isNonNegativeInteger( niters ) ) {
throw new TypeError( 'invalid argument. Must provide a nonnegative integer. Value: `' + niters + '`.' );
}
N = niters;
} else {
N = MAX_ITERATIONS;
}
self = this;
// Initialize the iteration index and counter:
i = this._i;
n = 0;
// Create an iterator protocol-compliant object:
iter = {};
setReadOnly( iter, 'next', next );
setReadOnly( iter, 'return', end );
if ( iteratorSymbol ) {
setReadOnly( iter, iteratorSymbol, factory );
}
return iter;
/**
* Returns an iterator protocol-compliant object containing the next iterated value.
*
* @private
* @returns {Object} iterator protocol-compliant object
*/
function next() {
/* eslint-disable no-underscore-dangle */
n += 1;
if ( FLG || n > N ) {
return {
'done': true
};
}
// If the buffer is only partially full, don't allow iteration over "undefined" elements (this ensures similar behavior with `toArray()`)...
if ( self._count !== self._length ) {
FLG = true;
return {
'done': true
};
}
i = (i+1) % self._length;
return {
'value': self._buffer[ i ],
'done': false
};
/* eslint-enable no-underscore-dangle */
}
/**
* Finishes an iterator.
*
* @private
* @param {*} [value] - value to return
* @returns {Object} iterator protocol-compliant object
*/
function end( value ) {
FLG = true;
if ( arguments.length ) {
return {
'value': value,
'done': true
};
}
return {
'done': true
};
}
/**
* Returns a new iterator.
*
* @private
* @returns {Iterator} iterator
*/
function factory() {
return self.iterator( N );
}
});
/**
* Circular buffer length (capacity).
*
* @name length
* @memberof CircularBuffer.prototype
* @type {NonNegativeInteger}
*
* @example
* var b = new CircularBuffer( 4 );
*
* // Get the buffer capacity:
* var len = b.length;
* // returns 4
*/
setReadOnlyAccessor( CircularBuffer.prototype, 'length', function get() {
return this._length;
});
/**
* Adds a value to the circular buffer.
*
* @name push
* @memberof CircularBuffer.prototype
* @type {Function}
* @returns {(*|void)} removed element or undefined
*
* @example
* var b = new CircularBuffer( 3 );
*
* // Fill the buffer:
* var v = b.push( 'foo' );
* // returns undefined
*
* v = b.push( 'bar' );
* // returns undefined
*
* v = b.push( 'beep' );
* // returns undefined
*
* // Add another value to the buffer and return the removed element:
* v = b.push( 'boop' );
* // returns 'foo'
*/
setReadOnly( CircularBuffer.prototype, 'push', function push( value ) {
var v;
// Compute the next buffer index:
this._i = (this._i+1) % this._length;
// Check if we are still filling the buffer...
if ( this._count < this._length ) {
this._buffer[ this._i ] = value;
this._count += 1;
return;
}
// Replace an existing buffer element...
v = this._buffer[ this._i ];
this._buffer[ this._i ] = value;
return v;
});
/**
* Returns an array of circular buffer values.
*
* @name toArray
* @memberof CircularBuffer.prototype
* @type {Function}
* @returns {Array} circular buffer values
*
* @example
* var b = new CircularBuffer( 3 );
*
* // Add values to the buffer:
* b.push( 'foo' );
* b.push( 'bar' );
* b.push( 'beep' );
* b.push( 'boop' );
*
* // Get an array of buffer values:
* var vals = b.toArray();
* // returns [ 'bar', 'beep', 'boop' ]
*/
setReadOnly( CircularBuffer.prototype, 'toArray', function toArray() {
var out;
var k;
var i;
out = [];
for ( i = 1; i <= this._count; i++ ) {
// Note: in a full buffer, `count == length`; in a partially full buffer, we need to ensure we always start at index `0`
k = (this._i+i) % this._count;
out.push( this._buffer[ k ] );
}
return out;
});
/**
* Serializes a circular buffer as JSON.
*
* ## Notes
*
* - `JSON.stringify()` implicitly calls this method when stringifying a `CircularBuffer` instance.
*
* @name toJSON
* @memberof CircularBuffer.prototype
* @type {Function}
* @returns {Object} serialized circular buffer
*
* @example
* var b = new CircularBuffer( 3 );
*
* // Add values to the buffer:
* b.push( 'foo' );
* b.push( 'bar' );
* b.push( 'beep' );
* b.push( 'boop' );
*
* // Serialize to JSON:
* var o = b.toJSON();
* // returns { 'type': 'circular-buffer', 'length': 3, 'data': [ 'bar', 'beep', 'boop' ] }
*/
setReadOnly( CircularBuffer.prototype, 'toJSON', function toJSON() {
var out = {};
out.type = 'circular-buffer';
out.length = this._length;
out.data = this.toArray();
return out;
});
// EXPORTS //
module.exports = CircularBuffer;
|