Open simbleau opened 2 years ago
It's also true that #35 would provide a better way to do this, (and in one line: queue.clear_over(5)
) but I believe it would be a major convenience for developers.
Sorting (even using the heapsort) is O(n * log(n)), which is worse then O(n). If you just need to iterate without removing the elements, I think the best you can do is
pq.iter().filter(|(_, p)| p > priority)...
Similar to #35 , the reasoning is the same. Return an iter with elements above or under a priority.
Consider the following, which is illegal by rust's standards:
This is because you cannot mutably modify an object when you have immutable borrows (
obj
, in this case)Thus, the equivalent workaround in the same time complexity would be:
But in fact, this would infinitely block since peek_max() is never being changed.
This means the best you can do is O(n) because you are required to iterate over every element.
This could be done by sorting O(logn) and returning a slice over the sorted structure.
Proposed example would be: