| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ListExtensions.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; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.Linq; |
| | | 12 | | using MyNet.Primitives; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 15 | | namespace MyNet.Collections; |
| | | 16 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 17 | | |
| | | 18 | | public static class ListExtensions |
| | | 19 | | { |
| | | 20 | | extension(IList list) |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Swaps the elements at the specified indices in the list. If either index is out of range or both indices are |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="firstIndex">The index of the first element to swap.</param> |
| | | 26 | | /// <param name="secondIndex">The index of the second element to swap.</param> |
| | | 27 | | /// <exception cref="ArgumentNullException">Thrown if the list is null.</exception> |
| | | 28 | | public void Swap(int firstIndex, int secondIndex) |
| | | 29 | | { |
| | 12 | 30 | | ArgumentNullException.ThrowIfNull(list); |
| | | 31 | | |
| | 12 | 32 | | if ((uint)firstIndex >= (uint)list.Count || (uint)secondIndex >= (uint)list.Count || firstIndex == secondInd |
| | 9 | 33 | | return; |
| | | 34 | | |
| | 3 | 35 | | (list[firstIndex], list[secondIndex]) = (list[secondIndex], list[firstIndex]); |
| | 3 | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | extension<T>(IList<T> list) |
| | | 40 | | { |
| | | 41 | | /// <summary> |
| | | 42 | | /// Sorts the collection in place using the specified key selector. |
| | | 43 | | /// </summary> |
| | | 44 | | public void Sort<TKey>(Func<T, TKey> keySelector) |
| | | 45 | | { |
| | 3 | 46 | | ArgumentNullException.ThrowIfNull(list); |
| | 3 | 47 | | ArgumentNullException.ThrowIfNull(keySelector); |
| | | 48 | | |
| | 3 | 49 | | if (list is List<T> concrete) |
| | | 50 | | { |
| | 3 | 51 | | concrete.Sort((a, b) => Comparer<TKey>.Default.Compare(keySelector(a), keySelector(b))); |
| | 3 | 52 | | return; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | var sorted = list.OrderBy(keySelector).ToArray(); |
| | 0 | 56 | | Replace(list, sorted); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Sorts the collection in place using the specified key selector and comparer. |
| | | 61 | | /// </summary> |
| | | 62 | | public void Sort<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer) |
| | | 63 | | { |
| | 9 | 64 | | ArgumentNullException.ThrowIfNull(list); |
| | 9 | 65 | | ArgumentNullException.ThrowIfNull(keySelector); |
| | 9 | 66 | | ArgumentNullException.ThrowIfNull(comparer); |
| | | 67 | | |
| | 9 | 68 | | if (list is List<T> concrete) |
| | | 69 | | { |
| | 3 | 70 | | concrete.Sort((a, b) => comparer.Compare(keySelector(a), keySelector(b))); |
| | 3 | 71 | | return; |
| | | 72 | | } |
| | | 73 | | |
| | 6 | 74 | | var sorted = list.OrderBy(keySelector, comparer).ToArray(); |
| | 6 | 75 | | Replace(list, sorted); |
| | 6 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Sorts the collection in descending order using the specified key selector. |
| | | 80 | | /// </summary> |
| | 3 | 81 | | public void SortDescending<TKey>(Func<T, TKey> keySelector) => list.Sort(keySelector, Comparer<TKey>.Default.Rev |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Sorts the collection in descending order using the specified key selector. |
| | | 85 | | /// </summary> |
| | 3 | 86 | | public void SortDescending<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer) => list.Sort(keySelector, |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Sorts the collection in place using culture-aware, case-insensitive display text. |
| | | 90 | | /// </summary> |
| | | 91 | | public void SortByDisplay( |
| | | 92 | | Func<T, string> displaySelector, |
| | | 93 | | CultureInfo? culture = null, |
| | | 94 | | CompareOptions options = CompareOptions.IgnoreCase) |
| | | 95 | | { |
| | 3 | 96 | | ArgumentNullException.ThrowIfNull(list); |
| | 3 | 97 | | ArgumentNullException.ThrowIfNull(displaySelector); |
| | | 98 | | |
| | 3 | 99 | | var compareInfo = (culture ?? CultureInfo.CurrentCulture).CompareInfo; |
| | | 100 | | |
| | 3 | 101 | | if (list is List<T> concrete) |
| | | 102 | | { |
| | 3 | 103 | | concrete.Sort((left, right) => |
| | 3 | 104 | | compareInfo.Compare(displaySelector(left), displaySelector(right), options)); |
| | 3 | 105 | | return; |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | list.Sort(displaySelector, StringComparer.Create(culture ?? CultureInfo.CurrentCulture, options)); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets the element at the specified index or returns a default value if the index is out of range. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="index">The index of the element to retrieve.</param> |
| | | 115 | | /// <param name="defaultValue">The default value to return if the index is out of range.</param> |
| | | 116 | | /// <returns>The element at the specified index or the default value if the index is out of range.</returns> |
| | | 117 | | /// <exception cref="ArgumentNullException">Thrown if the list is null.</exception> |
| | | 118 | | public T? GetByIndex(int index, T? defaultValue = default) |
| | | 119 | | { |
| | 6 | 120 | | ArgumentNullException.ThrowIfNull(list); |
| | 6 | 121 | | return index >= 0 && index < list.Count ? list[index] : defaultValue; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public void UpdateFrom<TSource, TKey>( |
| | | 125 | | IEnumerable<TSource> source, |
| | | 126 | | Func<TSource, TKey> sourceKey, |
| | | 127 | | Func<T, TKey> destKey, |
| | | 128 | | Action<TSource> add, |
| | | 129 | | Action<T> remove, |
| | | 130 | | Action<T, TSource> update) |
| | | 131 | | where TKey : notnull |
| | | 132 | | { |
| | 3 | 133 | | ArgumentNullException.ThrowIfNull(list); |
| | 3 | 134 | | ArgumentNullException.ThrowIfNull(source); |
| | 3 | 135 | | ArgumentNullException.ThrowIfNull(sourceKey); |
| | 3 | 136 | | ArgumentNullException.ThrowIfNull(destKey); |
| | 3 | 137 | | ArgumentNullException.ThrowIfNull(add); |
| | 3 | 138 | | ArgumentNullException.ThrowIfNull(remove); |
| | 3 | 139 | | ArgumentNullException.ThrowIfNull(update); |
| | | 140 | | |
| | 3 | 141 | | var sourceList = source as IList<TSource> ?? [.. source]; |
| | | 142 | | |
| | 3 | 143 | | var sourceMap = new Dictionary<TKey, TSource>(sourceList.Count); |
| | 18 | 144 | | foreach (var s in sourceList) |
| | 6 | 145 | | sourceMap[sourceKey(s)] = s; |
| | | 146 | | |
| | 3 | 147 | | var destMap = new Dictionary<TKey, T>(list.Count); |
| | 18 | 148 | | foreach (var d in list) |
| | 6 | 149 | | destMap[destKey(d)] = d; |
| | | 150 | | |
| | | 151 | | // REMOVE |
| | 18 | 152 | | foreach (var (key, dest) in destMap) |
| | | 153 | | { |
| | 6 | 154 | | if (!sourceMap.ContainsKey(key)) |
| | 3 | 155 | | remove(dest); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | // UPDATE |
| | 18 | 159 | | foreach (var (key, dest) in destMap) |
| | | 160 | | { |
| | 6 | 161 | | if (sourceMap.TryGetValue(key, out var src)) |
| | 3 | 162 | | update(dest, src); |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | // ADD |
| | 18 | 166 | | foreach (var (key, src) in sourceMap) |
| | | 167 | | { |
| | 6 | 168 | | if (!destMap.ContainsKey(key)) |
| | 3 | 169 | | add(src); |
| | | 170 | | } |
| | 3 | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Replaces the contents of the list with the specified items. The list is cleared and the items are added in order |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="list">The list to replace the contents of.</param> |
| | | 178 | | /// <param name="items">The items to replace the list with.</param> |
| | | 179 | | /// <typeparam name="T">The type of the elements in the list.</typeparam> |
| | | 180 | | private static void Replace<T>(IList<T> list, T[] items) |
| | | 181 | | { |
| | 6 | 182 | | list.Clear(); |
| | 48 | 183 | | foreach (var item in items) |
| | 18 | 184 | | list.Add(item); |
| | 6 | 185 | | } |
| | | 186 | | } |
| | | 187 | | |