Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixill committed Dec 21, 2024
2 parents 3165472 + 44b557f commit be64c05
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions CSharp.Nixill/src/Utils/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@ namespace Nixill.Utils.Extensions;
/// </summary>
public static class CollectionExtensions
{
/// <summary>
/// Adds all of the given entries to the dictionary. Key collisions
/// are resolved in favor of existing entries.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">
/// The type of values in the dictionary.
/// </typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <param name="items">The items to add.</param>
public static void AddMissing<K, V>(this IDictionary<K, V> dictionary, IEnumerable<(K Key, V Value)> items)
=> dictionary.AddMissing(items.Select(i => new KeyValuePair<K, V>(i.Key, i.Value)));

/// <summary>
/// Adds all of the given entries to the dictionary. Key collisions
/// are resolved in favor of existing entries.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">
/// The type of values in the dictionary.
/// </typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <param name="items">The items to add.</param>
public static void AddMissing<K, V>(this IDictionary<K, V> dictionary, IEnumerable<KeyValuePair<K, V>> items)
{
foreach (var item in items)
{
dictionary.GetOrSet(item.Key, item.Value);
}
}

/// <summary>
/// Returns a value associated with a given key from the dictionary,
/// or saves a new association and returns it if the given key isn't
Expand Down Expand Up @@ -75,6 +106,37 @@ public static T Pop<T>(this IList<T> list)
return value;
}

/// <summary>
/// Sets all of the given entries in the dictionary. Key collisions
/// are resolved in favor of new entries.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">
/// The type of values in the dictionary.
/// </typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <param name="items">The items to add.</param>
public static void SetAll<K, V>(this IDictionary<K, V> dictionary, IEnumerable<(K Key, V Value)> items)
=> dictionary.SetAll(items.Select(i => new KeyValuePair<K, V>(i.Key, i.Value)));

/// <summary>
/// Sets all of the given entries in the dictionary. Key collisions
/// are resolved in favor of new entries.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">
/// The type of values in the dictionary.
/// </typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <param name="items">The items to add.</param>
public static void SetAll<K, V>(this IDictionary<K, V> dictionary, IEnumerable<KeyValuePair<K, V>> items)
{
foreach (var item in items)
{
dictionary[item.Key] = item.Value;
}
}

/// <summary>
/// Converts a sequence of <c>IGrouping&lt;K, V&gt;</c>s to a single
/// dictionary of type <c>IDictionary&lt;K, IEnumerable&lt;V&gt;&gt;</c>.
Expand Down

0 comments on commit be64c05

Please sign in to comment.