BitArray.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (c) 2019-2020 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Runtime.CompilerServices;
  19. namespace Ryujinx.Audio.Renderer.Utils
  20. {
  21. /// <summary>
  22. /// A simple bit array implementation backed by a <see cref="Memory{T}"/>.
  23. /// </summary>
  24. public class BitArray
  25. {
  26. /// <summary>
  27. /// The backing storage of the <see cref="BitArray"/>.
  28. /// </summary>
  29. private Memory<byte> _storage;
  30. /// <summary>
  31. /// Create a new <see cref="BitArray"/> from <see cref="Memory{T}"/>.
  32. /// </summary>
  33. /// <param name="storage">The backing storage of the <see cref="BitArray"/>.</param>
  34. public BitArray(Memory<byte> storage)
  35. {
  36. _storage = storage;
  37. }
  38. /// <summary>
  39. /// Get the byte position of a given bit index.
  40. /// </summary>
  41. /// <param name="index">A bit index.</param>
  42. /// <returns>The byte position of a given bit index.</returns>
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. private static int ToPosition(int index) => index / 8;
  45. /// <summary>
  46. /// Get the bit position of a given bit index inside a byte.
  47. /// </summary>
  48. /// <param name="index">A bit index.</param>
  49. /// <returns>The bit position of a given bit index inside a byte.</returns>
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. private static int ToBitPosition(int index) => index % 8;
  52. /// <summary>
  53. /// Test if the bit at the given index is set.
  54. /// </summary>
  55. /// <param name="index">A bit index.</param>
  56. /// <returns>Return true if the bit at the given index is set</returns>
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public bool Test(int index)
  59. {
  60. ulong mask = 1ul << ToBitPosition(index);
  61. return (_storage.Span[ToPosition(index)] & mask) == mask;
  62. }
  63. /// <summary>
  64. /// Set the bit at the given index.
  65. /// </summary>
  66. /// <param name="index">A bit index.</param>
  67. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  68. public void Set(int index)
  69. {
  70. Set(index, true);
  71. }
  72. /// <summary>
  73. /// Reset the bit at the given index.
  74. /// </summary>
  75. /// <param name="index">A bit index.</param>
  76. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  77. public void Reset(int index)
  78. {
  79. Set(index, false);
  80. }
  81. /// <summary>
  82. /// Set a bit value at the given index.
  83. /// </summary>
  84. /// <param name="index">A bit index.</param>
  85. /// <param name="value">The new bit value.</param>
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. private void Set(int index, bool value)
  88. {
  89. byte mask = (byte)(1 << ToBitPosition(index));
  90. if (value)
  91. {
  92. _storage.Span[ToPosition(index)] |= mask;
  93. }
  94. else
  95. {
  96. _storage.Span[ToPosition(index)] &= (byte)~mask;
  97. }
  98. }
  99. /// <summary>
  100. /// Reset all bits in the storage.
  101. /// </summary>
  102. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  103. public void Reset()
  104. {
  105. _storage.Span.Fill(0);
  106. }
  107. }
  108. }