ktsu.Containers 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package ktsu.Containers --version 1.0.0
                    
NuGet\Install-Package ktsu.Containers -Version 1.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="ktsu.Containers" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.Containers" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.Containers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ktsu.Containers --version 1.0.0
                    
#r "nuget: ktsu.Containers, 1.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.
#addin nuget:?package=ktsu.Containers&version=1.0.0
                    
Install ktsu.Containers as a Cake Addin
#tool nuget:?package=ktsu.Containers&version=1.0.0
                    
Install ktsu.Containers as a Cake Tool

ktsu.Containers

A high-performance, modern C# container library for .NET 8 and .NET 9, featuring a fixed-size circular buffer (ring buffer) implementation.

Overview

The ktsu.Containers library focuses on providing specialized container types to fill gaps in the standard .NET collection libraries. Each container is designed with specific use cases in mind, balancing performance, memory efficiency, and ease of use.

Features

  • RingBuffer<T>: A fixed-size, power-of-two circular buffer for fast, efficient storage and retrieval.
    • Overwrites oldest elements when full
    • Access elements by logical index, front, or back
    • Resizing and resampling support
    • Prefill with a sequence or a single repeated value
    • Implements IEnumerable<T>, IReadOnlyCollection<T>, and IReadOnlyList<T>
  • .NET 8 and .NET 9 support
  • MIT License

Installation

Add the NuGet package:

dotnet add package ktsu.Containers

Usage Examples

Basic Usage

using ktsu.Containers;

// Create a ring buffer of length 3
var buffer = new RingBuffer<int>(3);
buffer.PushBack(1);
buffer.PushBack(2);
buffer.PushBack(3);
buffer.PushBack(4); // Overwrites 1
Console.WriteLine(buffer.At(0)); // 2
Console.WriteLine(buffer.Back()); // 4

Creating Pre-filled Buffers

// Prefill with a single value
var zeros = new RingBuffer<int>(0, 5); // [0,0,0,0,0]

// Prefill with a sequence
var prefilled = new RingBuffer<int>(new[] { 1, 2, 3, 4, 5 }, 3); // [3,4,5]

Accessing Elements

var buffer = new RingBuffer<int>(new[] { 1, 2, 3 }, 3);

// Access by index (via At method or indexer)
int first = buffer.At(0); // 1
int second = buffer[1];   // 2

// Access first/last elements
int oldest = buffer.Front(); // 1 
int newest = buffer.Back();  // 3

Resizing and Resampling

var buffer = new RingBuffer<int>(new[] { 10, 20, 30 }, 3);

// Resize (discards all elements)
buffer.Resize(5);

// Add new elements
buffer.PushBack(100);
buffer.PushBack(200);

// Resample (interpolates values to the new size)
buffer.Resample(4); // Will spread the elements across the new size

Enumeration

var buffer = new RingBuffer<int>(new[] { 1, 2, 3, 4, 5 }, 5);

// Use in foreach loop
foreach (var item in buffer)
{
    Console.WriteLine(item);
}

// Use LINQ
var sum = buffer.Sum();
var doubled = buffer.Select(x => x * 2).ToList();

Performance Focus

The library emphasizes performance through several optimization techniques:

  • Power-of-two buffer sizes: Replaces modulo operations with more efficient bitwise AND operations for buffer wrapping
  • Minimal memory allocations: Operations like PushBack have no allocations in the common case
  • O(1) time complexity: Key operations like access and insertion maintain constant time regardless of buffer size
  • Zero-copy operations: Where possible, data is accessed in-place without copying

The RingBuffer implementation is particularly efficient for scenarios that involve:

  • Time-series data processing
  • Audio/video buffering
  • Signal processing
  • Rolling window calculations
  • Game state history for replay/undo functionality
  • Event and log management with fixed history
  • Any scenario requiring a fixed-size sliding window over a data stream

Advanced Usage

Custom Types

The RingBuffer works with any .NET type:

// With reference types
var messageBuffer = new RingBuffer<string>("", 100);
messageBuffer.PushBack("Hello");
messageBuffer.PushBack("World");

// With structs
var pointBuffer = new RingBuffer<Point>(10);
pointBuffer.PushBack(new Point(1, 2));
pointBuffer.PushBack(new Point(3, 4));

// With complex types
var userBuffer = new RingBuffer<User>(5);
userBuffer.PushBack(new User { Id = 1, Name = "Alice" });

License

MIT License. Copyright (c) ktsu.dev

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

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ktsu.Containers:

Package Downloads
Containers.Benchmarks

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 220 6/13/2025
1.0.4 220 6/13/2025
1.0.3 237 6/13/2025
1.0.2 256 6/13/2025
1.0.2-pre.2 112 5/20/2025
1.0.2-pre.1 118 5/18/2025
1.0.1 106 5/17/2025
1.0.0 108 5/17/2025

## v1.0.0 (major)

- Remove locking to simplify, I can make a concurrent one if I need that ([@matt-edmondson](https://github.com/matt-edmondson))
- Code chaneg to force a build ([@matt-edmondson](https://github.com/matt-edmondson))
- Improve tag handling in Get-VersionInfoFromGit function ([@matt-edmondson](https://github.com/matt-edmondson))
- Reset licence file ([@matt-edmondson](https://github.com/matt-edmondson))
- Initial Commit ([@matt-edmondson](https://github.com/matt-edmondson))
- Update SDK versions and enhance documentation tags ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor locking mechanism in RingBuffer class ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor PSBuild.psm1 to improve array handling ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance documentation and update project files ([@matt-edmondson](https://github.com/matt-edmondson))
- Rename from collections to containers, and implement standard interfaces ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor tag handling and error management in PSBuild.psm1 ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance versioning logic in PSBuild.psm1 ([@matt-edmondson](https://github.com/matt-edmondson))
- Add tests and constructors for RingBuffer functionality ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor RingBuffer class and add unit tests ([@matt-edmondson](https://github.com/matt-edmondson))
- Rename library from ktsu.Collections to ktsu.Containers ([@matt-edmondson](https://github.com/matt-edmondson))