| | 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; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | |
|
| | 8 | | namespace GDX.Collections |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// A 16-bit array. |
| | 12 | | /// </summary> |
| | 13 | | /// <example> |
| | 14 | | /// Useful for packing a bunch of data with known indices tightly. |
| | 15 | | /// <code> |
| | 16 | | /// if(myBitArray16[1]) |
| | 17 | | /// { |
| | 18 | | /// BeAwesome(); |
| | 19 | | /// } |
| | 20 | | /// </code> |
| | 21 | | /// </example> |
| | 22 | | [Serializable] |
| | 23 | | public struct BitArray16 |
| | 24 | | { |
| | 25 | | /// <summary> |
| | 26 | | /// First reserved <see cref="ushort" /> memory block (16-bits). |
| | 27 | | /// </summary> |
| | 28 | | /// <remarks>Indices 0-15</remarks> |
| | 29 | | public ushort Bits0; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Create a new <see cref="BitArray16" /> based on provided <paramref name="bits0" />. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="bits0">An existing value to be used to create the backing data for a <see cref="BitArray16" />< |
| | 35 | | public BitArray16(ushort bits0) |
| 0 | 36 | | { |
| 0 | 37 | | Bits0 = bits0; |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Access bit in array. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="index">Target bit index.</param> |
| | 44 | | public bool this[byte index] |
| | 45 | | { |
| | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 384 | 47 | | get => (Bits0 & (1 << index)) != 0; |
| | 48 | |
|
| | 49 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 50 | | set |
| 4 | 51 | | { |
| 4 | 52 | | if (value) |
| 4 | 53 | | { |
| 4 | 54 | | Bits0 = (ushort)(Bits0 | (1 << index)); |
| 4 | 55 | | } |
| | 56 | | else |
| 0 | 57 | | { |
| 0 | 58 | | Bits0 = (ushort)(Bits0 & ~(1 << index)); |
| 0 | 59 | | } |
| 4 | 60 | | } |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |