Created
November 21, 2023 20:19
-
-
Save Echooff3/e48ec612a3cd869437ffe1449df84e61 to your computer and use it in GitHub Desktop.
c# circular buffer
This file contains hidden or 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
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