Table of Contents

Class SwiftSlotMap<T>

Namespace
SwiftCollections
Assembly
SwiftCollections.dll

Represents a high-performance map collection that assigns and manages stable integer indices for stored items. Provides O(1) insertion, removal, and lookup by internally generated index.

[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public sealed class SwiftSlotMap<T> : IStateBacked<SwiftSlotMapState<T>>, ISwiftCloneable<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, IMemoryPackable<SwiftSlotMap<T>>, IMemoryPackFormatterRegister

Type Parameters

T

Specifies the type of elements in the map.

Inheritance
SwiftSlotMap<T>
Implements
IStateBacked<SwiftSlotMapState<T>>
IMemoryPackable<SwiftSlotMap<T>>
IMemoryPackFormatterRegister
Inherited Members
Extension Methods

Remarks

Unlike SwiftSparseMap<T>, which requires callers to provide the key used to store values, SwiftSlotMap<T> internally generates and manages indices for each inserted item.

These indices remain stable for the lifetime of the item unless it is removed.

The container is optimized for scenarios requiring:

  • Stable integer slots.
  • Fast addition and removal.
  • Array-backed storage; iteration scans allocated slots and skips holes.

Efficient Lookups Using Indices: When you add items to the map using the Add(T) method, it returns an arrayIndex that you can store externally. You can then use this arrayIndex to access the item directly via the indexer, and check if it's still present using the IsAllocated(int) method. This approach allows for O(1) time complexity for lookups and existence checks, avoiding the need for O(n) searches using methods like IndexOf(T) or Contains(T).

Note: iteration over the collection does not follow any guaranteed order and depends on internal allocation.

Constructors

SwiftSlotMap()

Initializes a new instance of the SwiftSlotMap<T> class.

public SwiftSlotMap()

SwiftSlotMap(SwiftSlotMapState<T>)

Initializes a new instance of the SwiftSlotMap<T> class with the specified SwiftArrayState<T>.

[MemoryPackConstructor]
public SwiftSlotMap(SwiftSlotMapState<T> state)

Parameters

state SwiftSlotMapState<T>

The state containing the internal array, count, offset, and version for initialization.

SwiftSlotMap(int)

Initializes a new instance of the SwiftSlotMap<T> class with the specified capacity.

public SwiftSlotMap(int capacity)

Parameters

capacity int

The initial capacity of the map.

Fields

DefaultCapacity

Represents the default initial capacity used when no specific capacity is provided.

public const int DefaultCapacity = 8

Field Value

int

Properties

Capacity

Gets the total capacity of the SwiftSlotMap<T>.

[JsonIgnore]
[MemoryPackIgnore]
public int Capacity { get; }

Property Value

int

Count

Gets the number of elements contained in the SwiftSlotMap<T>.

[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }

Property Value

int

IsReadOnly

[JsonIgnore]
[MemoryPackIgnore]
public bool IsReadOnly { get; }

Property Value

bool

IsSynchronized

[JsonIgnore]
[MemoryPackIgnore]
public bool IsSynchronized { get; }

Property Value

bool

this[int]

Gets or sets the element at the specified arrayIndex. Throws InvalidOperationException if the arrayIndex is invalid or unallocated.

[JsonIgnore]
[MemoryPackIgnore]
public T this[int index] { get; set; }

Parameters

index int

The zero-based arrayIndex of the element to get or set.

Property Value

T

PeakCount

Gets the highest value recorded for the count during the lifetime of the object.

[JsonIgnore]
[MemoryPackIgnore]
public int PeakCount { get; }

Property Value

int

State

Gets or sets the current state of the map, including all items, allocation status, and free indices.

[JsonInclude]
[MemoryPackInclude]
public SwiftSlotMapState<T> State { get; }

Property Value

SwiftSlotMapState<T>

Remarks

Use this property to capture or restore the complete state of the map, such as for serialization or checkpointing scenarios. Setting this property replaces the entire internal state, including items and allocation metadata.

SyncRoot

[JsonIgnore]
[MemoryPackIgnore]
public object SyncRoot { get; }

Property Value

object

Methods

Add(T)

Adds an item to the map and returns its arrayIndex.

public int Add(T item)

Parameters

item T

The item to add.

Returns

int

The arrayIndex where the item was added.

Clear()

Removes all items from the map.

public void Clear()

Clear(bool)

Removes all elements, optionally releasing references throughout retained backing storage.

public void Clear(bool clearRetainedReferences)

Parameters

clearRetainedReferences bool

If true, also releases references outside the live range; otherwise uses ordinary clearing.

Remarks

Capacity is preserved. Full reference cleanup can take time proportional to retained capacity.

CloneTo(ICollection<T>)

Clones the entire ISwiftCloneable<T> into a new target ICollection<T>, ensuring that the target list is an exact copy. Clears the target list first to match the structure and state of the source list exactly.

public void CloneTo(ICollection<T> output)

Parameters

output ICollection<T>

Contains(T)

Determines whether the map contains a specific value.

public bool Contains(T item)

Parameters

item T

The object to locate in the map.

Returns

bool

true if item is found; otherwise, false.

Remarks

This method performs a linear search and has a time complexity of O(n). It is recommended to store the indices returned by the Add(T) method for faster lookups using the indexer.

CopyTo(T[], int)

Copies the elements of the map to an Array, starting at a particular Array arrayIndex.

public void CopyTo(T[] array, int arrayIndex)

Parameters

array T[]

The one-dimensional Array that is the destination of the elements copied from map.

arrayIndex int

The zero-based arrayIndex in array at which copying begins.

EnsureCapacity(int)

Ensures that the internal storage has at least the specified capacity, expanding it if necessary.

public void EnsureCapacity(int capacity)

Parameters

capacity int

The minimum number of elements that the internal storage should be able to hold. Must be a non-negative value.

Remarks

If the current capacity is less than the specified value, the internal storage is increased to the next power of two greater than or equal to the requested capacity. No action is taken if the current capacity is sufficient.

Exists(Predicate<T>)

Determines whether the SwiftSlotMap<T> contains an element that matches the conditions defined by the specified predicate.

public bool Exists(Predicate<T> match)

Parameters

match Predicate<T>

The predicate that defines the conditions of the element to search for.

Returns

bool

true if the SwiftSlotMap<T> contains one or more elements that match the specified predicate; otherwise, false.

Find(Predicate<T>)

Searches for an element that matches the conditions defined by the specified predicate, and returns the first matching element in map iteration order.

public T Find(Predicate<T> match)

Parameters

match Predicate<T>

The predicate that defines the conditions of the element to search for.

Returns

T

The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

GetEnumerator()

Returns an enumerator that iterates through the SwiftSlotMap<T>.

public SwiftSlotMap<T>.SwiftSlotMapEnumerator GetEnumerator()

Returns

SwiftSlotMap<T>.SwiftSlotMapEnumerator

An enumerator for the map.

IndexOf(T)

Searches for the specified object and returns the zero-based arrayIndex of the first occurrence within the map.

public int IndexOf(T item)

Parameters

item T

The object to locate in the map.

Returns

int

The zero-based arrayIndex of the first occurrence of item within the map, if found; otherwise, -1.

Remarks

This method performs a linear search and has a time complexity of O(n). It is recommended to store the indices returned by the Add(T) method for faster lookups using the indexer.

InsertAt(int, T)

Inserts an item at the specified arrayIndex. If an item already exists at that arrayIndex, it will be replaced.

public void InsertAt(int index, T item)

Parameters

index int

The arrayIndex at which to insert the item.

item T

The item to insert.

IsAllocated(int)

Determines whether the element at the specified index is currently allocated.

public bool IsAllocated(int index)

Parameters

index int

The zero-based index of the element to check. Must be greater than or equal to 0 and less than the length of the underlying array.

Returns

bool

true if the element at the specified index is allocated; otherwise, false.

RemoveAt(int)

Removes the item at the specified arrayIndex.

public void RemoveAt(int index)

Parameters

index int

The arrayIndex of the item to remove.

TrimExcessCapacity()

Reduces unused tail capacity while preserving stable indices for all currently allocated entries.

public void TrimExcessCapacity()

TryGetValue(int, out T)

Attempts to get the value at the specified arrayIndex.

public bool TryGetValue(int key, out T value)

Parameters

key int

The arrayIndex of the item to get.

value T

When this method returns, contains the value associated with the specified arrayIndex, if the arrayIndex is found; otherwise, the default value for the type of the value parameter.

Returns

bool

true if the map contains an element at the specified arrayIndex; otherwise, false.

TryRemove(T)

Removes the first occurrence of a specific object from the map.

public bool TryRemove(T item)

Parameters

item T

The object to remove.

Returns

bool

true if item was successfully removed; otherwise, false.

TryRemoveAt(int)

Removes the item at the specified arrayIndex if it has been allocated.

public bool TryRemoveAt(int index)

Parameters

index int

Returns

bool