UNPKG

@js-sdsl/queue

Version:

javascript standard data structure library which benchmark against C++ STL

166 lines (155 loc) 5.52 kB
/*! * @js-sdsl/queue v4.4.0 * https://github.com/js-sdsl/js-sdsl * (c) 2021-present ZLY201 <zilongyao1366@gmail.com> * MIT license */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sdsl = {})); })(this, (function (exports) { 'use strict'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; } || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var Base = /** @class */function () { function Base() { /** * @description Container's size. * @internal */ this._length = 0; } Object.defineProperty(Base.prototype, "length", { /** * @returns The size of the container. * @example * const container = new Vector([1, 2]); * console.log(container.length); // 2 */ get: function () { return this._length; }, enumerable: false, configurable: true }); /** * @returns The size of the container. * @example * const container = new Vector([1, 2]); * console.log(container.size()); // 2 */ Base.prototype.size = function () { return this._length; }; /** * @returns Whether the container is empty. * @example * container.clear(); * console.log(container.empty()); // true */ Base.prototype.empty = function () { return this._length === 0; }; return Base; }(); /** @class */(function (_super) { __extends(Container, _super); function Container() { return _super !== null && _super.apply(this, arguments) || this; } return Container; })(Base); var Queue = /** @class */function (_super) { __extends(Queue, _super); function Queue(container) { if (container === void 0) { container = []; } var _this = _super.call(this) || this; /** * @internal */ _this._first = 0; /** * @internal */ _this._queue = []; var self = _this; container.forEach(function (el) { self.push(el); }); return _this; } Queue.prototype.clear = function () { this._queue = []; this._length = this._first = 0; }; /** * @description Inserts element to queue's end. * @param element - The element you want to push to the front. * @returns The container length after pushing. */ Queue.prototype.push = function (element) { var capacity = this._queue.length; if (this._first / capacity > 0.5 /* QUEUE_CONSTANT.ALLOCATE_SIGMA */ && this._first + this._length >= capacity && capacity > 4096 /* QUEUE_CONSTANT.MIN_ALLOCATE_SIZE */) { var length_1 = this._length; for (var i = 0; i < length_1; ++i) { this._queue[i] = this._queue[this._first + i]; } this._first = 0; this._queue[this._length] = element; } else this._queue[this._first + this._length] = element; return ++this._length; }; /** * @description Removes the first element. * @returns The element you popped. */ Queue.prototype.pop = function () { if (this._length === 0) return; var el = this._queue[this._first++]; this._length -= 1; return el; }; /** * @description Access the first element. * @returns The first element. */ Queue.prototype.front = function () { if (this._length === 0) return; return this._queue[this._first]; }; return Queue; }(Base); exports.Queue = Queue; Object.defineProperty(exports, '__esModule', { value: true }); }));