ConcurrentPriorityQueue 1.0.0
See the version list below for details.
dotnet add package ConcurrentPriorityQueue --version 1.0.0
NuGet\Install-Package ConcurrentPriorityQueue -Version 1.0.0
<PackageReference Include="ConcurrentPriorityQueue" Version="1.0.0" />
paket add ConcurrentPriorityQueue --version 1.0.0
#r "nuget: ConcurrentPriorityQueue, 1.0.0"
// Install ConcurrentPriorityQueue as a Cake Addin #addin nuget:?package=ConcurrentPriorityQueue&version=1.0.0 // Install ConcurrentPriorityQueue as a Cake Tool #tool nuget:?package=ConcurrentPriorityQueue&version=1.0.0
ConcurrentPriorityQueue
A thread-safe generic first in first out (FIFO) collection with support for priority queuing.
Nuget: https://www.nuget.org/packages/ConcurrentPriorityQueue
Features
- Thread-Safe.
- Manages items according to a
First in first out
policy andpriority
on top of that. - Implements
IProducerConsumerCollection<T>
interface. - Extends to a
BlockingCollection<T>
.
Examples:
Items in the collection must implement the generic interface IHavePriority<T>
where T: implements IEquatable<T>
, IComparable<T>
and also overrides Object.GetHashCode()
:
// Simplest implementation of IHavePriority<T>
public class SomeClass : IHavePriority<int> {
int Priority {get; set;}
}
Simple flow for creating a Priority-By-Integer
queue and adding an item:
// Create a new prioritized item.
var itemWithPriority = new SomeClass { Priority = 0 };
// Initialize an unbounded priority-by-integer queue.
var priorityQueue = new ConcurrentPriorityQueue<IHavePriority<int>, int>();
// Enqueue item and handle result.
Result result = priorityQueue.Enqueue(itemWithPriority);
Use the ConcurrentPriorityByIntegerQueue
implementation to simplify the above example:
var priorityQueue = new ConcurrentPriorityByIntegerQueue<IHavePriority<int>>();
Consume items by priority/first-in-first-out policy, using Dequeue()
and Peek()
:
// Lower value -> Higher priority.
var item1 = new SomeClass { Priority = 1 };
var item2 = new SomeClass { Priority = 0 };
var item3 = new SomeClass { Priority = 0 };
priorityQueue.Enqueue(item1);
priorityQueue.Enqueue(item2);
priorityQueue.Enqueue(item3);
var result = priority.Dequeue(); // item2
var result = priority.Dequeue(); // item3
var result = priority.Dequeue(); // item1
Iterating over the collection will yield items according to their priority and position (FIFO):
var item1 = new SomeClass { Priority = 1 };
var item2 = new SomeClass { Priority = 0 };
var item3 = new SomeClass { Priority = 0 };
priorityQueue.Enqueue(item1);
priorityQueue.Enqueue(item2);
priorityQueue.Enqueue(item3);
foreach(var item in priorityQueue) {
// Iteration 1 -> item2
// Iteration 2 -> item3
// Iteration 3 -> item1
}
ConcurrentPriorityQueue
supports Generic Priorities.
Implement your own Business Priority object and configure the queue to handle it:
// TimeToProcess class implements IEquatable<T>, IComparable<T> and overrides Object.GetHashCode().
public class TimeToProcess : IEquatable<TimeToProcess>, IComparable<TimeToProcess> {
public decimal TimeInMilliseconds { get; set;}
public int CompareTo(TimeToProcess other) =>
TimeInMilliseconds.CompareTo(other.TimeInMilliseconds);
public bool Equals(TimeToProcess other) =>
TimeInMilliseconds.Equals(other.TimeInMilliseconds);
public override int GetHashCode() => TimeInMilliseconds.GetHashCode();
}
// BusinessPriorityItem implements IHavePriority<T>
public class BusinessPriorityItem : IHavePriority<TimeToProcess> {
TimeToProcess Priority {get; set;}
}
// Create a new prioritized item.
var item = new BusinessPriorityItem { Priority = new TimeToProcess { TimeInMilliseconds = 0.25M } };
// Initialize an unbounded priority-by-TimeToProcess queue.
var priorityQueue = new ConcurrentPriorityQueue<IHavePriority<TimeToProcess>, TimeToProcess>();
// Enqueue item and handle result.
Result result = priorityQueue.Enqueue(item);
ConcurrentPriorityQueue<T>
can be bounded to a fixed amount of priorities:
// Create a bounded ConcurrentPriorityQueue to support a fixed amount of priorities.
var maxAmountOfPriorities = 2;
var priorityQueue = new ConcurrentPriorityByIntegerQueue<IHavePriority<int>>(maxAmountOfPriorities);
Result result = PriorityQueue.Enqueue(new SomeClass {Priority = 0}); // result.OK
Result result = PriorityQueue.Enqueue(new SomeClass {Priority = 1}); // result.OK
Result result = PriorityQueue.Enqueue(new SomeClass {Priority = 2}); // result.Fail -> Queue supports [0, 1]
Result result = PriorityQueue.Enqueue(new SomeClass {Priority = 0}); // result.OK
ConcurrentPriorityQueue<T>
can be extended to a BlockingCollection<T>
using the ToBlockingCollection<T>
extension method:
var blockingPriorityQueue = new ConcurrentPriorityByIntegerQueue<IHavePriority<int>>()
.ToBlockingCollection();
foreach(var item in blockingPriorityQueue.GetConsumingEnumerable()) {
// Do something...
// Blocks until signaled on completion.
}
Additional information and resources:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- CSharpFunctionalExtensions (>= 1.18.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on ConcurrentPriorityQueue:
Repository | Stars |
---|---|
zhuxb711/RX-Explorer
一款优雅的UWP文件管理器 | An elegant UWP Explorer
|