Files
spark/src/cs/Bts.Collections/SetMap.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

198 lines
7.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace bts.collections {
public interface IGroupingSet<TKey, TElement> : IGrouping<TKey, TElement>, ISet<TElement> { }
public class SetMap<TKey, TElement> : IEnumerable<IGroupingSet<TKey, TElement>>, ICollection, IReadOnlyDictionary<TKey, IEnumerable<TElement>> {
readonly IEqualityComparer<TKey> _key_comparer;
readonly IEqualityComparer<TElement> _element_comparer;
readonly Dictionary<TKey, InternalGroupSet> _dictionary;
public SetMap()
: this(EqualityComparer<TKey>.Default) { }
public SetMap(IEqualityComparer<TKey> key_comparer)
: this(key_comparer, EqualityComparer<TElement>.Default) { }
public SetMap(IEqualityComparer<TKey> key_comparer, IEqualityComparer<TElement> element_comparer)
: this(new Dictionary<TKey, InternalGroupSet>(), key_comparer, element_comparer) { }
SetMap(Dictionary<TKey, InternalGroupSet> dictionary, IEqualityComparer<TKey> key_comparer, IEqualityComparer<TElement> element_comparer) {
_dictionary = dictionary;
_key_comparer = key_comparer;
_element_comparer = element_comparer;
}
public int Count => _dictionary.Count;
public object SyncRoot => null;
public bool IsSynchronized => false;
public IEnumerable<TKey> Keys => _dictionary.Keys;
public IEnumerable<IEnumerable<TElement>> Values => _dictionary.Values;
IEnumerable<TElement> IReadOnlyDictionary<TKey, IEnumerable<TElement>>.this[TKey key] => _dictionary[key];
public ISet<TElement> this[TKey key] => _dictionary[key];
public void CopyTo(Array array, int index) =>
((ICollection)_dictionary).CopyTo(array, index);
public IEnumerator<IGroupingSet<TKey, TElement>> GetEnumerator() =>
_dictionary.Values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
public bool add(TKey key, TElement value) {
if (!_dictionary.TryGetValue(key, out InternalGroupSet values))
_dictionary.Add(key, values = new InternalGroupSet(key, _element_comparer));
return values.Add(value);
}
public void add(TKey key, IEnumerable<TElement> values) {
if (!_dictionary.TryGetValue(key, out InternalGroupSet igs))
_dictionary.Add(key, igs = new InternalGroupSet(key, _element_comparer));
igs.AddRange(values);
}
public bool add(TKey key, out ISet<TElement> values) {
if (!_dictionary.TryGetValue(key, out InternalGroupSet igs)) {
_dictionary.Add(key, igs = new InternalGroupSet(key, _element_comparer));
values = igs;
return true;
}
values = igs;
return false;
}
/// <summary>
/// If '<paramref name="value"/>' is already contained in the specified <paramref name="key"/>'s set (according to the element-comparer being used by this instance),
/// it will be replaced with '<paramref name="value"/>,' and false will be returned. Otherwise, '<paramref name="value"/>' will be added and true will be returned.
/// </summary>
/// <param name="key">The key bucket to which the value should be added.</param>
/// <param name="value">The value to added.</param>
/// <returns>False if equivalent value was already present in the bucket, otherwise true.</returns>
public bool set(TKey key, TElement value) {
if (!_dictionary.TryGetValue(key, out InternalGroupSet values)) {
_dictionary[key] = values = new InternalGroupSet(key, _element_comparer);
} else {
if (values.Remove(value)) {
values.Add(value);
return false;
}
}
values.Add(value);
return true;
}
public bool remove(TKey key, TElement value) {
if (_dictionary.TryGetValue(key, out InternalGroupSet values)) {
bool removed = values.Remove(value);
if (values.Count == 0)
_dictionary.Remove(key);
return removed;
}
return false;
}
public bool remove(TKey key) =>
_dictionary.Remove(key);
public void clear() => _dictionary.Clear();
public bool try_pop(TKey key, out ISet<TElement> value) {
if (try_lookup(key, out value)) {
remove(key);
return true;
} else return false;
}
public bool try_lookup(TKey key, out ISet<TElement> value) {
if (_dictionary.TryGetValue(key, out InternalGroupSet result)) {
value = result;
return true;
}
value = null;
return false;
}
public SetMap<TKey, TResult> convert<TResult>(Func<TElement, TResult> selector) =>
convert(selector, EqualityComparer<TResult>.Default);
public SetMap<TKey, TResult> convert<TResult>(Func<TElement, TResult> selector, IEqualityComparer<TResult> result_comparer) =>
new SetMap<TKey, TResult>(
_dictionary.ToDictionary(
kv => kv.Key,
kv => new SetMap<TKey, TResult>.InternalGroupSet(kv.Key, kv.Value.Select(selector), result_comparer)
), _key_comparer,
result_comparer);
public SetMap<TKey, TElement> clone() =>
new SetMap<TKey, TElement>(
_dictionary.ToDictionary(
kv => kv.Key,
kv => new InternalGroupSet(kv.Key, kv.Value, _element_comparer)
), _key_comparer,
_element_comparer);
public bool ContainsKey(TKey key) => _dictionary.ContainsKey(key);
public bool TryGetValue(TKey key, out IEnumerable<TElement> value) {
if (_dictionary.TryGetValue(key, out InternalGroupSet igs)) {
value = igs;
return true;
}
value = null;
return false;
}
IEnumerator<KeyValuePair<TKey, IEnumerable<TElement>>> IEnumerable<KeyValuePair<TKey, IEnumerable<TElement>>>.GetEnumerator() {
foreach (var kv in _dictionary)
yield return new KeyValuePair<TKey, IEnumerable<TElement>>(kv.Key, kv.Value);
}
class InternalGroupSet : HashSet<TElement>, IGroupingSet<TKey, TElement> {
public InternalGroupSet(TKey key, IEqualityComparer<TElement> comparer)
: base(comparer) => Key = key;
public InternalGroupSet(TKey key, IEnumerable<TElement> values, IEqualityComparer<TElement> comparer)
: base(values, comparer) => Key = key;
public TKey Key { get; }
}
}
public static class SetMap {
public static SetMap<TKey, TValue> create<TKey, TElement, TValue>(IEnumerable<TElement> source, Func<TElement, TKey> key_selector, Func<TElement, TValue> value_selector) =>
create(source, key_selector, value_selector, EqualityComparer<TKey>.Default, EqualityComparer<TValue>.Default);
public static SetMap<TKey, TValue> create<TKey, TElement, TValue>(
IEnumerable<TElement> source,
Func<TElement, TKey> key_selector,
Func<TElement, TValue> value_selector,
IEqualityComparer<TKey> key_comparer,
IEqualityComparer<TValue> value_comparer
) {
var sm = new SetMap<TKey, TValue>(key_comparer, value_comparer);
foreach (var el in source) {
sm.add(key_selector(el), value_selector(el));
}
return sm;
}
public static bool removes_key<TKey, TElement>(this SetMap<TKey, TElement> set_map, TKey key, TElement element) {
int count_before = set_map.Count;
if (set_map.remove(key, element))
return set_map.Count < count_before;
return false;
}
public static bool adds_key<TKey, TElement>(this SetMap<TKey, TElement> set_map, TKey key, TElement element) {
int count_before = set_map.Count;
if (set_map.add(key, element))
return set_map.Count > count_before;
return false;
}
}
}