Table of Contents

Class SparseSet<T>

Namespace
Misaki.HighPerformance.Collections
Assembly
Misaki.HighPerformance.dll

A sparse set data structure that provides O(1) insertion, deletion, and lookup operations. The sparse set uses three arrays: a dense array for storing values, a sparse array for mapping indices, and a reverse array for mapping dense indices back to sparse indices. Sparse indices work like entity IDs and are automatically generated.

public class SparseSet<T> : IEnumerable<T>, IEnumerable where T : notnull

Type Parameters

T

Represents a type that can be stored in the sparse set.

Inheritance
SparseSet<T>
Implements
Inherited Members

Constructors

SparseSet(int)

Initializes a new instance of SparseSet with a specified capacity and an allocation handle.

public SparseSet(int capacity = 4)

Parameters

capacity int

Specifies the initial capacity of the sparse set, which must be greater than zero.

Exceptions

ArgumentOutOfRangeException

Thrown when the specified capacity is less than or equal to zero.

Properties

Capacity

public int Capacity { get; }

Property Value

int

Count

public int Count { get; }

Property Value

int

Methods

Add(T, out int)

Adds a value to the sparse set and returns a unique sparse index for the value.

public int Add(T value, out int generation)

Parameters

value T

The value to add to the sparse set.

generation int

Outputs the generation number associated with the added value.

Returns

int

A unique sparse index that can be used to reference this value.

Clear()

Clears the sparse set, removing all values and resetting internal state.

public void Clear()

Contains(int, int)

Checks if the sparse set contains a value at the specified sparse index.

public bool Contains(int sparseIndex, int generation)

Parameters

sparseIndex int

The sparse index to check.

generation int

The generation number to validate against the stored generation.

Returns

bool

True if the sparse index is valid and contains a value, false otherwise.

GetEnumerator()

public SparseSet<T>.Enumerator GetEnumerator()

Returns

SparseSet<T>.Enumerator

GetValue(int, int)

Gets the value at the specified sparse index and generation.

public T GetValue(int sparseIndex, int generation)

Parameters

sparseIndex int

The sparse index to retrieve the value from.

generation int

The generation number to validate against the stored generation.

Returns

T

The value at the specified sparse index.

Exceptions

ArgumentOutOfRangeException

Thrown when the sparse index is not found.

GetValueReference(int, int, out bool)

Gets reference of the value at the specified sparse index and generation.

public ref T GetValueReference(int sparseIndex, int generation, out bool exist)

Parameters

sparseIndex int

The sparse index to retrieve the value from.

generation int

The generation number to validate against the stored generation.

exist bool

Outputs whether the sparse index exists in the set.

Returns

T

Reference of the value at the specified sparse index.

Exceptions

ArgumentOutOfRangeException

Thrown when the sparse index is not found.

Remove(int, int)

Removes the value at the specified sparse index.

public bool Remove(int sparseIndex, int generation)

Parameters

sparseIndex int

The sparse index of the value to remove.

generation int

The generation number associated with the sparse index to validate.

Returns

bool

True if the value was removed, false if the sparse index was not found.

Resize(int)

Resizes the sparse set to accommodate more elements.

public void Resize(int newSize)

Parameters

newSize int

The new size of the sparse set.

Exceptions

ArgumentOutOfRangeException

Thrown when the new size is not positive.

SetValue(int, int, T)

Updates the value at the specified sparse index.

public bool SetValue(int sparseIndex, int generation, T value)

Parameters

sparseIndex int

The sparse index of the value to update.

generation int

The generation number to validate against the stored generation.

value T

The new value.

Returns

bool

True if the value was updated, false if the sparse index was not found.

TryGetValue(int, int, out T)

Gets the value at the specified sparse index and generation.

public bool TryGetValue(int sparseIndex, int generation, out T value)

Parameters

sparseIndex int

The sparse index to retrieve the value from.

generation int

The generation number to validate against the stored generation.

value T

When this method returns, contains the value at the specified sparse index, if found.

Returns

bool

True if the sparse index contains a value, false otherwise.