From 7daed2f8431e89e2bd041a54bc1bef8c45329092 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Wed, 11 Feb 2015 17:55:04 +0100 Subject: improved pathto --- src/core/model/Domain.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'src/core/model/Domain.cpp') diff --git a/src/core/model/Domain.cpp b/src/core/model/Domain.cpp index 3fb525f..228348d 100644 --- a/src/core/model/Domain.cpp +++ b/src/core/model/Domain.cpp @@ -207,16 +207,15 @@ bool Descriptor::doValidate(Logger &logger) const return valid & continueValidationCheckDuplicates(fds, logger); } -std::vector> Descriptor::pathTo( - Handle target) const +NodeVector Descriptor::pathTo(Handle target) const { - std::vector> path; - continuePath(target, path); - return path; + NodeVector path; + continuePath(target, path, true); + return std::move(path); } bool Descriptor::continuePath(Handle target, - std::vector> ¤tPath) const + NodeVector ¤tPath, bool start) const { // check if we are at the target already if (this == target) { @@ -225,27 +224,34 @@ bool Descriptor::continuePath(Handle target, // a variable to determine if we already found a solution bool found = false; // the currently optimal path. - std::vector> optimum; + NodeVector optimum; // use recursive depth-first search from the top to reach the given child // get the list of effective FieldDescriptors. NodeVector fields = getFieldDescriptors(); + Rooted thisHandle{const_cast(this)}; + for (auto &fd : fields) { for (auto &c : fd->getChildren()) { // check if a child is the target node. if (c == target) { // if we have made the connection, stop the search. + if (!start) { + currentPath.push_back(thisHandle); + } currentPath.push_back(fd); return true; } // look for transparent intermediate nodes. if (c->isTransparent()) { // copy the path. - std::vector> cPath = currentPath; + NodeVector cPath = currentPath; + if (!start) { + cPath.push_back(thisHandle); + } cPath.push_back(fd); - cPath.push_back(c); // recursion. - if (c->continuePath(target, cPath) && + if (c->continuePath(target, cPath, false) && (!found || optimum.size() > cPath.size())) { // look if this path is better than the current optimum. optimum = std::move(cPath); @@ -260,8 +266,8 @@ bool Descriptor::continuePath(Handle target, // if this is a StructuredClass we also can call the subclasses. for (auto &c : tis->getSubclasses()) { // copy the path. - std::vector> cPath = currentPath; - if (c->continuePath(target, cPath) && + NodeVector cPath = currentPath; + if (c->continuePath(target, cPath, false) && (!found || optimum.size() > cPath.size())) { // look if this path is better than the current optimum. optimum = std::move(cPath); -- cgit v1.2.3