repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
build(deps-dev): bump @types/node
[poolifier.git]
/
src
/
circular-buffer.ts
diff --git
a/src/circular-buffer.ts
b/src/circular-buffer.ts
index 2975ef7797389b2cb370c0184de493c81fe85ed7..7d12069fe369b48c72bf592d9b46a8eae0294031 100644
(file)
--- a/
src/circular-buffer.ts
+++ b/
src/circular-buffer.ts
@@
-4,17
+4,15
@@
export const defaultBufferSize = 2048
/**
export const defaultBufferSize = 2048
/**
- * Circular buffer.
+ * Circular buffer
designed for positive numbers
.
*
*
- * @typeParam T - Type of buffer data.
* @internal
*/
* @internal
*/
-export class CircularBuffer
<T>
{
+export class CircularBuffer {
private readIdx: number
private writeIdx: number
private readIdx: number
private writeIdx: number
- private
items: Array<T | undefined>
+ private
readonly items: Float32Array
private readonly maxArrayIdx: number
private readonly maxArrayIdx: number
- /* Buffer number of elements */
public size: number
/**
public size: number
/**
@@
-27,7
+25,7
@@
export class CircularBuffer<T> {
this.writeIdx = 0
this.maxArrayIdx = size - 1
this.size = 0
this.writeIdx = 0
this.maxArrayIdx = size - 1
this.size = 0
- this.items = new
Array<T | undefined>(size
)
+ this.items = new
Float32Array(size).fill(-1
)
}
/**
}
/**
@@
-49,12
+47,12
@@
export class CircularBuffer<T> {
}
/**
}
/**
- * Puts
data
into buffer.
+ * Puts
number
into buffer.
*
*
- * @param
data - Data
to put 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
this.writeIdx = this.writeIdx === this.maxArrayIdx ? 0 : this.writeIdx + 1
if (this.size < this.items.length) {
++this.size
@@
-62,34
+60,39
@@
export class CircularBuffer<T> {
}
/**
}
/**
- * Gets
data
from buffer.
+ * Gets
number
from buffer.
*
*
- * @returns
Data
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
}
return
}
- this.items[this.readIdx] =
undefined
+ this.items[this.readIdx] =
-1
this.readIdx = this.readIdx === this.maxArrayIdx ? 0 : this.readIdx + 1
--this.size
this.readIdx = this.readIdx === this.maxArrayIdx ? 0 : this.readIdx + 1
--this.size
- return
data
+ return
number
}
/**
}
/**
- * Returns buffer as array.
+ * Returns buffer as
numbers'
array.
*
*
- * @returns
Array of buffer data
.
+ * @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(
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}'
is not an integer`
)
}
if (size < 0) {
)
}
if (size < 0) {