From 67f25b93b847b43a62a4148e626481c3f6d0cd9b Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Sun, 15 Feb 2015 20:16:04 +0100 Subject: added getChildrenWithSubclasses and thus made pathTo and collect more compact. --- src/core/model/Domain.cpp | 68 +++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 44 deletions(-) (limited to 'src/core/model/Domain.cpp') diff --git a/src/core/model/Domain.cpp b/src/core/model/Domain.cpp index 698d49d..ae20068 100644 --- a/src/core/model/Domain.cpp +++ b/src/core/model/Domain.cpp @@ -81,7 +81,7 @@ static NodeVector pathTo(const Node *start, Logger &logger, const FieldDescriptor *field = static_cast(start); // initially put every child and its subclasses to the queue. - for (auto c : field->getChildren()) { + for (auto c : field->getChildrenWithSubclasses()) { // if we have found the target directly, return without search. if (c == target) { success = true; @@ -90,16 +90,6 @@ static NodeVector pathTo(const Node *start, Logger &logger, if (c->isTransparent()) { states.push(std::make_shared(nullptr, c.get())); } - for (auto sub : c->getSubclasses()) { - if (sub == target) { - success = true; - return shortest; - } - if (sub->isTransparent()) { - states.push( - std::make_shared(nullptr, sub.get())); - } - } } } // set of visited nodes. @@ -140,7 +130,7 @@ static NodeVector pathTo(const Node *start, Logger &logger, const FieldDescriptor *field = static_cast(current->node); // and we proceed by visiting all permitted children. - for (auto c : field->getChildren()) { + for (auto c : field->getChildrenWithSubclasses()) { // if we found our target, break off the search in this branch. if (c == target) { fin = true; @@ -150,17 +140,6 @@ static NodeVector pathTo(const Node *start, Logger &logger, if (c->isTransparent()) { states.push(std::make_shared(current, c.get())); } - // ... or their transparent subclasses. - for (auto sub : c->getSubclasses()) { - if (sub == target) { - fin = true; - continue; - } - if (sub->isTransparent()) { - states.push( - std::make_shared(current, sub.get())); - } - } } } // check if we are finished. @@ -224,29 +203,11 @@ static NodeVector collect(const Node *start, F match) q.push(fd); } } - - /* - * Furthermore we have to consider that all subclasses of this - * StructuredClass are allowed in place of this StructuredClass as - * well, so we continue the search for them as well. - */ - - NodeVector subs = strct->getSubclasses(); - for (auto sub : subs) { - // note matches. - if (match(sub)) { - res.push_back(sub); - } - // We only continue our search via transparent classes. - if (sub->isTransparent()) { - q.push(sub); - } - } } else { // otherwise this is a FieldDescriptor. Rooted field = n.cast(); // and we proceed by visiting all permitted children. - for (auto c : field->getChildren()) { + for (auto c : field->getChildrenWithSubclasses()) { // note matches. if (match(c)) { res.push_back(c); @@ -350,7 +311,7 @@ bool FieldDescriptor::doValidate(Logger &logger) const * there are duplicates. */ std::set names; - for (Handle c : children) { + for (Handle c : getChildrenWithSubclasses()) { if (!names.insert(c->getName()).second) { logger.error(std::string("Field \"") + getName() + "\" had multiple children with the name \"" + @@ -363,6 +324,25 @@ bool FieldDescriptor::doValidate(Logger &logger) const return valid; } +static void gatherSubclasses(NodeVector &res, + Handle strct) +{ + for (auto sub : strct->getSubclasses()) { + res.push_back(sub); + gatherSubclasses(res, sub); + } +} + +NodeVector FieldDescriptor::getChildrenWithSubclasses() const +{ + NodeVector res; + for (auto c : children) { + res.push_back(c); + gatherSubclasses(res, c); + } + return res; +} + bool FieldDescriptor::removeChild(Handle c) { auto it = children.find(c); @@ -642,7 +622,7 @@ void Descriptor::copyFieldDescriptor(Handle fd, Logger &logger) copy = Rooted{ new FieldDescriptor(getManager(), this, fd->getFieldType(), fd->getName(), fd->isOptional())}; - for (auto &c : fd->getChildren()) { + for (auto c : fd->getChildren()) { copy->addChild(c); } } -- cgit v1.2.3