< Summary

Class:GDX.Collections.Generic.SimpleList[T]
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Collections/Generic/SimpleList.cs
Covered lines:308
Uncovered lines:7
Coverable lines:315
Total lines:580
Line coverage:97.7% (308 of 315)
Covered branches:0
Total branches:0
Covered methods:27
Total methods:28
Method coverage:96.4% (27 of 28)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SimpleList(...)0%110100%
SimpleList()0%110100%
SimpleList(...)0%110100%
AddUnchecked(...)0%110100%
AddWithExpandCheck(...)0%440100%
AddWithExpandCheck(...)0%220100%
AddExpandNoClear(...)0%440100%
AddExpandClearOld(...)0%440100%
AddExpandClearNew(...)0%440100%
AddExpandClearBoth(...)0%440100%
Clear()0%110100%
Compact()0%6200%
InsertUnchecked(...)0%110100%
InsertWithExpandCheck(...)0%440100%
InsertWithExpandCheck(...)0%440100%
InsertExpandNoClear(...)0%440100%
InsertExpandClearOld(...)0%440100%
InsertExpandClearNew(...)0%440100%
InsertExpandClearBoth(...)0%440100%
RemoveAt(...)0%220100%
RemoveAtSwapBack(...)0%110100%
RemoveFromBack()0%110100%
Reserve(...)0%220100%
ReserveNoClear(...)0%220100%
ReserveClearOld(...)0%220100%
ReserveClearNew(...)0%220100%
ReserveClearBoth(...)0%220100%
Reverse()0%220100%

File(s)

./Packages/com.dotbunny.gdx/GDX/Collections/Generic/SimpleList.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 GDX.Collections.Pooling;
 6
 7namespace GDX.Collections.Generic
 8{
 9    /// <summary>
 10    ///     An optimized <see cref="System.Collections.Generic.List{T}" />-like data structure.
 11    /// </summary>
 12    /// <typeparam name="T">The type of the <see cref="object" />s contained within.</typeparam>
 13    [VisualScriptingCompatible(1)]
 14    public struct SimpleList<T>
 15    {
 16        /// <summary>
 17        ///     Internal array of backed data for the <see cref="SimpleList{T}" />.
 18        /// </summary>
 19        public T[] Array;
 20
 21        /// <summary>
 22        ///     The current number of occupied elements in the <see cref="CircularBuffer{T}" />.
 23        /// </summary>
 24        /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks>
 25        public int Count;
 26
 27        /// <summary>
 28        ///     Create a <see cref="SimpleList{T}" /> with an initial <paramref name="capacity" />.
 29        /// </summary>
 30        /// <param name="capacity">An initial sizing for the <see cref="Array" />.</param>
 31        public SimpleList(int capacity)
 12632        {
 12633            Array = new T[capacity];
 12634            Count = 0;
 12635        }
 36
 37        /// <summary>
 38        ///     Create a <see cref="SimpleList{T}" /> providing an existing <paramref name="arrayToUse" />.
 39        /// </summary>
 40        /// <param name="arrayToUse">An existing array to use in the <see cref="SimpleList{T}" />.</param>
 41        public SimpleList(T[] arrayToUse)
 342        {
 343            Array = arrayToUse;
 344            Count = 0;
 345        }
 46
 47        /// <summary>
 48        ///     Create a <see cref="SimpleList{T}" /> providing an existing <paramref name="arrayToUse" /> and setting t
 49        ///     <see cref="Count" />.
 50        /// </summary>
 51        /// <param name="arrayToUse">An existing array to use in the <see cref="SimpleList{T}" />.</param>
 52        /// <param name="count">An existing element count.</param>
 53        public SimpleList(T[] arrayToUse, int count)
 2754        {
 2755            Array = arrayToUse;
 2756            Count = count;
 2757        }
 58
 59        /// <summary>
 60        ///     Add an item to the <see cref="SimpleList{T}" /> without checking the <see cref="Array" /> size.
 61        /// </summary>
 62        /// <param name="item">A typed <see cref="object" /> to add.</param>
 63        public void AddUnchecked(T item)
 33764        {
 33765            Array[Count] = item;
 33466            ++Count;
 33467        }
 68
 69        /// <summary>
 70        ///     Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized.
 71        /// </summary>
 72        /// <param name="item">A typed <see cref="object" /> to add.</param>
 73        public void AddWithExpandCheck(T item)
 3874        {
 3875            int arrayLength = Array.Length;
 76
 3877            if (Count >= arrayLength)
 578            {
 579                arrayLength = arrayLength == 0 ? 1 : arrayLength;
 580                System.Array.Resize(ref Array, arrayLength * 2);
 581            }
 82
 3883            Array[Count] = item;
 3884            ++Count;
 3885        }
 86
 87        /// <summary>
 88        ///     Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized.
 89        /// </summary>
 90        /// <param name="item">A typed <see cref="object" /> to add.</param>
 91        /// <param name="howMuchToExpand">How much to expand the array by.</param>
 92        public void AddWithExpandCheck(T item, int howMuchToExpand)
 193        {
 194            if (Count >= Array.Length)
 195            {
 196                System.Array.Resize(ref Array, Array.Length + howMuchToExpand);
 197            }
 98
 199            Array[Count] = item;
 1100            ++Count;
 1101        }
 102
 103        /// <summary>
 104        ///     Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized.
 105        /// </summary>
 106        /// <param name="item">A typed <see cref="object" /> to add.</param>
 107        /// <param name="pool">Pool to allocate the array from if resizing.</param>
 108        public void AddExpandNoClear(T item, ArrayPool<T> pool)
 5109        {
 5110            T[] array = Array;
 5111            int count = Count;
 5112            int arrayLength = array.Length;
 113
 5114            if (Count >= arrayLength)
 1115            {
 1116                arrayLength = arrayLength == 0 ? 1 : arrayLength;
 1117                T[] newArray = pool.Get(arrayLength * 2);
 1118                System.Array.Copy(array, 0, newArray, 0, arrayLength);
 1119                pool.Return(array);
 1120                array = newArray;
 1121                Array = newArray;
 1122            }
 123
 5124            array[count] = item;
 5125            ++count;
 5126            Count = count;
 5127        }
 128
 129        /// <summary>
 130        ///     Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. C
 131        ///     old array on resize after copying its contents.
 132        /// </summary>
 133        /// <param name="item">A typed <see cref="object" /> to add.</param>
 134        /// <param name="pool">Pool to allocate the array from if resizing.</param>
 135        public void AddExpandClearOld(T item, ArrayPool<T> pool)
 3136        {
 3137            T[] array = Array;
 3138            int count = Count;
 3139            int arrayLength = array.Length;
 140
 3141            if (Count >= arrayLength)
 1142            {
 1143                arrayLength = arrayLength == 0 ? 1 : arrayLength;
 1144                T[] newArray = pool.Get(arrayLength * 2);
 1145                System.Array.Copy(array, 0, newArray, 0, arrayLength);
 1146                System.Array.Clear(array, 0, arrayLength);
 1147                pool.Return(array);
 1148                array = newArray;
 1149                Array = newArray;
 1150            }
 151
 3152            array[count] = item;
 3153            ++count;
 3154            Count = count;
 3155        }
 156
 157        /// <summary>
 158        ///     Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized. C
 159        ///     unused indices of the new array on resize.
 160        /// </summary>
 161        /// <param name="item">A typed <see cref="object" /> to add.</param>
 162        /// <param name="pool">Pool to allocate the array from if resizing.</param>
 163        public void AddExpandClearNew(T item, ArrayPool<T> pool)
 5164        {
 5165            T[] array = Array;
 5166            int count = Count;
 5167            int arrayLength = array.Length;
 168
 5169            if (Count >= arrayLength)
 1170            {
 1171                arrayLength = arrayLength == 0 ? 1 : arrayLength;
 1172                T[] newArray = pool.Get(arrayLength * 2);
 1173                System.Array.Copy(array, 0, newArray, 0, arrayLength);
 1174                System.Array.Clear(newArray, arrayLength, arrayLength);
 1175                pool.Return(array);
 1176                array = newArray;
 1177                Array = newArray;
 1178            }
 179
 5180            array[count] = item;
 5181            ++count;
 5182            Count = count;
 5183        }
 184
 185        /// <summary>
 186        ///     Add an item to the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resized.  
 187        ///     and new arrays on resize.
 188        /// </summary>
 189        /// <param name="item">A typed <see cref="object" /> to add.</param>
 190        /// <param name="pool">Pool to allocate the array from if resizing.</param>
 191        public void AddExpandClearBoth(T item, ArrayPool<T> pool)
 5192        {
 5193            T[] array = Array;
 5194            int count = Count;
 5195            int arrayLength = array.Length;
 196
 5197            if (Count >= arrayLength)
 1198            {
 1199                arrayLength = arrayLength == 0 ? 1 : arrayLength;
 1200                T[] newArray = pool.Get(arrayLength * 2);
 1201                System.Array.Copy(array, 0, newArray, 0, arrayLength);
 1202                System.Array.Clear(array, 0, arrayLength);
 1203                System.Array.Clear(newArray, arrayLength, arrayLength);
 1204                pool.Return(array);
 1205                array = newArray;
 1206                Array = newArray;
 1207            }
 208
 5209            array[count] = item;
 5210            ++count;
 5211            Count = count;
 5212        }
 213
 214        /// <summary>
 215        ///     Clear out the <see cref="Array" /> in <see cref="SimpleList{T}" /> and sets the <see cref="Count" /> to 
 216        /// </summary>
 217        public void Clear()
 57218        {
 57219            System.Array.Clear(Array, 0, Count);
 57220            Count = 0;
 57221        }
 222
 223        /// <summary>
 224        ///     Shrink/compact the backing <see cref="Array" /> so there is no unused space.
 225        /// </summary>
 226        public void Compact()
 0227        {
 0228            int arrayLength = Array.Length;
 0229            if (Count < arrayLength)
 0230            {
 0231                System.Array.Resize(ref Array, Count);
 0232            }
 0233        }
 234
 235        /// <summary>
 236        ///     Insert an item into the <see cref="SimpleList{T}" /> without checking the <see cref="Array" /> size.
 237        /// </summary>
 238        /// <param name="item">A typed <see cref="object" /> to insert.</param>
 239        /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param>
 240        public void InsertUnchecked(int index, T item)
 3241        {
 3242            System.Array.Copy(Array, index, Array, index + 1, Count - index);
 3243            Array[index] = item;
 3244            ++Count;
 3245        }
 246
 247        /// <summary>
 248        ///     Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz
 249        /// </summary>
 250        /// <param name="item">A typed <see cref="object" /> to insert.</param>
 251        /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param>
 252        public void InsertWithExpandCheck(int index, T item)
 3253        {
 3254            int arrayLength = Array.Length;
 3255            if (Count >= arrayLength)
 3256            {
 3257                arrayLength = arrayLength == 0 ? 1 : arrayLength;
 3258                System.Array.Resize(ref Array, arrayLength * 2);
 3259            }
 260
 3261            System.Array.Copy(Array, index, Array, index + 1, Count - index);
 3262            Array[index] = item;
 3263            ++Count;
 3264        }
 265
 266        /// <summary>
 267        ///     Insert an item into the <see cref="SimpleList{T}" />, checking if <see cref="Array" /> needs to be resiz
 268        /// </summary>
 269        /// <param name="item">A typed <see cref="object" /> to insert.</param>
 270        /// <param name="index">The index in <see cref="Array" /> to add the <paramref name="item" /> at.</param>
 271        /// <param name="howMuchToExpand">The number of elements to add at the end of the array when expanding.</param>
 272        public void InsertWithExpandCheck(int index, T item, int howMuchToExpand)