build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / utils / CircularArray.ts
CommitLineData
edd13439 1// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
c8eeb62b 2
9a15316c 3const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024;
edfb206c 4
f59858d8 5/**
db59d71e 6 * Array with a maximum length and shifting items when full.
f59858d8 7 */
ef72d3f5 8export class CircularArray<T> extends Array<T> {
365f6bc9 9 public size: number;
edfb206c 10
534434b0 11 constructor(size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) {
edfb206c 12 super();
534434b0
JB
13 this.checkSize(size);
14 this.size = size;
15 if (arguments.length > 1) {
16 this.push(...items);
17 }
edfb206c
JB
18 }
19
d43a619b 20 public push(...items: T[]): number {
534434b0
JB
21 const length = super.push(...items);
22 if (length > this.size) {
23 super.splice(0, length - this.size);
edfb206c 24 }
534434b0 25 return this.length;
edfb206c
JB
26 }
27
d43a619b 28 public unshift(...items: T[]): number {
534434b0
JB
29 const length = super.unshift(...items);
30 if (length > this.size) {
31 super.splice(this.size, items.length);
edfb206c 32 }
35e15933 33 return this.length;
edfb206c 34 }
c70ff58d 35
534434b0 36 public concat(...items: (T | ConcatArray<T>)[]): CircularArray<T> {
e7aeea18 37 const concatenatedCircularArray = super.concat(items as T[]) as CircularArray<T>;
534434b0
JB
38 concatenatedCircularArray.size = this.size;
39 if (concatenatedCircularArray.length > concatenatedCircularArray.size) {
40 concatenatedCircularArray.splice(
41 0,
5edd8ba0 42 concatenatedCircularArray.length - concatenatedCircularArray.size,
534434b0 43 );
c70ff58d 44 }
534434b0 45 return concatenatedCircularArray;
c70ff58d
JB
46 }
47
d43a619b 48 public splice(start: number, deleteCount?: number, ...items: T[]): T[] {
534434b0 49 let itemsRemoved: T[];
551e477c 50 if (arguments.length >= 3 && deleteCount !== undefined) {
534434b0
JB
51 itemsRemoved = super.splice(start, deleteCount);
52 // FIXME: that makes the items insert not in place
53 this.push(...items);
54 } else if (arguments.length === 2) {
55 itemsRemoved = super.splice(start, deleteCount);
56 } else {
57 itemsRemoved = super.splice(start);
58 }
59 return itemsRemoved;
c70ff58d 60 }
d43a619b
JB
61
62 public resize(size: number): void {
534434b0 63 this.checkSize(size);
d43a619b
JB
64 if (size === 0) {
65 this.length = 0;
534434b0
JB
66 } else if (size < this.size) {
67 for (let i = size; i < this.size; i++) {
68 super.pop();
69 }
d43a619b
JB
70 }
71 this.size = size;
72 }
73
74 public empty(): boolean {
75 return this.length === 0;
76 }
77
78 public full(): boolean {
79 return this.length === this.size;
80 }
534434b0 81
260f9dcc 82 private checkSize(size: number): void {
4cf1f68c
JB
83 if (!Number.isSafeInteger(size)) {
84 throw new TypeError(`Invalid circular array size: ${size} is not a safe integer`);
85 }
534434b0 86 if (size < 0) {
4cf1f68c 87 throw new RangeError(`Invalid circular array size: ${size} < 0`);
534434b0
JB
88 }
89 }
edfb206c 90}