EdgeMatrix.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2019-2021 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 Ryujinx.Audio.Renderer.Utils;
  18. using Ryujinx.Common;
  19. using System;
  20. using System.Diagnostics;
  21. using System.Runtime.CompilerServices;
  22. namespace Ryujinx.Audio.Renderer.Common
  23. {
  24. /// <summary>
  25. /// Represents a adjacent matrix.
  26. /// </summary>
  27. /// <remarks>This is used for splitter routing.</remarks>
  28. public class EdgeMatrix
  29. {
  30. /// <summary>
  31. /// Backing <see cref="BitArray"/> used for node connections.
  32. /// </summary>
  33. private BitArray _storage;
  34. /// <summary>
  35. /// The count of nodes of the current instance.
  36. /// </summary>
  37. private int _nodeCount;
  38. /// <summary>
  39. /// Get the required work buffer size memory needed for the <see cref="EdgeMatrix"/>.
  40. /// </summary>
  41. /// <param name="nodeCount">The count of nodes.</param>
  42. /// <returns>The size required for the given <paramref name="nodeCount"/>.</returns>
  43. public static int GetWorkBufferSize(int nodeCount)
  44. {
  45. int size = BitUtils.AlignUp(nodeCount * nodeCount, Constants.BufferAlignment);
  46. return size / Unsafe.SizeOf<byte>();
  47. }
  48. /// <summary>
  49. /// Initializes the <see cref="EdgeMatrix"/> instance with backing memory.
  50. /// </summary>
  51. /// <param name="edgeMatrixWorkBuffer">The backing memory.</param>
  52. /// <param name="nodeCount">The count of nodes.</param>
  53. public void Initialize(Memory<byte> edgeMatrixWorkBuffer, int nodeCount)
  54. {
  55. Debug.Assert(edgeMatrixWorkBuffer.Length >= GetWorkBufferSize(nodeCount));
  56. _storage = new BitArray(edgeMatrixWorkBuffer);
  57. _nodeCount = nodeCount;
  58. _storage.Reset();
  59. }
  60. /// <summary>
  61. /// Test if the bit at the given index is set.
  62. /// </summary>
  63. /// <param name="index">A bit index.</param>
  64. /// <returns>Returns true if the bit at the given index is set</returns>
  65. public bool Test(int index)
  66. {
  67. return _storage.Test(index);
  68. }
  69. /// <summary>
  70. /// Reset all bits in the storage.
  71. /// </summary>
  72. public void Reset()
  73. {
  74. _storage.Reset();
  75. }
  76. /// <summary>
  77. /// Reset the bit at the given index.
  78. /// </summary>
  79. /// <param name="index">A bit index.</param>
  80. public void Reset(int index)
  81. {
  82. _storage.Reset(index);
  83. }
  84. /// <summary>
  85. /// Set the bit at the given index.
  86. /// </summary>
  87. /// <param name="index">A bit index.</param>
  88. public void Set(int index)
  89. {
  90. _storage.Set(index);
  91. }
  92. /// <summary>
  93. /// Connect a given source to a given destination.
  94. /// </summary>
  95. /// <param name="source">The source index.</param>
  96. /// <param name="destination">The destination index.</param>
  97. public void Connect(int source, int destination)
  98. {
  99. Debug.Assert(source < _nodeCount);
  100. Debug.Assert(destination < _nodeCount);
  101. _storage.Set(_nodeCount * source + destination);
  102. }
  103. /// <summary>
  104. /// Check if the given source is connected to the given destination.
  105. /// </summary>
  106. /// <param name="source">The source index.</param>
  107. /// <param name="destination">The destination index.</param>
  108. /// <returns>Returns true if the given source is connected to the given destination.</returns>
  109. public bool Connected(int source, int destination)
  110. {
  111. Debug.Assert(source < _nodeCount);
  112. Debug.Assert(destination < _nodeCount);
  113. return _storage.Test(_nodeCount * source + destination);
  114. }
  115. /// <summary>
  116. /// Disconnect a given source from a given destination.
  117. /// </summary>
  118. /// <param name="source">The source index.</param>
  119. /// <param name="destination">The destination index.</param>
  120. public void Disconnect(int source, int destination)
  121. {
  122. Debug.Assert(source < _nodeCount);
  123. Debug.Assert(destination < _nodeCount);
  124. _storage.Reset(_nodeCount * source + destination);
  125. }
  126. /// <summary>
  127. /// Remove all edges from a given source.
  128. /// </summary>
  129. /// <param name="source">The source index.</param>
  130. public void RemoveEdges(int source)
  131. {
  132. for (int i = 0; i < _nodeCount; i++)
  133. {
  134. Disconnect(source, i);
  135. }
  136. }
  137. /// <summary>
  138. /// Get the total node count.
  139. /// </summary>
  140. /// <returns>The total node count.</returns>
  141. public int GetNodeCount()
  142. {
  143. return _nodeCount;
  144. }
  145. }
  146. }