#nullable enable namespace Spark.Core.Collections; using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; public static class DictionaryKeySet { public static DictionaryKeySet Create(IDictionary source, IEqualityComparer? comparer = null) => new(source, comparer); } public class DictionaryKeySet : IReadOnlySet { readonly IDictionary source; readonly IEqualityComparer comparer; public DictionaryKeySet(IDictionary source, IEqualityComparer? comparer = null) { this.source = source; this.comparer = comparer ?? ImplicitComparer(source) ?? EqualityComparer.Default; static IEqualityComparer? ImplicitComparer(IDictionary source) => source switch { #nullable disable Dictionary dct => dct.Comparer, ImmutableDictionary idct => idct.KeyComparer, #nullable enable _ => null, }; } public int Count => source.Count; public bool Contains(TKey item) => source.ContainsKey(item); public bool IsProperSubsetOf(IEnumerable other) => isSubsetOf(other, true); public bool IsProperSupersetOf(IEnumerable other) { int count = source.Count; if (count == 0) { return false; } var knownKeys = new HashSet(comparer); foreach (var k in other) { if (!source.ContainsKey(k) || (knownKeys.Add(k) && knownKeys.Count == count)) { return false; } } return true; } /// Determines whether a object is a subset of the specified collection. /// The collection to compare to the current object. /// true if the object is a subset of ; otherwise, false. public bool IsSubsetOf(IEnumerable other) => isSubsetOf(other); public bool IsSupersetOf(IEnumerable other) => other.All(source.ContainsKey); public bool Overlaps(IEnumerable other) => source.Count > 0 && other.Any(source.ContainsKey); public bool SetEquals(IEnumerable other) { var otherSet = new HashSet(comparer); int count = source.Count; foreach (var k in other) { if (!source.ContainsKey(k) || (otherSet.Add(k) && otherSet.Count > count)) { return false; } } return true; } public IEnumerable Intersection(IEnumerable other) { int count = Count; var knownKeys = new HashSet(0, comparer); using var en = other.GetEnumerator(); while (en.MoveNext() && knownKeys.Count < count) { var k = en.Current; if (source.ContainsKey(k) && knownKeys.Add(k)) { yield return k; } } } public IEnumerator GetEnumerator() => source.Keys.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); bool isSubsetOf(IEnumerable other, bool proper = false) { var otherSet = new HashSet(comparer); using var en = other.GetEnumerator(); foreach (var k in source.Keys) { if (otherSet.Contains(k)) { continue; } var hash = comparer.GetHashCode(k!); while (true) { if (!en.MoveNext()) { return false; } var k2 = en.Current!; // Compare hash codes before objects, because the hash fn is likely cheap, and collisions rare. if (otherSet.Add(k2) && comparer.GetHashCode(k2) == hash && comparer.Equals(k2, k)) { break; } } } int count = source.Count; if (proper) { count++; } while (otherSet.Count < count && en.MoveNext()) { otherSet.Add(en.Current); } return otherSet.Count >= count; } }