Files
spark/src/client/spark_core/Collections/ImmutableDictionaryKeyCollection.cs
T

48 lines
1.4 KiB
C#

#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<TKey, TValue> create<TKey, TValue>(
ImmutableDictionary<TKey, TValue> source) where TKey : notnull => new(source);
}
public class ImmutableDictionaryKeyCollection<TKey, TValue> : ICollection<TKey> where TKey : notnull {
readonly ImmutableDictionary<TKey, TValue> _source;
public ImmutableDictionaryKeyCollection(ImmutableDictionary<TKey, TValue> 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<TKey> GetEnumerator() =>
_source.Keys.GetEnumerator();
public bool Remove(TKey item) =>
throw new NotSupportedException();
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
}