Skip to content

Commit

Permalink
change old DAG implementation to pass unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jan 22, 2017
1 parent 6105ea7 commit 72c7a8a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
72 changes: 36 additions & 36 deletions src/App/Document.cpp
Expand Up @@ -1965,42 +1965,6 @@ int Document::recompute()

#else //ifdef USE_OLD_DAG

std::vector<App::DocumentObject*> 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;
Expand Down Expand Up @@ -2047,6 +2011,42 @@ int Document::recompute()

#endif // USE_OLD_DAG

std::vector<App::DocumentObject*> 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<App::DocumentObjectExecReturn*>::const_iterator it=_RecomputeLog.begin();it!=_RecomputeLog.end();++it)
Expand Down
4 changes: 0 additions & 4 deletions src/App/DocumentPyImp.cpp
Expand Up @@ -492,7 +492,6 @@ Py::List DocumentPy::getObjects(void) const

Py::List DocumentPy::getToplogicalSortedObjects(void) const
{
#ifndef USE_OLD_DAG
std::vector<DocumentObject*> objs = getDocumentPtr()->topologicalSort();
Py::List res;

Expand All @@ -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
Expand Down

0 comments on commit 72c7a8a

Please sign in to comment.