Exercism v3 launches on Sept 1st 2021. Learn more! ðŸš€ðŸš€ðŸš€

# neiesc's solution

## to Circular Buffer in the C# Track

Published at Apr 23 2020 · 0 comments
Instructions
Test suite
Solution

A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.

A circular buffer first starts empty and of some predefined length. For example, this is a 7-element buffer:

``````[ ][ ][ ][ ][ ][ ][ ]
``````

Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):

``````[ ][ ][ ][1][ ][ ][ ]
``````

Then assume that two more elements are added â€” 2 & 3 â€” which get appended after the 1:

``````[ ][ ][ ][1][2][3][ ]
``````

If two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 & 2, leaving the buffer with just a 3:

``````[ ][ ][ ][ ][ ][3][ ]
``````

If the buffer has 7 elements then it is completely full:

``````[6][7][8][9][3][4][5]
``````

When the buffer is full an error will be raised, alerting the client that further writes are blocked until a slot becomes free.

When the buffer is full, the client can opt to overwrite the oldest data with a forced write. In this case, two more elements â€” A & B â€” are added and they overwrite the 3 & 4:

``````[6][7][8][9][A][B][5]
``````

3 & 4 have been replaced by A & B making 5 now the oldest data in the buffer. Finally, if two elements are removed then what would be returned is 5 & 6 yielding the buffer:

``````[ ][7][8][9][A][B][ ]
``````

Because there is space available, if the client again uses overwrite to store C & D then the space where 5 & 6 were stored previously will be used not the location of 7 & 8. 7 is still the oldest element and the buffer is once again full.

``````[D][7][8][9][A][B][C]
``````

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit CircularBuffer.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

### CircularBufferTests.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using System;
using Xunit;

public class CircularBufferTests
{
[Fact]
{
var buffer = new CircularBuffer<int>(capacity: 1);
}

[Fact(Skip = "Remove this Skip property to run this test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
}

[Fact(Skip = "Remove this Skip property to run this test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
}

[Fact(Skip = "Remove this Skip property to run this test")]
{
var buffer = new CircularBuffer<int>(capacity: 2);
buffer.Write(1);
buffer.Write(2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Full_buffer_cant_be_written_to()
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
Assert.Throws<InvalidOperationException>(() => buffer.Write(2));
}

[Fact(Skip = "Remove this Skip property to run this test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
buffer.Write(2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
{
var buffer = new CircularBuffer<int>(capacity: 3);
buffer.Write(1);
buffer.Write(2);
buffer.Write(3);
}

[Fact(Skip = "Remove this Skip property to run this test")]
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
buffer.Clear();
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Clear_frees_up_capacity_for_another_write()
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Write(1);
buffer.Clear();
buffer.Write(2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Clear_does_nothing_on_empty_buffer()
{
var buffer = new CircularBuffer<int>(capacity: 1);
buffer.Clear();
buffer.Write(1);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Overwrite_acts_like_write_on_non_full_buffer()
{
var buffer = new CircularBuffer<int>(capacity: 2);
buffer.Write(1);
buffer.Overwrite(2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Overwrite_replaces_the_oldest_item_on_full_buffer()
{
var buffer = new CircularBuffer<int>(capacity: 2);
buffer.Write(1);
buffer.Write(2);
buffer.Overwrite(3);
}

[Fact(Skip = "Remove this Skip property to run this test")]
{
var buffer = new CircularBuffer<int>(capacity: 3);
buffer.Write(1);
buffer.Write(2);
buffer.Write(3);
buffer.Write(4);
buffer.Overwrite(5);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Initial_clear_does_not_affect_wrapping_around()
{
var buffer = new CircularBuffer<int>(capacity: 2);
buffer.Clear();
buffer.Write(1);
buffer.Write(2);
buffer.Overwrite(3);
buffer.Overwrite(4);
}
}``````
``````ï»¿using System;
using System.Collections.Generic;

public class CircularBuffer<T>
{
private readonly Queue<T> _buffer;
private readonly int _capacity;
public CircularBuffer(int capacity)
{
_buffer = new Queue<T>(capacity);
_capacity = capacity;
}

public T Read() => _buffer.Dequeue();

public void Write(T value)
{
if (_buffer.Count + 1 > _capacity)
{
throw new InvalidOperationException();
}
_buffer.Enqueue(value);
}

public void Overwrite(T value)
{
if (_buffer.Count + 1 > _capacity)
{
_buffer.Dequeue();
}
_buffer.Enqueue(value);
}

public void Clear() => _buffer.Clear();
}``````

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?