A .NET Standard 2.0 library for mutable ILookup
collections.
Defines an interface IMutableLookup
for defining mutable ILookup
containers:
public interface IMutableLookup<TKey, TElement> : ILookup<TKey, TElement>
{
void Add(TKey key, TElement value);
bool Remove(TKey key);
bool Remove(TKey key, TElement value);
void Clear();
bool TryGetValues(TKey key, out IEnumerable<TElement> values);
}
Comes with a few predefined types that utilise .NET's own generic collection types:
ListLookup<TKey, TElement>
equivalent toDictionary<TKey, List<TElement>>
LinkedListLookup<TKey, TElement>
equivalent toDictionary<TKey, LinkedList<TElement>>
HashSetLookup<TKey, TElement>
equivalent toDictionary<TKey, HashSet<TElement>>
Each type has its own advantages and disadvantages based on use case requirements.
Install MutableLookup
through NuGet:
NuGet install MutableLookup
NuGet package page: https://www.nuget.org/packages/MutableLookup
The library comes with the MutableLookup
abstract class which allows trivial definition of a new IMutableLookup. Given a type MyCollection<TElement>
that implements ICollection<TElement>
, you can define a new IMutableLookup type like this:
public sealed class MyCollectionLookup<TKey, TElement>
: MutableLookup<TKey, TElement, MyCollection<TElement>> { }
If your collection does not implement a constant time .Count
property, you should override the bool IsContainerEmpty(TContainer container)
method to efficiently see if the container is empty.