| | | 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.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace GDX.Collections.Generic |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// <see cref="GDX.Collections.Generic.SimpleList{T}" /> Based Extension Methods |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Methods found in this extensions class are less performant then the included methods in |
| | | 15 | | /// <see cref="GDX.Collections.Generic.SimpleList{T}" />. They are seperated out to clearly delineate this |
| | | 16 | | /// performance regression. |
| | | 17 | | /// </remarks> |
| | | 18 | | public static class SimpleListExtensions |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Add an item to the <see cref="SimpleList{T}" /> without checking the internal size, |
| | | 22 | | /// making sure that the item is not already contained in the <see cref="SimpleList{T}" />. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 25 | | /// <param name="targetItem">The target class object to add.</param> |
| | | 26 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 27 | | /// <returns>true/false if the operation was able to add the item successfully.</returns> |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 29 | | public static bool AddUncheckedUniqueItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) |
| | | 30 | | where T : class |
| | 9 | 31 | | { |
| | 9 | 32 | | if (targetSimpleList.ContainsItem(targetItem)) |
| | 1 | 33 | | { |
| | 1 | 34 | | return false; |
| | | 35 | | } |
| | | 36 | | |
| | 8 | 37 | | targetSimpleList.AddUnchecked(targetItem); |
| | 7 | 38 | | return true; |
| | 8 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Add an object reference to the <see cref="SimpleList{T}" /> without checking the internal size, |
| | | 43 | | /// making sure that the reference is not already contained in the <see cref="SimpleList{T}" />. |
| | | 44 | | /// Does not prevent addition of different objects for which Equals returns true. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 47 | | /// <param name="targetReference">The target class object to add.</param> |
| | | 48 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 49 | | /// <returns>true/false if the operation was able to add the reference successfully.</returns> |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 51 | | public static bool AddUncheckedUniqueReference<T>(ref this SimpleList<T> targetSimpleList, T targetReference) |
| | | 52 | | where T : class |
| | 9 | 53 | | { |
| | 9 | 54 | | if (targetSimpleList.ContainsReference(targetReference)) |
| | 1 | 55 | | { |
| | 1 | 56 | | return false; |
| | | 57 | | } |
| | | 58 | | |
| | 8 | 59 | | targetSimpleList.AddUnchecked(targetReference); |
| | 7 | 60 | | return true; |
| | 8 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Add an item to the <see cref="SimpleList{T}" /> with checking the internal size (expanding as necessary) |
| | | 65 | | /// making sure that the item is not already contained in the <see cref="SimpleList{T}" />. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 68 | | /// <param name="targetItem">The target class object to add.</param> |
| | | 69 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 70 | | /// <returns>true/false if the operation was able to add the item successfully.</returns> |
| | | 71 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 72 | | public static bool AddWithExpandCheckUniqueItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) |
| | | 73 | | where T : class |
| | 8 | 74 | | { |
| | 8 | 75 | | if (targetSimpleList.ContainsItem(targetItem)) |
| | 1 | 76 | | { |
| | 1 | 77 | | return false; |
| | | 78 | | } |
| | | 79 | | |
| | 7 | 80 | | targetSimpleList.AddWithExpandCheck(targetItem); |
| | 7 | 81 | | return true; |
| | 8 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Add an item to the <see cref="SimpleList{T}" /> with checking the internal size (expanding as necessary) |
| | | 86 | | /// making sure that the item is not already contained in the <see cref="SimpleList{T}" />. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 89 | | /// <param name="targetItem">The target class object to add.</param> |
| | | 90 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 91 | | /// <returns>true/false if the operation was able to add the item successfully.</returns> |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 93 | | public static bool AddWithExpandCheckUniqueValue<T>(ref this SimpleList<T> targetSimpleList, T targetItem) |
| | | 94 | | where T : IEquatable<T> |
| | 8 | 95 | | { |
| | 8 | 96 | | int count = targetSimpleList.Count; |
| | 34 | 97 | | for (int i = 0; i < count; i++) |
| | 10 | 98 | | { |
| | 10 | 99 | | if (targetSimpleList.Array[i].Equals(targetItem)) |
| | 1 | 100 | | { |
| | 1 | 101 | | return false; |
| | | 102 | | } |
| | 9 | 103 | | } |
| | | 104 | | |
| | 7 | 105 | | targetSimpleList.AddWithExpandCheck(targetItem); |
| | 7 | 106 | | return true; |
| | 8 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Add an object reference to the <see cref="SimpleList{T}" /> with checking the internal size (expanding a |
| | | 111 | | /// necessary), |
| | | 112 | | /// making sure that the reference is not already contained in the <see cref="SimpleList{T}" />. |
| | | 113 | | /// Does not prevent addition of different objects for which Equals returns true. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" /> to add to.</param> |
| | | 116 | | /// <param name="targetReference">The target class object to add.</param> |
| | | 117 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 118 | | /// <returns>true/false if the operation was able to add the reference successfully.</returns> |
| | | 119 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 120 | | public static bool AddWithExpandCheckUniqueReference<T>(ref this SimpleList<T> targetSimpleList, |
| | | 121 | | T targetReference) |
| | | 122 | | where T : class |
| | 8 | 123 | | { |
| | 8 | 124 | | if (targetSimpleList.ContainsReference(targetReference)) |
| | 1 | 125 | | { |
| | 1 | 126 | | return false; |
| | | 127 | | } |
| | | 128 | | |
| | 7 | 129 | | targetSimpleList.AddWithExpandCheck(targetReference); |
| | 7 | 130 | | return true; |
| | 8 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 135 | | /// </summary> |
| | | 136 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 137 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 138 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 139 | | /// <returns>true/false</returns> |
| | | 140 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 141 | | public static bool Contains<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : IEquatable<T> |
| | 0 | 142 | | { |
| | 0 | 143 | | int length = targetSimpleList.Count; |
| | 0 | 144 | | T[] array = targetSimpleList.Array; |
| | | 145 | | |
| | 0 | 146 | | for (int i = 0; i < length; i++) |
| | 0 | 147 | | { |
| | 0 | 148 | | if (array[i].Equals(targetItem)) |
| | 0 | 149 | | { |
| | 0 | 150 | | return true; |
| | | 151 | | } |
| | 0 | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | return false; |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 159 | | /// </summary> |
| | | 160 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 161 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 162 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 163 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 164 | | /// <returns>true/false</returns> |
| | | 165 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 166 | | public static bool ContainsItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 22 | 167 | | { |
| | 22 | 168 | | int length = targetSimpleList.Count; |
| | 22 | 169 | | T[] array = targetSimpleList.Array; |
| | | 170 | | |
| | 96 | 171 | | for (int i = 0; i < length; i++) |
| | 32 | 172 | | { |
| | 32 | 173 | | if (array[i] == targetItem) |
| | 6 | 174 | | { |
| | 6 | 175 | | return true; |
| | | 176 | | } |
| | 26 | 177 | | } |
| | | 178 | | |
| | 16 | 179 | | return false; |
| | 22 | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 184 | | /// </summary> |
| | | 185 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 186 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 187 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 188 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 189 | | /// <returns>true/false</returns> |
| | | 190 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 191 | | public static bool ContainsValue<T>(ref this SimpleList<T> targetSimpleList, T targetItem) |
| | | 192 | | where T : IEquatable<T> |
| | 2 | 193 | | { |
| | 2 | 194 | | int length = targetSimpleList.Count; |
| | 2 | 195 | | T[] array = targetSimpleList.Array; |
| | | 196 | | |
| | 12 | 197 | | for (int i = 0; i < length; i++) |
| | 5 | 198 | | { |
| | 5 | 199 | | if (array[i].Equals(targetItem)) |
| | 1 | 200 | | { |
| | 1 | 201 | | return true; |
| | | 202 | | } |
| | 4 | 203 | | } |
| | | 204 | | |
| | 1 | 205 | | return false; |
| | 2 | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// <para>Does <paramref name="targetSimpleList" /> contain <paramref name="targetItem" />?</para> |
| | | 210 | | /// </summary> |
| | | 211 | | /// <remarks>Ignores equality check and end up comparing object pointers.</remarks> |
| | | 212 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 213 | | /// <param name="targetItem">The target class object to look for.</param> |
| | | 214 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 215 | | /// <returns>true/false</returns> |
| | | 216 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 217 | | public static bool ContainsReference<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 22 | 218 | | { |
| | 22 | 219 | | int length = targetSimpleList.Count; |
| | 22 | 220 | | T[] array = targetSimpleList.Array; |
| | | 221 | | |
| | 96 | 222 | | for (int i = 0; i < length; i++) |
| | 32 | 223 | | { |
| | | 224 | | #pragma warning disable |
| | | 225 | | // ReSharper disable All |
| | 32 | 226 | | if ((System.Object)array[i] == (System.Object)targetItem) |
| | 6 | 227 | | { |
| | 6 | 228 | | return true; |
| | | 229 | | } |
| | | 230 | | // ReSharper restore All |
| | | 231 | | #pragma warning restore |
| | 26 | 232 | | } |
| | | 233 | | |
| | 16 | 234 | | return false; |
| | 22 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Find the first index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 239 | | /// </summary> |
| | | 240 | | /// <remarks>This will work for <see cref="string" /> comparisons.</remarks> |
| | | 241 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 242 | | /// <param name="targetItem">The object to be found.</param> |
| | | 243 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 244 | | /// <returns> |
| | | 245 | | /// The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, or -1 |
| | | 246 | | /// not found. |
| | | 247 | | /// </returns> |
| | | 248 | | public static int FirstIndexOf<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : IEquatable<T> |
| | 52 | 249 | | { |
| | 52 | 250 | | int length = targetSimpleList.Count; |
| | 156 | 251 | | for (int i = 0; i < length; i++) |
| | 78 | 252 | | { |
| | 78 | 253 | | if (targetSimpleList.Array[i].Equals(targetItem)) |
| | 52 | 254 | | { |
| | 52 | 255 | | return i; |
| | | 256 | | } |
| | 26 | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | return -1; |
| | 52 | 260 | | } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Find the first index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 264 | | /// </summary> |
| | | 265 | | /// <remarks>Ignores equality check and end up comparing object pointers. Do NOT use this for <see cref="string" |
| | | 266 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 267 | | /// <param name="targetItem">The object to be found.</param> |
| | | 268 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 269 | | /// <returns> |
| | | 270 | | /// The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, or -1 |
| | | 271 | | /// not found. |
| | | 272 | | /// </returns> |
| | | 273 | | public static int FirstIndexOfItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 0 | 274 | | { |
| | 0 | 275 | | int length = targetSimpleList.Count; |
| | 0 | 276 | | for (int i = 0; i < length; i++) |
| | 0 | 277 | | { |
| | 0 | 278 | | if (targetSimpleList.Array[i] == targetItem) |
| | 0 | 279 | | { |
| | 0 | 280 | | return i; |
| | | 281 | | } |
| | 0 | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | return -1; |
| | 0 | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Find the first index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" />. |
| | | 289 | | /// </summary> |
| | | 290 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 291 | | /// <param name="targetValue">The value to be found.</param> |
| | | 292 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 293 | | /// <returns> |
| | | 294 | | /// The index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" /> backing array, or - |
| | | 295 | | /// not found. |
| | | 296 | | /// </returns> |
| | | 297 | | public static int FirstIndexOfValue<T>(ref this SimpleList<T> targetSimpleList, T targetValue) where T : struct |
| | 0 | 298 | | { |
| | 0 | 299 | | int length = targetSimpleList.Count; |
| | 0 | 300 | | for (int i = 0; i < length; i++) |
| | 0 | 301 | | { |
| | 0 | 302 | | if (targetSimpleList.Array[i].Equals(targetValue)) |
| | 0 | 303 | | { |
| | 0 | 304 | | return i; |
| | | 305 | | } |
| | 0 | 306 | | } |
| | | 307 | | |
| | 0 | 308 | | return -1; |
| | 0 | 309 | | } |
| | | 310 | | |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Find the last index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 314 | | /// </summary> |
| | | 315 | | /// <remarks>This will work for <see cref="string" /> comparisons.</remarks> |
| | | 316 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 317 | | /// <param name="targetItem">The object to be found.</param> |
| | | 318 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 319 | | /// <returns> |
| | | 320 | | /// The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, or -1 |
| | | 321 | | /// not found. |
| | | 322 | | /// </returns> |
| | | 323 | | public static int LastIndexOf<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : IEquatable<T> |
| | 0 | 324 | | { |
| | 0 | 325 | | int length = targetSimpleList.Count; |
| | 0 | 326 | | for (int i = length - 1; i >= 0; i--) |
| | 0 | 327 | | { |
| | 0 | 328 | | if (targetSimpleList.Array[i].Equals(targetItem)) |
| | 0 | 329 | | { |
| | 0 | 330 | | return i; |
| | | 331 | | } |
| | 0 | 332 | | } |
| | | 333 | | |
| | 0 | 334 | | return -1; |
| | 0 | 335 | | } |
| | | 336 | | |
| | | 337 | | /// <summary> |
| | | 338 | | /// Find the last index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" />. |
| | | 339 | | /// </summary> |
| | | 340 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 341 | | /// <param name="targetItem">The object to be found.</param> |
| | | 342 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 343 | | /// <returns> |
| | | 344 | | /// The index of <paramref name="targetItem" /> in <paramref name="targetSimpleList" /> backing array, or -1 |
| | | 345 | | /// not found. |
| | | 346 | | /// </returns> |
| | | 347 | | public static int LastIndexOfItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 0 | 348 | | { |
| | 0 | 349 | | int length = targetSimpleList.Count; |
| | 0 | 350 | | for (int i = length - 1; i >= 0; i--) |
| | 0 | 351 | | { |
| | 0 | 352 | | if (targetSimpleList.Array[i] == targetItem) |
| | 0 | 353 | | { |
| | 0 | 354 | | return i; |
| | | 355 | | } |
| | 0 | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | return -1; |
| | 0 | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Find the last index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" />. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <param name="targetSimpleList">The <see cref="SimpleList{T}" /> to look in.</param> |
| | | 365 | | /// <param name="targetValue">The value to be found.</param> |
| | | 366 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | | 367 | | /// <returns> |
| | | 368 | | /// The index of <paramref name="targetValue" /> in <paramref name="targetSimpleList" /> backing array, or - |
| | | 369 | | /// not found. |
| | | 370 | | /// </returns> |
| | | 371 | | public static int LastIndexOfValue<T>(ref this SimpleList<T> targetSimpleList, T targetValue) where T : struct |
| | 0 | 372 | | { |
| | 0 | 373 | | int length = targetSimpleList.Count; |
| | 0 | 374 | | for (int i = length - 1; i >= 0; i--) |
| | 0 | 375 | | { |
| | 0 | 376 | | if (targetSimpleList.Array[i].Equals(targetValue)) |
| | 0 | 377 | | { |
| | 0 | 378 | | return i; |
| | | 379 | | } |
| | 0 | 380 | | } |
| | | 381 | | |
| | 0 | 382 | | return -1; |
| | 0 | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// <para>Removes the first <paramref name="targetItem" /> from the provided <paramref name="targetSimpleLis |
| | | 387 | | /// </summary> |
| | | 388 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 389 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 390 | | /// <param name="targetItem">The target object to remove from the <paramref name="targetSimpleList" />.</param> |
| | | 391 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 392 | | /// <returns>true/false if an item was removed.</returns> |
| | | 393 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 394 | | public static bool RemoveFirstItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 2 | 395 | | { |
| | 2 | 396 | | int length = targetSimpleList.Count; |
| | 2 | 397 | | T[] array = targetSimpleList.Array; |
| | | 398 | | |
| | 18 | 399 | | for (int i = 0; i < length; i++) |
| | 8 | 400 | | { |
| | | 401 | | // Skip to next if its not what we are looking for |
| | 8 | 402 | | if (array[i] != targetItem) |
| | 7 | 403 | | { |
| | 7 | 404 | | continue; |
| | | 405 | | } |
| | | 406 | | |
| | 1 | 407 | | targetSimpleList.RemoveAt(i); |
| | 1 | 408 | | return true; |
| | | 409 | | } |
| | | 410 | | |
| | 1 | 411 | | return false; |
| | 2 | 412 | | } |
| | | 413 | | |
| | | 414 | | /// <summary> |
| | | 415 | | /// <para>Removes the first <paramref name="targetReference" /> from the provided <paramref name="targetSimp |
| | | 416 | | /// Only removes direct object references, i.e. equivalent strings at different memory addresses would not b |
| | | 417 | | /// </summary> |
| | | 418 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 419 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 420 | | /// <param name="targetReference">The target object to remove from the <paramref name="targetSimpleList" />.</pa |
| | | 421 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 422 | | /// <returns>true/false if an object reference was removed.</returns> |
| | | 423 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 424 | | public static bool RemoveFirstReference<T>(ref this SimpleList<T> targetSimpleList, T targetReference) |
| | | 425 | | where T : class |
| | 2 | 426 | | { |
| | 2 | 427 | | int length = targetSimpleList.Count; |
| | 2 | 428 | | T[] array = targetSimpleList.Array; |
| | | 429 | | |
| | 18 | 430 | | for (int i = 0; i < length; i++) |
| | 8 | 431 | | { |
| | | 432 | | #pragma warning disable |
| | | 433 | | // ReSharper disable All |
| | 8 | 434 | | if ((System.Object)array[i] == (System.Object)targetReference) |
| | 1 | 435 | | { |
| | 1 | 436 | | targetSimpleList.RemoveAt(i); |
| | 1 | 437 | | return true; |
| | | 438 | | } |
| | | 439 | | // ReSharper restore All |
| | | 440 | | #pragma warning restore |
| | 7 | 441 | | } |
| | | 442 | | |
| | 1 | 443 | | return false; |
| | 2 | 444 | | } |
| | | 445 | | |
| | | 446 | | /// <summary> |
| | | 447 | | /// <para>Removes all <paramref name="targetItem" /> from the provided <paramref name="targetSimpleList" />. |
| | | 448 | | /// </summary> |
| | | 449 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 450 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 451 | | /// <param name="targetItem">The item to remove from the <paramref name="targetSimpleList" />.</param> |
| | | 452 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 453 | | /// <returns>true/false if items were removed.</returns> |
| | | 454 | | public static bool RemoveItems<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 1 | 455 | | { |
| | 1 | 456 | | int length = targetSimpleList.Count; |
| | 1 | 457 | | T[] array = targetSimpleList.Array; |
| | 1 | 458 | | bool removedItems = false; |
| | | 459 | | |
| | 14 | 460 | | for (int i = length - 1; i >= 0; i--) |
| | 6 | 461 | | { |
| | | 462 | | // Skip to next if its not what we are looking for |
| | 6 | 463 | | if (array[i] != targetItem) |
| | 4 | 464 | | { |
| | 4 | 465 | | continue; |
| | | 466 | | } |
| | | 467 | | |
| | 2 | 468 | | targetSimpleList.RemoveAt(i); |
| | 2 | 469 | | removedItems = true; |
| | 2 | 470 | | } |
| | | 471 | | |
| | 1 | 472 | | return removedItems; |
| | 1 | 473 | | } |
| | | 474 | | |
| | | 475 | | /// <summary> |
| | | 476 | | /// <para> |
| | | 477 | | /// Removes all instances of references to <paramref name="targetReference" /> from the provided |
| | | 478 | | /// <paramref name="targetSimpleList" />. |
| | | 479 | | /// </para> |
| | | 480 | | /// Only removes direct object references, i.e. equivalent strings at different memory addresses would not b |
| | | 481 | | /// </summary> |
| | | 482 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 483 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 484 | | /// <param name="targetReference">The object reference to remove from the <paramref name="targetSimpleList" />.< |
| | | 485 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 486 | | /// <returns>true/false if any references were removed.</returns> |
| | | 487 | | public static bool RemoveReferences<T>(ref this SimpleList<T> targetSimpleList, T targetReference) |
| | | 488 | | where T : class |
| | 1 | 489 | | { |
| | 1 | 490 | | int length = targetSimpleList.Count; |
| | 1 | 491 | | T[] array = targetSimpleList.Array; |
| | 1 | 492 | | bool removedItems = false; |
| | | 493 | | |
| | 14 | 494 | | for (int i = length - 1; i >= 0; i--) |
| | 6 | 495 | | { |
| | | 496 | | #pragma warning disable |
| | | 497 | | // ReSharper disable All |
| | 6 | 498 | | if ((System.Object)array[i] == (System.Object)targetReference) |
| | 2 | 499 | | { |
| | 2 | 500 | | targetSimpleList.RemoveAt(i); |
| | 2 | 501 | | removedItems = true; |
| | 2 | 502 | | } |
| | | 503 | | // ReSharper restore All |
| | | 504 | | #pragma warning restore |
| | 6 | 505 | | } |
| | | 506 | | |
| | 1 | 507 | | return removedItems; |
| | 1 | 508 | | } |
| | | 509 | | |
| | | 510 | | /// <summary> |
| | | 511 | | /// <para>Removes the last <paramref name="targetItem" /> from the provided <paramref name="targetSimpleList |
| | | 512 | | /// </summary> |
| | | 513 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 514 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 515 | | /// <param name="targetItem">The target object to remove from the <paramref name="targetSimpleList" />.</param> |
| | | 516 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 517 | | /// <returns>true/false if an item was removed.</returns> |
| | | 518 | | public static bool RemoveLastItem<T>(ref this SimpleList<T> targetSimpleList, T targetItem) where T : class |
| | 2 | 519 | | { |
| | 2 | 520 | | int length = targetSimpleList.Count; |
| | 2 | 521 | | T[] array = targetSimpleList.Array; |
| | | 522 | | |
| | 18 | 523 | | for (int i = length - 1; i >= 0; i--) |
| | 8 | 524 | | { |
| | | 525 | | // Skip to next if its not what we are looking for |
| | 8 | 526 | | if (array[i] != targetItem) |
| | 7 | 527 | | { |
| | 7 | 528 | | continue; |
| | | 529 | | } |
| | | 530 | | |
| | 1 | 531 | | targetSimpleList.RemoveAt(i); |
| | 1 | 532 | | return true; |
| | | 533 | | } |
| | | 534 | | |
| | 1 | 535 | | return false; |
| | 2 | 536 | | } |
| | | 537 | | |
| | | 538 | | /// <summary> |
| | | 539 | | /// <para> |
| | | 540 | | /// Removes the last reference to <paramref name="targetReference" /> from the provided |
| | | 541 | | /// <paramref name="targetSimpleList" />. |
| | | 542 | | /// </para> |
| | | 543 | | /// Only removes direct object references, i.e. equivalent strings at different memory addresses would not b |
| | | 544 | | /// </summary> |
| | | 545 | | /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks> |
| | | 546 | | /// <param name="targetSimpleList">The target <see cref="SimpleList{T}" />.</param> |
| | | 547 | | /// <param name="targetReference">The target object reference to remove from the <paramref name="targetSimpleLis |
| | | 548 | | /// <typeparam name="T">The type of the <see cref="SimpleList{T}" />.</typeparam> |
| | | 549 | | /// <returns>true/false if an object reference was removed.</returns> |
| | | 550 | | public static bool RemoveLastReference<T>(ref this SimpleList<T> targetSimpleList, T targetReference) |
| | | 551 | | where T : class |
| | 2 | 552 | | { |
| | 2 | 553 | | int length = targetSimpleList.Count; |
| | 2 | 554 | | T[] array = targetSimpleList.Array; |
| | | 555 | | |
| | 18 | 556 | | for (int i = length - 1; i >= 0; i--) |
| | 8 | 557 | | { |
| | | 558 | | #pragma warning disable |
| | | 559 | | // ReSharper disable All |
| | 8 | 560 | | if ((System.Object)array[i] == (System.Object)targetReference) |
| | 1 | 561 | | { |
| | 1 | 562 | | targetSimpleList.RemoveAt(i); |
| | 1 | 563 | | return true; |
| | | 564 | | } |
| | | 565 | | // ReSharper restore All |
| | | 566 | | #pragma warning restore |
| | 7 | 567 | | } |
| | | 568 | | |
| | 1 | 569 | | return false; |
| | 2 | 570 | | } |
| | | 571 | | } |
| | | 572 | | } |