Update submodules reference.
[e-mobility-charging-stations-simulator.git] / src / utils / CircularArray.ts
index 5dab1e2c072425a894b3a641e063aafc4c0f9198..535fb00e5cd34c811d03e96125e608d4ae6693d3 100644 (file)
@@ -1,24 +1,36 @@
-import Constants from './Constants';
 
 export default class CircularArray<T> extends Array<T> {
-  public size: number;
+  size: number;
+  private readonly maximumCircularArraySize = 2000;
 
-  constructor(size: number = Constants.MAXIMUM_MEASUREMENTS_NUMBER) {
+  constructor(size?: number) {
     super();
-    this.size = size;
+    this.size = size && size <= this.maximumCircularArraySize ? size : this.maximumCircularArraySize;
   }
 
   push(...items: T[]): number {
-    while (this.length > this.size) {
-      this.shift();
+    if (this.length + items.length > this.size) {
+      super.splice(0, (this.length + items.length) - this.size);
     }
     return super.push(...items);
   }
 
   unshift(...items: T[]): number {
-    while (this.length > this.size) {
-      this.pop();
+    if (this.length + items.length > this.size) {
+      super.splice(this.size - items.length, (this.length + items.length) - this.size);
     }
     return super.unshift(...items);
   }
+
+  concat(...items: (T | ConcatArray<T>)[]): T[] {
+    if (this.length + items.length > this.size) {
+      super.splice(0, (this.length + items.length) - this.size);
+    }
+    return super.concat(items as T[]);
+  }
+
+  splice(start: number, deleteCount?: number, ...items: T[]): T[] {
+    this.push(...items);
+    return super.splice(start, deleteCount);
+  }
 }