EdgeMatrix.cs 4.7 KB

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