'use strict'; class SlidingWindow { _WINDOW_SIZE; _compare; _window; _front; _end; _size; _leftBoundary; _rightBoundary; constructor(props) { const { WINDOW_SIZE, startIndex = 0, compare } = props; this._WINDOW_SIZE = WINDOW_SIZE; this._compare = compare; this._window = new Array(WINDOW_SIZE); this._front = 0; this._end = -1; this._size = 0; this._leftBoundary = startIndex; this._rightBoundary = startIndex; } reset(options = {}) { const { startIndex = 0, WINDOW_SIZE = this._WINDOW_SIZE } = options; this._WINDOW_SIZE = WINDOW_SIZE; this._window.length = WINDOW_SIZE; this._front = 0; this._end = -1; this._size = 0; this._leftBoundary = startIndex; this._rightBoundary = startIndex; } forwardRightBoundary(steps = 1) { if (steps < 1) return; const { _WINDOW_SIZE, _window, _compare } = this; let { _rightBoundary, _front, _end, _size } = this; const nextRightBoundary = _rightBoundary + steps; const nextLeftBoundary = Math.max(this._leftBoundary, nextRightBoundary - _WINDOW_SIZE); if (_rightBoundary < nextLeftBoundary) _rightBoundary = nextLeftBoundary; while (_size > 0 && _window[_front] < nextLeftBoundary) { _front = this._forwardIndex(_front); _size -= 1; } for (; _rightBoundary < nextRightBoundary; ++_rightBoundary) { for (; _size > 0; --_size) { if (_compare(_window[_end], _rightBoundary) < 0) break; _end = this._backwardIndex(_end); } _end = this._forwardIndex(_end); _window[_end] = _rightBoundary; _size += 1; } this._front = _front; this._end = _end; this._size = _size; this._leftBoundary = nextLeftBoundary; this._rightBoundary = nextRightBoundary; } forwardLeftBoundary(steps = 1) { if (steps < 1) return; const nextLeftBoundary = this._leftBoundary + steps; const { _window } = this; while (this._size > 0 && _window[this._front] < nextLeftBoundary) { this._front = this._forwardIndex(this._front); this._size -= 1; } this._leftBoundary = nextLeftBoundary; } min() { return this._size > 0 ? this._window[this._front] : undefined; } _forwardIndex(index) { const nextIndex = index + 1; return nextIndex === this._WINDOW_SIZE ? 0 : nextIndex; } _backwardIndex(index) { const nextIndex = index - 1; return nextIndex < 0 ? this._WINDOW_SIZE - 1 : nextIndex; } } exports.SlidingWindow = SlidingWindow;