| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | |
|
| | 9 | | namespace GDX.Collections.Generic |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// <see cref="System.Collections.Generic.IList{T}" /> Based Extension Methods |
| | 13 | | /// </summary> |
| | 14 | | [VisualScriptingCompatible(2)] |
| | 15 | | public static class IListExtensions |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Add an item to a <see cref="System.Collections.Generic.IList{T}" />, but only if it is not already conta |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="targetList">The <see cref="System.Collections.Generic.IList{T}" /> to add too.</param> |
| | 21 | | /// <param name="targetItem">The target object to add.</param> |
| | 22 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 23 | | /// <returns>true/false if this operation was able to add the item.</returns> |
| | 24 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 25 | | public static bool AddUniqueItem<T>(this IList<T> targetList, T targetItem) where T : class |
| 2 | 26 | | { |
| 2 | 27 | | if (targetList.ContainsItem(targetItem)) |
| 1 | 28 | | { |
| 1 | 29 | | return false; |
| | 30 | | } |
| | 31 | |
|
| 1 | 32 | | targetList.Add(targetItem); |
| 1 | 33 | | return true; |
| 2 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Add a range of items to a <see cref="System.Collections.Generic.IList{T}" />, checking if each item is |
| | 38 | | /// unique prior to adding. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="targetList">The <see cref="System.Collections.Generic.IList{T}" /> to add too.</param> |
| | 41 | | /// <param name="targetItems">The array of items to add.</param> |
| | 42 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 43 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 44 | | public static void AddUniqueRange<T>(this IList<T> targetList, T[] targetItems) where T : class |
| 0 | 45 | | { |
| 0 | 46 | | int itemCount = targetItems.Length; |
| 0 | 47 | | for (int i = 0; i < itemCount; i++) |
| 0 | 48 | | { |
| 0 | 49 | | targetList.AddUniqueItem(targetItems[i]); |
| 0 | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// <para>Does <paramref name="targetList" /> contain <paramref name="targetItem" />?</para> |
| | 55 | | /// </summary> |
| | 56 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | 57 | | /// <param name="targetList">The <see cref="System.Collections.Generic.IList{T}" /> to look in.</param> |
| | 58 | | /// <param name="targetItem">The target object to look for.</param> |
| | 59 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 60 | | /// <returns>true/false</returns> |
| | 61 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 62 | | public static bool ContainsItem<T>(this IList<T> targetList, T targetItem) where T : class |
| 7 | 63 | | { |
| 7 | 64 | | int length = targetList.Count; |
| 46 | 65 | | for (int i = 0; i < length; i++) |
| 20 | 66 | | { |
| 20 | 67 | | if (targetList[i] == targetItem) |
| 4 | 68 | | { |
| 4 | 69 | | return true; |
| | 70 | | } |
| 16 | 71 | | } |
| | 72 | |
|
| 3 | 73 | | return false; |
| 7 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// <para>Does <paramref name="targetList" /> contain <paramref name="targetItem" />?</para> |
| | 78 | | /// </summary> |
| | 79 | | /// <remarks>Ignores equality check and end up comparing object pointers.</remarks> |
| | 80 | | /// <param name="targetList">The <see cref="System.Collections.Generic.IList{T}" /> to look in.</param> |
| | 81 | | /// <param name="targetItem">The target object to look for.</param> |
| | 82 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 83 | | /// <returns>true/false</returns> |
| | 84 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 85 | | public static bool ContainsReference<T>(this IList<T> targetList, T targetItem) where T : class |
| 0 | 86 | | { |
| 0 | 87 | | int count = targetList.Count; |
| 0 | 88 | | for (int i = 0; i < count; i++) |
| 0 | 89 | | { |
| | 90 | | #pragma warning disable |
| | 91 | | // ReSharper disable All |
| 0 | 92 | | if ((Object)targetList[i] == (Object)targetItem) |
| 0 | 93 | | { |
| 0 | 94 | | return true; |
| | 95 | | } |
| | 96 | | // ReSharper restore All |
| | 97 | | #pragma warning restore |
| 0 | 98 | | } |
| | 99 | |
|
| 0 | 100 | | return false; |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// <para>Removes the first <paramref name="targetItem" /> from the provided <paramref name="targetList" />. |
| | 105 | | /// </summary> |
| | 106 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | 107 | | /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param> |
| | 108 | | /// <param name="targetItem">The target object to remove from the <paramref name="targetList" />.</param> |
| | 109 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 110 | | /// <returns>true/false if the item was removed.</returns> |
| | 111 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 112 | | public static bool RemoveFirstItem<T>(this IList<T> targetList, T targetItem) where T : class |
| 2 | 113 | | { |
| 2 | 114 | | int length = targetList.Count; |
| 16 | 115 | | for (int i = 0; i < length; i++) |
| 7 | 116 | | { |
| 7 | 117 | | if (targetList[i] != targetItem) |
| 6 | 118 | | { |
| 6 | 119 | | continue; |
| | 120 | | } |
| | 121 | |
|
| 1 | 122 | | targetList.RemoveAt(i); |
| 1 | 123 | | return true; |
| | 124 | | } |
| | 125 | |
|
| 1 | 126 | | return false; |
| 2 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// <para>Removes all <paramref name="targetItem" /> from the provided <paramref name="targetList" />.</para |
| | 131 | | /// </summary> |
| | 132 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />, uses object pointers.</ |
| | 133 | | /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param> |
| | 134 | | /// <param name="targetItem">The target object to remove from the <paramref name="targetList" />.</param> |
| | 135 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 136 | | /// <returns>true/false if the item was removed.</returns> |
| | 137 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 138 | | public static bool RemoveItems<T>(this IList<T> targetList, T targetItem) where T : class |
| 1 | 139 | | { |
| 1 | 140 | | int length = targetList.Count; |
| 1 | 141 | | bool removedItem = false; |
| 12 | 142 | | for (int i = length - 1; i >= 0; i--) |
| 5 | 143 | | { |
| 5 | 144 | | if (targetList[i] != targetItem) |
| 3 | 145 | | { |
| 3 | 146 | | continue; |
| | 147 | | } |
| | 148 | |
|
| 2 | 149 | | targetList.RemoveAt(i); |
| 2 | 150 | | removedItem = true; |
| 2 | 151 | | } |
| | 152 | |
|
| 1 | 153 | | return removedItem; |
| 1 | 154 | | } |
| | 155 | |
|
| | 156 | | /// <summary> |
| | 157 | | /// <para> |
| | 158 | | /// Replaces the object found at the provided <paramref name="index" /> with the last object in |
| | 159 | | /// <paramref name="targetList" />, then removes the last item from the <paramref name="targetList" />. |
| | 160 | | /// </para> |
| | 161 | | /// </summary> |
| | 162 | | /// <remarks> |
| | 163 | | /// This make sure that you are always removing from the end of a |
| | 164 | | /// <see cref="System.Collections.Generic.IList{T}" />. |
| | 165 | | /// </remarks> |
| | 166 | | /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param> |
| | 167 | | /// <param name="index">The index of the item to remove.</param> |
| | 168 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 169 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 170 | | public static void RemoveItemSwap<T>(this IList<T> targetList, int index) |
| 1 | 171 | | { |
| 1 | 172 | | int lastIndex = targetList.Count - 1; |
| 1 | 173 | | targetList[index] = targetList[lastIndex]; |
| 1 | 174 | | targetList.RemoveAt(lastIndex); |
| 1 | 175 | | } |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// <para>Removes the last <paramref name="targetItem" /> from the provided <paramref name="targetList" />.< |
| | 179 | | /// </summary> |
| | 180 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | 181 | | /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param> |
| | 182 | | /// <param name="targetItem">The target object to remove from the <paramref name="targetList" />.</param> |
| | 183 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 184 | | /// <returns>true/false if the item was removed.</returns> |
| | 185 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 186 | | public static bool RemoveLastItem<T>(this IList<T> targetList, T targetItem) where T : class |
| 2 | 187 | | { |
| 2 | 188 | | int length = targetList.Count; |
| 10 | 189 | | for (int i = length - 1; i >= 0; i--) |
| 4 | 190 | | { |
| 4 | 191 | | if (targetList[i] != targetItem) |
| 3 | 192 | | { |
| 3 | 193 | | continue; |
| | 194 | | } |
| | 195 | |
|
| 1 | 196 | | targetList.RemoveAt(i); |
| 1 | 197 | | return true; |
| | 198 | | } |
| | 199 | |
|
| 1 | 200 | | return false; |
| 2 | 201 | | } |
| | 202 | |
|
| | 203 | | /// <summary> |
| | 204 | | /// Shuffle the items in the <paramref name="targetList" />. |
| | 205 | | /// </summary> |
| | 206 | | /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param> |
| | 207 | | /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam> |
| | 208 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 209 | | public static void Shuffle<T>(this IList<T> targetList) |
| 1 | 210 | | { |
| 1 | 211 | | int length = targetList.Count; |
| 54 | 212 | | for (int i = 0; i < length; i++) |
| 26 | 213 | | { |
| 26 | 214 | | T t = targetList[i]; |
| 26 | 215 | | int r = Core.Random.NextIntegerExclusive(i, length); |
| 26 | 216 | | targetList[i] = targetList[r]; |
| 26 | 217 | | targetList[r] = t; |
| 26 | 218 | | } |
| 1 | 219 | | } |
| | 220 | | } |
| | 221 | | } |