Skip to content

Commit

Permalink
Add IReadOnlyCollectionDictionary for read-only access to collection-…
Browse files Browse the repository at this point in the history
…dictionaries/sortedlists
  • Loading branch information
NathanKell committed Nov 30, 2023
1 parent 0f5973b commit dc298fd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Source/ROUtils/DataTypes/DictionaryHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

namespace ROUtils.DataTypes
{
public interface IReadOnlyCollectionDictionary<TKey, TValue, TCollection> : IReadOnlyDictionary<TKey, TCollection> where TCollection : ICollection<TValue>, new()
{
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues { get; }
}

public abstract class DictionaryPersistence<TKey, TValue> : PersistenceHelper, IConfigNode
{
protected static readonly Type _KeyType = typeof(TKey);
Expand Down
48 changes: 44 additions & 4 deletions Source/ROUtils/DataTypes/PersistentDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public bool Remove(TKey key, TValue value)
}
}

public class PersistentDictionaryBothObjects<TKey, TValue, TCollection> : PersistentDictionaryBothObjects<TKey, TCollection>, IConfigNode where TKey : IConfigNode where TCollection : ICollection<TValue>, IConfigNode, new()
public class PersistentDictionaryBothObjects<TKey, TValue, TCollection> : PersistentDictionaryBothObjects<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TKey : IConfigNode where TCollection : ICollection<TValue>, IConfigNode, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;
Expand Down Expand Up @@ -194,7 +194,7 @@ public bool Remove(TKey key, TValue value)
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class PersistentDictionaryNodeValueKeyed<TKey, TValue, TCollection> : PersistentDictionaryNodeValueKeyed<TKey, TCollection>, IConfigNode where TCollection : ICollection<TValue>, IConfigNode, new()
public class PersistentDictionaryNodeValueKeyed<TKey, TValue, TCollection> : PersistentDictionaryNodeValueKeyed<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;
Expand Down Expand Up @@ -234,7 +234,7 @@ public bool Remove(TKey key, TValue value)
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class PersistentDictionaryValueTypeKey<TKey, TValue, TCollection> : PersistentDictionaryValueTypeKey<TKey, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
public class PersistentDictionaryValueTypeKey<TKey, TValue, TCollection> : PersistentDictionaryValueTypeKey<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;
Expand Down Expand Up @@ -273,7 +273,7 @@ public bool Remove(TKey key, TValue value)
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class PersistentDictionaryValueTypes<TKey, TValue, TCollection> : PersistentDictionaryValueTypeKey<TKey, TCollection>, IConfigNode where TCollection : ICollection<TValue>, IConfigNode, new()
public class PersistentDictionaryValueTypes<TKey, TValue, TCollection> : PersistentDictionaryValueTypeKey<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;
Expand Down Expand Up @@ -305,4 +305,44 @@ public bool Remove(TKey key, TValue value)
return true;
}
}

// Reimplementation of KSP's ListDictionary - the non-persistent form of Collection-dictionaries as above.
public class CollectionDictionary<TKey, TValue, TCollection> : Dictionary<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;

public CollectionDictionary() : base() { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }

public CollectionDictionary(int capacity) : base(capacity) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionDictionary(IEqualityComparer<TKey> comparer) : base(comparer) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionDictionary(IDictionary<TKey, TCollection> dictionary) : base(dictionary) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionDictionary(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionDictionary(IDictionary<TKey, TCollection> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }

public void Add(TKey key, TValue value)
{
if (!TryGetValue(key, out var coll))
{
coll = new TCollection();
Add(key, coll);
}

coll.Add(value);
}

public bool Remove(TKey key, TValue value)
{
if (!TryGetValue(key, out var coll))
return false;

if (!coll.Remove(value))
return false;

if (coll.Count == 0)
Remove(key);

return true;
}
}
}
46 changes: 43 additions & 3 deletions Source/ROUtils/DataTypes/PersistentSortedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PersistentSortedListValueTypes<TKey, TValue> : PersistentSortedList
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class PersistentSortedListNodeValueKeyed<TKey, TValue, TCollection> : PersistentSortedListNodeValueKeyed<TKey, TCollection>, IConfigNode where TCollection : ICollection<TValue>, IConfigNode, new()
public class PersistentSortedListNodeValueKeyed<TKey, TValue, TCollection> : PersistentSortedListNodeValueKeyed<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;
Expand Down Expand Up @@ -95,7 +95,7 @@ public bool Remove(TKey key, TValue value)
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class PersistentSortedListValueTypeKey<TKey, TValue, TCollection> : PersistentSortedListValueTypeKey<TKey, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
public class PersistentSortedListValueTypeKey<TKey, TValue, TCollection> : PersistentSortedListValueTypeKey<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;
Expand Down Expand Up @@ -134,7 +134,7 @@ public bool Remove(TKey key, TValue value)
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class PersistentSortedListValueTypes<TKey, TValue, TCollection> : PersistentSortedListValueTypeKey<TKey, TCollection>, IConfigNode where TCollection : ICollection<TValue>, IConfigNode, new()
public class PersistentSortedListValueTypes<TKey, TValue, TCollection> : PersistentSortedListValueTypeKey<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, IConfigNode, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;
Expand Down Expand Up @@ -166,4 +166,44 @@ public bool Remove(TKey key, TValue value)
return true;
}
}

// Reimplementation of KSP's ListDictionary as SortedList - the non-persistent form of Collection-SortedLists as above.
public class CollectionSortedList<TKey, TValue, TCollection> : SortedList<TKey, TCollection>, IReadOnlyCollectionDictionary<TKey, TValue, TCollection> where TCollection : ICollection<TValue>, new()
{
private CollectionDictionaryAllValues<TKey, TValue, TCollection> _allValues;
public CollectionDictionaryAllValues<TKey, TValue, TCollection> AllValues => _allValues;

public CollectionSortedList() : base() { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }

public CollectionSortedList(int capacity) : base(capacity) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionSortedList(IComparer<TKey> comparer) : base(comparer) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionSortedList(IDictionary<TKey, TCollection> dictionary) : base(dictionary) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionSortedList(int capacity, IComparer<TKey> comparer) : base(capacity, comparer) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }
public CollectionSortedList(IDictionary<TKey, TCollection> dictionary, IComparer<TKey> comparer) : base(dictionary, comparer) { _allValues = new CollectionDictionaryAllValues<TKey, TValue, TCollection>(this); }

public void Add(TKey key, TValue value)
{
if (!TryGetValue(key, out var coll))
{
coll = new TCollection();
Add(key, coll);
}

coll.Add(value);
}

public bool Remove(TKey key, TValue value)
{
if (!TryGetValue(key, out var coll))
return false;

if (!coll.Remove(value))
return false;

if (coll.Count == 0)
Remove(key);

return true;
}
}
}

0 comments on commit dc298fd

Please sign in to comment.