Files
spark/src/cs/Bts.Collections/DictionaryKeySet.cs
T
John Colvin 2f7e75fec0 refactor: Break up Bts.Spark.Core into a few diff projs
This is in anticipation of decoupling BlueOrb from Spark.

* Bts.Collections
* Bts.Core
* Bts.Disposables
* Bts.Numerics
2026-03-03 15:06:06 +00:00

124 lines
4.0 KiB
C#

#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<TKey, TValue> Create<TKey, TValue>(IDictionary<TKey, TValue> source, IEqualityComparer<TKey>? comparer = null) =>
new(source, comparer);
}
public class DictionaryKeySet<TKey, TValue> : IReadOnlySet<TKey> {
readonly IDictionary<TKey, TValue> source;
readonly IEqualityComparer<TKey> comparer;
public DictionaryKeySet(IDictionary<TKey, TValue> source, IEqualityComparer<TKey>? comparer = null) {
this.source = source;
this.comparer = comparer ?? ImplicitComparer(source) ?? EqualityComparer<TKey>.Default;
static IEqualityComparer<TKey>? ImplicitComparer(IDictionary<TKey, TValue> source) =>
source switch {
#nullable disable
Dictionary<TKey, TValue> dct => dct.Comparer,
ImmutableDictionary<TKey, TValue> idct => idct.KeyComparer,
#nullable enable
_ => null,
};
}
public int Count => source.Count;
public bool Contains(TKey item) =>
source.ContainsKey(item);
public bool IsProperSubsetOf(IEnumerable<TKey> other) =>
isSubsetOf(other, true);
public bool IsProperSupersetOf(IEnumerable<TKey> other) {
int count = source.Count;
if (count == 0) {
return false;
}
var knownKeys = new HashSet<TKey>(comparer);
foreach (var k in other) {
if (!source.ContainsKey(k) || (knownKeys.Add(k) && knownKeys.Count == count)) {
return false;
}
}
return true;
}
/// <summary>Determines whether a <see cref="HashSet{T}"/> object is a subset of the specified collection.</summary>
/// <param name="other">The collection to compare to the current <see cref="HashSet{T}"/> object.</param>
/// <returns>true if the <see cref="HashSet{T}"/> object is a subset of <paramref name="other"/>; otherwise, false.</returns>
public bool IsSubsetOf(IEnumerable<TKey> other) =>
isSubsetOf(other);
public bool IsSupersetOf(IEnumerable<TKey> other) =>
other.All(source.ContainsKey);
public bool Overlaps(IEnumerable<TKey> other) =>
source.Count > 0 && other.Any(source.ContainsKey);
public bool SetEquals(IEnumerable<TKey> other) {
var otherSet = new HashSet<TKey>(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<TKey> Intersection(IEnumerable<TKey> other) {
int count = Count;
var knownKeys = new HashSet<TKey>(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<TKey> GetEnumerator() =>
source.Keys.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
bool isSubsetOf(IEnumerable<TKey> other, bool proper = false) {
var otherSet = new HashSet<TKey>(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;
}
}