| 1 | |
| 2 | export default class CircularArray<T> extends Array<T> { |
| 3 | public size: number; |
| 4 | private readonly MAXIMUM_CIRCULAR_ARRAY_SIZE = 2000; |
| 5 | |
| 6 | constructor(size?: number) { |
| 7 | super(); |
| 8 | this.size = size && size <= this.MAXIMUM_CIRCULAR_ARRAY_SIZE ? size : this.MAXIMUM_CIRCULAR_ARRAY_SIZE; |
| 9 | } |
| 10 | |
| 11 | push(...items: T[]): number { |
| 12 | if (this.length + items.length > this.size) { |
| 13 | super.splice(0, (this.length + items.length) - this.size); |
| 14 | } |
| 15 | return super.push(...items); |
| 16 | } |
| 17 | |
| 18 | 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); |
| 21 | } |
| 22 | return super.unshift(...items); |
| 23 | } |
| 24 | |
| 25 | concat(...items: (T|ConcatArray<T>)[]): T[] { |
| 26 | if (this.length + items.length > this.size) { |
| 27 | super.splice(0, (this.length + items.length) - this.size); |
| 28 | } |
| 29 | return super.concat(items as T[]); |
| 30 | } |
| 31 | |
| 32 | splice(start: number, deleteCount?: number, ...items: T[]): T[] { |
| 33 | this.push(...items); |
| 34 | return super.splice(start, deleteCount); |
| 35 | } |
| 36 | } |