Refactor sequende index mapping in GroupTree

This commit is contained in:
Joakim Hove
2019-07-25 16:27:46 +02:00
parent 8873eb29e4
commit 787d9d3948
3 changed files with 26 additions and 29 deletions

View File

@@ -43,7 +43,6 @@ public:
void update( const std::string& name);
void update( const std::string& name, const std::string& parent);
void updateSeqIndex( const std::string& name, const std::string& other_parent);
bool exists( const std::string& group ) const;
const std::string& parent( const std::string& name ) const;
std::vector< std::string > children( const std::string& parent ) const;
@@ -57,8 +56,18 @@ private:
friend bool operator<( const std::string&, const group& );
std::vector<GroupTree::group>::const_iterator begin() const;
std::vector<GroupTree::group>::const_iterator end() const;
void updateSeqIndex( const std::string& name );
std::vector< group > groups = { group { "FIELD", "" } };
/*
These two maps maintain an insert order <-> name mapping for the groups in
the group tree. Observe that these maps are only updated if the model has
a non-trivial group structure; i.e. it contains the GRUPTREE keyword. In
the simple case of FIELD : GROUP : WELL these maps will be empty, for
models with two group levels like: FIELD : G1 : G2 : WELL the maps will
index both groups 'G1' and 'G2' but not 'FIELD'.
*/
std::map<std::string , size_t> m_nameSeqIndMap;
std::map<size_t, std::string > m_seqIndNameMap;
};

View File

@@ -45,46 +45,35 @@ const std::map<size_t, std::string >& GroupTree::seqIndNameMap() const {
return m_seqIndNameMap;
}
void GroupTree::update( const std::string& name, const std::string& other_parent) {
void GroupTree::update( const std::string& name, const std::string& parent) {
if( name == "FIELD" )
throw std::invalid_argument( "The FIELD group name is reserved." );
if( other_parent.empty() )
if( parent.empty() )
throw std::invalid_argument( "Parent group must have a name." );
auto root = this->find( other_parent );
if( root == this->groups.end() || root->name != other_parent )
this->groups.insert( root, 1, group { other_parent, "FIELD" } );
auto root = this->find( parent );
if( root == this->groups.end() || root->name != parent )
this->groups.insert( root, 1, group { parent, "FIELD" } );
auto node = this->find( name );
if( node == this->groups.end() || node->name != name ) {
this->groups.insert( node, 1, group { name, other_parent } );
return;
}
if( node == this->groups.end() || node->name != name )
this->groups.insert( node, 1, group { name, parent } );
else
node->parent = parent;
node->parent = other_parent;
if (parent != "FIELD") {
this->updateSeqIndex(name);
this->updateSeqIndex(parent);
}
}
void GroupTree::updateSeqIndex( const std::string& name, const std::string& other_parent) {
if( name == "FIELD" )
throw std::invalid_argument( "The FIELD group name is reserved." );
void GroupTree::updateSeqIndex( const std::string& name ) {
if (this->m_nameSeqIndMap.count(name) == 0) {
size_t index = this->m_nameSeqIndMap.size();
if( other_parent.empty() )
throw std::invalid_argument( "Parent group must have a name." );
// add code to set an index that determine the sequence of the groups
// defined in the group tree
size_t index = this->m_nameSeqIndMap.size();
auto name_itr = this->m_nameSeqIndMap.find(name);
if (name_itr == this->m_nameSeqIndMap.end()) {
this->m_nameSeqIndMap.insert(std::make_pair(name, index));
this->m_seqIndNameMap.insert(std::make_pair(index, name));
index +=1;
}
auto parent_itr = this->m_nameSeqIndMap.find(other_parent);
if (parent_itr == this->m_nameSeqIndMap.end()) {
this->m_nameSeqIndMap.insert(std::make_pair(other_parent, index));
this->m_seqIndNameMap.insert(std::make_pair(index, other_parent));
}
}

View File

@@ -1760,7 +1760,6 @@ namespace {
const std::string& childName = record.getItem("CHILD_GROUP").getTrimmedString(0);
const std::string& parentName = record.getItem("PARENT_GROUP").getTrimmedString(0);
newTree.update(childName, parentName);
newTree.updateSeqIndex(childName, parentName);
if (!hasGroup(parentName))
addGroup( parentName , currentStep );