Add generic version of dictionary merge

This commit is contained in:
Matt Nadareski
2026-07-09 09:52:40 -04:00
parent 433f37a789
commit 6045830bbb
2 changed files with 111 additions and 6 deletions

View File

@@ -5,10 +5,10 @@ namespace SabreTools.Collections.Extensions.Test
{
public class DictionaryExtensionsTests
{
#region MergeWith
#region MergeWith (String)
[Fact]
public void MergeWith_EmptySource_EmptyOther_Empty()
public void MergeWithString_EmptySource_EmptyOther_Empty()
{
Dictionary<string, List<string>> source = [];
Dictionary<string, List<string>> other = [];
@@ -18,7 +18,7 @@ namespace SabreTools.Collections.Extensions.Test
}
[Fact]
public void MergeWith_EmptySource_EmptyKeyOther_Empty()
public void MergeWithString_EmptySource_EmptyKeyOther_Empty()
{
Dictionary<string, List<string>> source = [];
Dictionary<string, List<string>> other = [];
@@ -29,7 +29,7 @@ namespace SabreTools.Collections.Extensions.Test
}
[Fact]
public void MergeWith_EmptySource_FilledOther_Filled()
public void MergeWithString_EmptySource_FilledOther_Filled()
{
Dictionary<string, List<string>> source = [];
Dictionary<string, List<string>> other = [];
@@ -44,7 +44,7 @@ namespace SabreTools.Collections.Extensions.Test
}
[Fact]
public void MergeWith_FilledSource_EmptyOther_Filled()
public void MergeWithString_FilledSource_EmptyOther_Filled()
{
Dictionary<string, List<string>> source = [];
source.Add("key", ["value"]);
@@ -59,7 +59,7 @@ namespace SabreTools.Collections.Extensions.Test
}
[Fact]
public void MergeWith_FilledSource_FilledOther_Filled()
public void MergeWithString_FilledSource_FilledOther_Filled()
{
Dictionary<string, List<string>> source = [];
source.Add("key1", ["value1"]);
@@ -81,5 +81,83 @@ namespace SabreTools.Collections.Extensions.Test
}
#endregion
#region MergeWith (Generic)
[Fact]
public void MergeWithGeneric_EmptySource_EmptyOther_Empty()
{
Dictionary<string, List<int>> source = [];
Dictionary<string, List<int>> other = [];
source.MergeWith(other);
Assert.Empty(source);
}
[Fact]
public void MergeWithGeneric_EmptySource_EmptyKeyOther_Empty()
{
Dictionary<string, List<int>> source = [];
Dictionary<string, List<int>> other = [];
other.Add("key", []);
source.MergeWith(other);
Assert.Empty(source);
}
[Fact]
public void MergeWithGeneric_EmptySource_FilledOther_Filled()
{
Dictionary<string, List<int>> source = [];
Dictionary<string, List<int>> other = [];
other.Add("key", [1]);
source.MergeWith(other);
string key = Assert.Single(source.Keys);
Assert.Equal("key", key);
List<int> actual = source[key];
int value = Assert.Single(actual);
Assert.Equal(1, value);
}
[Fact]
public void MergeWithGeneric_FilledSource_EmptyOther_Filled()
{
Dictionary<string, List<int>> source = [];
source.Add("key", [1]);
Dictionary<string, List<int>> other = [];
source.MergeWith(other);
string key = Assert.Single(source.Keys);
Assert.Equal("key", key);
List<int> actual = source[key];
int value = Assert.Single(actual);
Assert.Equal(1, value);
}
[Fact]
public void MergeWithGeneric_FilledSource_FilledOther_Filled()
{
Dictionary<string, List<int>> source = [];
source.Add("key1", [1]);
Dictionary<string, List<int>> other = [];
other.Add("key2", [2]);
source.MergeWith(other);
Assert.Equal(2, source.Keys.Count);
Assert.Contains("key1", source.Keys);
List<int> actualKey1 = source["key1"];
int value1 = Assert.Single(actualKey1);
Assert.Equal(1, value1);
Assert.Contains("key2", source.Keys);
List<int> actualKey2 = source["key2"];
int value2 = Assert.Single(actualKey2);
Assert.Equal(2, value2);
}
#endregion
}
}

View File

@@ -29,5 +29,32 @@ namespace SabreTools.Collections.Extensions
dict[kvp.Key].AddRange(kvp.Value);
}
}
/// <summary>
/// Merge a dictionary into an existing one, if possible
/// </summary>
/// <param name="dict">Source dictionary to add to</param>
/// <param name="other">Second dictionary to add from</param>
/// <remarks>This only performs a shallow copy</remarks>
public static void MergeWith<TKey, TValue>(this Dictionary<TKey, List<TValue>> dict, Dictionary<TKey, List<TValue>> other)
where TKey : notnull
{
// Ignore if there are no values to append
if (other.Count == 0)
return;
// Loop through and add from the new dictionary
foreach (var kvp in other)
{
// Ignore empty values
if (kvp.Value.Count == 0)
continue;
if (!dict.ContainsKey(kvp.Key))
dict[kvp.Key] = [];
dict[kvp.Key].AddRange(kvp.Value);
}
}
}
}