|
@@ -7,7 +7,7 @@ export class BufferEmptyError extends Error {
|
|
export default class CircularBuffer<T> {
|
|
export default class CircularBuffer<T> {
|
|
protected base: number;
|
|
protected base: number;
|
|
protected cap: number;
|
|
protected cap: number;
|
|
- protected data: Array<any>
|
|
|
|
|
|
+ protected data: Array<T>
|
|
protected len: number;
|
|
protected len: number;
|
|
|
|
|
|
constructor(initial: number) {
|
|
constructor(initial: number) {
|
|
@@ -16,11 +16,11 @@ export default class CircularBuffer<T> {
|
|
}
|
|
}
|
|
this.base = 0;
|
|
this.base = 0;
|
|
this.cap = initial;
|
|
this.cap = initial;
|
|
- this.data = new Array<any>(this.cap)
|
|
|
|
|
|
+ this.data = new Array<T>(this.cap)
|
|
this.len = 0;
|
|
this.len = 0;
|
|
}
|
|
}
|
|
|
|
|
|
- write(value: any): void {
|
|
|
|
|
|
+ write(value: T): void {
|
|
if (this.len === this.cap) {
|
|
if (this.len === this.cap) {
|
|
throw new BufferFullError();
|
|
throw new BufferFullError();
|
|
}
|
|
}
|
|
@@ -28,7 +28,7 @@ export default class CircularBuffer<T> {
|
|
this.len++;
|
|
this.len++;
|
|
}
|
|
}
|
|
|
|
|
|
- read(): any {
|
|
|
|
|
|
+ read(): T {
|
|
if (this.len === 0) {
|
|
if (this.len === 0) {
|
|
throw new BufferEmptyError();
|
|
throw new BufferEmptyError();
|
|
}
|
|
}
|
|
@@ -38,7 +38,7 @@ export default class CircularBuffer<T> {
|
|
return value;
|
|
return value;
|
|
}
|
|
}
|
|
|
|
|
|
- forceWrite(value: any): void {
|
|
|
|
|
|
+ forceWrite(value: T): void {
|
|
this.data[(this.base + this.len) % this.cap] = value;
|
|
this.data[(this.base + this.len) % this.cap] = value;
|
|
if (this.cap === this.len) {
|
|
if (this.cap === this.len) {
|
|
this.base = (this.base + 1) % this.cap;
|
|
this.base = (this.base + 1) % this.cap;
|
|
@@ -49,7 +49,7 @@ export default class CircularBuffer<T> {
|
|
|
|
|
|
clear(): void {
|
|
clear(): void {
|
|
this.base = 0;
|
|
this.base = 0;
|
|
- this.data = new Array<any>(this.cap)
|
|
|
|
|
|
+ this.data = new Array<T>(this.cap)
|
|
this.len = 0;
|
|
this.len = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|