2025-11-13 10:35:02 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2026-03-23 10:50:09 -04:00
|
|
|
namespace SabreTools.Collections.Extensions
|
2025-11-13 10:35:02 -05:00
|
|
|
{
|
|
|
|
|
public static class DictionaryExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <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(this Dictionary<string, List<string>> dict, Dictionary<string, List<string>> 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-09 09:52:40 -04:00
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-13 10:35:02 -05:00
|
|
|
}
|
|
|
|
|
}
|