Table of Contents

IndexedSet<T> Class

Definition

Namespace
Bodu.Collections.Generic
Assembly
Bodu.Collections.dll
Package
Bodu.Collections 1.0.0
Source
IndexedSet{T}.Enumerator.cs

Represents an index-addressable unique list - an insertion-ordered collection of unique elements that exposes the full IList<T> contract.

public sealed class IndexedSet<T> : IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable where T : notnull

Type Parameters

T

The type of elements in the set. Elements must not be null.

Inheritance
IndexedSet<T>
Implements
Inherited Members
Extension Methods

Examples

// Build a deduplicated playlist that can still be reordered by index.
var playlist = new IndexedSet<string>(StringComparer.OrdinalIgnoreCase);

playlist.Add("Intro");
playlist.Add("Verse");
playlist.Add("Chorus");
bool added = playlist.Add("verse"); // false - already present under case-insensitive comparer

playlist.Insert(0, "Cold Open");    // positional insertion preserves uniqueness
playlist.Move(oldIndex: 1, newIndex: 3);

int chorusIndex = playlist.IndexOf("Chorus"); // O(1) lookup via the hash index
string second   = playlist[1];                // O(1) positional read

Remarks

IndexedSet<T> shares its backing storage with OrderedSet<T> via the internal Bodu.Collections.Generic.OrderedSetStorage<T> engine: a contiguous element array for deterministic index order plus an open-addressing hash table for O(1) Contains(T) and IndexOf(T). No BCL collection types are used as backing storage.

Use IndexedSet<T> when callers need positional mutation - Insert(int, T), RemoveAt(int), Move(int, int), or the indexer setter. Use OrderedSet<T> when the public surface is conceptually a set and indices exist only as a read-only view onto insertion order.

This type is not thread-safe.

Constructors

IndexedSet()

Initializes a new instance of the IndexedSet<T> class using the default capacity and comparer.

public IndexedSet()

IndexedSet(IEnumerable<T>)

Initializes a new instance of the IndexedSet<T> class containing the unique elements from collection.

public IndexedSet(IEnumerable<T> collection)

Parameters

collection IEnumerable<T>

The source collection. Must not be null.

Exceptions

ArgumentNullException

collection is null.

IndexedSet(IEnumerable<T>, IEqualityComparer<T>?)

Initializes a new instance of the IndexedSet<T> class containing the unique elements from collection.

public IndexedSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer)

Parameters

collection IEnumerable<T>

The source collection. Must not be null.

comparer IEqualityComparer<T>

The equality comparer, or null to use the default comparer.

Exceptions

ArgumentNullException

collection is null.

IndexedSet(IEqualityComparer<T>?)

Initializes a new instance of the IndexedSet<T> class using the specified comparer.

public IndexedSet(IEqualityComparer<T>? comparer)

Parameters

comparer IEqualityComparer<T>

The equality comparer, or null to use the default comparer.

IndexedSet(int)

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

public IndexedSet(int capacity)

Parameters

capacity int

The initial element capacity.

Exceptions

ArgumentOutOfRangeException

capacity is negative.

IndexedSet(int, IEqualityComparer<T>?)

Initializes a new instance of the IndexedSet<T> class with the specified initial capacity and comparer.

public IndexedSet(int capacity, IEqualityComparer<T>? comparer)

Parameters

capacity int

The initial element capacity.

comparer IEqualityComparer<T>

The equality comparer, or null to use the default comparer.

Exceptions

ArgumentOutOfRangeException

capacity is negative.

Properties

Capacity

Gets the allocated element capacity.

public int Capacity { get; }

Property Value

int

The current allocated capacity of the underlying element storage.

Comparer

Gets the equality comparer used to compare elements.

public IEqualityComparer<T> Comparer { get; }

Property Value

IEqualityComparer<T>

The active equality comparer.

Count

Gets the number of elements in the set.

public int Count { get; }

Property Value

int

The number of elements currently stored in the set.

IsReadOnly

Gets a value indicating whether the set is read-only.

public bool IsReadOnly { get; }

Property Value

bool

Always false.

this[int]

Gets or replaces the element at the specified zero-based index.

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

Parameters

index int

The zero-based index of the element to access.

Property Value

T

The element at index.

Exceptions

ArgumentNullException

The replacement value is null.

ArgumentOutOfRangeException

index is negative or greater than or equal to Count.

ArgumentException

The replacement value already exists at another index.

Methods

Add(T)

Adds the specified item to the end of the set.

public bool Add(T item)

Parameters

item T

The item to add. Must not be null.

Returns

bool

true if the item was added; otherwise, false when the set already contained it.

Exceptions

ArgumentNullException

item is null.

AddRange(IEnumerable<T>)

Adds each unique item from collection.

public int AddRange(IEnumerable<T> collection)

Parameters

collection IEnumerable<T>

The source collection. Must not be null.

Returns

int

The number of items added.

Exceptions

ArgumentNullException

collection is null.

Clear()

Removes all elements from the set.

public void Clear()

Contains(T)

Determines whether the set contains the specified item.

public bool Contains(T item)

Parameters

item T

The item to locate. Must not be null.

Returns

bool

true if the item exists; otherwise, false.

Exceptions

ArgumentNullException

item is null.

CopyTo(T[], int)

Copies the elements to the specified array starting at arrayIndex.

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

Parameters

array T[]

The destination array. Must not be null.

arrayIndex int

The destination start index.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

arrayIndex is negative.

ArgumentException

array does not have enough space starting at arrayIndex.

EnsureCapacity(int)

Ensures that the set can hold at least the specified number of items without reallocating storage.

public int EnsureCapacity(int capacity)

Parameters

capacity int

The desired item capacity.

Returns

int

The resulting item capacity.

Exceptions

ArgumentOutOfRangeException

capacity is negative.

GetEnumerator()

Returns an enumerator that iterates through the set in insertion order.

public IndexedSet<T>.Enumerator GetEnumerator()

Returns

IndexedSet<T>.Enumerator

An IndexedSet<T>.Enumerator over the set elements.

IndexOf(T)

Returns the zero-based index of the specified item.

public int IndexOf(T item)

Parameters

item T

The item to locate. Must not be null.

Returns

int

The zero-based index of the item, or -1 if it is not present.

Exceptions

ArgumentNullException

item is null.

Insert(int, T)

Inserts the specified item at the specified position.

public void Insert(int index, T item)

Parameters

index int

The insertion index in the range [0, Count].

item T

The item to insert. Must not be null.

Exceptions

ArgumentNullException

item is null.

ArgumentOutOfRangeException

index is negative or greater than Count.

ArgumentException

The item already exists in the set.

Move(int, int)

Moves an existing element from one index to another.

public void Move(int oldIndex, int newIndex)

Parameters

oldIndex int

The current zero-based index.

newIndex int

The target zero-based index.

Exceptions

ArgumentOutOfRangeException

oldIndex or newIndex is negative or greater than or equal to Count.

Remove(T)

Removes the specified item from the set.

public bool Remove(T item)

Parameters

item T

The item to remove. Must not be null.

Returns

bool

true if the item was removed; otherwise, false.

Exceptions

ArgumentNullException

item is null.

RemoveAt(int)

Removes the element at the specified index.

public void RemoveAt(int index)

Parameters

index int

The zero-based index to remove.

Exceptions

ArgumentOutOfRangeException

index is negative or greater than or equal to Count.

ToArray()

Copies the elements to a new array in insertion order.

public T[] ToArray()

Returns

T[]

A new array containing the set elements.

TrimExcess()

Shrinks the underlying storage to the current element count.

public void TrimExcess()

TryInsert(int, T)

Attempts to insert the specified item at the specified position.

public bool TryInsert(int index, T item)

Parameters

index int

The insertion index in the range [0, Count].

item T

The item to insert. Must not be null.

Returns

bool

true if the item was inserted; otherwise, false when the set already contained it.

Exceptions

ArgumentNullException

item is null.

ArgumentOutOfRangeException

index is negative or greater than Count.

Explicit Interface Implementations

ICollection<T>.Add(T)

Adds item via the Add(T) contract.

void ICollection<T>.Add(T item)

Parameters

item T

The item to add.

Remarks

Discards the boolean result of Add(T); callers that need to detect a duplicate-add should invoke the typed Add(T) overload directly.

IEnumerable<T>.GetEnumerator()

Returns an enumerator that iterates through the collection.

IEnumerator<T> IEnumerable<T>.GetEnumerator()

Returns

IEnumerator<T>

An enumerator that can be used to iterate through the collection.

IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

IEnumerator IEnumerable.GetEnumerator()

Returns

IEnumerator

An IEnumerator object that can be used to iterate through the collection.

Applies to

ProductVersions
.NET8, 10