From 7adc3478bddbde6f628c7737c267a851a55f20ca Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Fri, 30 Oct 2015 14:10:32 +0100 Subject: [PATCH] useful static method to find lowest common ancestor given absolute names. --- src/engine/ComposedNode.cxx | 25 +++++++++++++++++++++++++ src/engine/ComposedNode.hxx | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/engine/ComposedNode.cxx b/src/engine/ComposedNode.cxx index 69870fa47..7b846bd57 100644 --- a/src/engine/ComposedNode.cxx +++ b/src/engine/ComposedNode.cxx @@ -1075,6 +1075,31 @@ ComposedNode *ComposedNode::getLowestCommonAncestor(Node *node1, Node *node2) th return *iter; } +/*! + * Same as getLowestCommonAncestor method except that absolute string representation is considered here instead of instances. + */ +std::string ComposedNode::getLowestCommonAncestorStr(const std::string& node1, const std::string& node2) +{ + std::string ret; + std::size_t it1_b(0),it1_e(0),it2_b(0),it2_e(0); + while(it1_b!=std::string::npos && it2_b!=std::string::npos) + { + it1_e=node1.find(SEP_CHAR_BTW_LEVEL,it1_b); + it2_e=node2.find(SEP_CHAR_BTW_LEVEL,it2_b); + if(it1_e!=it2_e && it1_e!=std::string::npos && it2_e!=std::string::npos) + break; + std::string elt1(node1.substr(it1_b,it1_e-it1_b)),elt2(node2.substr(it2_b,it2_e-it2_b)); + if(elt1!=elt2) + break; + if(!ret.empty()) + ret+=SEP_CHAR_BTW_LEVEL; + ret+=elt1; + it1_b=node1.find_first_not_of(SEP_CHAR_BTW_LEVEL,it1_e); + it2_b=node2.find_first_not_of(SEP_CHAR_BTW_LEVEL,it2_e); + } + return ret; +} + list ComposedNode::getRecursiveConstituents() const { list ret; diff --git a/src/engine/ComposedNode.hxx b/src/engine/ComposedNode.hxx index aadd2804c..47481334f 100644 --- a/src/engine/ComposedNode.hxx +++ b/src/engine/ComposedNode.hxx @@ -116,11 +116,12 @@ namespace YACS virtual std::string getMyQualifiedName(const Node *directSon) const; Node *getChildByName(const std::string& name) const throw(Exception); static ComposedNode *getLowestCommonAncestor(Node *node1, Node *node2) throw(Exception); + static std::string getLowestCommonAncestorStr(const std::string& node1, const std::string& node2); void loaded(); void connected(); void accept(Visitor *visitor); virtual void cleanNodes(); - virtual std::string getProgress() const {return "0";}; + virtual std::string getProgress() const { return "0"; } protected: struct SortHierarc { -- 2.30.2