|
| 1 | +/** Options for configuring a ring buffer. */ |
| 2 | +export interface RingBufferOptions { |
| 3 | + /** Hard bound: pushing onto a full buffer evicts from the opposite end (keep-last-N semantics). Omit for an unbounded, growable buffer. */ |
| 4 | + capacity?: number; |
| 5 | + /** Initial slot allocation hint for the growable mode (default 16; rounded up to a power of two). */ |
| 6 | + initialCapacity?: number; |
| 7 | +} |
| 8 | + |
| 9 | +/** |
| 10 | + * Array-backed double-ended queue on a circular buffer: contiguous storage, |
| 11 | + * O(1) push/pop/peek at both ends, O(1) random access via `at()`, and no |
| 12 | + * allocation at steady state. Shares the `Deque` API (including the Array-parity |
| 13 | + * aliases `push`/`pop`/`shift`/`unshift`), so the two are drop-in replacements |
| 14 | + * for each other; the list-backed `Deque` keeps stable node identity, the ring |
| 15 | + * buffer wins on throughput and memory locality. |
| 16 | + * |
| 17 | + * With `{capacity}` the buffer is bounded: pushing onto a full buffer evicts the |
| 18 | + * element at the opposite end — a "keep the last N" sliding window. |
| 19 | + */ |
| 20 | +export class RingBuffer<V = unknown> { |
| 21 | + /** Hard bound, or `Infinity` when growable. */ |
| 22 | + capacity: number; |
| 23 | + /** The backing array (a power-of-two ring). */ |
| 24 | + array: (V | undefined)[]; |
| 25 | + /** Index of the front element in `array`. */ |
| 26 | + head: number; |
| 27 | + /** Number of elements. */ |
| 28 | + size: number; |
| 29 | + |
| 30 | + /** @param options - Capacity semantics. */ |
| 31 | + constructor(options?: RingBufferOptions); |
| 32 | + |
| 33 | + /** Whether the buffer has no elements. */ |
| 34 | + get isEmpty(): boolean; |
| 35 | + |
| 36 | + /** Whether the buffer reached its `capacity` (always `false` when growable). */ |
| 37 | + get isFull(): boolean; |
| 38 | + |
| 39 | + /** The front element without removing it, or `undefined` if empty. */ |
| 40 | + get front(): V | undefined; |
| 41 | + |
| 42 | + /** The back element without removing it, or `undefined` if empty. */ |
| 43 | + get back(): V | undefined; |
| 44 | + |
| 45 | + /** The front element without removing it, or `undefined` if empty. */ |
| 46 | + peekFront(): V | undefined; |
| 47 | + |
| 48 | + /** The back element without removing it, or `undefined` if empty. */ |
| 49 | + peekBack(): V | undefined; |
| 50 | + |
| 51 | + /** |
| 52 | + * Random access by position in O(1). Negative indices count from the back |
| 53 | + * (like `Array.prototype.at`). |
| 54 | + * @param index - Position from the front (or from the back when negative). |
| 55 | + * @returns The element, or `undefined` when out of range. |
| 56 | + */ |
| 57 | + at(index: number): V | undefined; |
| 58 | + |
| 59 | + /** |
| 60 | + * Add a value to the front. Evicts the back element when bounded and full. |
| 61 | + * @param value - Value to add. |
| 62 | + * @returns `this` for chaining. |
| 63 | + */ |
| 64 | + pushFront(value: V): this; |
| 65 | + |
| 66 | + /** Alias for {@link pushFront}. */ |
| 67 | + unshift(value: V): this; |
| 68 | + |
| 69 | + /** Alias for {@link pushFront}. */ |
| 70 | + addFront(value: V): this; |
| 71 | + |
| 72 | + /** |
| 73 | + * Add a value to the back. Evicts the front element when bounded and full. |
| 74 | + * @param value - Value to add. |
| 75 | + * @returns `this` for chaining. |
| 76 | + */ |
| 77 | + pushBack(value: V): this; |
| 78 | + |
| 79 | + /** Alias for {@link pushBack}. */ |
| 80 | + push(value: V): this; |
| 81 | + |
| 82 | + /** Alias for {@link pushBack}. */ |
| 83 | + add(value: V): this; |
| 84 | + |
| 85 | + /** Alias for {@link pushBack}. */ |
| 86 | + addBack(value: V): this; |
| 87 | + |
| 88 | + /** |
| 89 | + * Remove and return the front value. |
| 90 | + * @returns The front value, or `undefined` if empty. |
| 91 | + */ |
| 92 | + popFront(): V | undefined; |
| 93 | + |
| 94 | + /** Alias for {@link popFront}. */ |
| 95 | + shift(): V | undefined; |
| 96 | + |
| 97 | + /** Alias for {@link popFront}. */ |
| 98 | + removeFront(): V | undefined; |
| 99 | + |
| 100 | + /** |
| 101 | + * Remove and return the back value. |
| 102 | + * @returns The back value, or `undefined` if empty. |
| 103 | + */ |
| 104 | + popBack(): V | undefined; |
| 105 | + |
| 106 | + /** Alias for {@link popBack}. */ |
| 107 | + pop(): V | undefined; |
| 108 | + |
| 109 | + /** Alias for {@link popBack}. */ |
| 110 | + removeBack(): V | undefined; |
| 111 | + |
| 112 | + /** |
| 113 | + * Add multiple values to the front (in iteration order, so they end up reversed). |
| 114 | + * @param values - Iterable of values. |
| 115 | + * @returns `this` for chaining. |
| 116 | + */ |
| 117 | + pushValuesFront(values: Iterable<V>): this; |
| 118 | + |
| 119 | + /** |
| 120 | + * Add multiple values to the back. |
| 121 | + * @param values - Iterable of values. |
| 122 | + * @returns `this` for chaining. |
| 123 | + */ |
| 124 | + pushValuesBack(values: Iterable<V>): this; |
| 125 | + |
| 126 | + /** |
| 127 | + * Rotate: positive `n` moves `n` elements from the back to the front (Python |
| 128 | + * `deque.rotate` semantics), negative from the front to the back. O(1) when the |
| 129 | + * ring is physically full (a head shift), otherwise at most `size / 2` moves. |
| 130 | + * @param n - Number of positions (default 1). |
| 131 | + * @returns `this` for chaining. |
| 132 | + */ |
| 133 | + rotate(n?: number): this; |
| 134 | + |
| 135 | + /** |
| 136 | + * Remove all elements (keeps the current allocation). |
| 137 | + * @returns `this` for chaining. |
| 138 | + */ |
| 139 | + clear(): this; |
| 140 | + |
| 141 | + /** |
| 142 | + * Grow the backing array (doubling). Called automatically; exposed for pre-sizing. |
| 143 | + * @returns `this` for chaining. |
| 144 | + */ |
| 145 | + grow(): this; |
| 146 | + |
| 147 | + /** Iterate over values from front to back. */ |
| 148 | + [Symbol.iterator](): IterableIterator<V>; |
| 149 | + |
| 150 | + /** |
| 151 | + * Iterate over values from back to front. |
| 152 | + * @returns An iterable of values. |
| 153 | + */ |
| 154 | + getReverseIterator(): Iterable<V>; |
| 155 | + |
| 156 | + /** |
| 157 | + * Build a RingBuffer from an iterable. |
| 158 | + * @param values - Values to add at the back. |
| 159 | + * @param options - Capacity semantics. |
| 160 | + * @returns A new RingBuffer. |
| 161 | + */ |
| 162 | + static from<V = unknown>(values: Iterable<V>, options?: RingBufferOptions): RingBuffer<V>; |
| 163 | +} |
| 164 | + |
| 165 | +export default RingBuffer; |
0 commit comments