Class ScyllaPriorityQueueItemComparer<T>
A bridge comparer that adapts IScyllaPriorityQueueItem to the IComparer<T> interface required by ScyllaPriorityQueue<T>, ordering elements by their QueuePriority integer value.
Implements
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaPriorityQueueItemComparer<T> : IComparer<T> where T : IScyllaPriorityQueueItem
Type Parameters
| Name | Description |
|---|---|
| T | The item type to compare. Must implement IScyllaPriorityQueueItem so that QueuePriority is accessible for comparison. Both class and struct types are supported. |
Remarks
This comparer eliminates the need to write a custom IComparer<T> implementation for the common case where an item already carries an integer priority via IScyllaPriorityQueueItem. Use the Default singleton to avoid unnecessary allocations.
The ordering contract of this comparer is consistent with CompareTo(int): a negative result means the left item has a lower priority number, which the heap interprets as higher priority in Min mode. For Max mode, ScyllaPriorityQueue<T> internally flips the operands so that no change to this comparer is required.
Tie-breaking when two items share the same QueuePriority value is unspecified. Compare(T, T) returns zero for equal priorities, and the heap makes no guarantees about the relative dequeue order of such items.
Typical usage with a ScyllaPriorityQueue<T>:
var pq = new ScyllaPriorityQueue<MyItem>(
order: ScyllaPriorityQueueOrder.Max,
comparer: ScyllaPriorityQueueItemComparer<MyItem>.Default);
Or equivalently using the fluent builder:
var pq = ScyllaPriorityQueue<MyItem>.CreateBuilder()
.WithCapacity(64)
.MaxHeap()
.WithComparer(ScyllaPriorityQueueItemComparer<MyItem>.Default)
.Build();
Fields
Default
Gets the shared singleton instance of ScyllaPriorityQueueItemComparer<T> for the
given type argument T.
Declaration
public static readonly ScyllaPriorityQueueItemComparer<T> Default
Field Value
| Type | Description |
|---|---|
| ScyllaPriorityQueueItemComparer<T> | A stateless, allocation-free singleton comparer instance. Because this comparer holds no mutable state, it is safe to share across multiple ScyllaPriorityQueue<T> instances and across threads without synchronization. |
Remarks
Prefer this property over constructing a new instance with new ScyllaPriorityQueueItemComparer<T>()
to avoid unnecessary heap allocations, particularly in performance-sensitive code that creates many
queues at runtime.
Methods
Compare(T, T)
Compares two items of type T by their
QueuePriority integer values and returns a value indicating
their relative ordering.
Declaration
public int Compare(T x, T y)
Parameters
| Type | Name | Description |
|---|---|---|
| T | x | The first item to compare. If |
| T | y | The second item to compare. If |
Returns
| Type | Description |
|---|---|
| int | A signed integer derived from CompareTo(int) applied to the two QueuePriority values:
When this comparer is used with a ScyllaPriorityQueue<T> configured as
Min, a negative result means |
Remarks
Reference equality is tested first as a fast-path shortcut: if both parameters refer to the exact
same object instance (or are both null), the method immediately returns zero without reading
QueuePriority. For value types, the null checks and
reference-equality test are optimised away by the JIT compiler because value types cannot be null.