Skip to content

Commit

Permalink
Add ToArraySegments support
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
joaoportela committed Apr 1, 2021
1 parent ab2645d commit c123ca4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
27 changes: 27 additions & 0 deletions CircularBuffer.Tests/CircularBufferTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using System;
using System.Linq;

namespace CircularBuffer.Tests
{
Expand Down Expand Up @@ -116,6 +117,32 @@ public void CircularBuffer_ToArrayOverflowedBuffer_CorrectContent()
Assert.That(buffer.ToArray(), Is.EqualTo(new[] { 5, 6, 7, 8, 9 }));
}

[Test]
public void CircularBuffer_ToArraySegmentsConstructorDefinedArray_CorrectContent()
{
var buffer = new CircularBuffer<int>(5, new[] { 0, 1, 2, 3 });

var arraySegments = buffer.ToArraySegments();

Assert.That(arraySegments.Count, Is.EqualTo(2)); // length of 2 is part of the contract.
Assert.That(arraySegments.SelectMany(x => x), Is.EqualTo(new[] { 0, 1, 2, 3 }));
}

[Test]
public void CircularBuffer_ToArraySegmentsOverflowedBuffer_CorrectContent()
{
var buffer = new CircularBuffer<int>(5);

for (int i = 0; i < 10; i++)
{
buffer.PushBack(i);
}

var arraySegments = buffer.ToArraySegments();
Assert.That(arraySegments.Count, Is.EqualTo(2)); // length of 2 is part of the contract.
Assert.That(arraySegments.SelectMany(x => x), Is.EqualTo(new[] { 5, 6, 7, 8, 9 }));
}

[Test]
public void CircularBuffer_PushFront_CorrectContent()
{
Expand Down
21 changes: 19 additions & 2 deletions CircularBuffer/CircularBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public T[] ToArray()
{
T[] newArray = new T[Size];
int newArrayOffset = 0;
var segments = new ArraySegment<T>[2] { ArrayOne(), ArrayTwo() };
var segments = ToArraySegments();
foreach (ArraySegment<T> segment in segments)
{
Array.Copy(segment.Array, segment.Offset, newArray, newArrayOffset, segment.Count);
Expand All @@ -269,14 +269,31 @@ public T[] ToArray()
return newArray;
}

/// <summary>
/// Get the contents of the buffer as 2 ArraySegments.
/// Respects the logical contents of the buffer, where
/// each segment and items in each segment are ordered
/// according to insertion.
///
/// Fast: does not copy the array elements.
/// Useful for methods like <c>Send(IList&lt;ArraySegment&lt;Byte&gt;&gt;)</c>.
///
/// <remarks>Segments may be empty.</remarks>
/// </summary>
/// <returns>An IList with 2 segments corresponding to the buffer content.</returns>
public IList<ArraySegment<T>> ToArraySegments()
{
return new [] { ArrayOne(), ArrayTwo() };
}

#region IEnumerable<T> implementation
/// <summary>
/// Returns an enumerator that iterates through this buffer.
/// </summary>
/// <returns>An enumerator that can be used to iterate this collection.</returns>
public IEnumerator<T> GetEnumerator()
{
var segments = new ArraySegment<T>[2] { ArrayOne(), ArrayTwo() };
var segments = ToArraySegments();
foreach (ArraySegment<T> segment in segments)
{
for (int i = 0; i < segment.Count; i++)
Expand Down

0 comments on commit c123ca4

Please sign in to comment.