Skip to content

Commit

Permalink
Upgraded the dictionary classes so that a custom implementation can e…
Browse files Browse the repository at this point in the history
…asily be passed in.
  • Loading branch information
MeltyPlayer committed Feb 11, 2024
1 parent 95260bb commit 9f85143
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace fin.data.dictionaries {
public class CaseInvariantStringDictionary<T> : IFinDictionary<string, T> {
Expand All @@ -15,6 +16,9 @@ public class CaseInvariantStringDictionary<T> : IFinDictionary<string, T> {

public bool TryGetValue(string key, out T value) => this.impl_.TryGetValue(key, out value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(T key) => this.impl_.Remove(key);

public T this[string key] {
get => this.impl_[key];
set => this.impl_[key] = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public interface IFinDictionary<TKey, TValue>
: IReadOnlyFinDictionary<TKey, TValue>,
IFinCollection<(TKey Key, TValue Value)> {
new TValue this[TKey key] { get; set; }
bool Remove(TKey key);
}
}
23 changes: 12 additions & 11 deletions FinModelUtility/Fin/Fin/src/data/dictionaries/ListDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,35 @@ namespace fin.data.dictionaries {
/// An implementation for a dictionary of lists. Each value added for a key
/// will be stored in that key's corresponding list.
/// </summary>
public class ListDictionary<TKey, TValue>
public class ListDictionary<TKey, TValue>(IFinDictionary<TKey, IList<TValue>> impl)
: IFinCollection<(TKey Key, IList<TValue> Value)> {
private readonly NullFriendlyDictionary<TKey, IList<TValue>> impl_ = new();
public ListDictionary() : this(
new NullFriendlyDictionary<TKey, IList<TValue>>()) { }

public void Clear() => this.impl_.Clear();
public void ClearList(TKey key) => this.impl_.Remove(key);
public void Clear() => impl.Clear();
public void ClearList(TKey key) => impl.Remove(key);

public int Count => this.impl_.Values.Select(list => list.Count).Sum();
public int Count => impl.Values.Select(list => list.Count).Sum();

public bool HasList(TKey key) => this.impl_.ContainsKey(key);
public bool HasList(TKey key) => impl.ContainsKey(key);

public void Add(TKey key, TValue value) {
IList<TValue> list;
if (!this.impl_.TryGetValue(key, out list)) {
this.impl_[key] = list = new List<TValue>();
if (!impl.TryGetValue(key, out list)) {
impl[key] = list = new List<TValue>();
}

list.Add(value);
}

public IList<TValue> this[TKey key] => this.impl_[key];
public IList<TValue> this[TKey key] => impl[key];

public bool TryGetList(TKey key, out IList<TValue>? list)
=> this.impl_.TryGetValue(key, out list);
=> impl.TryGetValue(key, out list);

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

public IEnumerator<(TKey Key, IList<TValue> Value)> GetEnumerator()
=> this.impl_.GetEnumerator();
=> impl.GetEnumerator();
}
}
21 changes: 11 additions & 10 deletions FinModelUtility/Fin/Fin/src/data/dictionaries/SetDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,32 @@ namespace fin.data.dictionaries {
/// An implementation for a dictionary of sets. Each value added for a key
/// will be stored in that key's corresponding set.
/// </summary>
public class SetDictionary<TKey, TValue>
public class SetDictionary<TKey, TValue>(
IFinDictionary<TKey, ISet<TValue>> impl)
: IFinCollection<(TKey Key, ISet<TValue> Value)> {
private readonly NullFriendlyDictionary<TKey, ISet<TValue>> impl_ = new();
public SetDictionary() : this(
new NullFriendlyDictionary<TKey, ISet<TValue>>()) { }

public void Clear() => this.impl_.Clear();
public void Clear() => impl.Clear();

public int Count => this.impl_.Values.Select(list => list.Count).Sum();
public int Count => impl.Values.Select(list => list.Count).Sum();

public void Add(TKey key, TValue value) {
ISet<TValue> set;
if (!this.impl_.TryGetValue(key, out set)) {
this.impl_[key] = set = new HashSet<TValue>();
if (!impl.TryGetValue(key, out var set)) {
impl[key] = set = new HashSet<TValue>();
}

set.Add(value);
}

public ISet<TValue> this[TKey key] => this.impl_[key];
public ISet<TValue> this[TKey key] => impl[key];

public bool TryGetSet(TKey key, out ISet<TValue>? set)
=> this.impl_.TryGetValue(key, out set);
=> impl.TryGetValue(key, out set);

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

public IEnumerator<(TKey Key, ISet<TValue> Value)> GetEnumerator()
=> this.impl_.GetEnumerator();
=> impl.GetEnumerator();
}
}
30 changes: 17 additions & 13 deletions FinModelUtility/Fin/Fin/src/data/dictionaries/SimpleDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@
using System.Collections.Generic;

namespace fin.data.dictionaries {
public class SimpleDictionary<TKey, TValue>(IDictionary<TKey, TValue>? impl = null)
public class SimpleDictionary<TKey, TValue>(IDictionary<TKey, TValue> impl)
: IFinDictionary<TKey, TValue> {
private readonly IDictionary<TKey, TValue> impl_ = impl ?? new ConcurrentDictionary<TKey, TValue>();
public SimpleDictionary() :
this(new ConcurrentDictionary<TKey, TValue>()) { }

public void Clear() => this.impl_.Clear();
public void Clear() => impl.Clear();

public int Count => this.impl_.Count;
public IEnumerable<TKey> Keys => this.impl_.Keys;
public IEnumerable<TValue> Values => this.impl_.Values;
public bool ContainsKey(TKey key) => this.impl_.ContainsKey(key);
public int Count => impl.Count;
public IEnumerable<TKey> Keys => impl.Keys;
public IEnumerable<TValue> Values => impl.Values;
public bool ContainsKey(TKey key) => impl.ContainsKey(key);

public bool TryGetValue(TKey key, out TValue value) => this.impl_.TryGetValue(key, out value);
public bool TryGetValue(TKey key, out TValue value)
=> impl.TryGetValue(key, out value);

public TValue this[TKey key] {
get => this.impl_[key];
set => this.impl_[key] = value;
get => impl[key];
set => impl[key] = value;
}

public bool Remove(TKey key) => impl.Remove(key);

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

public IEnumerator<(TKey Key, TValue Value)> GetEnumerator() {
foreach (var (key, value) in this.impl_) {
foreach (var (key, value) in impl) {
yield return (key, value);
}
}

}
}
}

0 comments on commit 9f85143

Please sign in to comment.