ktsu.Containers 2.2.0

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package ktsu.Containers --version 2.2.0
                    
NuGet\Install-Package ktsu.Containers -Version 2.2.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="2.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.Containers" Version="2.2.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 2.2.0
                    
#r "nuget: ktsu.Containers, 2.2.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.
#:package ktsu.Containers@2.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ktsu.Containers&version=2.2.0
                    
Install as a Cake Addin
#tool nuget:?package=ktsu.Containers&version=2.2.0
                    
Install as a Cake Tool

ktsu.Containers

A high-performance collection library for .NET providing specialized container implementations with focus on performance, memory efficiency, and developer experience.

License NuGet Version NuGet Version NuGet Downloads GitHub commit activity GitHub contributors GitHub Actions Workflow Status

🚀 Features

  • OrderedCollection<T>: Maintains elements in sorted order with efficient binary search operations
  • OrderedSet<T>: Unique elements maintained in sorted order with full ISet<T> support
  • RingBuffer<T>: Fixed-size circular buffer optimized for streaming data scenarios
  • Comprehensive Benchmarking: World-class performance validation against .NET collections

📦 Collections

OrderedCollection<T>

A collection that maintains elements in sorted order with efficient insertion and search operations.

Key Features:

  • O(log n) insertion maintaining sort order
  • O(log n) search operations via binary search
  • Multiple constructors with capacity and comparer support
  • Implements ICollection<T>, IReadOnlyCollection<T>, IReadOnlyList<T>

Best Use Cases:

  • Incremental sorted data building
  • Frequent search operations on ordered data
  • Scenarios requiring both order and random access

OrderedSet<T>

A set implementation that maintains unique elements in sorted order.

Key Features:

  • O(log n) Add/Remove/Contains operations
  • Full ISet<T> interface support (Union, Intersection, Except, etc.)
  • Sorted enumeration without additional sorting cost
  • Memory-efficient list-based implementation

Best Use Cases:

  • Unique sorted collections
  • Set operations with maintained order
  • Mathematical set operations with sorted results

RingBuffer<T>

A fixed-size circular buffer optimized for continuous data streams.

Key Features:

  • O(1) insertion and access operations
  • Automatic wrapping when capacity is reached
  • Length and IsFull tell whether the next push evicts the oldest element
  • Index-based access to buffer elements
  • Resizing and resampling capabilities
  • Power-of-two sizing for optimal performance

Best Use Cases:

  • Streaming data processing
  • Fixed-size caches
  • Sliding window algorithms
  • Audio/signal processing buffers

🏆 Performance & Benchmarking

We've implemented a comprehensive benchmarking system using BenchmarkDotNet that validates our implementations against standard .NET collections.

Benchmark Categories

🎯 Container-Specific Benchmarks
  • OrderedCollection vs List<T> + Sort vs SortedList<T>
  • OrderedSet vs HashSet<T> vs SortedSet<T>
  • RingBuffer vs Queue<T> vs List<T> with size limiting
📊 Standard Collection Baselines

Performance baselines for all major .NET collections:

  • List<T>, HashSet<T>, SortedSet<T>
  • Dictionary<TKey,TValue>, SortedDictionary<TKey,TValue>
  • Queue<T>, Stack<T>
  • ConcurrentDictionary<TKey,TValue>, ConcurrentQueue<T>
🔄 Cross-Collection Comparisons

Direct comparisons for common scenarios:

  • Building ordered collections (incremental vs bulk)
  • Set operations across different implementations
  • Memory efficiency patterns
  • Real-world usage workflows
📈 Performance Analysis

Advanced scenarios including:

  • Scalability testing (1K to 100K+ elements)
  • Edge cases and worst-case scenarios
  • Memory pressure analysis
  • Specialized use cases (sliding windows, priority queues)

Running Benchmarks

Quick Start

The benchmarks are a BenchmarkDotNet console app. Everything after -- goes to BenchmarkDotNet.

# Fast development feedback (short job, fewer iterations)
dotnet run --project Containers.Benchmarks --configuration Release -- --filter "*" --job short

# Compare specific containers
dotnet run --project Containers.Benchmarks --configuration Release -- --filter "*.OrderedSetBenchmarks.*" --exporters html

# Cross-collection analysis
dotnet run --project Containers.Benchmarks --configuration Release -- --filter "*.CrossCollectionComparisonBenchmarks.*" --exporters html csv json

# Full benchmark suite
dotnet run --project Containers.Benchmarks --configuration Release -- --filter "*" --exporters html
Filters
Filter Description
*.OrderedCollectionBenchmarks.* OrderedCollection<T> vs alternatives
*.OrderedSetBenchmarks.* OrderedSet<T> vs alternatives
*.RingBufferBenchmarks.* RingBuffer<T> vs alternatives
*.StandardCollectionBenchmarks.* Built-in .NET collection baselines
*.CrossCollectionComparisonBenchmarks.* Direct cross-collection comparisons
*.PerformanceAnalysisBenchmarks.* Scalability and edge case analysis
* Complete benchmark suite

Add --job short to any of these for a quicker, less precise run. Run with no --filter to pick benchmarks from an interactive menu.

Command Line Options
# Manual benchmark execution
dotnet run --project Containers.Benchmarks --configuration Release

# Filtered benchmarks
dotnet run --project Containers.Benchmarks --configuration Release -- --filter "*Add*"

# Export results
dotnet run --project Containers.Benchmarks --configuration Release -- --exporters html,csv,json

📋 Installation

<PackageReference Include="ktsu.Containers" Version="1.0.0" />

🔧 Usage Examples

OrderedCollection<T>

// Create and populate
var collection = new OrderedCollection<int>();
collection.Add(3);
collection.Add(1);
collection.Add(2);
// Collection automatically maintains order: [1, 2, 3]

// Efficient search
bool contains = collection.Contains(2); // O(log n) binary search

// Access by index
int first = collection[0]; // 1

// Add a batch in one sorted merge rather than one insert per element
collection.AddRange([5, 0, 4]); // [0, 1, 2, 3, 4, 5]

OrderedSet<T>

// Create with initial data
var set = new OrderedSet<int> { 3, 1, 4, 1, 5 };
// Set contains unique elements in order: [1, 3, 4, 5]

// Set operations
var other = new OrderedSet<int> { 4, 5, 6 };
set.UnionWith(other); // [1, 3, 4, 5, 6]

RingBuffer<T>

// Create fixed-size buffer
var buffer = new RingBuffer<int>(3);
buffer.PushBack(1);
buffer.PushBack(2);
buffer.PushBack(3);
buffer.PushBack(4); // Overwrites first element
// Buffer contains: [2, 3, 4]

// Index access
int latest = buffer[buffer.Count - 1]; // 4 (most recent)

// Length is the size given to the constructor, Resize or Resample
bool evictsNext = buffer.IsFull; // true: Count == Length == 3
buffer.Resample(buffer.Length * 2);

🛠️ Development

Building

dotnet build

Testing

dotnet test

Benchmarking

# Quick performance check
dotnet run --project Containers.Benchmarks --configuration Release -- --filter "*" --job short

# Comprehensive analysis
dotnet run --project Containers.Benchmarks --configuration Release -- --filter "*" --exporters html

📊 Performance Characteristics

OrderedCollection<T>

  • ✅ Excellent for: Incremental sorted insertions, frequent searches
  • ⚠️ Consider alternatives for: Large bulk operations (List<T> + Sort may be faster)
  • 🎯 Sweet spot: 100-10,000 elements with mixed insert/search operations

OrderedSet<T>

  • ✅ Excellent for: Unique sorted collections, set operations with order
  • ⚠️ Consider alternatives for: Pure membership testing (HashSet<T> is faster)
  • 🎯 Sweet spot: Mathematical set operations requiring sorted results

RingBuffer<T>

  • ✅ Excellent for: Streaming data, fixed-size caches, sliding windows
  • ⚠️ Consider alternatives for: Dynamic growth requirements
  • 🎯 Sweet spot: Continuous data streams with occasional random access

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add comprehensive tests
  5. Run benchmarks to validate performance
  6. Submit a pull request

Adding New Containers

When adding new containers:

  1. Implement the container in Containers/
  2. Add comprehensive tests in Containers.Test/
  3. Create benchmark comparisons in Containers.Benchmarks/
  4. Update documentation

📝 License

Licensed under the MIT License. See LICENSE.md for details.

🏗️ Architecture

The library is built with performance and reliability in mind:

  • Zero-allocation operations where possible
  • Power-of-two sizing for optimal memory access patterns
  • Binary search algorithms for O(log n) performance
  • Comprehensive test coverage ensuring reliability
  • Benchmark-driven development validating performance claims

🎯 Design Principles

  1. Performance First: Every operation is optimized for speed and memory efficiency
  2. Standard Compliance: Full compatibility with .NET collection interfaces
  3. Predictable Behavior: Clear performance characteristics and edge case handling
  4. Developer Experience: Intuitive APIs with comprehensive documentation
  5. Validation: Extensive testing and benchmarking ensures reliability

This library provides high-performance alternatives to standard .NET collections while maintaining familiar APIs and adding specialized functionality for common use cases.

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 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 is compatible.  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 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 is compatible. 
.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. 
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

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.1-pre.2 0 11/12/2026
2.2.1-pre.1 44 11/11/2026
2.2.0 60 11/11/2026
2.1.0 46 11/11/2026
2.0.3 237 10/9/2026
2.0.3-pre.1 67 10/8/2026
2.0.2 112 10/8/2026
2.0.1 106 10/7/2026
2.0.0 101 10/7/2026
1.1.31 122 10/6/2026
1.1.31-pre.1 74 10/5/2026
1.1.30 123 9/29/2026
1.1.29 117 9/29/2026
1.1.29-pre.1 76 9/28/2026
1.1.28 124 9/28/2026
1.1.27 129 9/26/2026
1.1.26 128 9/26/2026
1.1.25 146 9/24/2026
1.1.24 121 9/23/2026
1.1.23 125 9/21/2026
Loading failed

## v2.2.0 (minor)

Changes since v2.1.0:

- Add AddRange to OrderedCollection and OrderedMap [minor] (#150) (matt-edmondson)