X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcircular-buffer.ts;h=5015483b290055e1a78e961c248024625800b6d6;hb=HEAD;hp=7d12069fe369b48c72bf592d9b46a8eae0294031;hpb=fcfc3353eb4053c02f64c80a14ae142d44388a71;p=poolifier.git diff --git a/src/circular-buffer.ts b/src/circular-buffer.ts index 7d12069f..803af129 100644 --- a/src/circular-buffer.ts +++ b/src/circular-buffer.ts @@ -5,14 +5,13 @@ export const defaultBufferSize = 2048 /** * Circular buffer designed for positive numbers. - * * @internal */ export class CircularBuffer { - private readIdx: number - private writeIdx: number private readonly items: Float32Array private readonly maxArrayIdx: number + private readIdx: number + private writeIdx: number public size: number /** @@ -28,9 +27,25 @@ export class CircularBuffer { this.items = new Float32Array(size).fill(-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.toString()}' is not an integer` + ) + } + if (size < 0) { + throw new RangeError( + `Invalid circular buffer size: ${size.toString()} < 0` + ) + } + } + /** * Checks whether the buffer is empty. - * * @returns Whether the buffer is empty. */ public empty (): boolean { @@ -39,29 +54,14 @@ export class CircularBuffer { /** * Checks whether the buffer is full. - * * @returns Whether the buffer is full. */ public full (): boolean { return this.size === this.items.length } - /** - * Puts number into buffer. - * - * @param number - Number to put into buffer. - */ - 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 - } - } - /** * Gets number from buffer. - * * @returns Number from buffer. */ public get (): number | undefined { @@ -76,27 +76,22 @@ export class CircularBuffer { } /** - * Returns buffer as numbers' array. - * - * @returns Numbers' array. + * Puts number into buffer. + * @param number - Number to put into buffer. */ - public toArray (): number[] { - return Array.from(this.items.filter(item => item !== -1)) + 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 + } } /** - * Checks the buffer size. - * - * @param size - Buffer size. + * Returns buffer as numbers' array. + * @returns Numbers' array. */ - private checkSize (size: number): void { - if (!Number.isSafeInteger(size)) { - throw new TypeError( - `Invalid circular buffer size: '${size}' is not an integer` - ) - } - if (size < 0) { - throw new RangeError(`Invalid circular buffer size: ${size} < 0`) - } + public toArray (): number[] { + return Array.from(this.items.filter(item => item !== -1)) } }