data structures - Circular buffer in JavaScript -
Has anyone already implemented a circular buffer in JavaScript? How would you do without the indicator?
Strange co-incident, I have already written today! I do not know what your needs are, but it can be used.
It presents an interface like an array of unlimited lengths, but forgets old items:
// circular buffer storage externally clear 'length 'Indefinitely increases / when indexed objects will be forgotten under length-N (if you try to get them, undefined // will come back, the attempt to set up is an exception). // n represents the initial length of the array, the maximum function is not circular buffer (n) {this._array = new array (n); This.length = 0; } Circular buffer.prototype.tasting = function () {returns '[object circular buffer (' + .hd.a.re.length + ') length' + this.length + ']'; }; CircularBuffer.prototype.get = function (i) {if (i & lt; 0 || i & lt; this.length-this._array.length) Return is undefined; Please return it. _or [i% this._array.length]; }; CircularBuffer.prototype.set = function (i, v) {if (i & lt; 0 || i & lt; this.length-this._array.length) Skip CircularBuffer.IndexError; While (i & gt; this.length) {this ._rere [this. Length% this._array.length] = undefined; This.length ++; } This._array [i% this._array.length] = v; If (i == this.length) this.length ++; }; CircularBuffer.IndexError = {};
Comments
Post a Comment