UNPKG

priority-queue-with-custom-comparator

Version:

Priority queue data structure where you are able to set your own compare function.

181 lines (180 loc) 9.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const queue_1 = __importDefault(require("../src/queue")); const test_helper_1 = require("./test.helper"); test('initial state (created without initialElements) and some added', () => { const numberPriorityQueue = new queue_1.default({ comparator: test_helper_1.defaultMaxComparator, initialElements: [185, 2, 6, 58, -2, 201, -9, -1, 6, 5], }); let topOfQueue; expect(numberPriorityQueue.has(5)).toBe(true); expect(numberPriorityQueue.has(2)).toBe(true); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(true); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(true); expect(numberPriorityQueue.has(185)).toBe(true); expect(numberPriorityQueue.has(201)).toBe(true); expect(numberPriorityQueue.values().toString()).toBe([201, 58, 185, 6, 5, 6, -9, -1, 2, -2].toString()); expect(numberPriorityQueue.size()).toBe(10); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(201); expect(numberPriorityQueue.has(5)).toBe(true); expect(numberPriorityQueue.has(2)).toBe(true); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(true); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(true); expect(numberPriorityQueue.has(185)).toBe(true); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([185, 58, 6, 6, 5, -2, -9, -1, 2].toString()); expect(numberPriorityQueue.size()).toBe(9); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(185); expect(numberPriorityQueue.has(5)).toBe(true); expect(numberPriorityQueue.has(2)).toBe(true); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(true); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(true); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([58, 6, 6, 2, 5, -2, -9, -1].toString()); expect(numberPriorityQueue.size()).toBe(8); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(58); expect(numberPriorityQueue.has(5)).toBe(true); expect(numberPriorityQueue.has(2)).toBe(true); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(true); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([6, 5, 6, 2, -1, -2, -9].toString()); expect(numberPriorityQueue.size()).toBe(7); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(6); expect(numberPriorityQueue.has(5)).toBe(true); expect(numberPriorityQueue.has(2)).toBe(true); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(true); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([6, 5, -2, 2, -1, -9].toString()); expect(numberPriorityQueue.size()).toBe(6); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(6); expect(numberPriorityQueue.has(5)).toBe(true); expect(numberPriorityQueue.has(2)).toBe(true); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(false); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([5, 2, -2, -9, -1].toString()); expect(numberPriorityQueue.size()).toBe(5); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(5); expect(numberPriorityQueue.has(5)).toBe(false); expect(numberPriorityQueue.has(2)).toBe(true); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(false); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([2, -1, -2, -9].toString()); expect(numberPriorityQueue.size()).toBe(4); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(2); expect(numberPriorityQueue.has(5)).toBe(false); expect(numberPriorityQueue.has(2)).toBe(false); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(true); expect(numberPriorityQueue.has(6)).toBe(false); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([-1, -9, -2].toString()); expect(numberPriorityQueue.size()).toBe(3); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(-1); expect(numberPriorityQueue.has(5)).toBe(false); expect(numberPriorityQueue.has(2)).toBe(false); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(false); expect(numberPriorityQueue.has(6)).toBe(false); expect(numberPriorityQueue.has(-2)).toBe(true); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([-2, -9].toString()); expect(numberPriorityQueue.size()).toBe(2); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(-2); expect(numberPriorityQueue.has(5)).toBe(false); expect(numberPriorityQueue.has(2)).toBe(false); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(false); expect(numberPriorityQueue.has(6)).toBe(false); expect(numberPriorityQueue.has(-2)).toBe(false); expect(numberPriorityQueue.has(-9)).toBe(true); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([-9].toString()); expect(numberPriorityQueue.size()).toBe(1); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBe(-9); expect(numberPriorityQueue.has(5)).toBe(false); expect(numberPriorityQueue.has(2)).toBe(false); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(false); expect(numberPriorityQueue.has(6)).toBe(false); expect(numberPriorityQueue.has(-2)).toBe(false); expect(numberPriorityQueue.has(-9)).toBe(false); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([].toString()); expect(numberPriorityQueue.size()).toBe(0); topOfQueue = numberPriorityQueue.pop(); expect(topOfQueue).toBeUndefined(); expect(numberPriorityQueue.has(5)).toBe(false); expect(numberPriorityQueue.has(2)).toBe(false); expect(numberPriorityQueue.has(0)).toBe(false); expect(numberPriorityQueue.has(-1)).toBe(false); expect(numberPriorityQueue.has(6)).toBe(false); expect(numberPriorityQueue.has(-2)).toBe(false); expect(numberPriorityQueue.has(-9)).toBe(false); expect(numberPriorityQueue.has(58)).toBe(false); expect(numberPriorityQueue.has(185)).toBe(false); expect(numberPriorityQueue.has(201)).toBe(false); expect(numberPriorityQueue.values().toString()).toBe([].toString()); expect(numberPriorityQueue.size()).toBe(0); });