Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c8eeb62b | 2 | |
9a15316c | 3 | const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024; |
edfb206c | 4 | |
f59858d8 | 5 | /** |
db59d71e | 6 | * Array with a maximum length and shifting items when full. |
f59858d8 | 7 | */ |
ef72d3f5 | 8 | export 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 | ||
f69c4304 JB |
48 | public splice(start: number, deleteCount?: number, ...items: T[]): CircularArray<T> { |
49 | let itemsRemoved: T[] = []; | |
551e477c | 50 | if (arguments.length >= 3 && deleteCount !== undefined) { |
f69c4304 JB |
51 | itemsRemoved = super.splice(start, deleteCount, ...items); |
52 | if (this.length > this.size) { | |
53 | const itemsOverflowing = super.splice(0, this.length - this.size); | |
54 | itemsRemoved = new CircularArray<T>( | |
55 | itemsRemoved.length + itemsOverflowing.length, | |
56 | ...itemsRemoved, | |
57 | ...itemsOverflowing, | |
58 | ); | |
59 | } | |
534434b0 JB |
60 | } else if (arguments.length === 2) { |
61 | itemsRemoved = super.splice(start, deleteCount); | |
62 | } else { | |
63 | itemsRemoved = super.splice(start); | |
64 | } | |
f69c4304 | 65 | return itemsRemoved as CircularArray<T>; |
c70ff58d | 66 | } |
d43a619b JB |
67 | |
68 | public resize(size: number): void { | |
534434b0 | 69 | this.checkSize(size); |
d43a619b JB |
70 | if (size === 0) { |
71 | this.length = 0; | |
534434b0 JB |
72 | } else if (size < this.size) { |
73 | for (let i = size; i < this.size; i++) { | |
74 | super.pop(); | |
75 | } | |
d43a619b JB |
76 | } |
77 | this.size = size; | |
78 | } | |
79 | ||
80 | public empty(): boolean { | |
81 | return this.length === 0; | |
82 | } | |
83 | ||
84 | public full(): boolean { | |
85 | return this.length === this.size; | |
86 | } | |
534434b0 | 87 | |
260f9dcc | 88 | private checkSize(size: number): void { |
4cf1f68c JB |
89 | if (!Number.isSafeInteger(size)) { |
90 | throw new TypeError(`Invalid circular array size: ${size} is not a safe integer`); | |
91 | } | |
534434b0 | 92 | if (size < 0) { |
4cf1f68c | 93 | throw new RangeError(`Invalid circular array size: ${size} < 0`); |
534434b0 JB |
94 | } |
95 | } | |
edfb206c | 96 | } |