| | 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 System.Runtime.InteropServices; |
| | 7 | |
|
| | 8 | | namespace GDX.Collections |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// A 512-bit array. |
| | 12 | | /// </summary> |
| | 13 | | /// <example> |
| | 14 | | /// Useful for packing a bunch of data with known indices tightly. |
| | 15 | | /// <code> |
| | 16 | | /// if(myBitArray512[1]) |
| | 17 | | /// { |
| | 18 | | /// BeAwesome(); |
| | 19 | | /// } |
| | 20 | | /// </code> |
| | 21 | | /// </example> |
| | 22 | | [StructLayout(LayoutKind.Sequential)] |
| | 23 | | public struct BitArray512 |
| | 24 | | { |
| | 25 | | /// <summary> |
| | 26 | | /// First reserved <see cref="System.Int32" /> memory block. |
| | 27 | | /// </summary> |
| | 28 | | /// <remarks>Indices 0-31</remarks> |
| | 29 | | public int Bits0; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Second reserved <see cref="System.Int32" /> memory block. |
| | 33 | | /// </summary> |
| | 34 | | /// <remarks>Indices 32-63</remarks> |
| | 35 | | public int Bits1; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Third reserved <see cref="System.Int32" /> memory block. |
| | 39 | | /// </summary> |
| | 40 | | /// <remarks>Indices 64-95</remarks> |
| | 41 | | public int Bits2; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Fourth reserved <see cref="System.Int32" /> memory block. |
| | 45 | | /// </summary> |
| | 46 | | /// <remarks>Indices 96-127</remarks> |
| | 47 | | public int Bits3; |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Fifth reserved <see cref="System.Int32" /> memory block. |
| | 51 | | /// </summary> |
| | 52 | | /// <remarks>Indices 128-159</remarks> |
| | 53 | | public int Bits4; |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Sixth reserved <see cref="System.Int32" /> memory block. |
| | 57 | | /// </summary> |
| | 58 | | /// <remarks>Indices 160-191</remarks> |
| | 59 | | public int Bits5; |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Seventh reserved <see cref="System.Int32" /> memory block. |
| | 63 | | /// </summary> |
| | 64 | | /// <remarks>Indices 192-223</remarks> |
| | 65 | | public int Bits6; |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Eighth reserved <see cref="System.Int32" /> memory block. |
| | 69 | | /// </summary> |
| | 70 | | /// <remarks>Indices 224-255</remarks> |
| | 71 | | public int Bits7; |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Ninth reserved <see cref="System.Int32" /> memory block. |
| | 75 | | /// </summary> |
| | 76 | | /// <remarks>Indices 256-287</remarks> |
| | 77 | | public int Bits8; |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Tenth reserved <see cref="System.Int32" /> memory block. |
| | 81 | | /// </summary> |
| | 82 | | /// <remarks>Indices 288-319</remarks> |
| | 83 | | public int Bits9; |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Eleventh reserved <see cref="System.Int32" /> memory block. |
| | 87 | | /// </summary> |
| | 88 | | /// <remarks>Indices 320-351</remarks> |
| | 89 | | public int Bits10; |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Twelfth reserved <see cref="System.Int32" /> memory block. |
| | 93 | | /// </summary> |
| | 94 | | /// <remarks>Indices 352-383</remarks> |
| | 95 | | public int Bits11; |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Thirteenth reserved <see cref="System.Int32" /> memory block. |
| | 99 | | /// </summary> |
| | 100 | | /// <remarks>Indices 384-415</remarks> |
| | 101 | | public int Bits12; |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Fourteenth reserved <see cref="System.Int32" /> memory block. |
| | 105 | | /// </summary> |
| | 106 | | /// <remarks>Indices 416-447</remarks> |
| | 107 | | public int Bits13; |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Fifteenth reserved <see cref="System.Int32" /> memory block. |
| | 111 | | /// </summary> |
| | 112 | | /// <remarks>Indices 448-479</remarks> |
| | 113 | | public int Bits14; |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Sixteenth reserved <see cref="System.Int32" /> memory block. |
| | 117 | | /// </summary> |
| | 118 | | /// <remarks>Indices 480-511</remarks> |
| | 119 | | public int Bits15; |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Access bit in array. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="index">Target bit index.</param> |
| | 125 | | public unsafe bool this[int index] |
| | 126 | | { |
| | 127 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 128 | | get |
| 0 | 129 | | { |
| 0 | 130 | | int intIndex = (index & 511) >> 5; |
| 0 | 131 | | int bitIndex = index & 31; |
| | 132 | | int intContainingBits; |
| | 133 | |
|
| 0 | 134 | | fixed (BitArray512* array = &this) |
| 0 | 135 | | { |
| 0 | 136 | | intContainingBits = ((int*)array)[intIndex]; |
| 0 | 137 | | } |
| | 138 | |
|
| 0 | 139 | | return (intContainingBits & (1 << bitIndex)) != 0; |
| 0 | 140 | | } |
| | 141 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 142 | | set |
| 0 | 143 | | { |
| 0 | 144 | | int intIndex = (index & 511) >> 5; |
| 0 | 145 | | int bitIndex = index & 31; |
| 0 | 146 | | int negativeVal = value ? -1 : 0; |
| | 147 | |
|
| 0 | 148 | | fixed (int* array = &Bits0) |
| 0 | 149 | | { |
| 0 | 150 | | array[intIndex] ^= (negativeVal ^ array[intIndex]) & (1 << bitIndex); |
| 0 | 151 | | } |
| 0 | 152 | | } |
| | 153 | | } |
| | 154 | | } |
| | 155 | | } |