X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FCircularArray.ts;h=535fb00e5cd34c811d03e96125e608d4ae6693d3;hb=0d7b35c4bb09627e56413d39ae6b2da56b6dcbd2;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..535fb00e 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; + 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); + } }