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

Provide a "real" priority queue abstraction? #56

Open
fingolfin opened this issue Aug 11, 2017 · 3 comments
Open

Provide a "real" priority queue abstraction? #56

fingolfin opened this issue Aug 11, 2017 · 3 comments

Comments

@fingolfin
Copy link
Member

fingolfin commented Aug 11, 2017

A "real" priority queue might have a Push method like this:

  function(prioqueue, priority, obj)
     Push(prioqueue!.heap, [priority, obj]);
  end;

where prioqueue!.heap is a heap with less-function is {x,y} -> x[1] < y[1].
The Pop method then would basically do

 function(prioqueue)
   local x;
   x:= Pop(prioqueue!.heap);
   if x = fail then return fail; fi;
   return x[2];
 end;

The constructor could optionally take a custom comparison function, I guess, and also optionally a heap constructor (if none given, default to e.g. binary heap)

@stevelinton
Copy link
Contributor

If you require that the objects in the queue support < or promise never to push two objects with the same priority then the underlying heap actually has default isLess which is nice for performance.

@fingolfin
Copy link
Member Author

@stevelinton Good point. Of course in that case, there is almost no reason left to not use a heap directly ;-). But some people will appreciate the extra sugar coating.

BTW, of course we could also add another special case to the heap implementations, to recognize {x,y} -> x[1] < y[1] (or rather: we'd define a function doing that, perhaps on the C-level, and add a special case for when that function is used).

@stevelinton
Copy link
Contributor

If we did that we should probably do the same thing in Position so that PositionFirstComponent could eventually go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants