UNPKG

@barchart/common-js

Version:
162 lines (156 loc) 4.38 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var EvictingList_exports = {}; __export(EvictingList_exports, { default: () => EvictingList }); module.exports = __toCommonJS(EvictingList_exports); var assert = __toESM(require("./../../lang/assert.js")); const empty = {}; class EvictingList { #capacity; #array; #head; /** * @param {number=} capacity - The maximum number of items the list can contain (defaults to ten). */ constructor(capacity) { assert.argumentIsOptional(capacity, "capacity", Number); this.#capacity = Math.max(capacity || 0, 0) || 10; this.#array = []; for (let i = 0; i < this.#capacity; i++) { this.#array[i] = empty; } this.#head = null; } /** * Adds an item to the list (possibly causing eviction, if the size of the * list exceeds the capacity). * * @public * @param {*} item */ add(item) { this.#array[this.#head = getNextIndex(this.#head, this.#capacity)] = item; } /** * Returns the first item in the list, throwing an error if the list is empty. * * @public * @returns {*} */ peek() { if (this.empty()) { throw new Error("EvictingList is empty"); } return this.#array[this.#head]; } /** * Returns true, if the list is empty; otherwise false. * * @public * @returns {boolean} */ empty() { return this.#head === null; } /** * The capacity of the list. * * @public * @returns {number} */ getCapacity() { return this.#capacity; } /** * Copies the items in the list to a new array. * * @returns {Array} */ toArray() { let returnRef = []; if (!this.empty()) { let current = this.#head; for (let i = 0; i < this.#capacity; i++) { const item = this.#array[current]; if (item === empty) { break; } returnRef.push(item); current = getPreviousIndex(current, this.#capacity); } } return returnRef; } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return "[EvictingList]"; } } const getNextIndex = (current, capacity) => { let returnVal; if (current === null) { returnVal = 0; } else { returnVal = current + 1; if (returnVal === capacity) { returnVal = 0; } } return returnVal; }; const getPreviousIndex = (current, capacity) => { let returnVal; if (current === null) { returnVal = 0; } else { returnVal = current - 1; if (returnVal < 0) { returnVal = capacity - 1; } } return returnVal; }; { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }