MCPcopy Create free account
hub / github.com/datastructures-js/priority-queue / MinPriorityQueue

Class MinPriorityQueue

src/minPriorityQueue.js:12–54  ·  view source on GitHub ↗

* @class MinPriorityQueue * @extends PriorityQueue

Source from the content-addressed store, hash-verified

10 * @extends PriorityQueue
11 */
12class MinPriorityQueue extends PriorityQueue {
13 constructor(options, values) {
14 // Handle legacy options format ({ compare: fn })
15 if (options && typeof options === 'object' && typeof options.compare === 'function') {
16 const compareFunction = (a, b) => options.compare(a, b) <= 0 ? -1 : 1;
17 super(compareFunction, values);
18 } else {
19 // Current format (direct compare function)
20 const getCompareValue = options;
21 if (getCompareValue && typeof getCompareValue !== 'function') {
22 throw new Error('MinPriorityQueue constructor requires a callback for object values');
23 }
24 // Create a MinHeap-compatible compare function
25 const compare = (a, b) => {
26 const aVal = typeof getCompareValue === 'function' ? getCompareValue(a) : a;
27 const bVal = typeof getCompareValue === 'function' ? getCompareValue(b) : b;
28 return aVal <= bVal ? -1 : 1;
29 };
30 super(compare, values);
31 }
32 }
33
34 /**
35 * Adds a value to the queue
36 * @public
37 * @param {number|string|object} value
38 * @returns {MinPriorityQueue}
39 */
40 enqueue(value) {
41 super.enqueue(value);
42 return this;
43 }
44
45 /**
46 * Adds a value to the queue
47 * @public
48 * @param {number|string|object} value
49 * @returns {MinPriorityQueue}
50 */
51 push(value) {
52 return this.enqueue(value);
53 }
54}
55
56/**
57 * Creates a priority queue from an existing array

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected