Commit | Line | Data |
---|---|---|
a19b897d | 1 | // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
c8eeb62b | 2 | |
66a7748d | 3 | export 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> { |
66a7748d | 9 | public size: number |
edfb206c | 10 | |
66a7748d JB |
11 | constructor (size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) { |
12 | super() | |
13 | this.checkSize(size) | |
14 | this.size = size | |
534434b0 | 15 | if (arguments.length > 1) { |
66a7748d | 16 | this.push(...items) |
534434b0 | 17 | } |
edfb206c JB |
18 | } |
19 | ||
66a7748d JB |
20 | public push (...items: T[]): number { |
21 | const length = super.push(...items) | |
534434b0 | 22 | if (length > this.size) { |
66a7748d | 23 | super.splice(0, length - this.size) |
edfb206c | 24 | } |
66a7748d | 25 | return this.length |
edfb206c JB |
26 | } |
27 | ||
66a7748d JB |
28 | public unshift (...items: T[]): number { |
29 | const length = super.unshift(...items) | |
534434b0 | 30 | if (length > this.size) { |
66a7748d | 31 | super.splice(this.size, items.length) |
edfb206c | 32 | } |
66a7748d | 33 | return this.length |
edfb206c | 34 | } |
c70ff58d | 35 | |
66a7748d JB |
36 | public concat (...items: Array<T | ConcatArray<T>>): CircularArray<T> { |
37 | const concatenatedCircularArray = super.concat(items as T[]) as CircularArray<T> | |
38 | concatenatedCircularArray.size = this.size | |
534434b0 JB |
39 | if (concatenatedCircularArray.length > concatenatedCircularArray.size) { |
40 | concatenatedCircularArray.splice( | |
41 | 0, | |
66a7748d JB |
42 | concatenatedCircularArray.length - concatenatedCircularArray.size |
43 | ) | |
c70ff58d | 44 | } |
66a7748d | 45 | return concatenatedCircularArray |
c70ff58d JB |
46 | } |
47 | ||
66a7748d JB |
48 | public splice (start: number, deleteCount?: number, ...items: T[]): CircularArray<T> { |
49 | let itemsRemoved: T[] = [] | |
a807045b | 50 | if (arguments.length >= 3 && deleteCount != null) { |
66a7748d | 51 | itemsRemoved = super.splice(start, deleteCount, ...items) |
f69c4304 | 52 | if (this.length > this.size) { |
66a7748d | 53 | const itemsOverflowing = super.splice(0, this.length - this.size) |
f69c4304 JB |
54 | itemsRemoved = new CircularArray<T>( |
55 | itemsRemoved.length + itemsOverflowing.length, | |
56 | ...itemsRemoved, | |
66a7748d JB |
57 | ...itemsOverflowing |
58 | ) | |
f69c4304 | 59 | } |
534434b0 | 60 | } else if (arguments.length === 2) { |
66a7748d | 61 | itemsRemoved = super.splice(start, deleteCount) |
534434b0 | 62 | } else { |
66a7748d | 63 | itemsRemoved = super.splice(start) |
534434b0 | 64 | } |
66a7748d | 65 | return itemsRemoved as CircularArray<T> |
c70ff58d | 66 | } |
d43a619b | 67 | |
66a7748d JB |
68 | public resize (size: number): void { |
69 | this.checkSize(size) | |
d43a619b | 70 | if (size === 0) { |
66a7748d | 71 | this.length = 0 |
534434b0 JB |
72 | } else if (size < this.size) { |
73 | for (let i = size; i < this.size; i++) { | |
66a7748d | 74 | super.pop() |
534434b0 | 75 | } |
d43a619b | 76 | } |
66a7748d | 77 | this.size = size |
d43a619b JB |
78 | } |
79 | ||
66a7748d JB |
80 | public empty (): boolean { |
81 | return this.length === 0 | |
d43a619b JB |
82 | } |
83 | ||
66a7748d JB |
84 | public full (): boolean { |
85 | return this.length === this.size | |
d43a619b | 86 | } |
534434b0 | 87 | |
66a7748d | 88 | private checkSize (size: number): void { |
4cf1f68c | 89 | if (!Number.isSafeInteger(size)) { |
66a7748d | 90 | throw new TypeError(`Invalid circular array size: ${size} is not a safe integer`) |
4cf1f68c | 91 | } |
534434b0 | 92 | if (size < 0) { |
66a7748d | 93 | throw new RangeError(`Invalid circular array size: ${size} < 0`) |
534434b0 JB |
94 | } |
95 | } | |
edfb206c | 96 | } |