Class Ring<T>
- Namespace
- LfrlAnvil.Collections
- Assembly
- LfrlAnvil.Collections.dll
Represents a generic circular range of elements with constant Count.
public class Ring<T> : IRing<T>, IReadOnlyRing<T>, IReadOnlyList<T?>, IReadOnlyCollection<T?>, IEnumerable<T?>, IEnumerable
Type Parameters
T
Element type.
- Inheritance
-
Ring<T>
- Implements
-
IRing<T>IEnumerable<T>
- Inherited Members
- Extension Methods
Constructors
Ring(IEnumerable<T?>)
Creates a new Ring<T> instance from the provided collection.
public Ring(IEnumerable<T?> range)
Parameters
range
IEnumerable<T>Initial collection of elements.
Exceptions
- ArgumentOutOfRangeException
When collection is empty.
Ring(int)
Creates a new Ring<T> instance.
public Ring(int count)
Parameters
count
intNumber of elements.
Exceptions
- ArgumentOutOfRangeException
When
count
is less than 1.
Ring(params T?[])
Creates a new Ring<T> instance from the provided collection.
public Ring(params T?[] range)
Parameters
range
T[]Initial collection of elements.
Exceptions
- ArgumentOutOfRangeException
When collection is empty.
Properties
Count
Gets the number of elements in the collection.
public int Count { get; }
Property Value
- int
The number of elements in the collection.
this[int]
Gets or sets an element at the specified position.
public T? this[int index] { get; set; }
Parameters
index
int0-based element position.
Property Value
- T
WriteIndex
Specifies a 0-based index of the next position that this ring will overwrite.
public int WriteIndex { get; set; }
Property Value
Methods
Clear()
Resets all elements in this ring to default value and sets WriteIndex to 0.
public void Clear()
GetEnumerator()
Creates a new Ring<T>.Enumerator instance for this ring.
[Pure]
public Ring<T>.Enumerator GetEnumerator()
Returns
- Ring<T>.Enumerator
New Ring<T>.Enumerator instance.
GetWrappedIndex(int)
Calculates a 0-based index within the bounds of this ring.
[Pure]
public int GetWrappedIndex(int index)
Parameters
index
intBase index to calculate.
Returns
- int
0-based index within the bounds of this ring.
Exceptions
- DivideByZeroException
When size of this ring is equal to 0.
GetWriteIndex(int)
Calculates a 0-based index within the bounds of this ring from (WriteIndex + offset
) expression.
[Pure]
public int GetWriteIndex(int offset)
Parameters
offset
intValue to add to the WriteIndex.
Returns
- int
0-based index within the bounds of this ring.
Exceptions
- DivideByZeroException
When size of this ring is equal to 0.
Read(int)
Creates a new IEnumerable<T> instance that enumerates over all elements of this ring,
starting from the provided readIndex
.
[Pure]
public IEnumerable<T?> Read(int readIndex)
Parameters
readIndex
int0-based index of an element to start at.
Returns
- IEnumerable<T>
New IEnumerable<T> instance.
SetNext(T)
Sets a value of the next element of this ring located at the WriteIndex and increments the WriteIndex by 1.
public void SetNext(T item)
Parameters
item
TValue to set.
Exceptions
- IndexOutOfRangeException
When size of this ring is equal to 0.