ctkDependencyGraph.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkDependencyGraph_h
  15. #define __ctkDependencyGraph_h
  16. // Qt includes
  17. #include <QScopedPointer>
  18. #include <QString>
  19. #include <QList>
  20. #include <QtGlobal>
  21. // CTK includes
  22. #if !defined(NO_SYMBOL_EXPORT)
  23. #include "ctkCoreExport.h"
  24. #else
  25. #define CTK_CORE_EXPORT
  26. #endif
  27. class ctkDependencyGraphPrivate;
  28. /// \ingroup Core
  29. class CTK_CORE_EXPORT ctkDependencyGraph
  30. {
  31. public:
  32. ctkDependencyGraph(int nvertices);
  33. virtual ~ctkDependencyGraph();
  34. void printAdditionalInfo()const;
  35. void printGraph()const;
  36. /// Get the number of vertices associated with current graph
  37. int numberOfVertices()const;
  38. /// Get the number of edges associated with current graph
  39. int numberOfEdges()const;
  40. /// Traverse graph and check for cycle
  41. bool checkForCycle();
  42. /// Return true if there is at least one cycle
  43. bool cycleDetected()const;
  44. /// If a cycle has been detected, return the origin of the cycle otherwise 0.
  45. int cycleOrigin()const;
  46. /// If a cycle has been detected, return the end of the cycle otherwise 0.
  47. int cycleEnd()const;
  48. // The traverse of the tree will print information on standard output
  49. void setVerbose(bool verbose);
  50. /// Insert edge
  51. /// (from, to) indicate a relation between two vertices
  52. /// Note also that vertex id should be >= 1
  53. void insertEdge(int from, int to);
  54. /// Retrieve the paths between two vertices
  55. /// Caller is responsible to clear paths list
  56. void findPaths(int from, int to, QList<QList<int>* >& paths);
  57. /// Retrieve the path between two vertices
  58. void findPath(int from, int to, QList<int>& path);
  59. /// List of edge to exclude
  60. /// An edge is specified using its extremity
  61. void setEdgeListToExclude(const QList<int>& list);
  62. /// The default implementation check if 'edge' is in the list of edge to exclude
  63. /// See setEdgeListToExclude
  64. virtual bool shouldExcludeEdge(int edge)const;
  65. /// Called each time an edge is visited
  66. virtual void processEdge(int /*from*/, int /*to*/){}
  67. /// Perform a topological sort
  68. /// Return false if the graph contains cycles
  69. /// If a rootId is given, the subgraph starting at the root id is sorted
  70. /// See cycleDetected, cycleOrigin, cycleEnd
  71. bool topologicalSort(QList<int>& sorted, int rootId = -1);
  72. /// Retrieve all vertices with indegree 0
  73. void sourceVertices(QList<int>& sources);
  74. protected:
  75. QScopedPointer<ctkDependencyGraphPrivate> d_ptr;
  76. private:
  77. Q_DECLARE_PRIVATE(ctkDependencyGraph);
  78. Q_DISABLE_COPY(ctkDependencyGraph);
  79. };
  80. #endif