You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.7KB

  1. /* Graph sort
  2. * Copyleft (C) 2002 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 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 General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <vector>
  19. #include <string>
  20. #include <list>
  21. #include <algorithm>
  22. #include <map>
  23. #ifndef GRAPH_SORT
  24. #define GRAPH_SORT
  25. #define GRAPHSORT_USE_TEST_SORT_BY_DEFAULT
  26. using namespace std;
  27. class GraphSort
  28. {
  29. public:
  30. #ifdef GRAPHSORT_USE_TEST_SORT_BY_DEFAULT
  31. GraphSort(bool UseTestSort=true);
  32. #else
  33. GraphSort(bool UseTestSort=false);
  34. #endif
  35. ~GraphSort();
  36. const list<int> &GetSortedList();
  37. void Sort();
  38. void TestSort();
  39. void OrigSort();
  40. void AddConnection(int SID, bool STerminal, int DID, bool DTerminal);
  41. void RemoveConnection(int SID, int DID);
  42. void Clear();
  43. void Dump();
  44. struct Node
  45. {
  46. list<int> Inputs;
  47. list<int> Outputs;
  48. bool IsTerminal;
  49. // temporaries used during sort
  50. int UnsatisfiedOutputs;
  51. bool IsCandidate;
  52. bool IsSorted;
  53. };
  54. private:
  55. void RecursiveWalk(int node);
  56. bool m_UseTestSort;
  57. map<int,Node> m_Graph;
  58. list<int> m_Sorted;
  59. };
  60. #endif