Overview
A cache-contiguous circular ring buffer and dynamic vector using power-of-two bitmask wrap-around, geometric doubling/halving with hysteresis, and potential-function telemetry.
Guided Implementation Walkthrough (4 Progressive Steps)
Step 1: Power-of-2 Bitmask Physical Index Mapping
- Core Mechanical Concept: Two’s-Complement Modular Arithmetic via (head + i) & (capacity - 1)
- Target Module:
index.js(CircularDeque._physIndex) - Engineering Specification: Implement
_physIndex(logicalIdx)to map any logical offset0 <= i < sizeto its physical slot inthis._bufwithout integer division. Becausethis._capacityis strictly a power of two ($2^k$),this._mask = this._capacity - 1has its lowest $k$ bits set to1. Therefore(this._head + logicalIdx) & this._maskcomputes(head + i) mod capacityin 1 CPU cycle, even whenthis._head - 1is negative duringpushFront. - Verification Invariant: maps logical indices across wrap-around boundaries via bitmask
Step 2: Order-Preserving Ring Linearization on Resize
- Core Mechanical Concept: Unwrapping Split Ring Segments During Geometric Doubling
- Target Module:
index.js(CircularDeque._resize) - Engineering Specification: Implement
_resize(newCap)to allocate a new backing array of lengthnewCap, copy allthis._sizelive elements in logical order0 .. size - 1to indices0 .. size - 1of the new buffer, and reset_head = 0. Never copy raw physical indices0 .. cap - 1directly when_head !== 0, or the newly opened empty space will be spliced into the middle of the logical sequence. - Verification Invariant: linearizes wrapped elements into contiguous order on geometric doubling
Step 3: Amortized O(1) pushBack and pushFront
- Core Mechanical Concept: Circular Head/Tail Advancement & Potential Function Φ = 2n - m
- Target Module:
index.js(CircularDeque.pushBack / CircularDeque.pushFront) - Engineering Specification: Implement
pushBack(value)andpushFront(value). 1. Ifthis._size === this._capacity, callthis._resize(this._capacity * 2). 2. ForpushBack: writevalueat(this._head + this._size) & this._maskand increment_size. 3. ForpushFront: updatethis._head = (this._head - 1) & this._mask, writevalueatthis._head, and increment_size. - Verification Invariant: supports alternating pushFront and pushBack in amortized O(1) time
Step 4: popFront, popBack & Quarter-Load Hysteresis Shrinking
- Core Mechanical Concept: Preventing Thrashing at Capacity Boundaries via 1/4 Shrink Threshold
- Target Module:
index.js(CircularDeque.popFront / CircularDeque.popBack) - Engineering Specification: Implement
popFront()andpopBack()with GC-friendly slot clearing (undefined) and quarter-capacity shrinking. Halving capacity at 50% fullness causes $O(N)$ thrashing on alternatingpush/popsequences at the boundary. Only halvethis._capacitywhenthis._capacity > 8 && this._size <= (this._capacity >> 2). - Verification Invariant: pops from both ends and shrinks only at quarter load without thrashing
Asymptotic & Memory Layout Guarantees
- Category: Linear Structures & Memory
- Difficulty: Intermediate (~30 minutes)
- Key Architectural Highlights:
• Power-of-2 capacity invariant enabling single-cycle (head + i) & mask wrap-around
• Order-preserving geometric doubling (2x) and quarter-load shrinking (0.25x)
• Potential-function Φ = 2n - m instrumentation verifying O(1) amortized bounds - Tags & Primitives:
Circular Buffer,Amortized O(1),Bitmask Indexing,Dynamic Array,Cache Locality
Production Systems Context & Verification
This from-scratch implementation is engineered with zero external dependencies and deterministic verification harnesses. Every internal invariant—from pointer restoration and bitmask addressing to amortized potential bounds and tree height balance—is asserted directly in test.js. Use the interactive browser workbench above to complete the guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.