1 // Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 export const DEFAULT_CIRCULAR_ARRAY_SIZE
= 1024
6 * Array with a maximum length and shifting items when full.
8 export class CircularArray
<T
> extends Array<T
> {
11 constructor (size
: number = DEFAULT_CIRCULAR_ARRAY_SIZE
, ...items
: T
[]) {
15 if (arguments.length
> 1) {
20 public push (...items
: T
[]): number {
21 const length
= super.push(...items
)
22 if (length
> this.size
) {
23 super.splice(0, length
- this.size
)
28 public unshift (...items
: T
[]): number {
29 const length
= super.unshift(...items
)
30 if (length
> this.size
) {
31 super.splice(this.size
, items
.length
)
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
39 if (concatenatedCircularArray
.length
> concatenatedCircularArray
.size
) {
40 concatenatedCircularArray
.splice(
42 concatenatedCircularArray
.length
- concatenatedCircularArray
.size
45 return concatenatedCircularArray
48 public splice (start
: number, deleteCount
?: number, ...items
: T
[]): CircularArray
<T
> {
49 let itemsRemoved
: T
[] = []
50 if (arguments.length
>= 3 && deleteCount
!= null) {
51 itemsRemoved
= super.splice(start
, deleteCount
, ...items
)
52 if (this.length
> this.size
) {
53 const itemsOverflowing
= super.splice(0, this.length
- this.size
)
54 itemsRemoved
= new CircularArray
<T
>(
55 itemsRemoved
.length
+ itemsOverflowing
.length
,
60 } else if (arguments.length
=== 2) {
61 itemsRemoved
= super.splice(start
, deleteCount
)
63 itemsRemoved
= super.splice(start
)
65 return itemsRemoved
as CircularArray
<T
>
68 public resize (size
: number): void {
72 } else if (size
< this.size
) {
73 for (let i
= size
; i
< this.size
; i
++) {
80 public empty (): boolean {
81 return this.length
=== 0
84 public full (): boolean {
85 return this.length
=== this.size
88 private checkSize (size
: number): void {
89 if (!Number.isSafeInteger(size
)) {
90 throw new TypeError(`Invalid circular array size: ${size} is not a safe integer`)
93 throw new RangeError(`Invalid circular array size: ${size} < 0`)