Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / circular-array.ts
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 public push (...items: T[]): number {
21 const length = super.push(...items)
22 if (length > this.size) {
23 super.splice(0, length - this.size)
24 }
25 return this.length
26 }
27
28 public unshift (...items: T[]): number {
29 const length = super.unshift(...items)
30 if (length > this.size) {
31 super.splice(this.size, items.length)
32 }
33 return this.length
34 }
35
36 public concat (...items: Array<T | ConcatArray<T>>): CircularArray<T> {
37 const concatenatedCircularArray = super.concat(
38 items as T[]
39 ) as CircularArray<T>
40 concatenatedCircularArray.size = this.size
41 if (concatenatedCircularArray.length > concatenatedCircularArray.size) {
42 concatenatedCircularArray.splice(
43 0,
44 concatenatedCircularArray.length - concatenatedCircularArray.size
45 )
46 }
47 return concatenatedCircularArray
48 }
49
50 public splice (start: number, deleteCount?: number, ...items: T[]): T[] {
51 let itemsRemoved: T[]
52 if (arguments.length >= 3 && deleteCount !== undefined) {
53 itemsRemoved = super.splice(start, deleteCount)
54 // FIXME: that makes the items insert not in place
55 this.push(...items)
56 } else if (arguments.length === 2) {
57 itemsRemoved = super.splice(start, deleteCount)
58 } else {
59 itemsRemoved = super.splice(start)
60 }
61 return itemsRemoved
62 }
63
64 public resize (size: number): void {
65 this.checkSize(size)
66 if (size === 0) {
67 this.length = 0
68 } else if (size < this.size) {
69 for (let i = size; i < this.size; i++) {
70 super.pop()
71 }
72 }
73 this.size = size
74 }
75
76 public empty (): boolean {
77 return this.length === 0
78 }
79
80 public full (): boolean {
81 return this.length === this.size
82 }
83
84 private checkSize (size: number): void {
85 if (!Number.isSafeInteger(size)) {
86 throw new TypeError(
87 `Invalid circular array size: ${size} is not a safe integer`
88 )
89 }
90 if (size < 0) {
91 throw new RangeError(`Invalid circular array size: ${size} < 0`)
92 }
93 }
94 }