UNPKG

@azure/cosmos

Version:
91 lines (90 loc) 3.17 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 fixedSizePriorityQueue_exports = {}; __export(fixedSizePriorityQueue_exports, { FixedSizePriorityQueue: () => FixedSizePriorityQueue }); module.exports = __toCommonJS(fixedSizePriorityQueue_exports); var import_priorityqueuejs = __toESM(require("priorityqueuejs")); class FixedSizePriorityQueue { pq; // The compare function should return a positive number if a is greater than b, a negative number if a is less than b, and 0 if a is equal to b. compareFn; pqMaxSize; constructor(compareFn, pqMaxSize) { this.compareFn = compareFn; this.pq = new import_priorityqueuejs.default(this.compareFn); this.pqMaxSize = pqMaxSize; } enqueue(item) { if (this.pq.size() < this.pqMaxSize) { this.pq.enq(item); } else { const topItem = this.pq.peek(); if (this.compareFn(topItem, item) > 0) { this.pq.deq(); this.pq.enq(item); } } } dequeue() { return this.pq.deq(); } size() { return this.pq.size(); } isEmpty() { return this.pq.isEmpty(); } peek() { return this.pq.peek(); } getTopElements() { const elements = []; while (!this.pq.isEmpty()) { elements.unshift(this.pq.deq()); } return elements; } // Create a new instance of FixedSizePriorityQueue with a reversed compare function and the same maximum size. // Enqueue all elements from the current priority queue into the reverse priority queue. reverse() { const reversePQ = new FixedSizePriorityQueue( (a, b) => -this.compareFn(a, b), this.pqMaxSize ); while (!this.pq.isEmpty()) { reversePQ.enqueue(this.pq.deq()); } return reversePQ; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FixedSizePriorityQueue });