1 const DEFAULT_CIRCULAR_ARRAY_SIZE
= 2000;
3 export default class CircularArray
<T
> extends Array<T
> {
6 constructor(size
: number = DEFAULT_CIRCULAR_ARRAY_SIZE
, ...items
: T
[]) {
10 if (arguments.length
> 1) {
15 public push(...items
: T
[]): number {
16 const length
= super.push(...items
);
17 if (length
> this.size
) {
18 super.splice(0, length
- this.size
);
23 public unshift(...items
: T
[]): number {
24 const length
= super.unshift(...items
);
25 if (length
> this.size
) {
26 super.splice(this.size
, items
.length
);
31 public concat(...items
: (T
| ConcatArray
<T
>)[]): CircularArray
<T
> {
32 const concatenatedCircularArray
= super.concat(
34 ) as CircularArray
<T
>;
35 concatenatedCircularArray
.size
= this.size
;
36 if (concatenatedCircularArray
.length
> concatenatedCircularArray
.size
) {
37 concatenatedCircularArray
.splice(
39 concatenatedCircularArray
.length
- this.size
42 return concatenatedCircularArray
;
45 public splice(start
: number, deleteCount
?: number, ...items
: T
[]): T
[] {
46 let itemsRemoved
: T
[];
47 if (arguments.length
>= 3 && typeof deleteCount
!== 'undefined') {
48 itemsRemoved
= super.splice(start
, deleteCount
);
49 // FIXME: that makes the items insert not in place
51 } else if (arguments.length
=== 2) {
52 itemsRemoved
= super.splice(start
, deleteCount
);
54 itemsRemoved
= super.splice(start
);
59 public resize(size
: number): void {
63 } else if (size
< this.size
) {
64 for (let i
= size
; i
< this.size
; i
++) {
71 public empty(): boolean {
72 return this.length
=== 0;
75 public full(): boolean {
76 return this.length
=== this.size
;
79 private checkSize(size
: number) {
81 throw new RangeError('Invalid circular array size');