NodeStates.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 Ryujinx.Audio.Renderer.Utils;
  18. using System;
  19. using System.Diagnostics;
  20. namespace Ryujinx.Audio.Renderer.Common
  21. {
  22. public class NodeStates
  23. {
  24. private class Stack
  25. {
  26. private Memory<int> _storage;
  27. private int _index;
  28. private int _nodeCount;
  29. public void Reset(Memory<int> storage, int nodeCount)
  30. {
  31. Debug.Assert(storage.Length * sizeof(int) >= CalcBufferSize(nodeCount));
  32. _storage = storage;
  33. _index = 0;
  34. _nodeCount = nodeCount;
  35. }
  36. public int GetCurrentCount()
  37. {
  38. return _index;
  39. }
  40. public void Push(int data)
  41. {
  42. Debug.Assert(_index + 1 <= _nodeCount);
  43. _storage.Span[_index++] = data;
  44. }
  45. public int Pop()
  46. {
  47. Debug.Assert(_index > 0);
  48. return _storage.Span[--_index];
  49. }
  50. public int Top()
  51. {
  52. return _storage.Span[_index - 1];
  53. }
  54. public static int CalcBufferSize(int nodeCount)
  55. {
  56. return nodeCount * sizeof(int);
  57. }
  58. }
  59. private int _nodeCount;
  60. private EdgeMatrix _discovered;
  61. private EdgeMatrix _finished;
  62. private Memory<int> _resultArray;
  63. private Stack _stack;
  64. private int _tsortResultIndex;
  65. private enum NodeState : byte
  66. {
  67. Unknown,
  68. Discovered,
  69. Finished
  70. }
  71. public NodeStates()
  72. {
  73. _stack = new Stack();
  74. _discovered = new EdgeMatrix();
  75. _finished = new EdgeMatrix();
  76. }
  77. public static int GetWorkBufferSize(int nodeCount)
  78. {
  79. return Stack.CalcBufferSize(nodeCount * nodeCount) + 0xC * nodeCount + 2 * EdgeMatrix.GetWorkBufferSize(nodeCount);
  80. }
  81. public void Initialize(Memory<byte> nodeStatesWorkBuffer, int nodeCount)
  82. {
  83. int workBufferSize = GetWorkBufferSize(nodeCount);
  84. Debug.Assert(nodeStatesWorkBuffer.Length >= workBufferSize);
  85. _nodeCount = nodeCount;
  86. int edgeMatrixWorkBufferSize = EdgeMatrix.GetWorkBufferSize(nodeCount);
  87. _discovered.Initialize(nodeStatesWorkBuffer.Slice(0, edgeMatrixWorkBufferSize), nodeCount);
  88. _finished.Initialize(nodeStatesWorkBuffer.Slice(edgeMatrixWorkBufferSize, edgeMatrixWorkBufferSize), nodeCount);
  89. nodeStatesWorkBuffer = nodeStatesWorkBuffer.Slice(edgeMatrixWorkBufferSize * 2);
  90. _resultArray = SpanMemoryManager<int>.Cast(nodeStatesWorkBuffer.Slice(0, sizeof(int) * nodeCount));
  91. nodeStatesWorkBuffer = nodeStatesWorkBuffer.Slice(sizeof(int) * nodeCount);
  92. Memory<int> stackWorkBuffer = SpanMemoryManager<int>.Cast(nodeStatesWorkBuffer.Slice(0, Stack.CalcBufferSize(nodeCount * nodeCount)));
  93. _stack.Reset(stackWorkBuffer, nodeCount * nodeCount);
  94. }
  95. private void Reset()
  96. {
  97. _discovered.Reset();
  98. _finished.Reset();
  99. _tsortResultIndex = 0;
  100. _resultArray.Span.Fill(-1);
  101. }
  102. private NodeState GetState(int index)
  103. {
  104. Debug.Assert(index < _nodeCount);
  105. if (_discovered.Test(index))
  106. {
  107. Debug.Assert(!_finished.Test(index));
  108. return NodeState.Discovered;
  109. }
  110. else if (_finished.Test(index))
  111. {
  112. Debug.Assert(!_discovered.Test(index));
  113. return NodeState.Finished;
  114. }
  115. return NodeState.Unknown;
  116. }
  117. private void SetState(int index, NodeState state)
  118. {
  119. switch (state)
  120. {
  121. case NodeState.Unknown:
  122. _discovered.Reset(index);
  123. _finished.Reset(index);
  124. break;
  125. case NodeState.Discovered:
  126. _discovered.Set(index);
  127. _finished.Reset(index);
  128. break;
  129. case NodeState.Finished:
  130. _finished.Set(index);
  131. _discovered.Reset(index);
  132. break;
  133. }
  134. }
  135. private void PushTsortResult(int index)
  136. {
  137. Debug.Assert(index < _nodeCount);
  138. _resultArray.Span[_tsortResultIndex++] = index;
  139. }
  140. public ReadOnlySpan<int> GetTsortResult()
  141. {
  142. return _resultArray.Span.Slice(0, _tsortResultIndex);
  143. }
  144. public bool Sort(EdgeMatrix edgeMatrix)
  145. {
  146. Reset();
  147. if (_nodeCount <= 0)
  148. {
  149. return true;
  150. }
  151. for (int i = 0; i < _nodeCount; i++)
  152. {
  153. if (GetState(i) == NodeState.Unknown)
  154. {
  155. _stack.Push(i);
  156. }
  157. while (_stack.GetCurrentCount() > 0)
  158. {
  159. int topIndex = _stack.Top();
  160. NodeState topState = GetState(topIndex);
  161. if (topState == NodeState.Discovered)
  162. {
  163. SetState(topIndex, NodeState.Finished);
  164. PushTsortResult(topIndex);
  165. _stack.Pop();
  166. }
  167. else if (topState == NodeState.Finished)
  168. {
  169. _stack.Pop();
  170. }
  171. else
  172. {
  173. if (topState == NodeState.Unknown)
  174. {
  175. SetState(topIndex, NodeState.Discovered);
  176. }
  177. for (int j = 0; j < edgeMatrix.GetNodeCount(); j++)
  178. {
  179. if (edgeMatrix.Connected(topIndex, j))
  180. {
  181. NodeState jState = GetState(j);
  182. if (jState == NodeState.Unknown)
  183. {
  184. _stack.Push(j);
  185. }
  186. // Found a loop, reset and propagate rejection.
  187. else if (jState == NodeState.Discovered)
  188. {
  189. Reset();
  190. return false;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. }
  200. }