using System.Collections.Generic;
namespace SabreTools.Collections.Extensions
{
public static class DictionaryExtensions
{
///
/// Merge a dictionary into an existing one, if possible
///
/// Source dictionary to add to
/// Second dictionary to add from
/// This only performs a shallow copy
public static void MergeWith(this Dictionary> dict, Dictionary> other)
{
// 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);
}
}
///
/// Merge a dictionary into an existing one, if possible
///
/// Source dictionary to add to
/// Second dictionary to add from
/// This only performs a shallow copy
public static void MergeWith(this Dictionary> dict, Dictionary> 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);
}
}
}
}