Diqueue
A performant updatable priority queue which can pop AND shift.
Install
$ npm install --save diqueue
Usage
var DiQueue = ; // An empty priority queuevar q = ; q;q;q; q; // returns 12q; // returns 88 // Initialized with datavar qData = 88 12 23 45 56; qDatalength; // returns 5qData; // returns 12qDatalength; // returns 4qData; // returns 88qDatalength; // returns 3qData; // returns 23qDatalength; // returns 3 (unchanged)qData; // returns 56qDatalength; // returns 3 (unchanged) // Initialized with custom comparatorvar qComp = 88 12 23 45 56 { return a < b ? a : b;}; qComp; // returns 88qComp; // returns 12 // Initialized with custom objectsvar qObj = name: 'A' lag: 54 name: 'B' lag: 22 name: 'C' lag: 37 { return alag > blag ? a : b;}; qObj // returns object { name: 'B', lag: 22 }qObj // returns object { name: 'A', lag: 54 } // Initialized an updatable priority queue with a custom keyvar qObj2 = name: 'A' lag: 54 name: 'B' lag: 22 name: 'C' lag: 37 name: 'D' lag: 15 { return alag > blag ? a : b;} 'name'; qObj2;qObj2; qObj2; // returns object { name: 'A', lag: 12 }qObj2; // returns object { name: 'B', lag: 22 }qObj2; // returns object { name: 'C', lag: 37 }
API
constructor(data, compareFn, identityKey)
data
- Optional
- Type:
Array
- Default:
[]
The array of items to insert into the priority queue.
compareFn
- Optional
- Type:
Function
- Default:
(a, b) => { return a > b ? a : b; }
A comparator function to order the elements in the queue.
identityKey
- Optional
- Type:
String
- Default:
id
For an updatable queue, the key which is used to identify the object to update.
push(data)
data
- Required
- Type:
Object
The item to insert into the priority queue.
remove(idValue)
idValue
- Required
- Type:
Any
The value used to match the object, which will be removed.
update(idValue, updatedItem)
idValue
- Required
- Type:
Any
The value used to match the object, which will be updated.
updatedItem
- Required
- Type:
Object
The new item which will replace the previous item that matched the given idValue.
License
MIT © 2017, Muaz Siddiqui
Inspired by:
js-priority-queue by Adam Hooper