Chickensoft.Collections 1.8.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chickensoft.Collections --version 1.8.9                
NuGet\Install-Package Chickensoft.Collections -Version 1.8.9                
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="Chickensoft.Collections" Version="1.8.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chickensoft.Collections --version 1.8.9                
#r "nuget: Chickensoft.Collections, 1.8.9"                
#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 Chickensoft.Collections as a Cake Addin
#addin nuget:?package=Chickensoft.Collections&version=1.8.9

// Install Chickensoft.Collections as a Cake Tool
#tool nuget:?package=Chickensoft.Collections&version=1.8.9                

Chickensoft Collections

Chickensoft Badge Discord Read the docs line coverage branch coverage

Lightweight collections, utilities, and general interface types to help make maintainable code.


<p align="center"> <img alt="Cardboard Box with Chickensoft Logo" src="Chickensoft.Collections/icon.png" width="200"> </p>

Install

dotnet add package Chickensoft.Collections

Map

A typed facade over OrderedDictionary. Provides a basic mechanism to store strongly typed keys and values while preserving key insertion order.

  var map = new Map<string, int>() {
    ["b"] = 2,
    ["a"] = 1,
  };

  map.Keys.ShouldBe(["b", "a"]);

Set and IReadOnlySet

For whatever reason, netstandard does not include IReadOnlySet. To workaround this, we've provided our own version of IReadOnlySet and a Set implementation that simply extends HashSet and adds the interface to it.

AutoProp

AutoProp allows you to make observable properties in the style of IObservable, but is implemented over plain C# events and modifies the API to be more ergonomic, a la Chickensoft style.

AutoProps are basically a simplified version of a BehaviorSubject that only updates when the new value is not equal to the previous value, as determined by the equality comparer (or the default one if you don't provide one). They operate synchronously and make guarantees about the order of changes in a very simple, easy to reason about manner.

using Chickensoft.Collections;

public class MyObject : IDisposable {
  // Read-only version exposed as interface.
  public IAutoProp<bool> MyValue => _myValue;

  // Read-write version.
  private readonly AutoProp<bool> _myValue = new AutoProp<bool>(false);

  public void Update() {
    // Update our values based on new information.
    _myValue.OnNext(true);

    // ...

    // Check the latest value.
    if (_myValue.Value) {
      // ...
    }

    // Subscribe to all future changes, **AND** get called immediately with the
    // current value.
    _myValue.Sync += OnMyValueChanged;

    // Subscribe to all future changes.
    _myValue.Changed += OnMyValueChanged;

    // Subscribe to completed
    _myValue.Completed += OnMyValueCompleted;

    // Subscribe to errors
    _myValue.Error += OnMyValueError;

    // Optional: inform completed listeners we're done updating values
    _myValue.OnCompleted();

    // Optional: send error listeners an error value
    _myValue.OnError(new System.InvalidOperationException());

    // ...

    // Always unsubscribe C# events when you're done :)
    _myValue.Sync -= OnMyValueChanged;
    _myValue.Changed -= OnMyValueChanged;
    _myValue.Completed -= OnMyValueCompleted;
    _myValue.Error -= OnMyValueError;

    // Or clear all subscriptions at once:
    _myValue.Clear();

    // When your object is disposing:
    _myValue.Dispose();
  }

  private void OnMyValueChanged(bool value) { }
  private void OnMyValueCompleted() { }
  private void OnMyValueError(Exception err) { }

  // ...
}
  • ✅ Uses plain C# events.

    Observers are called one-at-a-time, in-order of subscription, on the invoking thread, and synchronously (and will always be that way unless Microsoft tampers with the underlying Multicast delegate implementation that powers C# events).

    Chickensoft prefers to keep everything synchronous and deterministic in game development, only adding parallelization or asynchronicity where it's absolutely necessary for performance. Otherwise, simpler is better.

  • ✅ Familiar API.

    If you've ever used IObservable and/or BehaviorSubject, you basically already know how to use this.

  • ✅ Guarantees order of events and allows updates from handlers.

    If you change the value from a changed event handler, it will queue up the next value and process it synchronously afterwards. This allows it to pass through each desired value, guaranteeing callbacks will be called in the correct order for each value it passes through.

  • ✅ Doesn't update if the value hasn't changed.

Blackboard

A blackboard datatype is provided that allows reference values to be stored by type. It implements two interfaces, IBlackboard and IReadOnlyBlackboard.

var blackboard = new Blackboard();

blackboard.Set("string value");
var stringValue = blackboard.Get<String>();

blackboard.Set(new MyObject());
var myObj = blackboard.Get<MyObject>();

// ...and various other convenience methods.

EntityTable

The EntityTable<TId> is a simple wrapper over a ConcurrentDictionary that is provided to help you conveniently associate any type of value with an identifier. Table entries are requested by their identifier and type. If the value exists and matches the requested type, it is returned. Otherwise, null is returned.

var table = new EntityTable<int>();

table.Set(42, "dolphins");

// Use pattern matching for an optimal experience.
if (table.Get<string>(42) is { } value) {
  Console.WriteLine("Dolphins are present.");
}

table.Remove(42);

A default implementation that uses string is also provided:

var table = new EntityTable();

table.Set("identifier", new object())

if (table.Get<object>("identifier") is { } value) {
  Console.WriteLine("Object is present.");
}

Boxless Queue

The boxless queue allows you to queue struct values on the heap without boxing them, and dequeue them without needing to unbox them.

To do so, you must make an object which implements the IBoxlessValueHandler interface. The HandleValue method will be invoked whenever the boxless queue dequeues a value.

public class MyValueHandler : IBoxlessValueHandler {
  public void HandleValue<TValue>(in TValue value) where TValue : struct {
    Console.WriteLine($"Received value {value}");
  }
}

Once you have implemented the IBoxlessValueHandler, you can create a boxless queue.

    var handler = new MyValueHandler();
    
    var queue = new BoxlessQueue(handler);

    // Add something to the queue.
    queue.Enqueue(valueA);
    
    // See if anything is in the queue.
    if (queue.HasValues) {
      Console.WriteLine("Something in the queue.");
    }

    // Take something out of the queue. Calls our value handler.
    queue.Dequeue();

Pool

A simple object pool implementation is provided that allows you to pre-allocate objects when memory churn is a concern. Internally, the pool is just a simple wrapper around a .NET concurrent dictionary that maps types to concurrent queues of objects. By leveraging .NET concurrent collections, we can create a type-safe and thread-safe object pool that's easy to use.

Any object you wish to store in a pool must conform to IPooled and implement the required Reset method. The reset method is called when the object is returned to the pool, allowing you to reset the object's state.

  public abstract class Shape : IPooled {
    public void Reset() { }
  }

  public class Cube : Entity { }
  public class Sphere : Entity { }

A pool can be easily created. Each derived type that you wish to pool can be "registered" with the pool. The pool will create instances of each type registered with it according to the provided capacity.

  var pool = new Pool<Shape>();

  pool.Register<Cube>(10); // Preallocate 10 cubes.
  pool.Register<Sphere>(5); // Preallocate 5 spheres.

  // Borrow a cube and a sphere, removing them from the pool:
  var cube = pool.Get<Cube>();

  // You can also get the an object without a generic type:
  var cube2 = pool.Get(typeof(Cube));
  var cube3 = pool.Get(cube.GetType());
  
  var sphere = pool.Get<Sphere>();

  // Return them to the pool (their Reset() methods will be called):
  pool.Return(cube);
  pool.Return(sphere);

🐣 Created with love by Chickensoft 🐤 — https://chickensoft.games

Product 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Chickensoft.Collections:

Package Downloads
Chickensoft.LogicBlocks

Human-friendly, hierarchical state machines for games and apps in C#.

Chickensoft.Introspection

Generate type metadata at build time.

Chickensoft.Serialization

Easy to use serializable models with AOT compilation support and System.Text.Json compatibility.

Chickensoft.SaveFileBuilder

Chickensoft.SaveFileBuilder description.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Chickensoft.Collections:

Repository Stars
chickensoft-games/GameDemo
The Chickensoft Game Demo — a fully tested, third-person 3D game built with Godot and C#. Now with saving and loading!
chickensoft-games/LogicBlocks
Human-friendly, hierarchical and serializable state machines for games and apps in C#.
Version Downloads Last updated
1.12.4 175 12/16/2024
1.12.3 333 11/19/2024
1.12.2 225 11/13/2024
1.12.1 149 11/8/2024
1.12.0 173 10/28/2024
1.11.2 950 10/18/2024
1.11.1 163 10/13/2024
1.11.0 131 10/12/2024
1.10.0 127 10/9/2024
1.9.4 187 10/9/2024
1.9.3 257 9/27/2024
1.9.2 419 9/25/2024
1.9.1 127 9/23/2024
1.9.0 282 9/17/2024
1.8.10 184 9/11/2024
1.8.9 195 9/7/2024
1.8.8 168 9/5/2024
1.8.7 231 9/3/2024
1.8.6 140 8/31/2024
1.8.5 565 8/20/2024
1.8.4 299 8/16/2024
1.8.3 181 8/14/2024
1.8.2 127 8/6/2024
1.8.1 115 7/31/2024
1.8.0 163 7/28/2024
1.7.5 325 7/24/2024
1.7.4 228 7/10/2024
1.7.3 109 7/8/2024
1.7.2 106 7/5/2024
1.7.1 231 6/12/2024
1.7.0 1,011 6/6/2024
1.6.0 2,275 6/3/2024
1.5.0-godot4.3.0-beta.1 52 6/3/2024
1.4.2-godot4.3.0-beta.1 62 5/31/2024
1.4.1 136 5/30/2024
1.4.0 129 5/29/2024
1.3.3 100 5/25/2024
1.3.2 124 5/22/2024
1.3.1 120 5/15/2024
1.3.0 163 5/4/2024
1.2.0 83 5/2/2024
1.1.0 141 4/27/2024
1.0.1 111 4/27/2024
1.0.0 115 4/27/2024
0.0.2 111 4/27/2024
0.0.1 106 4/27/2024

Chickensoft.Collections release.