System.Collections.Immutable by Microsoft

<PackageReference Include="System.Collections.Immutable" Version="10.0.0-preview.4.25258.110" />

.NET API 251,704 bytes

 FrozenSet<T>

public abstract class FrozenSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, ICollection
Provides an immutable, read-only set optimized for fast lookup and enumeration.
using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace System.Collections.Frozen { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] [CollectionBuilder(typeof(FrozenSet), "Create")] [DebuggerTypeProxy(typeof(ImmutableEnumerableDebuggerProxy<>))] [DebuggerDisplay("Count = {Count}")] public abstract class FrozenSet<[System.Runtime.CompilerServices.Nullable(2)] T> : ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, ICollection { internal delegate int AlternateLookupDelegate<TAlternate> (FrozenSet<T> set, TAlternate key); private static class AlternateLookupDelegateHolder<TAlternate> { public static readonly AlternateLookupDelegate<TAlternate> ReturnsNullRef = (FrozenSet<T> _, TAlternate _) => -1; } [System.Runtime.CompilerServices.NullableContext(0)] public struct Enumerator : IEnumerator<T>, IEnumerator, IDisposable { private readonly T[] _entries; private int _index; [System.Runtime.CompilerServices.Nullable(1)] public T Current { [System.Runtime.CompilerServices.IsReadOnly] [System.Runtime.CompilerServices.NullableContext(1)] get { if ((uint)_index >= (uint)_entries.Length) ThrowHelper.ThrowInvalidOperationException(); return _entries[_index]; } } [System.Runtime.CompilerServices.Nullable(1)] object IEnumerator.Current { get { return Current; } } internal Enumerator(T[] entries) { _entries = entries; _index = -1; } public bool MoveNext() { _index++; if ((uint)_index < (uint)_entries.Length) return true; _index = _entries.Length; return false; } void IEnumerator.Reset() { _index = -1; } void IDisposable.Dispose() { } } public static FrozenSet<T> Empty { get; } = new EmptyFrozenSet<T>(EqualityComparer<T>.Default); public IEqualityComparer<T> Comparer { get; } [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public ImmutableArray<T> Items { [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] get { return ImmutableCollectionsMarshal.AsImmutableArray<T>(ItemsCore); } } private protected abstract T[] ItemsCore { get; } public int Count => CountCore; private protected abstract int CountCore { get; } bool ICollection<T>.IsReadOnly { get { return true; } } bool ICollection.IsSynchronized { get { return false; } } object ICollection.SyncRoot { get { return this; } } private protected FrozenSet(IEqualityComparer<T> comparer) { Comparer = comparer; } public void CopyTo(T[] destination, int destinationIndex) { ExceptionPolyfills.ThrowIfNull(destination, "destination"); CopyTo(MemoryExtensions.AsSpan<T>(destination, destinationIndex)); } public void CopyTo([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] Span<T> destination) { Items.AsSpan().CopyTo(destination); } void ICollection.CopyTo(Array array, int index) { if (array != null && array.Rank != 1) throw new ArgumentException(System.SR.Arg_RankMultiDimNotSupported, "array"); T[] itemsCore = ItemsCore; Array.Copy(itemsCore, 0, array, index, itemsCore.Length); } public bool Contains(T item) { return FindItemIndex(item) >= 0; } public bool TryGetValue(T equalValue, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out T actualValue) { int num = FindItemIndex(equalValue); if (num >= 0) { actualValue = Items[num]; return true; } actualValue = default(T); return false; } private protected abstract int FindItemIndex(T item); private protected virtual AlternateLookupDelegate<TAlternate> GetAlternateLookupDelegate<TAlternate>() { return AlternateLookupDelegateHolder<TAlternate>.ReturnsNullRef; } [System.Runtime.CompilerServices.NullableContext(0)] public Enumerator GetEnumerator() { return GetEnumeratorCore(); } private protected abstract Enumerator GetEnumeratorCore(); IEnumerator<T> IEnumerable<T>.GetEnumerator() { if (Count != 0) return GetEnumerator(); return ((IEnumerable<T>)Array.Empty<T>()).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { if (Count != 0) return GetEnumerator(); return Array.Empty<T>().GetEnumerator(); } bool ISet<T>.Add(T item) { throw new NotSupportedException(); } void ISet<T>.ExceptWith(IEnumerable<T> other) { throw new NotSupportedException(); } void ISet<T>.IntersectWith(IEnumerable<T> other) { throw new NotSupportedException(); } void ISet<T>.SymmetricExceptWith(IEnumerable<T> other) { throw new NotSupportedException(); } void ISet<T>.UnionWith(IEnumerable<T> other) { throw new NotSupportedException(); } void ICollection<T>.Add(T item) { throw new NotSupportedException(); } void ICollection<T>.Clear() { throw new NotSupportedException(); } bool ICollection<T>.Remove(T item) { throw new NotSupportedException(); } public bool IsProperSubsetOf(IEnumerable<T> other) { ExceptionPolyfills.ThrowIfNull(other, "other"); return IsProperSubsetOfCore(other); } private protected abstract bool IsProperSubsetOfCore(IEnumerable<T> other); public bool IsProperSupersetOf(IEnumerable<T> other) { ExceptionPolyfills.ThrowIfNull(other, "other"); return IsProperSupersetOfCore(other); } private protected abstract bool IsProperSupersetOfCore(IEnumerable<T> other); public bool IsSubsetOf(IEnumerable<T> other) { ExceptionPolyfills.ThrowIfNull(other, "other"); return IsSubsetOfCore(other); } private protected abstract bool IsSubsetOfCore(IEnumerable<T> other); public bool IsSupersetOf(IEnumerable<T> other) { ExceptionPolyfills.ThrowIfNull(other, "other"); return IsSupersetOfCore(other); } private protected abstract bool IsSupersetOfCore(IEnumerable<T> other); public bool Overlaps(IEnumerable<T> other) { ExceptionPolyfills.ThrowIfNull(other, "other"); return OverlapsCore(other); } private protected abstract bool OverlapsCore(IEnumerable<T> other); public bool SetEquals(IEnumerable<T> other) { ExceptionPolyfills.ThrowIfNull(other, "other"); return SetEqualsCore(other); } private protected abstract bool SetEqualsCore(IEnumerable<T> other); } }