FrozenSet<T>
public abstract class FrozenSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlySet<T>, 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)]
[DebuggerTypeProxy(typeof(ImmutableEnumerableDebuggerProxy<>))]
[DebuggerDisplay("Count = {Count}")]
public abstract class FrozenSet<[System.Runtime.CompilerServices.Nullable(2)] T> : ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlySet<T>, IReadOnlyCollection<T>, ICollection
{
[System.Runtime.CompilerServices.Nullable(0)]
public struct Enumerator : IEnumerator<T>, IEnumerator, IDisposable
{
private readonly T[] _entries;
private int _index;
public T Current {
[IsReadOnly]
get {
if ((uint)_index >= (uint)_entries.Length)
ThrowHelper.ThrowInvalidOperationException();
return _entries[_index];
}
}
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)
{
ThrowHelper.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, [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);
[System.Runtime.CompilerServices.NullableContext(0)]
public Enumerator GetEnumerator()
{
return GetEnumeratorCore();
}
[System.Runtime.CompilerServices.NullableContext(0)]
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)
{
ThrowHelper.ThrowIfNull(other, "other");
return IsProperSubsetOfCore(other);
}
private protected abstract bool IsProperSubsetOfCore(IEnumerable<T> other);
public bool IsProperSupersetOf(IEnumerable<T> other)
{
ThrowHelper.ThrowIfNull(other, "other");
return IsProperSupersetOfCore(other);
}
private protected abstract bool IsProperSupersetOfCore(IEnumerable<T> other);
public bool IsSubsetOf(IEnumerable<T> other)
{
ThrowHelper.ThrowIfNull(other, "other");
return IsSubsetOfCore(other);
}
private protected abstract bool IsSubsetOfCore(IEnumerable<T> other);
public bool IsSupersetOf(IEnumerable<T> other)
{
ThrowHelper.ThrowIfNull(other, "other");
return IsSupersetOfCore(other);
}
private protected abstract bool IsSupersetOfCore(IEnumerable<T> other);
public bool Overlaps(IEnumerable<T> other)
{
ThrowHelper.ThrowIfNull(other, "other");
return OverlapsCore(other);
}
private protected abstract bool OverlapsCore(IEnumerable<T> other);
public bool SetEquals(IEnumerable<T> other)
{
ThrowHelper.ThrowIfNull(other, "other");
return SetEqualsCore(other);
}
private protected abstract bool SetEqualsCore(IEnumerable<T> other);
}
}