Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / circular-array.ts
CommitLineData
78099a15
JB
1// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
3const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024
4
5/**
4342a45e 6 * Array with a maximum length and shifting items when full.
78099a15
JB
7 */
8export 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
4888bc9c 20 /** @inheritDoc */
78099a15
JB
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
4888bc9c 29 /** @inheritDoc */
78099a15
JB
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
4888bc9c 38 /** @inheritDoc */
78099a15
JB
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
4888bc9c 53 /** @inheritDoc */
4077981d
JB
54 public splice (
55 start: number,
56 deleteCount?: number,
57 ...items: T[]
58 ): CircularArray<T> {
59 let itemsRemoved: T[] = []
1b6e07ba 60 if (arguments.length >= 3 && deleteCount != null) {
4077981d
JB
61 itemsRemoved = super.splice(start, deleteCount, ...items)
62 if (this.length > this.size) {
63 const itemsOverflowing = super.splice(0, this.length - this.size)
64 itemsRemoved = new CircularArray<T>(
65 itemsRemoved.length + itemsOverflowing.length,
66 ...itemsRemoved,
67 ...itemsOverflowing
68 )
69 }
78099a15
JB
70 } else if (arguments.length === 2) {
71 itemsRemoved = super.splice(start, deleteCount)
72 } else {
73 itemsRemoved = super.splice(start)
74 }
4077981d 75 return itemsRemoved as CircularArray<T>
78099a15
JB
76 }
77
78 public resize (size: number): void {
79 this.checkSize(size)
80 if (size === 0) {
81 this.length = 0
82 } else if (size < this.size) {
83 for (let i = size; i < this.size; i++) {
84 super.pop()
85 }
86 }
87 this.size = size
88 }
89
90 public empty (): boolean {
91 return this.length === 0
92 }
93
94 public full (): boolean {
95 return this.length === this.size
96 }
97
98 private checkSize (size: number): void {
99 if (!Number.isSafeInteger(size)) {
100 throw new TypeError(
101 `Invalid circular array size: ${size} is not a safe integer`
102 )
103 }
104 if (size < 0) {
105 throw new RangeError(`Invalid circular array size: ${size} < 0`)
106 }
107 }
108}