| | 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 | |
|
| | 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 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// The internal arrays storage |
| | 22 | | /// </summary> |
| | 23 | | SimpleList<T[]> m_Buckets; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The block size used to allocate new arrays. |
| | 27 | | /// </summary> |
| | 28 | | readonly ulong m_BucketSize; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Cached version of the bucket size, minus one used in <see cref="GetBucketLocation" />. |
| | 32 | | /// </summary> |
| | 33 | | readonly ulong m_BucketSizeMinusOne; |
| | 34 | |
|
| | 35 | | public CoalesceArray(ulong length) |
| 0 | 36 | | { |
| 0 | 37 | | int sizeOf = UnsafeUtility.SizeOf(typeof(T)); |
| 0 | 38 | | ulong expectedSize = length * (ulong)sizeOf; |
| | 39 | |
|
| | 40 | | // Check if we've wrapped around, if the size is bigger then our max data size, or if the length will exceed |
| | 41 | | // the array address space. |
| 0 | 42 | | if (expectedSize < length || expectedSize > k_MaxByteSize || length >= k_MaxByteSize) |
| 0 | 43 | | { |
| 0 | 44 | | m_BucketSize = k_MaxByteSize / (ulong)sizeOf; |
| 0 | 45 | | } |
| | 46 | | else |
| 0 | 47 | | { |
| 0 | 48 | | m_BucketSize = math.ceilpow2(length); |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | m_BucketSizeMinusOne = m_BucketSize - 1; |
| | 52 | |
|
| | 53 | | // Build our empty arrays |
| 0 | 54 | | ulong remainder = length & m_BucketSizeMinusOne; |
| 0 | 55 | | int extraArray = 0; |
| 0 | 56 | | if (remainder > 0) |
| 0 | 57 | | { |
| 0 | 58 | | extraArray = 1; |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | int placesToShift = math.tzcnt(m_BucketSize); |
| 0 | 62 | | int arrayCount = (int)(length >> placesToShift) + extraArray; |
| 0 | 63 | | m_Buckets = new SimpleList<T[]>(arrayCount); |
| 0 | 64 | | for (int i = 0; i < arrayCount; i++) |
| 0 | 65 | | { |
| 0 | 66 | | m_Buckets.Array[i] = new T[(int)m_BucketSize]; |
| 0 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | Length = length; |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public T this[ulong index] |
| | 73 | | { |
| | 74 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 75 | | get |
| 0 | 76 | | { |
| 0 | 77 | | (int bucketIndex, int bucketOffset) info = GetBucketLocation(index); |
| 0 | 78 | | return m_Buckets.Array[info.bucketIndex][info.bucketOffset]; |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 82 | | set |
| 0 | 83 | | { |
| 0 | 84 | | (int bucketIndex, int bucketOffset) info = GetBucketLocation(index); |
| 0 | 85 | | m_Buckets.Array[info.bucketIndex][info.bucketOffset] = value; |
| 0 | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// The number of elements the <see cref="CoalesceArray{T}" /> is capable of holding. |
| | 91 | | /// </summary> |
| 0 | 92 | | public ulong Length { get; private set; } |
| | 93 | |
|
| | 94 | | public ref T[] GetBucket(int bucketIndex) |
| 0 | 95 | | { |
| 0 | 96 | | return ref m_Buckets.Array[bucketIndex]; |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public int GetBucketCount() |
| 0 | 100 | | { |
| 0 | 101 | | return m_Buckets.Count; |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 105 | | public (int bucketIndex, int bucketOffset) GetBucketLocation(ulong index) |
| 0 | 106 | | { |
| 0 | 107 | | int bucketOffset = (int)(index & m_BucketSizeMinusOne); |
| 0 | 108 | | int placesToShift = math.tzcnt(m_BucketSize); |
| 0 | 109 | | int bucketIndex = (int)(index >> placesToShift); |
| 0 | 110 | | return (bucketIndex, bucketOffset); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public bool Resize(ulong desiredLength) |
| 0 | 114 | | { |
| 0 | 115 | | ulong remainder = desiredLength & m_BucketSizeMinusOne; |
| 0 | 116 | | int extraArray = 0; |
| 0 | 117 | | if (remainder > 0) |
| 0 | 118 | | { |
| 0 | 119 | | extraArray = 1; |
| 0 | 120 | | } |
| | 121 | |
|
| 0 | 122 | | int arrayCount = (int)((desiredLength - remainder) / m_BucketSize) + extraArray; |
| 0 | 123 | | int previousCount = m_Buckets.Count; |
| | 124 | |
|
| | 125 | | // Grow |
| 0 | 126 | | if (arrayCount > previousCount) |
| 0 | 127 | | { |
| 0 | 128 | | int additionalCount = arrayCount - previousCount; |
| 0 | 129 | | for (int i = 0; i < additionalCount; i++) |
| 0 | 130 | | { |
| 0 | 131 | | m_Buckets.AddWithExpandCheck(new T[m_BucketSize]); |
| 0 | 132 | | } |
| | 133 | |
|
| 0 | 134 | | 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 | | } |
| | 146 | |
|
| 0 | 147 | | Length = desiredLength; |
| 0 | 148 | | return true; |
| | 149 | | } |
| | 150 | |
|
| | 151 | | // Do nothing |
| 0 | 152 | | Length = desiredLength; |
| 0 | 153 | | return false; |
| 0 | 154 | | } |
| | 155 | | } |
| | 156 | | } |