Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/DotNet.MultiMap/MultiMapHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ public MultiMapHashSet(IEnumerable<KeyValuePair<TKey, HashSet<TValue>>> collecti
/// <returns>true if the value has been map to the specified key element; otherwise, false.</returns>
public bool TryToAddMapping(TKey key, TValue val)
{
if (ContainsKey(key))
if (TryGetValue(key, out var set))
{
var set = this[key];
return set.Add(val);
}
var tmp = new HashSet<TValue>();
var tmp = new HashSet<TValue> { val };
Add(key, tmp);
return tmp.Add(val);
return true;
}

/// <summary>
Expand All @@ -115,9 +114,8 @@ public bool TryToAddMapping(TKey key, TValue val)
public bool TryToRemoveMapping(TKey key, TValue val)
{
bool result = false;
if (ContainsKey(key))
if (TryGetValue(key, out var set))
{
var set = this[key];
result = set.Remove(val);
if (set.Count == 0)
{
Expand All @@ -135,13 +133,8 @@ public bool TryToRemoveMapping(TKey key, TValue val)
/// <returns>the number of links (duplicates).</returns>
public int ContainsMapping(TKey key, TValue val)
{
if (ContainsKey(key))
{
var set = this[key];
if (set.Contains(val))
{
return 1;
}
if (TryGetValue(key, out var set) && set.Contains(val)) {
return 1;
}
return 0;
}
Expand Down
15 changes: 5 additions & 10 deletions src/DotNet.MultiMap/MultiMapList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,14 @@ public MultiMapList(IEnumerable<KeyValuePair<TKey, List<TValue>>> collection, IE
/// <returns>true if the value has been map to the specified key element; otherwise, false.</returns>
public bool TryToAddMapping(TKey key, TValue val)
{
if (ContainsKey(key))
if (TryGetValue(key, out var set))
{
var set = this[key];
set.Add(val);
}
else
{
var tmp = new List<TValue>();
var tmp = new List<TValue> { val };
Add(key, tmp);
tmp.Add(val);
}
return true;
}
Expand All @@ -122,12 +120,10 @@ public bool TryToAddMapping(TKey key, TValue val)
public bool TryToRemoveMapping(TKey key, TValue val)
{
bool result = false;
if (ContainsKey(key))
if (TryGetValue(key, out var set))
{
var set = this[key];
result = set.Remove(val);
if (set.Count == 0)
{
if (set.Count == 0) {
Remove(key);
}
}
Expand All @@ -142,9 +138,8 @@ public bool TryToRemoveMapping(TKey key, TValue val)
/// <returns>the number of links (duplicates).</returns>
public int ContainsMapping(TKey key, TValue val)
{
if (ContainsKey(key))
if (TryGetValue(key, out var set))
{
var set = this[key];
return set.Count((v) => v.Equals(val));
}
return 0;
Expand Down