| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using Hertzole.ScriptableValues.Helpers; |
| | | 8 | | |
| | | 9 | | namespace Hertzole.ScriptableValues |
| | | 10 | | { |
| | | 11 | | internal ref struct CollectionScope<T> |
| | | 12 | | { |
| | | 13 | | private T[]? array; |
| | | 14 | | |
| | 15156 | 15 | | public int Length { get; private set; } |
| | | 16 | | |
| | | 17 | | public ReadOnlySpan<T> Span |
| | | 18 | | { |
| | 12384 | 19 | | get { return array == null ? ReadOnlySpan<T>.Empty : new ReadOnlySpan<T>(array, 0, Length); } |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public CollectionScope(IEnumerable<T> items) |
| | 3744 | 23 | | { |
| | 3744 | 24 | | if (items.TryGetNonEnumeratedCount(out int count)) |
| | 3660 | 25 | | { |
| | 3660 | 26 | | T[]? tempArray = ArrayPool<T>.Shared.Rent(count); |
| | | 27 | | |
| | 3660 | 28 | | if (items is ICollection<T> collection) |
| | 3660 | 29 | | { |
| | 3660 | 30 | | collection.CopyTo(tempArray, 0); |
| | 3660 | 31 | | } |
| | | 32 | | else |
| | 0 | 33 | | { |
| | 0 | 34 | | int i = 0; |
| | 0 | 35 | | foreach (T item in items) |
| | 0 | 36 | | { |
| | 0 | 37 | | tempArray[i++] = item; |
| | 0 | 38 | | } |
| | 0 | 39 | | } |
| | | 40 | | |
| | 3660 | 41 | | array = tempArray; |
| | 3660 | 42 | | Length = count; |
| | 3660 | 43 | | } |
| | | 44 | | else |
| | 84 | 45 | | { |
| | 84 | 46 | | T[]? tempArray = ArrayPool<T>.Shared.Rent(4); |
| | | 47 | | |
| | 84 | 48 | | int i = 0; |
| | 12252 | 49 | | foreach (T item in items) |
| | 6000 | 50 | | { |
| | 6000 | 51 | | ArrayHelpers.EnsureCapacity(ref tempArray, i); |
| | 6000 | 52 | | tempArray[i++] = item; |
| | 6000 | 53 | | } |
| | | 54 | | |
| | 84 | 55 | | array = tempArray; |
| | 84 | 56 | | Length = i; |
| | 84 | 57 | | } |
| | 3744 | 58 | | } |
| | | 59 | | |
| | | 60 | | public CollectionEnumerator<T> GetEnumerator() |
| | 1314 | 61 | | { |
| | 1314 | 62 | | Guard.IsNotNull(array, nameof(array)); |
| | | 63 | | |
| | 1314 | 64 | | CollectionEnumerator<T> enumerator = CollectionEnumerator<T>.enumeratorPool.Get(); |
| | 1314 | 65 | | enumerator.SetArray(array, Length); |
| | 1314 | 66 | | return enumerator; |
| | 1314 | 67 | | } |
| | | 68 | | |
| | | 69 | | public void Dispose() |
| | 4176 | 70 | | { |
| | 4176 | 71 | | if (array != null) |
| | 4176 | 72 | | { |
| | 4176 | 73 | | ArrayPool<T>.Shared.Return(array, RuntimeHelpers.IsReferenceOrContainsReferences<T>()); |
| | 4176 | 74 | | array = null; |
| | 4176 | 75 | | Length = 0; |
| | 4176 | 76 | | } |
| | 4176 | 77 | | } |
| | | 78 | | |
| | | 79 | | public static CollectionScope<T> FromListSlice(IReadOnlyList<T> list, int index, int count) |
| | 450 | 80 | | { |
| | 450 | 81 | | T[]? array = ArrayPool<T>.Shared.Rent(count); |
| | 296232 | 82 | | for (int i = 0; i < count; i++) |
| | 147684 | 83 | | { |
| | 147684 | 84 | | array[i] = list[index + i]; |
| | 147666 | 85 | | } |
| | | 86 | | |
| | 432 | 87 | | return new CollectionScope<T> |
| | | 88 | | { |
| | | 89 | | array = array, |
| | | 90 | | Length = count |
| | | 91 | | }; |
| | 432 | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |