Add operator<< to GroupTree class + whitespace
This commit is contained in:
parent
bf6964522e
commit
894ad7a226
@ -27,39 +27,46 @@
|
||||
namespace Opm {
|
||||
|
||||
class GroupTree {
|
||||
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;
|
||||
const std::map<std::string , size_t>& nameSeqIndMap() const;
|
||||
const std::map<size_t, std::string >& seqIndNameMap() const;
|
||||
bool operator==( const GroupTree& ) const;
|
||||
bool operator!=( const GroupTree& ) const;
|
||||
public:
|
||||
struct group {
|
||||
std::string name;
|
||||
std::string parent;
|
||||
|
||||
private:
|
||||
struct group {
|
||||
std::string name;
|
||||
std::string parent;
|
||||
bool operator<( const group& rhs ) const;
|
||||
bool operator==( const std::string& name ) const;
|
||||
bool operator!=( const std::string& name ) const;
|
||||
|
||||
bool operator<( const group& rhs ) const;
|
||||
bool operator==( const std::string& name ) const;
|
||||
bool operator!=( const std::string& name ) const;
|
||||
bool operator<( const std::string& name ) const;
|
||||
bool operator==( const group& rhs ) const;
|
||||
bool operator!=( const group& rhs ) const;
|
||||
};
|
||||
|
||||
bool operator<( const std::string& name ) const;
|
||||
bool operator==( const group& rhs ) const;
|
||||
bool operator!=( const group& rhs ) const;
|
||||
};
|
||||
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;
|
||||
const std::map<std::string , size_t>& nameSeqIndMap() const;
|
||||
const std::map<size_t, std::string >& seqIndNameMap() const;
|
||||
bool operator==( const GroupTree& ) const;
|
||||
bool operator!=( const GroupTree& ) const;
|
||||
std::vector<GroupTree::group>::const_iterator begin() const;
|
||||
std::vector<GroupTree::group>::const_iterator end() const;
|
||||
|
||||
std::vector< group > groups = { group { "FIELD", "" } };
|
||||
friend bool operator<( const std::string&, const group& );
|
||||
std::vector< group >::iterator find( const std::string& );
|
||||
std::map<std::string , size_t> m_nameSeqIndMap;
|
||||
std::map<size_t, std::string > m_seqIndNameMap;
|
||||
private:
|
||||
std::vector< group >::iterator find( const std::string& );
|
||||
friend bool operator<( const std::string&, const group& );
|
||||
|
||||
std::vector< group > groups = { group { "FIELD", "" } };
|
||||
std::map<std::string , size_t> m_nameSeqIndMap;
|
||||
std::map<size_t, std::string > m_seqIndNameMap;
|
||||
};
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream& stream, const GroupTree& gt);
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif /* GROUPTREE_HPP */
|
||||
|
@ -36,13 +36,13 @@ void GroupTree::update( const std::string& name) {
|
||||
* this grouptree class is just meta data for the actual group objects (stored
|
||||
* and represented elsewhere)
|
||||
*/
|
||||
|
||||
|
||||
const std::map<std::string , size_t>& GroupTree::nameSeqIndMap() const {
|
||||
return m_nameSeqIndMap;
|
||||
return m_nameSeqIndMap;
|
||||
}
|
||||
|
||||
const std::map<size_t, std::string >& GroupTree::seqIndNameMap() const {
|
||||
return m_seqIndNameMap;
|
||||
return m_seqIndNameMap;
|
||||
}
|
||||
|
||||
void GroupTree::update( const std::string& name, const std::string& other_parent) {
|
||||
@ -53,7 +53,7 @@ void GroupTree::update( const std::string& name, const std::string& other_parent
|
||||
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 )
|
||||
if( root == this->groups.end() || root->name != other_parent )
|
||||
this->groups.insert( root, 1, group { other_parent, "FIELD" } );
|
||||
|
||||
auto node = this->find( name );
|
||||
@ -77,14 +77,14 @@ void GroupTree::updateSeqIndex( const std::string& name, const std::string& othe
|
||||
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;
|
||||
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));
|
||||
this->m_nameSeqIndMap.insert(std::make_pair(other_parent, index));
|
||||
this->m_seqIndNameMap.insert(std::make_pair(index, other_parent));
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ const std::string& GroupTree::parent( const std::string& name ) const {
|
||||
auto node = std::find( this->groups.begin(), this->groups.end(), name );
|
||||
|
||||
if( node == this->groups.end() )
|
||||
throw std::out_of_range( "No such parent '" + name + "'." );
|
||||
throw std::out_of_range( "No such group: '" + name + "'." );
|
||||
|
||||
return node->parent;
|
||||
}
|
||||
@ -109,11 +109,6 @@ std::vector< std::string > GroupTree::children( const std::string& other_parent
|
||||
throw std::out_of_range( "Node '" + other_parent + "' does not exist." );
|
||||
|
||||
std::vector< std::string > kids;
|
||||
/* for( const auto& node : this->groups ) {
|
||||
if( node.parent != other_parent ) continue;
|
||||
kids.push_back( node.name );
|
||||
}
|
||||
*/
|
||||
for( auto it = this->groups.begin(); it != this->groups.end(); it++ ) {
|
||||
if( (*it).parent != other_parent ) continue;
|
||||
kids.push_back( (*it).name );
|
||||
@ -167,4 +162,20 @@ std::vector< GroupTree::group >::iterator GroupTree::find( const std::string& na
|
||||
name );
|
||||
}
|
||||
|
||||
std::vector<GroupTree::group>::const_iterator GroupTree::begin() const {
|
||||
return this->groups.begin();
|
||||
}
|
||||
|
||||
std::vector<GroupTree::group>::const_iterator GroupTree::end() const {
|
||||
return this->groups.end();
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& stream, const GroupTree& gt) {
|
||||
for (const auto& group_pair : gt)
|
||||
stream << group_pair.parent << " -> " << group_pair.name << std::endl;
|
||||
|
||||
stream << std::endl << std::endl;
|
||||
return stream;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user