Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for complex data to PriorityQueue #186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant