UNPKG

veffect

Version:

powerful TypeScript validation library built on the robust foundation of Effect combining exceptional type safety, high performance, and developer experience. Taking inspiration from Effect's functional principles, VEffect delivers a balanced approach tha

88 lines (87 loc) 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RingBuffer = void 0; var Chunk = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../Chunk.js")); var _Function = /*#__PURE__*/require("../Function.js"); var Option = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../Option.js")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** @internal */ class RingBuffer { capacity; array; size = 0; current = 0; constructor(capacity) { this.capacity = capacity; this.array = Array.from({ length: capacity }, _Function.constUndefined); } head() { return Option.fromNullable(this.array[this.current]); } lastOrNull() { if (this.size === 0) { return undefined; } const index = this.current === 0 ? this.array.length - 1 : this.current - 1; return this.array[index] ?? undefined; } put(value) { this.array[this.current] = value; this.increment(); } dropLast() { if (this.size > 0) { this.decrement(); this.array[this.current] = undefined; } } toChunk() { const begin = this.current - this.size; const newArray = begin < 0 ? [...this.array.slice(this.capacity + begin, this.capacity), ...this.array.slice(0, this.current)] : this.array.slice(begin, this.current); return Chunk.fromIterable(newArray); } increment() { if (this.size < this.capacity) { this.size += 1; } this.current = (this.current + 1) % this.capacity; } decrement() { this.size -= 1; if (this.current > 0) { this.current -= 1; } else { this.current = this.capacity - 1; } } } exports.RingBuffer = RingBuffer; //# sourceMappingURL=ringBuffer.js.map