-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
46 lines (39 loc) · 1.12 KB
/
Copy pathmain.ts
File metadata and controls
46 lines (39 loc) · 1.12 KB
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
/**
* Circular Buffer class with basic functionality.
* Circular Buffer (or Ring Buffer) is a data structure which uses fixed-length FIFO buffer.
* If next item overflowed a buffer, the oldest one is dropped.
*
* @example
* const linksBuffer = new CircularBuffer(3);
*
* linksBuffer.list(); // []
* linksBuffer.push('URL 1');
* linksBuffer.push('URL 2');
* linksBuffer.push('URL 3');
* linksBuffer.list(); // ['URL 1', 'URL 2', 'URL 3']
* [...linksBuffer]; // ['URL 1', 'URL 2', 'URL 3'] => buffer is iterable
* linksBuffer.push('URL 4');
* linksBuffer.list(); // ['URL 2', 'URL 3', 'URL 4'] => oldest entry dropped
*/
class CircularBuffer {
#data;
#size;
constructor (size: number) {
this.#data = [];
this.#size = size;
}
push (value) {
if (value != undefined) {
if (this.#data.length === this.#size) {
this.#data.shift();
}
this.#data.push(value);
}
}
list () {
return this.#data;
}
*[Symbol.iterator]() {
for (let i = 0, data = this.#data; i < data.length; yield data[i], ++i);
}
}