Skip to content

Commit c123ca4

Browse files
committed
Add ToArraySegments support
Fixes #26
1 parent ab2645d commit c123ca4

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

CircularBuffer.Tests/CircularBufferTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NUnit.Framework;
22
using System;
3+
using System.Linq;
34

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

120+
[Test]
121+
public void CircularBuffer_ToArraySegmentsConstructorDefinedArray_CorrectContent()
122+
{
123+
var buffer = new CircularBuffer<int>(5, new[] { 0, 1, 2, 3 });
124+
125+
var arraySegments = buffer.ToArraySegments();
126+
127+
Assert.That(arraySegments.Count, Is.EqualTo(2)); // length of 2 is part of the contract.
128+
Assert.That(arraySegments.SelectMany(x => x), Is.EqualTo(new[] { 0, 1, 2, 3 }));
129+
}
130+
131+
[Test]
132+
public void CircularBuffer_ToArraySegmentsOverflowedBuffer_CorrectContent()
133+
{
134+
var buffer = new CircularBuffer<int>(5);
135+
136+
for (int i = 0; i < 10; i++)
137+
{
138+
buffer.PushBack(i);
139+
}
140+
141+
var arraySegments = buffer.ToArraySegments();
142+
Assert.That(arraySegments.Count, Is.EqualTo(2)); // length of 2 is part of the contract.
143+
Assert.That(arraySegments.SelectMany(x => x), Is.EqualTo(new[] { 5, 6, 7, 8, 9 }));
144+
}
145+
119146
[Test]
120147
public void CircularBuffer_PushFront_CorrectContent()
121148
{

CircularBuffer/CircularBuffer.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public T[] ToArray()
260260
{
261261
T[] newArray = new T[Size];
262262
int newArrayOffset = 0;
263-
var segments = new ArraySegment<T>[2] { ArrayOne(), ArrayTwo() };
263+
var segments = ToArraySegments();
264264
foreach (ArraySegment<T> segment in segments)
265265
{
266266
Array.Copy(segment.Array, segment.Offset, newArray, newArrayOffset, segment.Count);
@@ -269,14 +269,31 @@ public T[] ToArray()
269269
return newArray;
270270
}
271271

272+
/// <summary>
273+
/// Get the contents of the buffer as 2 ArraySegments.
274+
/// Respects the logical contents of the buffer, where
275+
/// each segment and items in each segment are ordered
276+
/// according to insertion.
277+
///
278+
/// Fast: does not copy the array elements.
279+
/// Useful for methods like <c>Send(IList&lt;ArraySegment&lt;Byte&gt;&gt;)</c>.
280+
///
281+
/// <remarks>Segments may be empty.</remarks>
282+
/// </summary>
283+
/// <returns>An IList with 2 segments corresponding to the buffer content.</returns>
284+
public IList<ArraySegment<T>> ToArraySegments()
285+
{
286+
return new [] { ArrayOne(), ArrayTwo() };
287+
}
288+
272289
#region IEnumerable<T> implementation
273290
/// <summary>
274291
/// Returns an enumerator that iterates through this buffer.
275292
/// </summary>
276293
/// <returns>An enumerator that can be used to iterate this collection.</returns>
277294
public IEnumerator<T> GetEnumerator()
278295
{
279-
var segments = new ArraySegment<T>[2] { ArrayOne(), ArrayTwo() };
296+
var segments = ToArraySegments();
280297
foreach (ArraySegment<T> segment in segments)
281298
{
282299
for (int i = 0; i < segment.Count; i++)

0 commit comments

Comments
 (0)