Skip to content

Instantly share code, notes, and snippets.

@Echooff3
Created November 21, 2023 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Echooff3/e48ec612a3cd869437ffe1449df84e61 to your computer and use it in GitHub Desktop.
Save Echooff3/e48ec612a3cd869437ffe1449df84e61 to your computer and use it in GitHub Desktop.
c# circular buffer
namespace Utils
{
public class CircularList<T> : List<T>
{
private int Index;
public CircularList() : this(0) { }
public CircularList(int index)
{
//if (index < 0 || index >= Count)
// throw new Exception(string.Format("Index must between {0} and {1}", 0, Count));
Index = index;
}
public T Current()
{
return this[Index];
}
public T Next()
{
Index++;
Index %= Count;
return this[Index];
}
public T Previous()
{
Index--;
if (Index < 0)
Index = Count - 1;
return this[Index];
}
public void Reset()
{
Index = 0;
}
public void MoveToEnd()
{
Index = Count - 1;
}
public bool IsLast()
{
return Index == Count - 1;
}
public T Offset(int offset)
{
Index += offset;
if(Index == 0 && offset == 0) { return this[Index]; }
Index %= Count;
if (Index < 0)
Index = Count - 1;
return this[Index];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment