php-ds / ext-ds

An extension providing efficient data structures for PHP 7
https://medium.com/p/9dda7af674cd
MIT License
2.11k stars 95 forks source link

[PriorityQueue] Custom comparator for priorities #121

Open enumag opened 6 years ago

enumag commented 6 years ago

I'd like to use the PriorityQueue but in my case the values I need to use for ordering are dates and I need the oldest one first. This means I can't use PriorityQueue because the order would be reversed.

In my opinion PriorityQueue should accept an optional comparator callback to decide the order.

rtheunissen commented 6 years ago

Could you use -timestamp for the priority?

enumag commented 6 years ago

Negative timestamp huh? Yeah, I guess that could work. It feels like a dirty workaround rather than proper solution though.

rtheunissen commented 6 years ago

It feels like a dirty workaround rather than proper solution though.

I agree. 2.0 will have a Heap with an optional comparator that would work here.

enumag commented 6 years ago

Would it be possible to have another similar internally-ordered structure with custom comparator and non-destructive iteration?

rtheunissen commented 5 years ago

Like a binary search tree? There is one on the way. :+1: