Skip to content

Commit

Permalink
Add IEnumerable interface to RingBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanite committed Jan 15, 2025
1 parent ed24f1c commit 49c9150
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Collections/RingBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Exanite.Core.Utilities;

namespace Exanite.Core.Collections
{
/// <summary>
/// Represents a fixed-sized circular queue.
/// </summary>
public class RingBuffer<T>
public class RingBuffer<T> : IEnumerable<T>
{
private readonly T[] data;
private readonly int capacityMask;
Expand Down Expand Up @@ -146,5 +148,18 @@ public bool TryPeek(out T? value)
value = data[capacityMask & readOffset];
return true;
}

public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i < Count; i++)
{
yield return this[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

0 comments on commit 49c9150

Please sign in to comment.