| | 1 | | // Copyright (c) 2020-2023 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 | |
|
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | | using Unity.Collections.LowLevel.Unsafe; |
| | 7 | | using Unity.Mathematics; |
| | 8 | |
|
| | 9 | | namespace GDX.Collections.Generic |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Multiple arrays acting as one uniform coalesced array. |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks>Stores a maximum of 18,446,744,073,709,551,615 elements.</remarks> |
| | 15 | | /// <typeparam name="T">The type of data to be stored.</typeparam> |
| | 16 | | public struct CoalesceArray<T> |
| | 17 | | { |
| | 18 | | const ulong k_MaxByteSize = 2147483648; |
| | 19 | | /// <summary> |
| | 20 | | /// The internal arrays storage |
| | 21 | | /// </summary> |
| | 22 | | SimpleList<T[]> m_Buckets; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// The block size used to allocate new arrays. |
| | 26 | | /// </summary> |
| | 27 | | readonly ulong m_BucketSize; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Cached version of the bucket size, minus one used in <see cref="GetBucketLocation"/>. |
| | 31 | | /// </summary> |
| | 32 | | readonly ulong m_BucketSizeMinusOne; |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// The number of elements the <see cref="CoalesceArray{T}"/> is capable of holding. |
| | 36 | | /// </summary> |
| | 37 | | ulong m_Length; |
| | 38 | |
|
| | 39 | | public CoalesceArray(ulong length) |
| 0 | 40 | | { |
| 0 | 41 | | int sizeOf = UnsafeUtility.SizeOf(typeof(T)); |
| 0 | 42 | | ulong expectedSize = length * (ulong)sizeOf; |
| | 43 | |
|
| | 44 | | // Check if we've wrapped around, if the size is bigger then our max data size, or if the length will exceed |
| | 45 | | // the array address space. |
| 0 | 46 | | if (expectedSize < length || expectedSize > k_MaxByteSize || length >= k_MaxByteSize) |
| 0 | 47 | | { |
| 0 | 48 | | m_BucketSize = k_MaxByteSize / (ulong)sizeOf; |
| 0 | 49 | | } |
| | 50 | | else |
| 0 | 51 | | { |
| 0 | 52 | | m_BucketSize = math.ceilpow2(length); |
| 0 | 53 | | } |
| 0 | 54 | | m_BucketSizeMinusOne = m_BucketSize - 1; |
| | 55 | |
|
| | 56 | | // Build our empty arrays |
| 0 | 57 | | ulong remainder = length & m_BucketSizeMinusOne; |
| 0 | 58 | | int extraArray = 0; |
| 0 | 59 | | if (remainder > 0) |
| 0 | 60 | | { |
| 0 | 61 | | extraArray = 1; |
| 0 | 62 | | } |
| 0 | 63 | | int placesToShift = math.tzcnt(m_BucketSize); |
| 0 | 64 | | int arrayCount = (int)(length >> placesToShift) + extraArray; |
| 0 | 65 | | m_Buckets = new SimpleList<T[]>(arrayCount); |
| 0 | 66 | | for (int i = 0; i < arrayCount; i++) |
| 0 | 67 | | { |
| 0 | 68 | | m_Buckets.Array[i] = new T[(int)m_BucketSize]; |
| 0 | 69 | | } |
| | 70 | |
|
| 0 | 71 | | m_Length = length; |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public T this[ulong index] |
| | 75 | | { |
| | 76 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 77 | | get |
| 0 | 78 | | { |
| 0 | 79 | | (int bucketIndex, int bucketOffset) info = GetBucketLocation(index); |
| 0 | 80 | | return m_Buckets.Array[info.bucketIndex][info.bucketOffset]; |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 84 | | set |
| 0 | 85 | | { |
| 0 | 86 | | (int bucketIndex, int bucketOffset) info = GetBucketLocation(index); |
| 0 | 87 | | m_Buckets.Array[info.bucketIndex][info.bucketOffset] = value; |
| 0 | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public ulong Length |
| | 92 | | { |
| 0 | 93 | | get => m_Length; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | public ref T[] GetBucket(int bucketIndex) |
| 0 | 97 | | { |
| 0 | 98 | | return ref m_Buckets.Array[bucketIndex]; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public int GetBucketCount() |
| 0 | 102 | | { |
| 0 | 103 | | return m_Buckets.Count; |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 107 | | public (int bucketIndex, int bucketOffset) GetBucketLocation(ulong index) |
| 0 | 108 | | { |
| 0 | 109 | | int bucketOffset = (int)(index & m_BucketSizeMinusOne); |
| 0 | 110 | | int placesToShift = math.tzcnt(m_BucketSize); |
| 0 | 111 | | int bucketIndex = (int)(index >> placesToShift); |
| 0 | 112 | | return (bucketIndex, bucketOffset); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public bool Resize(ulong desiredLength) |
| 0 | 116 | | { |
| 0 | 117 | | ulong remainder = desiredLength & m_BucketSizeMinusOne; |
| 0 | 118 | | int extraArray = 0; |
| 0 | 119 | | if (remainder > 0) |
| 0 | 120 | | { |
| 0 | 121 | | extraArray = 1; |
| 0 | 122 | | } |
| 0 | 123 | | int arrayCount = (int)((desiredLength - remainder) / m_BucketSize) + extraArray; |
| 0 | 124 | | int previousCount = m_Buckets.Count; |
| | 125 | |
|
| | 126 | | // Grow |
| 0 | 127 | | if (arrayCount > previousCount) |
| 0 | 128 | | { |
| 0 | 129 | | int additionalCount = arrayCount - previousCount; |
| 0 | 130 | | for (int i = 0; i < additionalCount; i++) |
| 0 | 131 | | { |
| 0 | 132 | | m_Buckets.AddWithExpandCheck(new T[m_BucketSize]); |
| 0 | 133 | | } |
| 0 | 134 | | m_Length = desiredLength; |
| 0 | 135 | | return true; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | // Shrink |
| 0 | 139 | | if (arrayCount < previousCount) |
| 0 | 140 | | { |
| 0 | 141 | | int extraCount = previousCount - arrayCount; |
| 0 | 142 | | for (int i = 0; i < extraCount; i++) |
| 0 | 143 | | { |
| 0 | 144 | | m_Buckets.RemoveFromBack(); |
| 0 | 145 | | } |
| 0 | 146 | | m_Length = desiredLength; |
| 0 | 147 | | return true; |
| | 148 | | } |
| | 149 | |
|
| | 150 | | // Do nothing |
| 0 | 151 | | m_Length = desiredLength; |
| 0 | 152 | | return false; |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | } |
| | 156 | | } |