| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DictionaryExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Collections; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Extension methods for dictionary-like collections. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class DictionaryExtensions |
| | | 18 | | { |
| | | 19 | | extension<TKey, TValue>(IDictionary<TKey, TValue> dictionary) |
| | | 20 | | where TKey : notnull |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the value for the specified key or adds the provided default value if missing. |
| | | 24 | | /// </summary> |
| | | 25 | | public TValue GetOrCreate(TKey key, Func<TValue> factory) |
| | | 26 | | { |
| | 6 | 27 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | | 28 | | |
| | 6 | 29 | | if (!dictionary.TryGetValue(key, out var value)) |
| | | 30 | | { |
| | 3 | 31 | | value = factory(); |
| | 3 | 32 | | dictionary.Add(key, value); |
| | | 33 | | } |
| | | 34 | | |
| | 6 | 35 | | return value; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the value for the specified key or returns a default when missing. |
| | | 40 | | /// </summary> |
| | | 41 | | public TValue? GetOrDefault(TKey key, TValue? defaultValue = default) |
| | | 42 | | { |
| | 9 | 43 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | | 44 | | |
| | 9 | 45 | | return dictionary.TryGetValue(key, out var value) ? value : defaultValue; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Merges two dictionaries into a new dictionary. Keys are expected to be unique. |
| | | 50 | | /// </summary> |
| | | 51 | | public Dictionary<TKey, TValue> Merge(IDictionary<TKey, TValue> dictionary2) |
| | | 52 | | { |
| | 6 | 53 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | | 54 | | |
| | 6 | 55 | | return new[] { dictionary, dictionary2 }.Merge(); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Merges multiple dictionaries into a new dictionary. Keys are expected to be unique. |
| | | 61 | | /// </summary> |
| | | 62 | | public static Dictionary<TKey, TValue> Merge<TKey, TValue>(this IEnumerable<IDictionary<TKey, TValue>> other) |
| | | 63 | | where TKey : notnull |
| | | 64 | | { |
| | 9 | 65 | | ArgumentNullException.ThrowIfNull(other); |
| | | 66 | | |
| | 9 | 67 | | var result = new Dictionary<TKey, TValue>(); |
| | | 68 | | |
| | 60 | 69 | | foreach (var dictionary in other) |
| | | 70 | | { |
| | 84 | 71 | | foreach (var pair in dictionary) |
| | 21 | 72 | | result[pair.Key] = pair.Value; |
| | | 73 | | } |
| | | 74 | | |
| | 9 | 75 | | return result; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |