UNPKG

ts-postgres

Version:
112 lines 6.93 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _Queue_head, _Queue_tail, _Queue_capacityMask, _Queue_list; Object.defineProperty(exports, "__esModule", { value: true }); exports.Queue = void 0; /** A double-ended queue. All queue operations are `O(1)`. */ class Queue { constructor() { _Queue_head.set(this, 0); _Queue_tail.set(this, 0); _Queue_capacityMask.set(this, 0b11); _Queue_list.set(this, new Array(__classPrivateFieldGet(this, _Queue_capacityMask, "f") + 1)); } /** Returns the current number of elements in the queue. */ get length() { return __classPrivateFieldGet(this, _Queue_head, "f") <= __classPrivateFieldGet(this, _Queue_tail, "f") ? __classPrivateFieldGet(this, _Queue_tail, "f") - __classPrivateFieldGet(this, _Queue_head, "f") : __classPrivateFieldGet(this, _Queue_capacityMask, "f") + 1 - (__classPrivateFieldGet(this, _Queue_head, "f") - __classPrivateFieldGet(this, _Queue_tail, "f")); } /** Returns whether the deque is empty. */ get empty() { return __classPrivateFieldGet(this, _Queue_head, "f") === __classPrivateFieldGet(this, _Queue_tail, "f"); } /** Inserts item to first slot. Returns the new length of the deque. */ unshift(item) { const len = __classPrivateFieldGet(this, _Queue_list, "f").length; __classPrivateFieldSet(this, _Queue_head, (__classPrivateFieldGet(this, _Queue_head, "f") - 1 + len) & __classPrivateFieldGet(this, _Queue_capacityMask, "f"), "f"); __classPrivateFieldGet(this, _Queue_list, "f")[__classPrivateFieldGet(this, _Queue_head, "f")] = item; if (__classPrivateFieldGet(this, _Queue_tail, "f") === __classPrivateFieldGet(this, _Queue_head, "f")) this.growArray(); if (__classPrivateFieldGet(this, _Queue_head, "f") < __classPrivateFieldGet(this, _Queue_tail, "f")) return __classPrivateFieldGet(this, _Queue_tail, "f") - __classPrivateFieldGet(this, _Queue_head, "f"); return __classPrivateFieldGet(this, _Queue_capacityMask, "f") + 1 - (__classPrivateFieldGet(this, _Queue_head, "f") - __classPrivateFieldGet(this, _Queue_tail, "f")); } /** Removes and returns the first element. */ shift() { const item = this.shiftMaybe(); if (item !== undefined) return item; throw new Error('Queue is empty'); } /** Removes and returns the first element or undefined. */ shiftMaybe() { if (this.empty) return; const head = __classPrivateFieldGet(this, _Queue_head, "f"); const item = __classPrivateFieldGet(this, _Queue_list, "f")[head]; __classPrivateFieldGet(this, _Queue_list, "f")[head] = undefined; __classPrivateFieldSet(this, _Queue_head, (head + 1) & __classPrivateFieldGet(this, _Queue_capacityMask, "f"), "f"); if (head < 2 && __classPrivateFieldGet(this, _Queue_tail, "f") > 10000 && __classPrivateFieldGet(this, _Queue_tail, "f") <= __classPrivateFieldGet(this, _Queue_list, "f").length >>> 2) this.shrinkArray(); return item; } expect(expected) { const item = this.shift(); if (item === undefined || (expected !== undefined && expected !== item)) { throw new Error(`Unexpected item: ${item} !== ${expected}`); } return item; } /** Inserts item to the last slot. Returns the new length of the deque. */ push(item) { const tail = __classPrivateFieldGet(this, _Queue_tail, "f"); __classPrivateFieldGet(this, _Queue_list, "f")[tail] = item; __classPrivateFieldSet(this, _Queue_tail, (tail + 1) & __classPrivateFieldGet(this, _Queue_capacityMask, "f"), "f"); if (this.empty) this.growArray(); if (__classPrivateFieldGet(this, _Queue_head, "f") < __classPrivateFieldGet(this, _Queue_tail, "f")) return __classPrivateFieldGet(this, _Queue_tail, "f") - __classPrivateFieldGet(this, _Queue_head, "f"); return __classPrivateFieldGet(this, _Queue_capacityMask, "f") + 1 - (__classPrivateFieldGet(this, _Queue_head, "f") - __classPrivateFieldGet(this, _Queue_tail, "f")); } shrinkArray() { __classPrivateFieldGet(this, _Queue_list, "f").length >>>= 1; __classPrivateFieldSet(this, _Queue_capacityMask, __classPrivateFieldGet(this, _Queue_capacityMask, "f") >>> 1, "f"); } growArray() { // Perform rotate-left if necessary if (__classPrivateFieldGet(this, _Queue_head, "f") > 0) { // Copy existing data from head to end const deleted = __classPrivateFieldGet(this, _Queue_list, "f").splice(__classPrivateFieldGet(this, _Queue_head, "f")); // Then, plop all preceding elements after `deleted` deleted.push(...__classPrivateFieldGet(this, _Queue_list, "f")); // Shift pointers accordingly __classPrivateFieldSet(this, _Queue_tail, __classPrivateFieldGet(this, _Queue_tail, "f") - __classPrivateFieldGet(this, _Queue_head, "f"), "f"); __classPrivateFieldSet(this, _Queue_head, 0, "f"); // Discard old array __classPrivateFieldSet(this, _Queue_list, deleted, "f"); } // Head is at 0 and array is now full, // therefore safe to extend __classPrivateFieldSet(this, _Queue_tail, __classPrivateFieldGet(this, _Queue_list, "f").length, "f"); // Double the capacity __classPrivateFieldGet(this, _Queue_list, "f").length *= 2; __classPrivateFieldSet(this, _Queue_capacityMask, (__classPrivateFieldGet(this, _Queue_capacityMask, "f") << 1) | 1, "f"); } } exports.Queue = Queue; _Queue_head = new WeakMap(), _Queue_tail = new WeakMap(), _Queue_capacityMask = new WeakMap(), _Queue_list = new WeakMap(); //# sourceMappingURL=queue.js.map