build(deps-dev): bump @rollup/plugin-typescript
[poolifier.git] / src / circular-array.ts
CommitLineData
78099a15
JB
1// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
dc021bcc 3export const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024
78099a15
JB
4
5/**
4342a45e 6 * Array with a maximum length and shifting items when full.
4bffc062
JB
7 *
8 * @internal
78099a15
JB
9 */
10export class CircularArray<T> extends Array<T> {
11 public size: number
12
13 constructor (size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) {
14 super()
15 this.checkSize(size)
16 this.size = size
17 if (arguments.length > 1) {
18 this.push(...items)
19 }
20 }
21
4888bc9c 22 /** @inheritDoc */
78099a15
JB
23 public push (...items: T[]): number {
24 const length = super.push(...items)
25 if (length > this.size) {
26 super.splice(0, length - this.size)
27 }
28 return this.length
29 }
30
4888bc9c 31 /** @inheritDoc */
78099a15
JB
32 public unshift (...items: T[]): number {
33 const length = super.unshift(...items)
34 if (length > this.size) {
35 super.splice(this.size, items.length)
36 }
37 return this.length
38 }
39
4888bc9c 40 /** @inheritDoc */
78099a15
JB
41 public concat (...items: Array<T | ConcatArray<T>>): CircularArray<T> {
42 const concatenatedCircularArray = super.concat(
43 items as T[]
44 ) as CircularArray<T>
45 concatenatedCircularArray.size = this.size
46 if (concatenatedCircularArray.length > concatenatedCircularArray.size) {
47 concatenatedCircularArray.splice(
48 0,
49 concatenatedCircularArray.length - concatenatedCircularArray.size
50 )
51 }
52 return concatenatedCircularArray
53 }
54
4888bc9c 55 /** @inheritDoc */
4077981d
JB
56 public splice (
57 start: number,
58 deleteCount?: number,
59 ...items: T[]
60 ): CircularArray<T> {
61 let itemsRemoved: T[] = []
1b6e07ba 62 if (arguments.length >= 3 && deleteCount != null) {
4077981d
JB
63 itemsRemoved = super.splice(start, deleteCount, ...items)
64 if (this.length > this.size) {
65 const itemsOverflowing = super.splice(0, this.length - this.size)
66 itemsRemoved = new CircularArray<T>(
67 itemsRemoved.length + itemsOverflowing.length,
68 ...itemsRemoved,
69 ...itemsOverflowing
70 )
71 }
78099a15
JB
72 } else if (arguments.length === 2) {
73 itemsRemoved = super.splice(start, deleteCount)
74 } else {
75 itemsRemoved = super.splice(start)
76 }
4077981d 77 return itemsRemoved as CircularArray<T>
78099a15
JB
78 }
79
80 public resize (size: number): void {
81 this.checkSize(size)
82 if (size === 0) {
83 this.length = 0
84 } else if (size < this.size) {
85 for (let i = size; i < this.size; i++) {
86 super.pop()
87 }
88 }
89 this.size = size
90 }
91
92 public empty (): boolean {
93 return this.length === 0
94 }
95
96 public full (): boolean {
97 return this.length === this.size
98 }
99
100 private checkSize (size: number): void {
101 if (!Number.isSafeInteger(size)) {
102 throw new TypeError(
103 `Invalid circular array size: ${size} is not a safe integer`
104 )
105 }
106 if (size < 0) {
107 throw new RangeError(`Invalid circular array size: ${size} < 0`)
108 }
109 }
110}