Skip to content

Instantly share code, notes, and snippets.

@efruchter
Last active April 28, 2017 18:45
Show Gist options
  • Select an option

  • Save efruchter/1709a34ee4902cc46c3657ce3d386360 to your computer and use it in GitHub Desktop.

Select an option

Save efruchter/1709a34ee4902cc46c3657ce3d386360 to your computer and use it in GitHub Desktop.
Fixed-size array ring buffer that allows direct access to underlying array if needed.
/// <summary>
/// An array-based ring buffer. The internal array can be accessed directly if needed.
/// </summary>
public class ArrayRingBuffer<T> : IEnumerable<T> {
public readonly T[] array;
private int startingIndex = 0;
private int count;
public ArrayRingBuffer(int size, T defaultValue) {
array = new T[size];
for (int i = 0; i < array.Length; i++) {
array[i] = defaultValue;
}
}
public ArrayRingBuffer(int size) : this(size, default(T)) {
;
}
private int nextWriteIndex {
get {
return (startingIndex + count) % Capacity;
}
}
public void Enqueue(T t) {
array [nextWriteIndex] = t;
count++;
if (count > Capacity) {
startingIndex = CalculateIndex (1);
count = Capacity;
}
}
private int CalculateIndex(int relativeIndex) {
if (relativeIndex < 0 || relativeIndex >= Count) {
throw new System.ArgumentOutOfRangeException ("Index is out of range: " + relativeIndex);
}
return (startingIndex + relativeIndex) % Capacity;
}
public T this [int index] {
set {
array [CalculateIndex (index)] = value;
}
get {
return array [CalculateIndex (index)];
}
}
public int Capacity { get { return array.Length; } }
public int Count { get { return count; } }
#region IEnumerable implementation
public IEnumerator<T> GetEnumerator () {
for (int i = 0; i < Count; i++) {
yield return this [i];
}
}
#endregion
#region IEnumerable implementation
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () {
return GetEnumerator ();
}
#endregion
public override string ToString() {
System.Text.StringBuilder str = new System.Text.StringBuilder ();
str.Append ("{");
foreach (var t in this) {
str.Append (" ");
str.Append (t);
str.Append (" ");
}
str.Append ("}");
return str.ToString ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment