Class CircularBuffer<T>
- Namespace
- FishMMO.Shared
- Assembly
- FishMMO-SharedUtility.dll
A circular doubly linked list designed for high-performance addition/removal of reference types. All public members are thread-safe.
public class CircularBuffer<T> where T : class
Type Parameters
TMust be a reference type.
- Inheritance
-
CircularBuffer<T>
- Inherited Members
Properties
Count
Gets the number of nodes currently in the buffer.
public int Count { get; }
Property Value
Head
Gets a snapshot of the head node. Note that the returned CircularBuffer<T>.Node reference is only a snapshot taken under the lock. By the time you use it, the node may have been removed or its Value may have been cleared by another thread. For safe value access use TryPeekHead(out T) instead.
public CircularBuffer<T>.Node? Head { get; }
Property Value
Tail
Gets a snapshot of the tail node. Note that the returned CircularBuffer<T>.Node reference is only a snapshot taken under the lock. By the time you use it, the node may have been removed or its Value may have been cleared by another thread. For safe value access use TryPeekTail(out T) instead.
public CircularBuffer<T>.Node? Tail { get; }
Property Value
Methods
Add(T, Action<Node>?, Action?)
Adds an item to the end (tail) of the buffer.
public CircularBuffer<T>.Node Add(T item, Action<CircularBuffer<T>.Node>? onAddCallback = null, Action? onRemoveCallback = null)
Parameters
itemTonAddCallbackAction<CircularBuffer<T>.Node>onRemoveCallbackAction
Returns
Clear()
Removes all nodes from the buffer.
public void Clear()
Empty()
Returns true if the buffer has no nodes. Uses the same underlying check as Peek() for consistency.
public bool Empty()
Returns
GetValues()
Enumerates the values once from Head to Tail. Returns a snapshot copy so enumeration is safe outside the lock.
public IEnumerable<T> GetValues()
Returns
- IEnumerable<T>
Peek()
Returns true if the buffer has at least one node, regardless of the head node's value. Uses the internal count instead of checking head.Value to avoid conflating an empty buffer with a null-valued node.
public bool Peek()
Returns
Pop()
Removes and returns the value of the Tail node.
public T? Pop()
Returns
- T
Remove(Node?)
Removes a specific node from the circle and repairs the links. Validates that the node belongs to this list before modifying links.
public void Remove(CircularBuffer<T>.Node? node)
Parameters
nodeCircularBuffer<T>.Node
TryPeekHead(out T)
Safely reads the head value under the lock.
public bool TryPeekHead(out T value)
Parameters
valueTWhen this method returns, contains the value of the head node, or
defaultif the buffer is empty.
Returns
- bool
trueif the buffer contained at least one node; otherwisefalse.
TryPeekTail(out T)
Safely reads the tail value under the lock.
public bool TryPeekTail(out T value)
Parameters
valueTWhen this method returns, contains the value of the tail node, or
defaultif the buffer is empty.
Returns
- bool
trueif the buffer contained at least one node; otherwisefalse.