| | 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 |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Array Based Extension Methods |
| | 12 | | /// </summary> |
| | 13 | | [VisualScriptingCompatible(2)] |
| | 14 | | public static class ArrayExtensions |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Set all elements in an array to the default values. |
| | 18 | | /// </summary> |
| | 19 | | /// <remarks> |
| | 20 | | /// This does not alter the <paramref name="targetArray" />'s length. |
| | 21 | | /// </remarks> |
| | 22 | | /// <param name="targetArray">The array to be defaulted.</param> |
| | 23 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | 24 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 25 | | public static void Clear<T>(this T[] targetArray) |
| 1 | 26 | | { |
| 1 | 27 | | Array.Clear(targetArray, 0, targetArray.Length); |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// <para>Does <paramref name="targetArray" /> contain <paramref name="targetValue" />?</para> |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="targetArray">The <see cref="System.Array" /> to look in.</param> |
| | 34 | | /// <param name="targetValue">The target item to look for.</param> |
| | 35 | | /// <typeparam name="T">The type of the <see cref="System.Array" />.</typeparam> |
| | 36 | | /// <returns>true/false</returns> |
| | 37 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 38 | | public static bool ContainsItem<T>(this T[] targetArray, T targetValue) where T : class |
| 3 | 39 | | { |
| 3 | 40 | | int count = targetArray.Length; |
| 20 | 41 | | for (int i = 0; i < count; i++) |
| 9 | 42 | | { |
| 9 | 43 | | if (targetArray[i] == targetValue) |
| 2 | 44 | | { |
| 2 | 45 | | return true; |
| | 46 | | } |
| 7 | 47 | | } |
| | 48 | |
|
| 1 | 49 | | return false; |
| 3 | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// <para>Does <paramref name="targetArray" /> contain <paramref name="targetItem" />?</para> |
| | 54 | | /// </summary> |
| | 55 | | /// <remarks>Ignores equality check and end up comparing object pointers.</remarks> |
| | 56 | | /// <param name="targetArray">The <see cref="System.Array" /> to look in.</param> |
| | 57 | | /// <param name="targetItem">The target item to look for.</param> |
| | 58 | | /// <typeparam name="T">The type of the <see cref="System.Array" />.</typeparam> |
| | 59 | | /// <returns>true/false</returns> |
| | 60 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 61 | | public static bool ContainsReference<T>(this T[] targetArray, T targetItem) where T : class |
| 2 | 62 | | { |
| 2 | 63 | | int count = targetArray.Length; |
| 12 | 64 | | for (int i = 0; i < count; i++) |
| 5 | 65 | | { |
| | 66 | | #pragma warning disable |
| | 67 | | // ReSharper disable All |
| 5 | 68 | | if ((System.Object)targetArray[i] == (System.Object)targetItem) |
| 1 | 69 | | { |
| 1 | 70 | | return true; |
| | 71 | | } |
| | 72 | | // ReSharper restore All |
| | 73 | | #pragma warning restore |
| 4 | 74 | | } |
| | 75 | |
|
| 1 | 76 | | return false; |
| 2 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// <para>Does <paramref name="targetArray" /> contain <paramref name="targetValue" />?</para> |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="targetArray">The <see cref="System.Array" /> to look in.</param> |
| | 83 | | /// <param name="targetValue">The target value to look for.</param> |
| | 84 | | /// <typeparam name="T">The type of the <see cref="System.Array" />.</typeparam> |
| | 85 | | /// <returns>true/false</returns> |
| | 86 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 87 | | public static bool ContainsValue<T>(this T[] targetArray, T targetValue) where T : IEquatable<T> |
| 2 | 88 | | { |
| 2 | 89 | | int count = targetArray.Length; |
| 18 | 90 | | for (int i = 0; i < count; i++) |
| 8 | 91 | | { |
| 8 | 92 | | if (targetArray[i].Equals(targetValue)) |
| 1 | 93 | | { |
| 1 | 94 | | return true; |
| | 95 | | } |
| 7 | 96 | | } |
| | 97 | |
|
| 1 | 98 | | return false; |
| 2 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Find the first index of <paramref name="targetItem" /> in <paramref name="targetArray" />. |
| | 103 | | /// </summary> |
| | 104 | | /// <remarks>This will work for <see cref="string" /> comparisons.</remarks> |
| | 105 | | /// <param name="targetArray">The array which to look in.</param> |
| | 106 | | /// <param name="targetItem">The object to be found.</param> |
| | 107 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | 108 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found. |
| | 109 | | public static int FirstIndexOf<T>(this T[] targetArray, T targetItem) where T : IEquatable<T> |
| 0 | 110 | | { |
| 0 | 111 | | int length = targetArray.Length; |
| 0 | 112 | | for (int i = 0; i < length; i++) |
| 0 | 113 | | { |
| 0 | 114 | | if (targetArray[i].Equals(targetItem)) |
| 0 | 115 | | { |
| 0 | 116 | | return i; |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | return -1; |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Find the first index of <paramref name="targetItem" /> in <paramref name="targetArray" />. |
| | 125 | | /// </summary> |
| | 126 | | /// <remarks>Ignores equality check and end up comparing object pointers. Do NOT use this for <see cref="string" |
| | 127 | | /// <param name="targetArray">The array which to look in.</param> |
| | 128 | | /// <param name="targetItem">The object to be found.</param> |
| | 129 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | 130 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found. |
| | 131 | | public static int FirstIndexOfItem<T>(this T[] targetArray, T targetItem) where T : class |
| 2 | 132 | | { |
| 2 | 133 | | int length = targetArray.Length; |
| 14 | 134 | | for (int i = 0; i < length; i++) |
| 6 | 135 | | { |
| 6 | 136 | | if (targetArray[i] == targetItem) |
| 1 | 137 | | { |
| 1 | 138 | | return i; |
| | 139 | | } |
| 5 | 140 | | } |
| | 141 | |
|
| 1 | 142 | | return -1; |
| 2 | 143 | | } |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Find the first index of <paramref name="targetValue" /> in <paramref name="targetArray" />. |
| | 147 | | /// </summary> |
| | 148 | | /// <param name="targetArray">The array which to look in.</param> |
| | 149 | | /// <param name="targetValue">The value to be found.</param> |
| | 150 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | 151 | | /// <returns>The index of <paramref name="targetValue" /> in <paramref name="targetArray" />, or -1 if not found |
| | 152 | | public static int FirstIndexOfValue<T>(this T[] targetArray, T targetValue) where T : struct |
| 2 | 153 | | { |
| 2 | 154 | | int length = targetArray.Length; |
| 32 | 155 | | for (int i = 0; i < length; i++) |
| 15 | 156 | | { |
| 15 | 157 | | if (targetArray[i].Equals(targetValue)) |
| 1 | 158 | | { |
| 1 | 159 | | return i; |
| | 160 | | } |
| 14 | 161 | | } |
| | 162 | |
|
| 1 | 163 | | return -1; |
| 2 | 164 | | } |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// Find the last index of <paramref name="targetItem" /> in <paramref name="targetArray" />. |
| | 168 | | /// </summary> |
| | 169 | | /// <remarks>This will work for <see cref="string" /> comparisons.</remarks> |
| | 170 | | /// <param name="targetArray">The array which to look in.</param> |
| | 171 | | /// <param name="targetItem">The object to be found.</param> |
| | 172 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | 173 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found. |
| | 174 | | public static int LastIndexOf<T>(this T[] targetArray, T targetItem) where T : IEquatable<T> |
| 0 | 175 | | { |
| 0 | 176 | | int length = targetArray.Length; |
| 0 | 177 | | for (int i = length - 1; i >= 0; i--) |
| 0 | 178 | | { |
| 0 | 179 | | if (targetArray[i].Equals(targetItem)) |
| 0 | 180 | | { |
| 0 | 181 | | return i; |
| | 182 | | } |
| 0 | 183 | | } |
| | 184 | |
|
| 0 | 185 | | return -1; |
| 0 | 186 | | } |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Find the last index of <paramref name="targetItem" /> in <paramref name="targetArray" />. |
| | 190 | | /// </summary> |
| | 191 | | /// <param name="targetArray">The array which to look in.</param> |
| | 192 | | /// <param name="targetItem">The object to be found.</param> |
| | 193 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | 194 | | /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found. |
| | 195 | | public static int LastIndexOfItem<T>(this T[] targetArray, T targetItem) where T : class |
| 2 | 196 | | { |
| 2 | 197 | | int length = targetArray.Length; |
| 12 | 198 | | for (int i = length - 1; i >= 0; i--) |
| 5 | 199 | | { |
| 5 | 200 | | if (targetArray[i] == targetItem) |
| 1 | 201 | | { |
| 1 | 202 | | return i; |
| | 203 | | } |
| 4 | 204 | | } |
| | 205 | |
|
| 1 | 206 | | return -1; |
| 2 | 207 | | } |
| | 208 | |
|
| | 209 | | /// <summary> |
| | 210 | | /// Find the last index of <paramref name="targetValue" /> in <paramref name="targetArray" />. |
| | 211 | | /// </summary> |
| | 212 | | /// <param name="targetArray">The array which to look in.</param> |
| | 213 | | /// <param name="targetValue">The value to be found.</param> |
| | 214 | | /// <typeparam name="T">The type of the array.</typeparam> |
| | 215 | | /// <returns>The index of <paramref name="targetValue" /> in <paramref name="targetArray" />, or -1 if not found |
| | 216 | | public static int LastIndexOfValue<T>(this T[] targetArray, T targetValue) where T : struct |
| 2 | 217 | | { |
| 2 | 218 | | int length = targetArray.Length; |
| 32 | 219 | | for (int i = length - 1; i >= 0; i--) |
| 15 | 220 | | { |
| 15 | 221 | | if (targetArray[i].Equals(targetValue)) |
| 1 | 222 | | { |
| 1 | 223 | | return i; |
| | 224 | | } |
| 14 | 225 | | } |
| | 226 | |
|
| 1 | 227 | | return -1; |
| 2 | 228 | | } |
| | 229 | | } |
| | 230 | | } |