Browse Source

08: Circular buffer, better typed.

Frédéric G. MARAND 1 month ago
parent
commit
aa449be9a2
1 changed files with 6 additions and 6 deletions
  1. 6 6
      circular-buffer/circular-buffer.ts

+ 6 - 6
circular-buffer/circular-buffer.ts

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