#nullable enable namespace Spark.Core.Collections { using System; using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; public static class ImmutableDictionaryKeyCollection { public static ImmutableDictionaryKeyCollection create( ImmutableDictionary source) where TKey : notnull => new(source); } public class ImmutableDictionaryKeyCollection : ICollection where TKey : notnull { readonly ImmutableDictionary _source; public ImmutableDictionaryKeyCollection(ImmutableDictionary source) => _source = source; public int Count => _source.Count; public bool IsReadOnly => true; public void Add(TKey item) => throw new NotSupportedException(); public void Clear() => throw new NotSupportedException(); public bool Contains(TKey item) => _source.ContainsKey(item); public void CopyTo(TKey[] array, int array_index) { foreach (var k in _source.Keys) { array[array_index++] = k; } } public IEnumerator GetEnumerator() => _source.Keys.GetEnumerator(); public bool Remove(TKey item) => throw new NotSupportedException(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } }