Merge pull request #118 from flikka/getnodes-grouptree
Added getNodes to get a vector of the group nodes in the tree
This commit is contained in:
@@ -75,6 +75,23 @@ namespace Opm {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<GroupTreeNodeConstPtr> GroupTree::getNodes() const {
|
||||
std::vector<GroupTreeNodeConstPtr> nodes;
|
||||
nodes.push_back(m_root);
|
||||
getNodes(m_root, nodes);
|
||||
return nodes;
|
||||
}
|
||||
|
||||
void GroupTree::getNodes(GroupTreeNodePtr fromNode, std::vector<GroupTreeNodeConstPtr>& nodes) const {
|
||||
std::map<std::string, GroupTreeNodePtr >::iterator iter = fromNode->begin();
|
||||
while (iter != fromNode->end()) {
|
||||
GroupTreeNodePtr child = (*iter).second;
|
||||
nodes.push_back(child);
|
||||
getNodes(child, nodes);
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
GroupTreeNodePtr GroupTree::getParent(const std::string& childName) const {
|
||||
GroupTreeNodePtr currentChild = m_root;
|
||||
return getParent(childName, currentChild, GroupTreeNodePtr());
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace Opm {
|
||||
|
||||
@@ -35,6 +36,7 @@ namespace Opm {
|
||||
void updateTree(const std::string& childName, const std::string& parentName);
|
||||
|
||||
GroupTreeNodePtr getNode(const std::string& nodeName) const;
|
||||
std::vector<GroupTreeNodeConstPtr> getNodes() const;
|
||||
GroupTreeNodePtr getParent(const std::string& childName) const;
|
||||
|
||||
std::shared_ptr<GroupTree> deepCopy() const;
|
||||
@@ -46,6 +48,7 @@ namespace Opm {
|
||||
GroupTreeNodePtr getNode(const std::string& nodeName, GroupTreeNodePtr current) const;
|
||||
GroupTreeNodePtr getParent(const std::string& childName, GroupTreeNodePtr currentChild, GroupTreeNodePtr parent) const;
|
||||
|
||||
void getNodes(GroupTreeNodePtr fromNode, std::vector<GroupTreeNodeConstPtr>& nodes) const;
|
||||
void deepCopy(GroupTreeNodePtr origin, GroupTreeNodePtr copy) const;
|
||||
void printTree(GroupTreeNodePtr fromNode) const;
|
||||
};
|
||||
|
||||
@@ -149,3 +149,21 @@ BOOST_AUTO_TEST_CASE(DeepCopy_TreeWithChildren_ObjectsDifferContentMatch) {
|
||||
BOOST_CHECK(!(L3CHILD1NodeCopy == L3CHILD1NodeOriginal));
|
||||
BOOST_CHECK_EQUAL(L3CHILD1NodeCopy->name(), L3CHILD1NodeOriginal->name());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(GetNodes_ReturnsAllNodes) {
|
||||
GroupTreePtr tree(new GroupTree());
|
||||
tree->updateTree("L1CHILD1", "FIELD");
|
||||
tree->updateTree("L1CHILD2", "FIELD");
|
||||
tree->updateTree("L2CHILD1", "L1CHILD1");
|
||||
tree->updateTree("L2CHILD2", "L1CHILD1");
|
||||
tree->updateTree("L3CHILD1", "L2CHILD1");
|
||||
|
||||
std::vector<GroupTreeNodeConstPtr> nodes = tree->getNodes();
|
||||
BOOST_CHECK_EQUAL(6U, nodes.size());
|
||||
BOOST_CHECK_EQUAL("FIELD", nodes[0U]->name());
|
||||
BOOST_CHECK_EQUAL("L1CHILD1", nodes[1U]->name());
|
||||
BOOST_CHECK_EQUAL("L2CHILD1", nodes[2U]->name());
|
||||
BOOST_CHECK_EQUAL("L3CHILD1", nodes[3U]->name());
|
||||
BOOST_CHECK_EQUAL("L2CHILD2", nodes[4U]->name());
|
||||
BOOST_CHECK_EQUAL("L1CHILD2", nodes[5U]->name());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user