< Summary

Class:GDX.Collections.Generic.IListExtensions
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Collections/Generic/IListExtensions.cs
Covered lines:67
Uncovered lines:17
Coverable lines:84
Total lines:221
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%2.092071.43%
AddUniqueRange[T](...)0%6200%
ContainsItem[T](...)0%3.013091.67%
ContainsReference[T](...)0%12300%
RemoveFirstItem[T](...)0%33092.31%
RemoveItems[T](...)0%330100%
RemoveItemSwap[T](...)0%110100%
RemoveLastItem[T](...)0%3.033084.62%
Shuffle[T](...)0%220100%

File(s)

./Packages/com.dotbunny.gdx/GDX/Collections/Generic/IListExtensions.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.Collections.Generic;
 7using System.Runtime.CompilerServices;
 8
 9namespace 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
 226        {
 227            if (targetList.ContainsItem(targetItem))
 128            {
 129                return false;
 30            }
 31
 132            targetList.Add(targetItem);
 133            return true;
 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 ((Object)targetList[i] == (Object)targetItem)
 093                {
 094                    return true;
 95                }
 96                // ReSharper restore All
 97#pragma warning restore
 098            }
 99
 0100            return false;
 0101        }
 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
 2113        {
 2114            int length = targetList.Count;
 16115            for (int i = 0; i < length; i++)
 7116            {
 7117                if (targetList[i] != targetItem)
 6118                {
 6119                    continue;
 120                }
 121
 1122                targetList.RemoveAt(i);
 1123                return true;
 124            }
 125
 1126            return false;
 2127        }
 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
 1139        {
 1140            int length = targetList.Count;
 1141            bool removedItem = false;
 12142            for (int i = length - 1; i >= 0; i--)
 5143            {
 5144                if (targetList[i] != targetItem)
 3145                {
 3146                    continue;
 147                }
 148
 2149                targetList.RemoveAt(i);
 2150                removedItem = true;
 2151            }
 152
 1153            return removedItem;
 1154        }
 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)
 1171        {
 1172            int lastIndex = targetList.Count - 1;
 1173            targetList[index] = targetList[lastIndex];
 1174            targetList.RemoveAt(lastIndex);
 1175        }
 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
 2187        {
 2188            int length = targetList.Count;
 10189            for (int i = length - 1; i >= 0; i--)
 4190            {
 4191                if (targetList[i] != targetItem)
 3192                {
 3193                    continue;
 194                }
 195
 1196                targetList.RemoveAt(i);
 1197                return true;
 198            }
 199
 1200            return false;
 2201        }
 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)
 1210        {
 1211            int length = targetList.Count;
 54212            for (int i = 0; i < length; i++)
 26213            {
 26214                T t = targetList[i];
 26215                int r = Core.Random.NextIntegerExclusive(i, length);
 26216                targetList[i] = targetList[r];
 26217                targetList[r] = t;
 26218            }
 1219        }
 220    }
 221}

Coverage by test methods












Methods/Properties

AddUniqueItem[T](System.Collections.Generic.IList[T], T)
AddUniqueRange[T](System.Collections.Generic.IList[T], )
ContainsItem[T](System.Collections.Generic.IList[T], T)
ContainsReference[T](System.Collections.Generic.IList[T], T)
RemoveFirstItem[T](System.Collections.Generic.IList[T], T)
RemoveItems[T](System.Collections.Generic.IList[T], T)
RemoveItemSwap[T](System.Collections.Generic.IList[T], System.Int32)
RemoveLastItem[T](System.Collections.Generic.IList[T], T)
Shuffle[T](System.Collections.Generic.IList[T])