X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FCircularArray.ts;h=e7edb3dd13e1a895883f0a24600dc774df540451;hb=b3f9c51558e73be4ce7915d6dbcb40f1378ba89e;hp=5dab1e2c072425a894b3a641e063aafc4c0f9198;hpb=edfb206c5a6a309cbe5a8e6bf3c3f52ab57d96fc;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/CircularArray.ts b/src/utils/CircularArray.ts index 5dab1e2c..e7edb3dd 100644 --- a/src/utils/CircularArray.ts +++ b/src/utils/CircularArray.ts @@ -1,24 +1,36 @@ -import Constants from './Constants'; export default class CircularArray extends Array { public 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[] { + 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); + } }