ConcurrentPriorityQueue 3.0.0

dotnet add package ConcurrentPriorityQueue --version 3.0.0
NuGet\Install-Package ConcurrentPriorityQueue -Version 3.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ConcurrentPriorityQueue" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConcurrentPriorityQueue --version 3.0.0
#r "nuget: ConcurrentPriorityQueue, 3.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ConcurrentPriorityQueue as a Cake Addin
#addin nuget:?package=ConcurrentPriorityQueue&version=3.0.0

// Install ConcurrentPriorityQueue as a Cake Tool
#tool nuget:?package=ConcurrentPriorityQueue&version=3.0.0

ConcurrentPriorityQueue Build Workflow Nuget

A thread-safe generic first in first out (FIFO) collection with support for priority queuing.

Nuget: https://www.nuget.org/packages/ConcurrentPriorityQueue

Features

  1. Thread-Safe.
  2. Manages items according to a First in first out policy and priority on top of that.
  3. Implements IProducerConsumerCollection<T> interface.
  4. Extends to a BlockingCollection<T>.
  5. Supports multi-frameworks, includes net48 netstandard2.0 net6.0 net8.0

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Version Downloads Last updated
3.0.0 771 1/16/2024
2.1.0 10,435 2/1/2023
2.0.0 323 1/17/2023
1.0.0 5,697 7/27/2019