Daniel Drywa

DD.Collections.Generic.CircularBuffer

A generic fixed-size Circular Buffer for .NET Standard 1.6

Description

This is a single-file implementation of a generic fixed-size Circular Buffer data structure for the .NET Standard 1.6.

The implementation itself is located within a single source file for easy inclusion in your projects. A library project is also included if you prefer a assembly dependency.

The implementation is tested via NUnit.

Usage

The CircularBuffer class is located within the DD.Collections.Generic namespace. To create a new instance you simply have to call the constructor:

// Creates a buffer with room for 1023 items
var default_capacity_instance = new CircularBuffer<int>();

// Creates a buffer with room for 50 items
var defined_capacity_instance = new CircularBuffer<int>( 50 );

To add a new item at the end of the buffer you have to call Push. To remove the item at the beginning of the buffer you have to call Pop:

defined_capacity_instance.Push( 1 );
defined_capacity_instance.Push( 2 );
defined_capacity_instance.Push( 3 );

var a = defined_capacity_instance.Pop(); // a == 1
var b = defined_capacity_instance.Pop(); // b == 2
var c = defined_capacity_instance.Pop(); // c == 3

You can also enumerate over all items within the buffer as the circular buffer inherits IReadOnlyCollection<T> which in return inherits IEnumerable<T>. (Iterating over the buffer does not consume its items):

defined_capacity_instance.Push( 1 );
defined_capacity_instance.Push( 2 );
defined_capacity_instance.Push( 3 );

foreach ( var item in defined_capacity_instance ) {
    Console.WriteLine( item ); // Prints all items
}

if ( defined_capacity_instance.Count == 3 ) {
    Console.WriteLine( "Still got all items!" );
}
Description Link
Source Code Repository

30 July 2017