Add key collection to weak dictionary and compact code

This commit is contained in:
Norbi Peti 2022-02-24 01:02:35 +01:00
parent 2db7b607f0
commit 3eecdf2cf5

View file

@ -7,7 +7,7 @@ namespace TechbloxModdingAPI.Utility
{ {
public class WeakDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TValue : class public class WeakDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TValue : class
{ {
private Dictionary<TKey, WeakReference<TValue>> _dictionary = new Dictionary<TKey, WeakReference<TValue>>(); private readonly Dictionary<TKey, WeakReference<TValue>> _dictionary = new();
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{ {
@ -19,53 +19,19 @@ namespace TechbloxModdingAPI.Utility
} }
} }
IEnumerator IEnumerable.GetEnumerator() IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
{ public void Add(KeyValuePair<TKey, TValue> item) => Add(item.Key, item.Value);
return GetEnumerator(); public void Clear() => _dictionary.Clear();
} public bool Contains(KeyValuePair<TKey, TValue> item) =>
TryGetValue(item.Key, out var value) && item.Value == value;
public void Add(KeyValuePair<TKey, TValue> item) public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) =>
{
Add(item.Key, item.Value);
}
public void Clear()
{
_dictionary.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return TryGetValue(item.Key, out var value) && item.Value == value;
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} public bool Remove(KeyValuePair<TKey, TValue> item) => Contains(item) && Remove(item.Key);
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return Contains(item) && Remove(item.Key);
}
public int Count => _dictionary.Count; public int Count => _dictionary.Count;
public bool IsReadOnly => false; public bool IsReadOnly => false;
public bool ContainsKey(TKey key) => TryGetValue(key, out _);
public bool ContainsKey(TKey key) public void Add(TKey key, TValue value) => _dictionary.Add(key, new WeakReference<TValue>(value));
{ public bool Remove(TKey key) => _dictionary.Remove(key);
return TryGetValue(key, out _);
}
public void Add(TKey key, TValue value)
{
_dictionary.Add(key, new WeakReference<TValue>(value));
}
public bool Remove(TKey key)
{
return _dictionary.Remove(key);
}
public bool TryGetValue(TKey key, out TValue value) public bool TryGetValue(TKey key, out TValue value)
{ {
@ -83,12 +49,37 @@ namespace TechbloxModdingAPI.Utility
set => _dictionary[key] = new WeakReference<TValue>(value); set => _dictionary[key] = new WeakReference<TValue>(value);
} }
public ICollection<TKey> Keys => _dictionary.Keys; public ICollection<TKey> Keys => new KeyCollection(this);
public ICollection<TValue> Values => new ValueCollection(this); public ICollection<TValue> Values => new ValueCollection(this);
public class KeyCollection : ICollection<TKey>, IReadOnlyCollection<TKey>
{
private readonly WeakDictionary<TKey, TValue> _dictionary;
internal KeyCollection(WeakDictionary<TKey, TValue> dictionary)
{
_dictionary = dictionary;
}
public IEnumerator<TKey> GetEnumerator()
{
using var enumerator = _dictionary.GetEnumerator();
while (enumerator.MoveNext())
yield return enumerator.Current.Key;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void Add(TKey item) => throw new NotSupportedException("The key collection is read only.");
public void Clear() => throw new NotSupportedException("The key collection is read only.");
public bool Contains(TKey item) => _dictionary.ContainsKey(item);
public void CopyTo(TKey[] array, int arrayIndex) => throw new NotImplementedException();
public bool Remove(TKey item) => throw new NotSupportedException("The key collection is read only.");
public int Count => _dictionary.Count;
public bool IsReadOnly => true;
}
public class ValueCollection : ICollection<TValue>, IReadOnlyCollection<TValue> public class ValueCollection : ICollection<TValue>, IReadOnlyCollection<TValue>
{ {
private WeakDictionary<TKey, TValue> _dictionary; private readonly WeakDictionary<TKey, TValue> _dictionary;
internal ValueCollection(WeakDictionary<TKey, TValue> dictionary) internal ValueCollection(WeakDictionary<TKey, TValue> dictionary)
{ {
_dictionary = dictionary; _dictionary = dictionary;
@ -98,41 +89,15 @@ namespace TechbloxModdingAPI.Utility
{ {
using var enumerator = _dictionary.GetEnumerator(); using var enumerator = _dictionary.GetEnumerator();
while (enumerator.MoveNext()) while (enumerator.MoveNext())
{
yield return enumerator.Current.Value; yield return enumerator.Current.Value;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(TValue item)
{
throw new NotSupportedException("The value collection is read only.");
}
public void Clear()
{
throw new NotSupportedException("The value collection is read only.");
}
public bool Contains(TValue item)
{
return _dictionary.Any(kv => kv.Value == item);
}
public void CopyTo(TValue[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(TValue item)
{
throw new NotSupportedException("The value collection is read only.");
} }
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void Add(TValue item) => throw new NotSupportedException("The value collection is read only.");
public void Clear() => throw new NotSupportedException("The value collection is read only.");
public bool Contains(TValue item) => _dictionary.Any(kv => kv.Value == item);
public void CopyTo(TValue[] array, int arrayIndex) => throw new NotImplementedException();
public bool Remove(TValue item) => throw new NotSupportedException("The value collection is read only.");
public int Count => _dictionary.Count; public int Count => _dictionary.Count;
public bool IsReadOnly => true; public bool IsReadOnly => true;
} }