2 export default class CircularArray
<T
> extends Array<T
> {
4 private readonly defaultCircularArraySize
= 2000;
6 constructor(size
?: number) {
8 this.size
= size
?? this.defaultCircularArraySize
;
11 public push(...items
: T
[]): number {
12 if (this.length
+ items
.length
> this.size
) {
13 super.splice(0, (this.length
+ items
.length
) - this.size
);
15 return super.push(...items
);
18 public unshift(...items
: T
[]): number {
19 if (this.length
+ items
.length
> this.size
) {
20 super.splice(this.size
- items
.length
, (this.length
+ items
.length
) - this.size
);
22 return super.unshift(...items
);
25 public concat(...items
: (T
| ConcatArray
<T
>)[]): T
[] {
26 if (this.length
+ items
.length
> this.size
) {
27 super.splice(0, (this.length
+ items
.length
) - this.size
);
29 return super.concat(items
as T
[]);
32 public splice(start
: number, deleteCount
?: number, ...items
: T
[]): T
[] {
34 return super.splice(start
, deleteCount
);
37 public resize(size
: number): void {
40 'circular array size does not allow negative values.'
45 } else if (size
!== this.size
) {
51 public empty(): boolean {
52 return this.length
=== 0;
55 public full(): boolean {
56 return this.length
=== this.size
;