Commit | Line | Data |
---|---|---|
ef72d3f5 | 1 | export const DEFAULT_CIRCULAR_ARRAY_SIZE = 2000; |
edfb206c | 2 | |
a01c18a0 | 3 | /** Array with a maximum length shifting items when full. */ |
ef72d3f5 | 4 | export class CircularArray<T> extends Array<T> { |
365f6bc9 | 5 | public size: number; |
edfb206c | 6 | |
534434b0 | 7 | constructor(size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) { |
edfb206c | 8 | super(); |
534434b0 JB |
9 | this.checkSize(size); |
10 | this.size = size; | |
11 | if (arguments.length > 1) { | |
12 | this.push(...items); | |
13 | } | |
edfb206c JB |
14 | } |
15 | ||
d43a619b | 16 | public push(...items: T[]): number { |
534434b0 JB |
17 | const length = super.push(...items); |
18 | if (length > this.size) { | |
19 | super.splice(0, length - this.size); | |
edfb206c | 20 | } |
534434b0 | 21 | return this.length; |
edfb206c JB |
22 | } |
23 | ||
d43a619b | 24 | public unshift(...items: T[]): number { |
534434b0 JB |
25 | const length = super.unshift(...items); |
26 | if (length > this.size) { | |
27 | super.splice(this.size, items.length); | |
edfb206c | 28 | } |
35e15933 | 29 | return this.length; |
edfb206c | 30 | } |
c70ff58d | 31 | |
534434b0 JB |
32 | public concat(...items: (T | ConcatArray<T>)[]): CircularArray<T> { |
33 | const concatenatedCircularArray = super.concat( | |
34 | items as T[] | |
35 | ) as CircularArray<T>; | |
36 | concatenatedCircularArray.size = this.size; | |
37 | if (concatenatedCircularArray.length > concatenatedCircularArray.size) { | |
38 | concatenatedCircularArray.splice( | |
39 | 0, | |
9be37455 | 40 | concatenatedCircularArray.length - concatenatedCircularArray.size |
534434b0 | 41 | ); |
c70ff58d | 42 | } |
534434b0 | 43 | return concatenatedCircularArray; |
c70ff58d JB |
44 | } |
45 | ||
d43a619b | 46 | public splice(start: number, deleteCount?: number, ...items: T[]): T[] { |
534434b0 JB |
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 | |
51 | this.push(...items); | |
52 | } else if (arguments.length === 2) { | |
53 | itemsRemoved = super.splice(start, deleteCount); | |
54 | } else { | |
55 | itemsRemoved = super.splice(start); | |
56 | } | |
57 | return itemsRemoved; | |
c70ff58d | 58 | } |
d43a619b JB |
59 | |
60 | public resize(size: number): void { | |
534434b0 | 61 | this.checkSize(size); |
d43a619b JB |
62 | if (size === 0) { |
63 | this.length = 0; | |
534434b0 JB |
64 | } else if (size < this.size) { |
65 | for (let i = size; i < this.size; i++) { | |
66 | super.pop(); | |
67 | } | |
d43a619b JB |
68 | } |
69 | this.size = size; | |
70 | } | |
71 | ||
72 | public empty(): boolean { | |
73 | return this.length === 0; | |
74 | } | |
75 | ||
76 | public full(): boolean { | |
77 | return this.length === this.size; | |
78 | } | |
534434b0 JB |
79 | |
80 | private checkSize(size: number) { | |
81 | if (size < 0) { | |
82 | throw new RangeError('Invalid circular array size'); | |
83 | } | |
84 | } | |
edfb206c | 85 | } |