Table of Contents

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

T

Must 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

int

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

CircularBuffer<T>.Node

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

CircularBuffer<T>.Node

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

item T
onAddCallback Action<CircularBuffer<T>.Node>
onRemoveCallback Action

Returns

CircularBuffer<T>.Node

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

bool

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

bool

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

node CircularBuffer<T>.Node

TryPeekHead(out T)

Safely reads the head value under the lock.

public bool TryPeekHead(out T value)

Parameters

value T

When this method returns, contains the value of the head node, or default if the buffer is empty.

Returns

bool

true if the buffer contained at least one node; otherwise false.

TryPeekTail(out T)

Safely reads the tail value under the lock.

public bool TryPeekTail(out T value)

Parameters

value T

When this method returns, contains the value of the tail node, or default if the buffer is empty.

Returns

bool

true if the buffer contained at least one node; otherwise false.