< Summary

Class:GDX.IListExtensions
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/IListExtensions.cs
Covered lines:67
Uncovered lines:17
Coverable lines:84
Total lines:220
Line coverage:79.7% (67 of 84)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:9
Method coverage:77.7% (7 of 9)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AddUniqueItem[T](...)0%220100%
AddUniqueRange[T](...)0%6200%
ContainsItem[T](...)0%330100%
ContainsReference[T](...)0%12300%
RemoveFirstItem[T](...)0%330100%
RemoveItems[T](...)0%330100%
RemoveItemSwap[T](...)0%110100%
RemoveLastItem[T](...)0%330100%
Shuffle[T](...)0%220100%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/IListExtensions.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 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.Collections.Generic;
 6using System.Runtime.CompilerServices;
 7
 8namespace GDX
 9{
 10    /// <summary>
 11    ///     <see cref="System.Collections.Generic.IList{T}" /> Based Extension Methods
 12    /// </summary>
 13    [VisualScriptingCompatible(2)]
 14    public static class IListExtensions
 15    {
 16        /// <summary>
 17        ///     Add an item to a <see cref="System.Collections.Generic.IList{T}" />, but only if it is not already conta
 18        /// </summary>
 19        /// <param name="targetList">The <see cref="System.Collections.Generic.IList{T}" /> to add too.</param>
 20        /// <param name="targetItem">The target object to add.</param>
 21        /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam>
 22        /// <returns>true/false if this operation was able to add the item.</returns>
 23        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 24        public static bool AddUniqueItem<T>(this IList<T> targetList, T targetItem) where T : class
 225        {
 226            if (targetList.ContainsItem(targetItem))
 127            {
 128                return false;
 29            }
 30
 131            targetList.Add(targetItem);
 132            return true;
 33
 234        }
 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
 045        {
 046            int itemCount = targetItems.Length;
 047            for (int i = 0; i < itemCount; i++)
 048            {
 049                targetList.AddUniqueItem(targetItems[i]);
 050            }
 051        }
 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
 763        {
 764            int length = targetList.Count;
 4665            for (int i = 0; i < length; i++)
 2066            {
 2067                if (targetList[i] == targetItem)
 468                {
 469                    return true;
 70                }
 1671            }
 72
 373            return false;
 774        }
 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
 086        {
 087            int count = targetList.Count;
 088            for (int i = 0; i < count; i++)
 089            {
 90#pragma warning disable
 91                // ReSharper disable All
 092                if ((System.Object)targetList[i] == (System.Object)targetItem)
 093                {
 094                    return true;
 95                }
 96                // ReSharper restore All
 97#pragma warning restore
 098            }
 099            return false;
 0100        }
 101
 102        /// <summary>
 103        ///     <para>Removes the first <paramref name="targetItem" /> from the provided <paramref name="targetList" />.
 104        /// </summary>
 105        /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks>
 106        /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param>
 107        /// <param name="targetItem">The target object to remove from the <paramref name="targetList" />.</param>
 108        /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam>
 109        /// <returns>true/false if the item was removed.</returns>
 110        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111        public static bool RemoveFirstItem<T>(this IList<T> targetList, T targetItem) where T : class
 2112        {
 2113            int length = targetList.Count;
 16114            for (int i = 0; i < length; i++)
 7115            {
 7116                if (targetList[i] != targetItem)
 6117                {
 6118                    continue;
 119                }
 120
 1121                targetList.RemoveAt(i);
 1122                return true;
 123            }
 124
 1125            return false;
 2126        }
 127
 128        /// <summary>
 129        ///     <para>Removes all <paramref name="targetItem" /> from the provided <paramref name="targetList" />.</para
 130        /// </summary>
 131        /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />, uses object pointers.</
 132        /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param>
 133        /// <param name="targetItem">The target object to remove from the <paramref name="targetList" />.</param>
 134        /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam>
 135        /// <returns>true/false if the item was removed.</returns>
 136        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 137        public static bool RemoveItems<T>(this IList<T> targetList, T targetItem) where T : class
 1138        {
 1139            int length = targetList.Count;
 1140            bool removedItem = false;
 12141            for (int i = length - 1; i >= 0; i--)
 5142            {
 5143                if (targetList[i] != targetItem)
 3144                {
 3145                    continue;
 146                }
 147
 2148                targetList.RemoveAt(i);
 2149                removedItem = true;
 2150            }
 151
 1152            return removedItem;
 1153        }
 154
 155        /// <summary>
 156        ///     <para>
 157        ///         Replaces the object found at the provided <paramref name="index" /> with the last object in
 158        ///         <paramref name="targetList" />, then removes the last item from the <paramref name="targetList" />.
 159        ///     </para>
 160        /// </summary>
 161        /// <remarks>
 162        ///     This make sure that you are always removing from the end of a
 163        ///     <see cref="System.Collections.Generic.IList{T}" />.
 164        /// </remarks>
 165        /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param>
 166        /// <param name="index">The index of the item to remove.</param>
 167        /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam>
 168        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 169        public static void RemoveItemSwap<T>(this IList<T> targetList, int index)
 1170        {
 1171            int lastIndex = targetList.Count - 1;
 1172            targetList[index] = targetList[lastIndex];
 1173            targetList.RemoveAt(lastIndex);
 1174        }
 175
 176        /// <summary>
 177        ///     <para>Removes the last <paramref name="targetItem" /> from the provided <paramref name="targetList" />.<
 178        /// </summary>
 179        /// <remarks>Avoids using <see cref="System.Collections.Generic.EqualityComparer{T}" />.</remarks>
 180        /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param>
 181        /// <param name="targetItem">The target object to remove from the <paramref name="targetList" />.</param>
 182        /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam>
 183        /// <returns>true/false if the item was removed.</returns>
 184        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 185        public static bool RemoveLastItem<T>(this IList<T> targetList, T targetItem) where T : class
 2186        {
 2187            int length = targetList.Count;
 10188            for (int i = length - 1; i >= 0; i--)
 4189            {
 4190                if (targetList[i] != targetItem)
 3191                {
 3192                    continue;
 193                }
 194
 1195                targetList.RemoveAt(i);
 1196                return true;
 197            }
 198
 1199            return false;
 2200        }
 201
 202        /// <summary>
 203        ///     Shuffle the items in the <paramref name="targetList" />.
 204        /// </summary>
 205        /// <param name="targetList">The target <see cref="System.Collections.Generic.IList{T}" />.</param>
 206        /// <typeparam name="T">The type of the <see cref="System.Collections.Generic.IList{T}" />.</typeparam>
 207        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 208        public static void Shuffle<T>(this IList<T> targetList)
 1209        {
 1210            int length = targetList.Count;
 54211            for (int i = 0; i < length; i++)
 26212            {
 26213                T t = targetList[i];
 26214                int r = Core.Random.NextIntegerExclusive(i, length);
 26215                targetList[i] = targetList[r];
 26216                targetList[r] = t;
 26217            }
 1218        }
 219    }
 220}