-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: Add a Remove
method to PriorityQueue
#93925
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsBackground and motivationWhen we shipped the Even though the In the case of the Java, users can encode priority updates in the API Proposalnamespace System.Collections.Generic;
public partial class PriorityQueue<TElement, TPriority>
{
// Scans the heap for elements equal to the provided value and removes the first occurrence.
public bool Remove(TElement element, out TPriority priority, IEqualityComparer<TElement>? equalityComparer = null);
} API UsageIt's possible to use the above API to encode a public static void EnqueueOrUpdate<TElement, TPriority>(this PriorityQueue<TElement, TPriority> queue, TElement element, TPriority priority)
{
queue.Remove(element, out _);
queue.Enqueue(element, priority);
} Alternative DesignsNo response RisksIt could end up being used in production code. I expect this risk would be mitigated since we're not explicitly adding an "update" API, instead users need to invoke a
|
namespace System.Collections.Generic;
public partial class PriorityQueue<TElement, TPriority>
{
// Scans the heap for elements equal to the provided value and removes the first occurrence.
public bool Remove(
TElement element,
[MaybeNullWhen(false)] out TElement removedElement,
[MaybeNullWhen(false)] out TPriority priority,
IEqualityComparer<TElement>? equalityComparer = null);
} |
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsBackground and motivationWhen we shipped the Even though the In the case of the Java, users can encode priority updates in the API Proposalnamespace System.Collections.Generic;
public partial class PriorityQueue<TElement, TPriority>
{
// Scans the heap for elements equal to the provided value and removes the first occurrence.
public bool Remove(TElement element, out TPriority priority, IEqualityComparer<TElement>? equalityComparer = null);
} API UsageIt's possible to use the above API to encode a public static void EnqueueOrUpdate<TElement, TPriority>(this PriorityQueue<TElement, TPriority> queue, TElement element, TPriority priority)
{
queue.Remove(element, out _);
queue.Enqueue(element, priority);
} Alternative DesignsNo response RisksIt could end up being used in production code. I expect this risk would be mitigated since we're not explicitly adding an "update" API, instead users need to invoke a
|
Background and motivation
When we shipped the$\mathcal O(\log n)$ priority updates. This was due to priority updates not being supported without sacrificing overall performance and lack of widespread use in .NET codebases. We have a separate issue that tracks adding a separate heap type that does support priority updates.
PriorityQueue
class back in .NET 6 we made the explicit decision to not add support for efficient,Even though the$\mathcal O(n)$ priority updates. This can be useful in educational contexts or coding interviews, where the actual asymptotic performance is not mission critical and can be overlooked. Put differently, users have asked for this facility so that they can implement Dijkstra's algorithm in leetCode using C#.
PriorityQueue
type itself cannot be retrofitted to support efficient priority updates, it can be made to supportIn the case of the Java, users can encode priority updates in the
PriorityQueue
class by piggybacking on its built-in remove method.API Proposal
API Usage
It's possible to use the above API to encode a$\mathcal O(n)$ priority update as follows:
Alternative Designs
No response
Risks
It could end up being used in production code. I expect this risk would be mitigated since we're not explicitly adding an "update" API, instead users need to invoke a
Remove
API that will be explicitly documented as a linear-time scan operation.The text was updated successfully, but these errors were encountered: