CodeJunkie.Collections
1.0.0
dotnet add package CodeJunkie.Collections --version 1.0.0
NuGet\Install-Package CodeJunkie.Collections -Version 1.0.0
<PackageReference Include="CodeJunkie.Collections" Version="1.0.0" />
<PackageVersion Include="CodeJunkie.Collections" Version="1.0.0" />
<PackageReference Include="CodeJunkie.Collections" />
paket add CodeJunkie.Collections --version 1.0.0
#r "nuget: CodeJunkie.Collections, 1.0.0"
#:package CodeJunkie.Collections@1.0.0
#addin nuget:?package=CodeJunkie.Collections&version=1.0.0
#tool nuget:?package=CodeJunkie.Collections&version=1.0.0
Collections
Lightweight collections, utilities, and generic interface types help in creating highly maintainable code.
Installation
Install the latest version of the [CodeJunkie.Collections] package from nuget:
dotnet add package CodeJunkie.Collections
Namespace
CodeJunkie.Collections
Features
- Balckboard: The
Blackboardclass is a shared data management system that maps types to their corresponding objects, allowing for efficient storage and retrieval. - Boxless Queue: The
BoxlessQueueclass is a specialized queue designed to store and process multiple struct types without boxing, minimizing heap allocations and improving performance. - Set and IReadOnlySet: The
Setclass is a custom implementation of a set that extendsHashSet<T>and adheres to theIReadOnlySet<T>interface for enhanced functionality and immutability. - Task Pool: The
TaskPoolclass is a task management system that efficiently handles task execution, prioritization, and resource allocation using a pool of task agents. - Reference Pool: The
ReferencePoolclass provides a mechanism for efficient object pooling, reducing memory allocation and improving performance by managing reusable object instances.
Key Components
Blackboard
Below is an example of how to use the Blackboard class:
using System;
using CodeJunkie.Collections;
public class Program {
public static void Main() {
// Create a new instance of the Blackboard
var blackboard = new Blackboard();
// Add data to the blackboard
blackboard.Set<string>("Hello, Blackboard!");
blackboard.Set<int>(42);
// Retrieve data from the blackboard
string message = blackboard.Get<string>();
int number = blackboard.Get<int>();
Console.WriteLine($"Message: {message}"); // Output: Message: Hello, Blackboard!
Console.WriteLine($"Number: {number}"); // Output: Number: 42
// Check if a type exists in the blackboard
bool hasString = blackboard.Has<string>();
bool hasDouble = blackboard.Has<double>();
Console.WriteLine($"Has string: {hasString}"); // Output: Has string: True
Console.WriteLine($"Has double: {hasDouble}"); // Output: Has double: False
// Overwrite existing data
blackboard.Overwrite<string>("Updated Message");
Console.WriteLine($"Updated Message: {blackboard.Get<string>()}"); // Output: Updated Message: Updated Message
// Attempt to retrieve a non-existent type (throws KeyNotFoundException)
try {
var nonExistent = blackboard.Get<double>();
} catch (KeyNotFoundException ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Explanation of Key Methods
Set<TData>(TData data):
- Adds a new object of type
TDatato the blackboard. - Throws an exception if data of the same type already exists.
- Adds a new object of type
Get<TData>():
- Retrieves an object of type
TDatafrom the blackboard. - Throws a
KeyNotFoundExceptionif the type is not found.
- Retrieves an object of type
Has<TData>():
- Checks if an object of type
TDataexists in the blackboard.
- Checks if an object of type
Overwrite<TData>(TData data):
- Overwrites existing data of type
TDatain the blackboard.
- Overwrites existing data of type
This example demonstrates how the Blackboard class can be used to manage shared data in a type-safe and flexible manner.
BoxlessQueue
The BoxlessQueue class in the CodeJunkie.Collections namespace is a specialized queue designed to store multiple struct types without incurring the overhead of boxing and unboxing operations. This reduces heap allocations and improves performance, making it ideal for scenarios where high-performance struct handling is required. It dynamically creates internal queues for each struct type and processes dequeued values using a handler.
How to Use BoxlessQueue
Creating a BoxlessQueue: To use
BoxlessQueue, you need to provide an implementation of theIBoxlessValueHandlerinterface, which defines how dequeued values are processed.var handler = new MyBoxlessValueHandler(); var queue = new BoxlessQueue(handler);Enqueuing Values: Use the
Enqueuemethod to add struct values to the queue. Each struct type gets its own internal queue.queue.Enqueue(42); // Enqueue an integer queue.Enqueue(3.14f); // Enqueue a float queue.Enqueue(new Vector2(1, 2)); // Enqueue a custom structDequeuing Values: Use the
Dequeuemethod to process the next value in the queue. The value is passed to the handler without boxing.queue.Dequeue(); // Processes the first value in the queueChecking for Values: Use the
HasValuesproperty to check if the queue contains any values.if (queue.HasValues) { Console.WriteLine("The queue has values to process."); }Clearing the Queue: Use the
Clearmethod to remove all values from the queue.queue.Clear();
Example
using CodeJunkie.Collections;
using System;
public struct Vector2
{
public float X { get; }
public float Y { get; }
public Vector2(float x, float y)
{
X = x;
Y = y;
}
}
public class MyBoxlessValueHandler : IBoxlessValueHandler
{
public void HandleValue<TValue>(in TValue value) where TValue : struct
{
Console.WriteLine($"Processing value: {value}");
}
}
class Program
{
static void Main()
{
var handler = new MyBoxlessValueHandler();
var queue = new BoxlessQueue(handler);
queue.Enqueue(42);
queue.Enqueue(3.14f);
queue.Enqueue(new Vector2(1, 2));
while (queue.HasValues)
{
queue.Dequeue();
}
}
}
Output:
Processing value: 42
Processing value: 3.14
Processing value: Vector2 { X = 1, Y = 2 }
The BoxlessQueue is a powerful tool for managing struct-based data in performance-critical applications, ensuring minimal memory overhead and efficient processing.
Set and IReadOnlySet
The Set<T> class in the CodeJunkie.Collections namespace is a specialized collection that extends the functionality of the standard HashSet<T> while also implementing the IReadOnlySet<T> interface. It is designed to store unique elements and provides efficient operations for adding, removing, and checking the existence of elements.
Usage
Creating a Set: You can create a
Set<T>instance by specifying the type of elements it will store.var mySet = new Set<int>();Adding Elements: Use the
Addmethod to insert elements into the set. Duplicate elements will not be added.mySet.Add(1); mySet.Add(2); mySet.Add(1); // Duplicate, will not be addedRemoving Elements: Use the
Removemethod to delete specific elements from the set.mySet.Remove(1);Checking for Elements: Use the
Containsmethod to check if an element exists in the set.if (mySet.Contains(2)) { Console.WriteLine("Element exists in the set."); }Iterating Over Elements: You can iterate over the elements in the set using a
foreachloop.foreach (var item in mySet) { Console.WriteLine(item); }Read-Only Access: Since
Set<T>implementsIReadOnlySet<T>, you can use it in contexts where read-only access to the set is required.
Example:
using CodeJunkie.Collections;
var mySet = new Set<string>();
mySet.Add("Apple");
mySet.Add("Banana");
mySet.Add("Apple"); // Duplicate, will not be added
Console.WriteLine("Set contains:");
foreach (var item in mySet)
{
Console.WriteLine(item);
}
// Output:
// Set contains:
// Apple
// Banana
The Set<T> class is ideal for scenarios where you need a collection of unique elements with efficient operations for membership testing and modification.
ReferencePool
A static class that serves as the entry point for managing reference pools.
- Acquire: Retrieve an object from the pool.
- Release: Return an object to the pool after use.
- Add: Preload objects into the pool.
- Remove: Remove objects from the pool.
- ClearAll: Clear all reference pools.
ReferenceCollection
An internal class that manages a specific type of reference.
- Tracks usage statistics such as:
- Unused references
- References in use
- Total acquired, released, added, and removed references
ReferencePoolInformation
A struct that provides detailed information about a specific reference pool.
- Includes properties for:
- Reference type
- Counts for unused, in-use, acquired, released, added, and removed references
Usage
Acquiring and Releasing References
// Acquire a reference of type MyReference
var reference = ReferencePool.Acquire<MyReference>();
// Use the reference
reference.DoSomething();
// Release the reference back to the pool
ReferencePool.Release(reference);
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.1
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on CodeJunkie.Collections:
| Package | Downloads |
|---|---|
|
CodeJunkie.Metadata
"CodeJunkie.Metadata is a metaprogramming tool for generating metadata and enabling reflection-like capabilities in ahead-of-time (AOT) environments such as iOS." |
|
|
CodeJunkie.Serialization
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 272 | 5/19/2025 |