| 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
| 2 | |
| 3 | const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024 |
| 4 | |
| 5 | /** |
| 6 | * Array with a maximum length and shifting items when full. |
| 7 | */ |
| 8 | export class CircularArray<T> extends Array<T> { |
| 9 | public size: number |
| 10 | |
| 11 | constructor (size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) { |
| 12 | super() |
| 13 | this.checkSize(size) |
| 14 | this.size = size |
| 15 | if (arguments.length > 1) { |
| 16 | this.push(...items) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | /** @inheritDoc */ |
| 21 | public push (...items: T[]): number { |
| 22 | const length = super.push(...items) |
| 23 | if (length > this.size) { |
| 24 | super.splice(0, length - this.size) |
| 25 | } |
| 26 | return this.length |
| 27 | } |
| 28 | |
| 29 | /** @inheritDoc */ |
| 30 | public unshift (...items: T[]): number { |
| 31 | const length = super.unshift(...items) |
| 32 | if (length > this.size) { |
| 33 | super.splice(this.size, items.length) |
| 34 | } |
| 35 | return this.length |
| 36 | } |
| 37 | |
| 38 | /** @inheritDoc */ |
| 39 | public concat (...items: Array<T | ConcatArray<T>>): CircularArray<T> { |
| 40 | const concatenatedCircularArray = super.concat( |
| 41 | items as T[] |
| 42 | ) as CircularArray<T> |
| 43 | concatenatedCircularArray.size = this.size |
| 44 | if (concatenatedCircularArray.length > concatenatedCircularArray.size) { |
| 45 | concatenatedCircularArray.splice( |
| 46 | 0, |
| 47 | concatenatedCircularArray.length - concatenatedCircularArray.size |
| 48 | ) |
| 49 | } |
| 50 | return concatenatedCircularArray |
| 51 | } |
| 52 | |
| 53 | /** @inheritDoc */ |
| 54 | public splice (start: number, deleteCount?: number, ...items: T[]): T[] { |
| 55 | let itemsRemoved: T[] |
| 56 | if (arguments.length >= 3 && deleteCount !== undefined) { |
| 57 | itemsRemoved = super.splice(start, deleteCount) |
| 58 | // FIXME: that makes the items insert not in place |
| 59 | this.push(...items) |
| 60 | } else if (arguments.length === 2) { |
| 61 | itemsRemoved = super.splice(start, deleteCount) |
| 62 | } else { |
| 63 | itemsRemoved = super.splice(start) |
| 64 | } |
| 65 | return itemsRemoved |
| 66 | } |
| 67 | |
| 68 | public resize (size: number): void { |
| 69 | this.checkSize(size) |
| 70 | if (size === 0) { |
| 71 | this.length = 0 |
| 72 | } else if (size < this.size) { |
| 73 | for (let i = size; i < this.size; i++) { |
| 74 | super.pop() |
| 75 | } |
| 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 | } |
| 87 | |
| 88 | private checkSize (size: number): void { |
| 89 | if (!Number.isSafeInteger(size)) { |
| 90 | throw new TypeError( |
| 91 | `Invalid circular array size: ${size} is not a safe integer` |
| 92 | ) |
| 93 | } |
| 94 | if (size < 0) { |
| 95 | throw new RangeError(`Invalid circular array size: ${size} < 0`) |
| 96 | } |
| 97 | } |
| 98 | } |