Last active
June 5, 2023 09:54
-
-
Save zhibirc/5bedda8032c181285589b75bf72f011b to your computer and use it in GitHub Desktop.
CircularBuffer class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implement Circular/Ring Buffer basic behaviour. | |
* | |
* @version 1 | |
* | |
* @example | |
* const buffer = new CircularBuffer(3); | |
* | |
* buffer.list(); // [] | |
* buffer.push('URL 1'); | |
* buffer.push('URL 2'); | |
* buffer.push('URL 3'); | |
* buffer.list(); // ['URL 1', 'URL 2', 'URL 3'] | |
* [...buffer]; // ['URL 1', 'URL 2', 'URL 3'] => buffer is iterable | |
* buffer.push('URL 4'); | |
* buffer.list(); // ['URL 2', 'URL 3', 'URL 4'] => oldest entry dropped | |
*/ | |
class CircularBuffer { | |
#data; | |
constructor ( length ) { | |
this.#data = []; | |
Object.defineProperty(this, 'length', { | |
value: length | |
}); | |
} | |
push ( value ) { | |
if ( value != undefined ) { | |
if ( this.#data.length + 1 > this.length ) { | |
this.#data.shift(); | |
} | |
this.#data.push(value); | |
} | |
} | |
list () { | |
return this.#data; | |
} | |
*[Symbol.iterator]() { | |
for (let index = 0; index < this.#data.length; yield this.#data[index++]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implement Circular/Ring Buffer basic behaviour. | |
* Read/write operations are done with O(1) complexity. | |
* | |
* @version 2 | |
* | |
* @example | |
* const buffer = new CircularBuffer(3); | |
* | |
* buffer.list(); // [] | |
* buffer.write('URL 1'); | |
* buffer.write('URL 2'); | |
* buffer.write('URL 3'); | |
* buffer.list(); // ['URL 1', 'URL 2', 'URL 3'] | |
* [...buffer]; // ['URL 1', 'URL 2', 'URL 3'] => buffer is iterable | |
* buffer.write('URL 4'); | |
* buffer.list(); // ['URL 4', 'URL 2', 'URL 3'] => oldest entry dropped | |
*/ | |
class CircularBuffer { | |
#data; | |
#readPointer; | |
#writePointer; | |
constructor ( length ) { | |
this.#data = []; | |
this.#readPointer = 0; | |
this.#writePointer = 0; | |
Object.defineProperty(this, 'length', { | |
value: length | |
}); | |
} | |
read () { | |
const result = this.#data[this.#readPointer]; | |
this.#readPointer = (this.#readPointer + 1) % this.length; | |
return result; | |
} | |
write ( value ) { | |
this.#data[this.#writePointer] = value; | |
this.#writePointer = (this.#writePointer + 1) % this.length; | |
} | |
list () { | |
return this.#data; | |
} | |
*[Symbol.iterator]() { | |
for (let index = 0; index < this.#data.length; yield this.#data[index++]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment