| | | 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 256-bit array. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <example> |
| | | 14 | | /// Useful for packing a bunch of data with known indices tightly. |
| | | 15 | | /// <code> |
| | | 16 | | /// if(myBitArray256[1]) |
| | | 17 | | /// { |
| | | 18 | | /// BeAwesome(); |
| | | 19 | | /// } |
| | | 20 | | /// </code> |
| | | 21 | | /// </example> |
| | | 22 | | [StructLayout(LayoutKind.Sequential)] |
| | | 23 | | public struct BitArray256 |
| | | 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 | | /// Access bit in array. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="index">Target bit index.</param> |
| | | 77 | | public unsafe bool this[int index] |
| | | 78 | | { |
| | | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 80 | | get |
| | 0 | 81 | | { |
| | 0 | 82 | | int intIndex = (index & 255) >> 5; |
| | 0 | 83 | | int bitIndex = index & 31; |
| | | 84 | | int intContainingBits; |
| | | 85 | | |
| | 0 | 86 | | fixed (BitArray256* array = &this) |
| | 0 | 87 | | { |
| | 0 | 88 | | intContainingBits = ((int*)array)[intIndex]; |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return (intContainingBits & (1 << bitIndex)) != 0; |
| | 0 | 92 | | } |
| | | 93 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 94 | | set |
| | 0 | 95 | | { |
| | 0 | 96 | | int intIndex = (index & 255) >> 5; |
| | 0 | 97 | | int bitIndex = index & 31; |
| | 0 | 98 | | int negativeVal = value ? -1 : 0; |
| | | 99 | | |
| | 0 | 100 | | fixed (int* array = &Bits0) |
| | 0 | 101 | | { |
| | 0 | 102 | | array[intIndex] ^= (negativeVal ^ array[intIndex]) & (1 << bitIndex); |
| | 0 | 103 | | } |
| | 0 | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | } |