// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
namespace System.Reactive.Concurrency
{
///
/// Efficient scheduler queue that maintains scheduled items sorted by absolute time.
///
/// Absolute time representation type.
/// This type is not thread safe; users should ensure proper synchronization.
[Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "But it *is* a queue!")]
public class SchedulerQueue
where TAbsolute : IComparable
{
private readonly PriorityQueue> _queue;
///
/// Creates a new scheduler queue with a default initial capacity.
///
public SchedulerQueue()
: this(1024)
{
}
///
/// Creates a new scheduler queue with the specified initial capacity.
///
/// Initial capacity of the scheduler queue.
/// is less than zero.
public SchedulerQueue(int capacity)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
_queue = new PriorityQueue>(capacity);
}
///
/// Gets the number of scheduled items in the scheduler queue.
///
public int Count => _queue.Count;
///
/// Enqueues the specified work item to be scheduled.
///
/// Work item to be scheduled.
public void Enqueue(ScheduledItem scheduledItem)
{
_queue.Enqueue(scheduledItem);
}
///
/// Removes the specified work item from the scheduler queue.
///
/// Work item to be removed from the scheduler queue.
/// true if the item was found; false otherwise.
public bool Remove(ScheduledItem scheduledItem) => _queue.Remove(scheduledItem);
///
/// Dequeues the next work item from the scheduler queue.
///
/// Next work item in the scheduler queue (removed).
public ScheduledItem Dequeue() => _queue.Dequeue();
///
/// Peeks the next work item in the scheduler queue.
///
/// Next work item in the scheduler queue (not removed).
public ScheduledItem Peek() => _queue.Peek();
}
}