SetMap implements IReadOnlyDictionary (was only a few methods short)
This commit is contained in:
1 parent
11652ac021
commit
a26b44b2cc
1 file changed
+18
-1
@@ -6,7 +6,7 @@ using System.Linq;
|
||||
namespace mbts.collections {
|
||||
interface IGroupingSet<TKey, TElement> : IGrouping<TKey, TElement>, ISet<TElement> { }
|
||||
|
||||
class SetMap<TKey, TElement> : IEnumerable<IGroupingSet<TKey, TElement>>, ICollection {
|
||||
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;
|
||||
@@ -30,6 +30,10 @@ namespace mbts.collections {
|
||||
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) =>
|
||||
@@ -103,6 +107,19 @@ namespace mbts.collections {
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user