27 lines
417 B
TypeScript
27 lines
417 B
TypeScript
export default class Window {
|
|
length: number;
|
|
buffer: number[] = [];
|
|
constructor(length: number) {
|
|
this.length = length;
|
|
}
|
|
|
|
push(element: number) {
|
|
if (this.buffer.length == this.length) {
|
|
this.buffer.shift();
|
|
}
|
|
this.buffer.push(element);
|
|
}
|
|
|
|
sum() {
|
|
return this.buffer.reduce((a, b) => a + b, 0);
|
|
}
|
|
|
|
clear() {
|
|
this.buffer = [];
|
|
}
|
|
|
|
isFull() {
|
|
return this.buffer.length == this.length;
|
|
}
|
|
}
|