diff --git a/README.md b/README.md index 293fefb..269c1ec 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A performant priority queue implementation using a Heap data structure. -# Contents +# Contents (v5) * [Install](#install) * [require](#require) * [import](#import) @@ -63,7 +63,7 @@ import { ### constructor #### PriorityQueue -The constructor requires a compare callback to compare between queue elements. compare works similar to javascript sort callback: returning a number less or equal 0, means do not swap. +The constructor requires a compare callback to compare between queue elements. compare works similar to javascript sort callback: returning a number less or equal 0, means do not swap. constructor also accepts a prop `from` as an initial array of elements that will be used without adding extra space, and heapified in case is not valid heap. ##### JS ```js @@ -77,6 +77,11 @@ const employeesQueue = new PriorityQueue({ return e1.rank < e2.rank ? 1 : -1; } }); + +const queueFromExistingList = new PriorityQueue({ + compare: (a, b) => a.id > b.id ? -1 : 1, + from: [{ id: 3 }, { id: 2 }, { id: 7 }, { id: 5 }], +}); ``` ##### TS @@ -98,6 +103,11 @@ const employeesQueue = new PriorityQueue({ return e1.rank < e2.rank ? 1 : -1; } }); + +const queueFromExistingList = new PriorityQueue<{ id: number }>({ + compare: (a: { id: number; }, b: { id: number; }) => a.id > b.id ? -1 : 1, + from: [{ id: 3 }, { id: 2 }, { id: 7 }, { id: 5 }], +}); ``` #### MinPriorityQueue/MaxPriorityQueue diff --git a/src/maxPriorityQueue.js b/src/maxPriorityQueue.js index 10d30d5..cd95873 100644 --- a/src/maxPriorityQueue.js +++ b/src/maxPriorityQueue.js @@ -14,7 +14,8 @@ class MaxPriorityQueue extends PriorityQueue { constructor(options) { super(options); if (!this._compare) { - this._heap = new MaxHeap(); + const from = options && options.from || []; + this._heap = new MaxHeap(from); } } diff --git a/src/minPriorityQueue.js b/src/minPriorityQueue.js index 238407a..9e2bd69 100644 --- a/src/minPriorityQueue.js +++ b/src/minPriorityQueue.js @@ -14,7 +14,8 @@ class MinPriorityQueue extends PriorityQueue { constructor(options) { super(options); if (!this._compare) { - this._heap = new MinHeap(); + const from = options && options.from || []; + this._heap = new MinHeap(from); } } diff --git a/src/priorityQueue.d.ts b/src/priorityQueue.d.ts index 4074e89..1c5eba9 100644 --- a/src/priorityQueue.d.ts +++ b/src/priorityQueue.d.ts @@ -1,6 +1,7 @@ export interface PriorityQueueOptions { priority?: (element: T) => number; compare?: (a: T, b: T) => number; + from?: T[]; } export interface PriorityQueueItem { diff --git a/src/priorityQueue.js b/src/priorityQueue.js index 3ce399b..876c9b0 100644 --- a/src/priorityQueue.js +++ b/src/priorityQueue.js @@ -15,13 +15,16 @@ class PriorityQueue { * @params {object} [options] */ constructor(options = {}) { - const { priority, compare } = options; + const { priority, compare, from } = options; if (compare) { if (typeof compare !== 'function') { throw new Error('.constructor expects a valid compare function'); } this._compare = compare; - this._heap = new CustomHeap(this._compare); + this._heap = new CustomHeap(this._compare, from || []); + if (from && from.length > 0 && !this._heap.isValid()) { + this._heap.fix(); + } } else { if (priority !== undefined && typeof priority !== 'function') { throw new Error('.constructor expects a valid priority function');