From 72c7a8ab00e37ee2d5f58e1902229e860a059dcc Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 22 Jan 2017 17:03:59 +0100 Subject: [PATCH] change old DAG implementation to pass unit tests --- src/App/Document.cpp | 72 +++++++++++++++++++-------------------- src/App/DocumentPyImp.cpp | 4 --- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 3080acac9..5eb299da2 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -1965,42 +1965,6 @@ int Document::recompute() #else //ifdef USE_OLD_DAG -std::vector Document::topologicalSort() const -{ - // topological sort algorithm described here: - // https://de.wikipedia.org/wiki/Topologische_Sortierung#Algorithmus_f.C3.BCr_das_Topologische_Sortieren - vector < App::DocumentObject* > ret; - ret.reserve(d->objectArray.size()); - map < App::DocumentObject*,int > countMap; - - for (auto objectIt : d->objectArray) - countMap[objectIt] = objectIt->getInList().size(); - - auto rootObjeIt = find_if(countMap.begin(), countMap.end(), [](pair < App::DocumentObject*, int > count)->bool { - return count.second == 0; - }); - - if (rootObjeIt == countMap.end()){ - cerr << "Document::topologicalSort: cyclic dependency detected (no root object)" << endl; - return ret; - } - - while (rootObjeIt != countMap.end()){ - rootObjeIt->second = rootObjeIt->second - 1; - for (auto outListIt : rootObjeIt->first->getOutList()){ - auto outListMapIt = countMap.find(outListIt); - outListMapIt->second = outListMapIt->second - 1; - } - ret.push_back(rootObjeIt->first); - - rootObjeIt = find_if(countMap.begin(), countMap.end(), [](pair < App::DocumentObject*, int > count)->bool { - return count.second == 0; - }); - } - - return ret; -} - int Document::recompute() { int objectCount = 0; @@ -2047,6 +2011,42 @@ int Document::recompute() #endif // USE_OLD_DAG +std::vector Document::topologicalSort() const +{ + // topological sort algorithm described here: + // https://de.wikipedia.org/wiki/Topologische_Sortierung#Algorithmus_f.C3.BCr_das_Topologische_Sortieren + vector < App::DocumentObject* > ret; + ret.reserve(d->objectArray.size()); + map < App::DocumentObject*,int > countMap; + + for (auto objectIt : d->objectArray) + countMap[objectIt] = objectIt->getInList().size(); + + auto rootObjeIt = find_if(countMap.begin(), countMap.end(), [](pair < App::DocumentObject*, int > count)->bool { + return count.second == 0; + }); + + if (rootObjeIt == countMap.end()){ + cerr << "Document::topologicalSort: cyclic dependency detected (no root object)" << endl; + return ret; + } + + while (rootObjeIt != countMap.end()){ + rootObjeIt->second = rootObjeIt->second - 1; + for (auto outListIt : rootObjeIt->first->getOutList()){ + auto outListMapIt = countMap.find(outListIt); + outListMapIt->second = outListMapIt->second - 1; + } + ret.push_back(rootObjeIt->first); + + rootObjeIt = find_if(countMap.begin(), countMap.end(), [](pair < App::DocumentObject*, int > count)->bool { + return count.second == 0; + }); + } + + return ret; +} + const char * Document::getErrorDescription(const App::DocumentObject*Obj) const { for (std::vector::const_iterator it=_RecomputeLog.begin();it!=_RecomputeLog.end();++it) diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index 79025bca1..185a764eb 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -492,7 +492,6 @@ Py::List DocumentPy::getObjects(void) const Py::List DocumentPy::getToplogicalSortedObjects(void) const { -#ifndef USE_OLD_DAG std::vector objs = getDocumentPtr()->topologicalSort(); Py::List res; @@ -501,9 +500,6 @@ Py::List DocumentPy::getToplogicalSortedObjects(void) const res.append(Py::Object((*It)->getPyObject(), true)); return res; -#else - return Py::List(); -#endif } Py::List DocumentPy::getRootObjects(void) const