Explorar el Código

08: Circular buffer, better typed.

Frédéric G. MARAND hace 7 meses
padre
commit
aa449be9a2
Se han modificado 1 ficheros con 6 adiciones y 6 borrados
  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;
     }
 }