Skip to content

Conversation

@appleJax
Copy link
Contributor

This PR adds support for complex data to the PriorityQueue class.

Changes:

  • uses native JS Map data structure to store priorities. This way, objects, functions, or primitives can be used as keys. A plain JS Object coerces keys to strings, so different objects could all have the same key of [object Object]
  • allows constructing a new PriorityQueue with a custom compareValue function, so complex data can be compared

example usage:

const JOB1 = { type: 'job1' }; const JOB2 = { type: 'job2' }; const JOB3 = { type: 'job3' }; const compareByType = (a, b) => { if (a.type === b.type) { return 0; } return a.type < b.type ? -1 : 1; }; const priorityQueue = new PriorityQueue(compareByType); priorityQueue.add(JOB2, 2); priorityQueue.peek(); // returns JOB2  priorityQueue.add(JOB3, 3); priorityQueue.peek(); // returns JOB2  priorityQueue.add(JOB1, 1); priorityQueue.peek(); // returns JOB1 priorityQueue.changePriority(JOB2, 0); priorityQueue.peek(); // returns JOB2  const existingJobType = { type: 'job1' }; const newJobType = { type: 'job4' }; priorityQueue.hasValue(existingJobType); // returns true priorityQueue.hasValue(newJobType); // returns false
@appleJax appleJax force-pushed the fix/PriorityQueue/ComplexData branch from 52a2c21 to de57051 Compare August 30, 2018 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant