1 export const DEFAULT_CIRCULAR_ARRAY_SIZE
= 2000;
3 /** Array with a maximum length shifting items when full. */
4 export class CircularArray
<T
> extends Array<T
> {
7 constructor(size
: number = DEFAULT_CIRCULAR_ARRAY_SIZE
, ...items
: T
[]) {
11 if (arguments.length
> 1) {
16 public push(...items
: T
[]): number {
17 const length
= super.push(...items
);
18 if (length
> this.size
) {
19 super.splice(0, length
- this.size
);
24 public unshift(...items
: T
[]): number {
25 const length
= super.unshift(...items
);
26 if (length
> this.size
) {
27 super.splice(this.size
, items
.length
);
32 public concat(...items
: (T
| ConcatArray
<T
>)[]): CircularArray
<T
> {
33 const concatenatedCircularArray
= super.concat(
35 ) as CircularArray
<T
>;
36 concatenatedCircularArray
.size
= this.size
;
37 if (concatenatedCircularArray
.length
> concatenatedCircularArray
.size
) {
38 concatenatedCircularArray
.splice(
40 concatenatedCircularArray
.length
- concatenatedCircularArray
.size
43 return concatenatedCircularArray
;
46 public splice(start
: number, deleteCount
?: number, ...items
: T
[]): T
[] {
47 let itemsRemoved
: T
[];
48 if (arguments.length
>= 3 && typeof deleteCount
!== 'undefined') {
49 itemsRemoved
= super.splice(start
, deleteCount
);
50 // FIXME: that makes the items insert not in place
52 } else if (arguments.length
=== 2) {
53 itemsRemoved
= super.splice(start
, deleteCount
);
55 itemsRemoved
= super.splice(start
);
60 public resize(size
: number): void {
64 } else if (size
< this.size
) {
65 for (let i
= size
; i
< this.size
; i
++) {
72 public empty(): boolean {
73 return this.length
=== 0;
76 public full(): boolean {
77 return this.length
=== this.size
;
80 private checkSize(size
: number) {
82 throw new RangeError('Invalid circular array size');