瀏覽代碼

ENH: DGraph: Restored old behavior for -paths argument. Added new
command line argument -sort, which outputs a list of topolically sorted
dependencies for a given label.

Sascha Zelzer 15 年之前
父節點
當前提交
1f59488b24
共有 1 個文件被更改,包括 52 次插入8 次删除
  1. 52 8
      Utilities/DGraph/DGraph.cpp

+ 52 - 8
Utilities/DGraph/DGraph.cpp

@@ -81,6 +81,7 @@ int main(int argc, char** argv)
     }
 
   bool outputPath = false;
+  bool outputSort = false;
   QString label;
   if (argc == 3)
     {
@@ -89,14 +90,23 @@ int main(int argc, char** argv)
     }
   if (argc == 4)
     {
-    if (QString(argv[2]).compare("-paths")!=0)
+    QString arg2 = QString::fromLatin1(argv[2]);
+    if (arg2.compare("-paths")!=0 && arg2.compare("-sort")!=0)
       {
-      displayError(argv[0], QString("Wrong argument: %1").arg(argv[2]));
+      displayError(argv[0], QString("Wrong argument: %1").arg(arg2));
       return EXIT_FAILURE;
       }
     label = QLatin1String(argv[3]);
     outputTopologicalOrder = false;
-    outputPath = true;
+    if (arg2.compare("-paths") == 0)
+      {
+      outputPath = true;
+      }
+    else
+      {
+      outputSort = true;
+      }
+
     if (verbose)
       {
       qDebug() << "label:" << label; 
@@ -207,7 +217,7 @@ int main(int argc, char** argv)
   if (mygraph.cycleDetected())
     {
     std::cerr << "Cycle detected !" << std::endl;
-    QList<int> path; 
+    QList<int> path;
     mygraph.findPath(mygraph.cycleOrigin(), mygraph.cycleEnd(), path);
     
     for(int i = 0; i < path.size(); ++i)
@@ -268,16 +278,50 @@ int main(int argc, char** argv)
     {
     // TODO Make sure label is valid
     QList<int> out;
+    if (mygraph.topologicalSort(out))
+      {
+      for(int i=0; i < out.size(); i++)
+        {
+        // Assume all targets depend on the first lib
+        // We could get all sinks and find all paths
+        // from the rootId to the sink vertices.
+        int rootId = out.last();
+        int labelId = vertexLabelToId[label];
+        QList<QList<int>*> paths;
+        mygraph.findPaths(labelId, rootId, paths);
+        for(int i=0; i < paths.size(); i++)
+          {
+          QList<int>* p = paths[i];
+          Q_ASSERT(p);
+          for(int j=0; j < p->size(); j++)
+            {
+            int id = p->at(j);
+            std::cout << vertexIdToLabel[id].toStdString();
+            if (j != p->size() - 1)
+              {
+              std::cout << " ";
+              }
+            }
+          if (i != paths.size() - 1)
+            {
+            std::cout << ";";
+            }
+          }
+        }
+      }
+    }
+
+  if (outputSort)
+    {
+    // TODO Make sure label is valid
+    QList<int> out;
     int labelId = vertexLabelToId[label];
     if (mygraph.topologicalSort(out, labelId))
       {
       for(int i=0; i < out.size(); i++)
         {
         int id = out.at(i);
-        if (vertexIdToLabel[id].isEmpty())
-          std::cout << "<" << id << ">";
-        else
-          std::cout << vertexIdToLabel[id].toStdString();
+        std::cout << vertexIdToLabel[id].toStdString();
 
         if (i != out.size() - 1)
           {