Commit | Line | Data |
---|---|---|
f12182ad | 1 | /** |
db89e3bc | 2 | * Default buffer size. |
f12182ad JB |
3 | */ |
4 | export const defaultBufferSize = 2048 | |
5 | ||
6 | /** | |
0cd1f28c | 7 | * Circular buffer designed for positive numbers. |
db89e3bc | 8 | * @internal |
f12182ad | 9 | */ |
0cd1f28c | 10 | export class CircularBuffer { |
fcfc3353 | 11 | private readonly items: Float32Array |
f12182ad | 12 | private readonly maxArrayIdx: number |
97231086 JB |
13 | private readIdx: number |
14 | private writeIdx: number | |
cf42a4cf | 15 | public size: number |
f12182ad JB |
16 | |
17 | /** | |
db89e3bc JB |
18 | * @param size - Buffer size. @defaultValue defaultBufferSize |
19 | * @returns CircularBuffer. | |
f12182ad JB |
20 | */ |
21 | constructor (size: number = defaultBufferSize) { | |
22 | this.checkSize(size) | |
23 | this.readIdx = 0 | |
24 | this.writeIdx = 0 | |
25 | this.maxArrayIdx = size - 1 | |
cf42a4cf | 26 | this.size = 0 |
0cd1f28c | 27 | this.items = new Float32Array(size).fill(-1) |
f12182ad JB |
28 | } |
29 | ||
97231086 JB |
30 | /** |
31 | * Checks the buffer size. | |
32 | * @param size - Buffer size. | |
33 | */ | |
34 | private checkSize (size: number): void { | |
35 | if (!Number.isSafeInteger(size)) { | |
36 | throw new TypeError( | |
37 | `Invalid circular buffer size: '${size.toString()}' is not an integer` | |
38 | ) | |
39 | } | |
40 | if (size < 0) { | |
41 | throw new RangeError( | |
42 | `Invalid circular buffer size: ${size.toString()} < 0` | |
43 | ) | |
44 | } | |
45 | } | |
46 | ||
cf42a4cf JB |
47 | /** |
48 | * Checks whether the buffer is empty. | |
cf42a4cf JB |
49 | * @returns Whether the buffer is empty. |
50 | */ | |
51 | public empty (): boolean { | |
52 | return this.size === 0 | |
53 | } | |
54 | ||
55 | /** | |
56 | * Checks whether the buffer is full. | |
cf42a4cf JB |
57 | * @returns Whether the buffer is full. |
58 | */ | |
59 | public full (): boolean { | |
60 | return this.size === this.items.length | |
61 | } | |
62 | ||
cf42a4cf | 63 | /** |
0cd1f28c | 64 | * Gets number from buffer. |
0cd1f28c | 65 | * @returns Number from buffer. |
cf42a4cf | 66 | */ |
0cd1f28c | 67 | public get (): number | undefined { |
f8d5d8fd JB |
68 | const number = this.items[this.readIdx] |
69 | if (number === -1) { | |
cf42a4cf JB |
70 | return |
71 | } | |
0cd1f28c | 72 | this.items[this.readIdx] = -1 |
cf42a4cf JB |
73 | this.readIdx = this.readIdx === this.maxArrayIdx ? 0 : this.readIdx + 1 |
74 | --this.size | |
f8d5d8fd | 75 | return number |
f12182ad JB |
76 | } |
77 | ||
78 | /** | |
97231086 JB |
79 | * Puts number into buffer. |
80 | * @param number - Number to put into buffer. | |
f12182ad | 81 | */ |
97231086 JB |
82 | public put (number: number): void { |
83 | this.items[this.writeIdx] = number | |
84 | this.writeIdx = this.writeIdx === this.maxArrayIdx ? 0 : this.writeIdx + 1 | |
85 | if (this.size < this.items.length) { | |
86 | ++this.size | |
87 | } | |
f12182ad JB |
88 | } |
89 | ||
fcfc3353 | 90 | /** |
97231086 JB |
91 | * Returns buffer as numbers' array. |
92 | * @returns Numbers' array. | |
fcfc3353 | 93 | */ |
97231086 JB |
94 | public toArray (): number[] { |
95 | return Array.from(this.items.filter(item => item !== -1)) | |
f12182ad JB |
96 | } |
97 | } |