BitArray.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Ryujinx.Audio.Renderer.Utils
  4. {
  5. /// <summary>
  6. /// A simple bit array implementation backed by a <see cref="Memory{T}"/>.
  7. /// </summary>
  8. public class BitArray
  9. {
  10. /// <summary>
  11. /// The backing storage of the <see cref="BitArray"/>.
  12. /// </summary>
  13. private Memory<byte> _storage;
  14. /// <summary>
  15. /// Create a new <see cref="BitArray"/> from <see cref="Memory{T}"/>.
  16. /// </summary>
  17. /// <param name="storage">The backing storage of the <see cref="BitArray"/>.</param>
  18. public BitArray(Memory<byte> storage)
  19. {
  20. _storage = storage;
  21. }
  22. /// <summary>
  23. /// Get the byte position of a given bit index.
  24. /// </summary>
  25. /// <param name="index">A bit index.</param>
  26. /// <returns>The byte position of a given bit index.</returns>
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. private static int ToPosition(int index) => index / 8;
  29. /// <summary>
  30. /// Get the bit position of a given bit index inside a byte.
  31. /// </summary>
  32. /// <param name="index">A bit index.</param>
  33. /// <returns>The bit position of a given bit index inside a byte.</returns>
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. private static int ToBitPosition(int index) => index % 8;
  36. /// <summary>
  37. /// Test if the bit at the given index is set.
  38. /// </summary>
  39. /// <param name="index">A bit index.</param>
  40. /// <returns>Return true if the bit at the given index is set</returns>
  41. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  42. public bool Test(int index)
  43. {
  44. ulong mask = 1ul << ToBitPosition(index);
  45. return (_storage.Span[ToPosition(index)] & mask) == mask;
  46. }
  47. /// <summary>
  48. /// Set the bit at the given index.
  49. /// </summary>
  50. /// <param name="index">A bit index.</param>
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. public void Set(int index)
  53. {
  54. Set(index, true);
  55. }
  56. /// <summary>
  57. /// Reset the bit at the given index.
  58. /// </summary>
  59. /// <param name="index">A bit index.</param>
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public void Reset(int index)
  62. {
  63. Set(index, false);
  64. }
  65. /// <summary>
  66. /// Set a bit value at the given index.
  67. /// </summary>
  68. /// <param name="index">A bit index.</param>
  69. /// <param name="value">The new bit value.</param>
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. private void Set(int index, bool value)
  72. {
  73. byte mask = (byte)(1 << ToBitPosition(index));
  74. if (value)
  75. {
  76. _storage.Span[ToPosition(index)] |= mask;
  77. }
  78. else
  79. {
  80. _storage.Span[ToPosition(index)] &= (byte)~mask;
  81. }
  82. }
  83. /// <summary>
  84. /// Reset all bits in the storage.
  85. /// </summary>
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. public void Reset()
  88. {
  89. _storage.Span.Fill(0);
  90. }
  91. }
  92. }