refactor: use ramdba helper for builtin types
[e-mobility-charging-stations-simulator.git] / src / utils / CircularArray.ts
index c46b06b6cb8983bb2cab53bf1a0e248a0452d005..f84b21aa3ac9d93a19075b8db1a2f889e05bdfa9 100644 (file)
@@ -1,58 +1,96 @@
-const DEFAULT_CIRCULAR_ARRAY_SIZE = 2000;
+// Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
 
-export default class CircularArray<T> extends Array<T> {
-  public size: number;
+export const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024
 
-  constructor(size?: number) {
-    super();
-    this.size = size ?? DEFAULT_CIRCULAR_ARRAY_SIZE;
-  }
+/**
+ * Array with a maximum length and shifting items when full.
+ */
+export class CircularArray<T> extends Array<T> {
+  public size: number
 
-  public push(...items: T[]): number {
-    if (this.length + items.length > this.size) {
-      super.splice(0, (this.length + items.length) - this.size);
+  constructor (size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) {
+    super()
+    this.checkSize(size)
+    this.size = size
+    if (arguments.length > 1) {
+      this.push(...items)
     }
-    return super.push(...items);
   }
 
-  public unshift(...items: T[]): number {
-    if (this.length + items.length > this.size) {
-      super.splice(this.size - items.length, (this.length + items.length) - this.size);
+  public push (...items: T[]): number {
+    const length = super.push(...items)
+    if (length > this.size) {
+      super.splice(0, length - this.size)
     }
-    return super.unshift(...items);
+    return this.length
   }
 
-  public concat(...items: (T | ConcatArray<T>)[]): T[] {
-    if (this.length + items.length > this.size) {
-      super.splice(0, (this.length + items.length) - this.size);
+  public unshift (...items: T[]): number {
+    const length = super.unshift(...items)
+    if (length > this.size) {
+      super.splice(this.size, items.length)
     }
-    return super.concat(items as T[]);
+    return this.length
   }
 
-  public splice(start: number, deleteCount?: number, ...items: T[]): T[] {
-    this.push(...items);
-    return super.splice(start, deleteCount);
+  public concat (...items: Array<T | ConcatArray<T>>): CircularArray<T> {
+    const concatenatedCircularArray = super.concat(items as T[]) as CircularArray<T>
+    concatenatedCircularArray.size = this.size
+    if (concatenatedCircularArray.length > concatenatedCircularArray.size) {
+      concatenatedCircularArray.splice(
+        0,
+        concatenatedCircularArray.length - concatenatedCircularArray.size
+      )
+    }
+    return concatenatedCircularArray
   }
 
-  public resize(size: number): void {
-    if (size < 0) {
-      throw new RangeError(
-        'circular array size does not allow negative values.'
-      );
+  public splice (start: number, deleteCount?: number, ...items: T[]): CircularArray<T> {
+    let itemsRemoved: T[] = []
+    if (arguments.length >= 3 && deleteCount != null) {
+      itemsRemoved = super.splice(start, deleteCount, ...items)
+      if (this.length > this.size) {
+        const itemsOverflowing = super.splice(0, this.length - this.size)
+        itemsRemoved = new CircularArray<T>(
+          itemsRemoved.length + itemsOverflowing.length,
+          ...itemsRemoved,
+          ...itemsOverflowing
+        )
+      }
+    } else if (arguments.length === 2) {
+      itemsRemoved = super.splice(start, deleteCount)
+    } else {
+      itemsRemoved = super.splice(start)
     }
+    return itemsRemoved as CircularArray<T>
+  }
+
+  public resize (size: number): void {
+    this.checkSize(size)
     if (size === 0) {
-      this.length = 0;
-    } else if (size !== this.size) {
-      this.slice(-size);
+      this.length = 0
+    } else if (size < this.size) {
+      for (let i = size; i < this.size; i++) {
+        super.pop()
+      }
     }
-    this.size = size;
+    this.size = size
   }
 
-  public empty(): boolean {
-    return this.length === 0;
+  public empty (): boolean {
+    return this.length === 0
   }
 
-  public full(): boolean {
-    return this.length === this.size;
+  public full (): boolean {
+    return this.length === this.size
+  }
+
+  private checkSize (size: number): void {
+    if (!Number.isSafeInteger(size)) {
+      throw new TypeError(`Invalid circular array size: ${size} is not a safe integer`)
+    }
+    if (size < 0) {
+      throw new RangeError(`Invalid circular array size: ${size} < 0`)
+    }
   }
 }