| | 1 | | namespace MoreStructures.PriorityQueues; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// An item of type <typeparamref name="T"/> with a priority and a "push timestamp" assigned to it. |
| | 5 | | /// </summary> |
| | 6 | | /// <typeparam name="T">The type of the <paramref name="Item"/>.</typeparam> |
| | 7 | | /// <param name="Item">The item.</param> |
| | 8 | | /// <param name="Priority">The <see cref="int"/> defining the priority of the <paramref name="Item"/>.</param> |
| | 9 | | /// <param name="PushTimestamp"> |
| | 10 | | /// A strictly monotonic increasing <see cref="int"/>, uniquely identifying an insertion of the <paramref name="Item"/> |
| | 11 | | /// in the queue via <see cref="IPriorityQueue{T}.Push(T, int)"/>. To be considered as offsets of |
| | 12 | | /// <paramref name="PushTimestampEra"/>. |
| | 13 | | /// </param> |
| | 14 | | /// <param name="PushTimestampEra"> |
| | 15 | | /// The base for <paramref name="PushTimestamp"/>. |
| | 16 | | /// <br/> |
| | 17 | | /// Same or equivalent <paramref name="PushTimestampEra"/> and same <paramref name="PushTimestamp"/> correspond to same |
| | 18 | | /// actual timestamps. |
| | 19 | | /// <br/> |
| | 20 | | /// Timestamps in different eras can never be equal (i.e. there is strict order of timestamps between eras). |
| | 21 | | /// </param> |
| 5063795 | 22 | | public record struct PrioritizedItem<T>(T Item, int Priority, int PushTimestamp, PushTimestampEra PushTimestampEra) |
| | 23 | | : IComparable<PrioritizedItem<T>> |
| | 24 | | { |
| | 25 | | /// <inheritdoc cref="PrioritizedItem{T}"/> |
| | 26 | | public PrioritizedItem(T Item, int Priority, int PushTimestamp) : |
| 29747 | 27 | | this(Item, Priority, PushTimestamp, new(0)) |
| 59494 | 28 | | { } |
| | 29 | |
|
| | 30 | | /// <inheritdoc path="//*[not(self::remarks)]"/> |
| | 31 | | /// <remarks> |
| | 32 | | /// Comparison is made by <see cref="Priority"/> first: bigger <see cref="Priority"/> determines bigger instance. |
| | 33 | | /// <br/> |
| | 34 | | /// If the two instances have the same <see cref="Priority"/>, comparison is made by <see cref="PushTimestampEra"/> |
| | 35 | | /// values: smaller <see cref="PushTimestampEra"/> determines higher priority instance. |
| | 36 | | /// <br/> |
| | 37 | | /// If the two instances have the same <see cref="PushTimestampEra"/>, comparison is made by |
| | 38 | | /// <see cref="PushTimestamp"/>: smaller <see cref="PushTimestamp"/> determines higher priority instance. |
| | 39 | | /// <br/> |
| | 40 | | /// If <see cref="Priority"/>, <see cref="PushTimestampEra"/> and <see cref="PushTimestamp"/> are the same, |
| | 41 | | /// returned value is 0. |
| | 42 | | /// </remarks> |
| | 43 | | public int CompareTo(PrioritizedItem<T> other) |
| 1234404 | 44 | | { |
| 1234404 | 45 | | var priorityDifference = Priority - other.Priority; |
| 1234404 | 46 | | if (priorityDifference != 0) |
| 945349 | 47 | | return priorityDifference; |
| 289055 | 48 | | var eraDifference = -PushTimestampEra.Era + other.PushTimestampEra.Era; |
| 289055 | 49 | | if (eraDifference != 0) |
| 4998 | 50 | | return eraDifference; |
| 284057 | 51 | | return -PushTimestamp + other.PushTimestamp; |
| 1234404 | 52 | | } |
| | 53 | | } |