-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
|
||
namespace DotRecast.Core.Buffers | ||
{ | ||
public class RcCyclicBuffer<T> | ||
{ | ||
public int MinIndex { get; private set; } | ||
public int MaxIndex { get; private set; } | ||
public int Count => MaxIndex - MinIndex + 1; | ||
public readonly int Size; | ||
|
||
public T this[int index] => Get(index); | ||
|
||
private readonly T[] _buffer; | ||
|
||
public RcCyclicBuffer(in int size) | ||
{ | ||
_buffer = new T[size]; | ||
Size = size; | ||
MinIndex = 0; | ||
MaxIndex = -1; | ||
} | ||
|
||
public void Add(in T item) | ||
{ | ||
MaxIndex++; | ||
var index = MaxIndex % Size; | ||
|
||
if (MaxIndex >= Size) | ||
MinIndex = MaxIndex - Size + 1; | ||
|
||
_buffer[index] = item; | ||
} | ||
|
||
public T Get(in int index) | ||
{ | ||
if (index < MinIndex || index > MaxIndex) | ||
throw new ArgumentOutOfRangeException(); | ||
|
||
return _buffer[index % Size]; | ||
} | ||
|
||
public Span<T> AsSpan() | ||
{ | ||
return _buffer.AsSpan(0, Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters