feb5e3e2b66fb99aef38c6d24db1a6c5eafccfe1
[e-mobility-charging-stations-simulator.git] / src / utils / CircularArray.ts
1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
2
3 export const DEFAULT_CIRCULAR_ARRAY_SIZE = Number.MAX_SAFE_INTEGER;
4
5 /** Array with a maximum length shifting items when full. */
6 export class CircularArray<T> extends Array<T> {
7 public size: number;
8
9 constructor(size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) {
10 super();
11 this.checkSize(size);
12 this.size = size;
13 if (arguments.length > 1) {
14 this.push(...items);
15 }
16 }
17
18 public push(...items: T[]): number {
19 const length = super.push(...items);
20 if (length > this.size) {
21 super.splice(0, length - this.size);
22 }
23 return this.length;
24 }
25
26 public unshift(...items: T[]): number {
27 const length = super.unshift(...items);
28 if (length > this.size) {
29 super.splice(this.size, items.length);
30 }
31 return this.length;
32 }
33
34 public concat(...items: (T | ConcatArray<T>)[]): CircularArray<T> {
35 const concatenatedCircularArray = super.concat(
36 items as T[]
37 ) as CircularArray<T>;
38 concatenatedCircularArray.size = this.size;
39 if (concatenatedCircularArray.length > concatenatedCircularArray.size) {
40 concatenatedCircularArray.splice(
41 0,
42 concatenatedCircularArray.length - concatenatedCircularArray.size
43 );
44 }
45 return concatenatedCircularArray;
46 }
47
48 public splice(start: number, deleteCount?: number, ...items: T[]): T[] {
49 let itemsRemoved: T[];
50 if (arguments.length >= 3 && typeof deleteCount !== 'undefined') {
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;
60 }
61
62 public resize(size: number): void {
63 this.checkSize(size);
64 if (size === 0) {
65 this.length = 0;
66 } else if (size < this.size) {
67 for (let i = size; i < this.size; i++) {
68 super.pop();
69 }
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 }
81
82 private checkSize(size: number) {
83 if (size < 0) {
84 throw new RangeError('Invalid circular array size');
85 }
86 }
87 }