Skip to content

Commit

Permalink
Sort requirement data before comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrox committed Feb 1, 2020
1 parent 3f4c657 commit 9b9ce3a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
20 changes: 19 additions & 1 deletion src/requirements.cpp
Expand Up @@ -1176,9 +1176,27 @@ void requirement_data::consolidate()
components = std::move( all_comps );
}

template<typename T>
static bool sorted_equal( std::vector<std::vector<T>> lhs, std::vector<std::vector<T>> rhs )
{
if( lhs.size() != rhs.size() ) {
return false;
}
for( auto &inner : lhs ) {
std::sort( inner.begin(), inner.end() );
}
for( auto &inner : rhs ) {
std::sort( inner.begin(), inner.end() );
}
std::sort( lhs.begin(), lhs.end() );
std::sort( rhs.begin(), rhs.end() );
return lhs == rhs;
}

bool requirement_data::has_same_requirements_as( const requirement_data &that ) const
{
return tools == that.tools && qualities == that.qualities && components == that.components;
return sorted_equal( tools, that.tools ) && sorted_equal( qualities, that.qualities )
&& sorted_equal( components, that.components );
}

template<typename T>
Expand Down
21 changes: 15 additions & 6 deletions src/requirements.h
Expand Up @@ -64,12 +64,17 @@ struct component {
// needs explicit specification due to the mutable member. update this when you add new
// members!
bool operator==( const component &rhs ) const {
return type == rhs.type && count == rhs.count && recoverable == rhs.recoverable
&& requirement == rhs.requirement;
return std::forward_as_tuple( type, requirement, count, recoverable )
== std::forward_as_tuple( rhs.type, rhs.requirement, rhs.count, rhs.recoverable );
}
bool operator!=( const component &rhs ) const {
return !operator==( rhs );
}
// lexicographic comparison
bool operator<( const component &rhs ) const {
return std::forward_as_tuple( type, requirement, count, recoverable )
< std::forward_as_tuple( rhs.type, rhs.requirement, rhs.count, rhs.recoverable );
}

component() = default;
component( const itype_id &TYPE, int COUNT ) : type( TYPE ), count( COUNT ) { }
Expand Down Expand Up @@ -123,12 +128,17 @@ struct quality_requirement {
// needs explicit specification due to the mutable member. update this when you add new
// members!
bool operator==( const quality_requirement &rhs ) const {
return type == rhs.type && count == rhs.count && level == rhs.level
&& requirement == rhs.requirement;
return std::forward_as_tuple( type, requirement, count, level )
== std::forward_as_tuple( rhs.type, rhs.requirement, rhs.count, rhs.level );
}
bool operator!=( const quality_requirement &rhs ) const {
return !operator==( rhs );
}
// lexicographic comparison
bool operator<( const quality_requirement &rhs ) const {
return std::forward_as_tuple( type, requirement, count, level )
< std::forward_as_tuple( rhs.type, rhs.requirement, rhs.count, rhs.level );
}

quality_requirement() = default;
quality_requirement( const quality_id &TYPE, int COUNT, int LEVEL ) : type( TYPE ), count( COUNT ),
Expand Down Expand Up @@ -335,8 +345,7 @@ struct requirement_data {

/**
* Compares if two requiremen_data are the same, but does not compare the requirement ids.
*
* TODO: sort and compare
* The order inside requirement vectors does not matter.
*/
bool has_same_requirements_as( const requirement_data &that ) const;

Expand Down

0 comments on commit 9b9ce3a

Please sign in to comment.