X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcircular-buffer.ts;h=5015483b290055e1a78e961c248024625800b6d6;hb=1352ca70c0b5c6b4ff3813b0c24354b5514b2796;hp=2975ef7797389b2cb370c0184de493c81fe85ed7;hpb=12c589b6bbb06466d2168e4867ff51c4b45d3bd3;p=poolifier.git diff --git a/src/circular-buffer.ts b/src/circular-buffer.ts index 2975ef77..5015483b 100644 --- a/src/circular-buffer.ts +++ b/src/circular-buffer.ts @@ -4,17 +4,14 @@ export const defaultBufferSize = 2048 /** - * Circular buffer. - * - * @typeParam T - Type of buffer data. + * Circular buffer designed for positive numbers. * @internal */ -export class CircularBuffer { +export class CircularBuffer { private readIdx: number private writeIdx: number - private items: Array + private readonly items: Float32Array private readonly maxArrayIdx: number - /* Buffer number of elements */ public size: number /** @@ -27,12 +24,11 @@ export class CircularBuffer { this.writeIdx = 0 this.maxArrayIdx = size - 1 this.size = 0 - this.items = new Array(size) + this.items = new Float32Array(size).fill(-1) } /** * Checks whether the buffer is empty. - * * @returns Whether the buffer is empty. */ public empty (): boolean { @@ -41,7 +37,6 @@ export class CircularBuffer { /** * Checks whether the buffer is full. - * * @returns Whether the buffer is full. */ public full (): boolean { @@ -49,12 +44,11 @@ export class CircularBuffer { } /** - * Puts data into buffer. - * - * @param data - Data to put into buffer. + * Puts number into buffer. + * @param number - Number to put into buffer. */ - public put (data: T): void { - this.items[this.writeIdx] = data + public put (number: number): void { + this.items[this.writeIdx] = number this.writeIdx = this.writeIdx === this.maxArrayIdx ? 0 : this.writeIdx + 1 if (this.size < this.items.length) { ++this.size @@ -62,38 +56,42 @@ export class CircularBuffer { } /** - * Gets data from buffer. - * - * @returns Data from buffer. + * Gets number from buffer. + * @returns Number from buffer. */ - public get (): T | undefined { - const data = this.items[this.readIdx] - if (data == null) { + public get (): number | undefined { + const number = this.items[this.readIdx] + if (number === -1) { return } - this.items[this.readIdx] = undefined + this.items[this.readIdx] = -1 this.readIdx = this.readIdx === this.maxArrayIdx ? 0 : this.readIdx + 1 --this.size - return data + return number } /** - * Returns buffer as array. - * - * @returns Array of buffer data. + * Returns buffer as numbers' array. + * @returns Numbers' array. */ - public toArray (): T[] { - return this.items.filter(item => item != null) as T[] + public toArray (): number[] { + return Array.from(this.items.filter(item => item !== -1)) } + /** + * Checks the buffer size. + * @param size - Buffer size. + */ private checkSize (size: number): void { if (!Number.isSafeInteger(size)) { throw new TypeError( - `Invalid circular buffer size: ${size} is not an integer` + `Invalid circular buffer size: '${size.toString()}' is not an integer` ) } if (size < 0) { - throw new RangeError(`Invalid circular buffer size: ${size} < 0`) + throw new RangeError( + `Invalid circular buffer size: ${size.toString()} < 0` + ) } } }