< Summary

Class:GDX.ArrayExtensions
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/ArrayExtensions.cs
Covered lines:73
Uncovered lines:20
Coverable lines:93
Total lines:230
Line coverage:78.4% (73 of 93)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:10
Method coverage:80% (8 of 10)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Clear[T]()0%110100%
ContainsItem[T](...)0%3.013091.67%
ContainsReference[T](...)0%3.013091.67%
ContainsValue[T](...)0%3.013091.67%
FirstIndexOf[T](...)0%12300%
FirstIndexOfItem[T](...)0%3.013091.67%
FirstIndexOfValue[T](...)0%3.013091.67%
LastIndexOf[T](...)0%12300%
LastIndexOfItem[T](...)0%3.043083.33%
LastIndexOfValue[T](...)0%3.013091.67%

File(s)

./Packages/com.dotbunny.gdx/GDX/ArrayExtensions.cs

#LineLine coverage
 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
 5using System;
 6using System.Runtime.CompilerServices;
 7
 8namespace 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)
 126        {
 127            Array.Clear(targetArray, 0, targetArray.Length);
 128        }
 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
 339        {
 340            int count = targetArray.Length;
 2041            for (int i = 0; i < count; i++)
 942            {
 943                if (targetArray[i] == targetValue)
 244                {
 245                    return true;
 46                }
 747            }
 48
 149            return false;
 350        }
 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
 262        {
 263            int count = targetArray.Length;
 1264            for (int i = 0; i < count; i++)
 565            {
 66#pragma warning disable
 67                // ReSharper disable All
 568                if ((System.Object)targetArray[i] == (System.Object)targetItem)
 169                {
 170                    return true;
 71                }
 72                // ReSharper restore All
 73#pragma warning restore
 474            }
 75
 176            return false;
 277        }
 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>
 288        {
 289            int count = targetArray.Length;
 1890            for (int i = 0; i < count; i++)
 891            {
 892                if (targetArray[i].Equals(targetValue))
 193                {
 194                    return true;
 95                }
 796            }
 97
 198            return false;
 299        }
 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>
 0110        {
 0111            int length = targetArray.Length;
 0112            for (int i = 0; i < length; i++)
 0113            {
 0114                if (targetArray[i].Equals(targetItem))
 0115                {
 0116                    return i;
 117                }
 0118            }
 119
 0120            return -1;
 0121        }
 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
 2132        {
 2133            int length = targetArray.Length;
 14134            for (int i = 0; i < length; i++)
 6135            {
 6136                if (targetArray[i] == targetItem)
 1137                {
 1138                    return i;
 139                }
 5140            }
 141
 1142            return -1;
 2143        }
 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
 2153        {
 2154            int length = targetArray.Length;
 32155            for (int i = 0; i < length; i++)
 15156            {
 15157                if (targetArray[i].Equals(targetValue))
 1158                {
 1159                    return i;
 160                }
 14161            }
 162
 1163            return -1;
 2164        }
 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>
 0175        {
 0176            int length = targetArray.Length;
 0177            for (int i = length - 1; i >= 0; i--)
 0178            {
 0179                if (targetArray[i].Equals(targetItem))
 0180                {
 0181                    return i;
 182                }
 0183            }
 184
 0185            return -1;
 0186        }
 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
 2196        {
 2197            int length = targetArray.Length;
 12198            for (int i = length - 1; i >= 0; i--)
 5199            {
 5200                if (targetArray[i] == targetItem)
 1201                {
 1202                    return i;
 203                }
 4204            }
 205
 1206            return -1;
 2207        }
 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
 2217        {
 2218            int length = targetArray.Length;
 32219            for (int i = length - 1; i >= 0; i--)
 15220            {
 15221                if (targetArray[i].Equals(targetValue))
 1222                {
 1223                    return i;
 224                }
 14225            }
 226
 1227            return -1;
 2228        }
 229    }
 230}

Coverage by test methods















































































































































































































































































































































































































































































































































































































































































































Methods/Properties

Clear[T]()
ContainsItem[T](, T)
ContainsReference[T](, T)
ContainsValue[T](, T)
FirstIndexOf[T](, T)
FirstIndexOfItem[T](, T)
FirstIndexOfValue[T](, T)
LastIndexOf[T](, T)
LastIndexOfItem[T](, T)
LastIndexOfValue[T](, T)