< Summary

Information
Class: Hertzole.ScriptableValues.CollectionScope[T]
Assembly: Hertzole.ScriptableValues
File(s): ./Packages/se.hertzole.scriptable-values/Runtime/Collections/Core/CollectionScope.cs
Tag: 52_28197466029
Line coverage
87%
Covered lines: 47
Uncovered lines: 7
Coverable lines: 54
Total lines: 94
Line coverage: 87%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CollectionScope(...)0%8.057072.22%
GetEnumerator()0%110100%
Dispose()0%220100%
FromListSlice(...)0%220100%

File(s)

./Packages/se.hertzole.scriptable-values/Runtime/Collections/Core/CollectionScope.cs

#LineLine coverage
 1#nullable enable
 2
 3using System;
 4using System.Buffers;
 5using System.Collections.Generic;
 6using System.Runtime.CompilerServices;
 7using Hertzole.ScriptableValues.Helpers;
 8
 9namespace Hertzole.ScriptableValues
 10{
 11    internal ref struct CollectionScope<T>
 12    {
 13        private T[]? array;
 14
 1515615        public int Length { get; private set; }
 16
 17        public ReadOnlySpan<T> Span
 18        {
 1238419            get { return array == null ? ReadOnlySpan<T>.Empty : new ReadOnlySpan<T>(array, 0, Length); }
 20        }
 21
 22        public CollectionScope(IEnumerable<T> items)
 374423        {
 374424            if (items.TryGetNonEnumeratedCount(out int count))
 366025            {
 366026                T[]? tempArray = ArrayPool<T>.Shared.Rent(count);
 27
 366028                if (items is ICollection<T> collection)
 366029                {
 366030                    collection.CopyTo(tempArray, 0);
 366031                }
 32                else
 033                {
 034                    int i = 0;
 035                    foreach (T item in items)
 036                    {
 037                        tempArray[i++] = item;
 038                    }
 039                }
 40
 366041                array = tempArray;
 366042                Length = count;
 366043            }
 44            else
 8445            {
 8446                T[]? tempArray = ArrayPool<T>.Shared.Rent(4);
 47
 8448                int i = 0;
 1225249                foreach (T item in items)
 600050                {
 600051                    ArrayHelpers.EnsureCapacity(ref tempArray, i);
 600052                    tempArray[i++] = item;
 600053                }
 54
 8455                array = tempArray;
 8456                Length = i;
 8457            }
 374458        }
 59
 60        public CollectionEnumerator<T> GetEnumerator()
 131461        {
 131462            Guard.IsNotNull(array, nameof(array));
 63
 131464            CollectionEnumerator<T> enumerator = CollectionEnumerator<T>.enumeratorPool.Get();
 131465            enumerator.SetArray(array, Length);
 131466            return enumerator;
 131467        }
 68
 69        public void Dispose()
 417670        {
 417671            if (array != null)
 417672            {
 417673                ArrayPool<T>.Shared.Return(array, RuntimeHelpers.IsReferenceOrContainsReferences<T>());
 417674                array = null;
 417675                Length = 0;
 417676            }
 417677        }
 78
 79        public static CollectionScope<T> FromListSlice(IReadOnlyList<T> list, int index, int count)
 45080        {
 45081            T[]? array = ArrayPool<T>.Shared.Rent(count);
 29623282            for (int i = 0; i < count; i++)
 14768483            {
 14768484                array[i] = list[index + i];
 14766685            }
 86
 43287            return new CollectionScope<T>
 88            {
 89                array = array,
 90                Length = count
 91            };
 43292        }
 93    }
 94}