< Summary

Class:GDX.Collections.BitArray256
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Collections/BitArray256.cs
Covered lines:0
Uncovered lines:18
Coverable lines:18
Total lines:107
Line coverage:0% (0 of 18)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:2
Method coverage:0% (0 of 2)

Coverage History

File(s)

./Packages/com.dotbunny.gdx/GDX/Collections/BitArray256.cs

#LineLine coverage
 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
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7
 8namespace 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
 081            {
 082                int intIndex = (index & 255) >> 5;
 083                int bitIndex = index & 31;
 84                int intContainingBits;
 85
 086                fixed (BitArray256* array = &this)
 087                {
 088                    intContainingBits = ((int*)array)[intIndex];
 089                }
 90
 091                return (intContainingBits & (1 << bitIndex)) != 0;
 092            }
 93            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 94            set
 095            {
 096                int intIndex = (index & 255) >> 5;
 097                int bitIndex = index & 31;
 098                int negativeVal = value ? -1 : 0;
 99
 0100                fixed (int* array = &Bits0)
 0101                {
 0102                    array[intIndex] ^= (negativeVal ^ array[intIndex]) & (1 << bitIndex);
 0103                }
 0104            }
 105        }
 106    }
 107}