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

A priority queue should guarantee order #129

Open
mmomtchev opened this issue Jan 19, 2023 · 1 comment
Open

A priority queue should guarantee order #129

mmomtchev opened this issue Jan 19, 2023 · 1 comment

Comments

@mmomtchev
Copy link

PriorityQueue doesn't seem to guarantee order:

import { PriorityQueue } from 'typescript-collections';

interface Obj {
  prio: number;
  id: string;
}

const q = new PriorityQueue((a: Obj, b: Obj) => a.prio - b.prio);

q.enqueue({ prio: 0, id: 'first' });
q.enqueue({ prio: 10, id: 'prio' });
q.enqueue({ prio: 0, id: 'another 1' });
q.enqueue({ prio: 0, id: 'another 2' });
q.enqueue({ prio: 0, id: 'last' });

let o: Obj | undefined;
while (o = q.dequeue()) {
  console.log(o.id);
}

outputs:

prio
last
another 2
another 1
first

(in this example the order is perfectly reversed but this won't be the case if enqueue/dequeue are interleaved)

@baloian
Copy link

baloian commented Dec 8, 2024

@mmomtchev I implemented a new version of this lib with modern approach. Feel free to use it. See: https://github.com/baloian/typescript-ds-lib/tree/master

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

No branches or pull requests

2 participants