Skip to content

Commit

Permalink
Changes component hashing method
Browse files Browse the repository at this point in the history
The first method was just using bitwise XOR of the string hashes of the
components' type IDs. This could be prone to collisions.
Instead, this method sorts, concatenates, and then hashes the IDs.
  • Loading branch information
Davi-DeGanne committed Dec 8, 2019
1 parent 4d6a5da commit ef00aa2
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8069,19 +8069,19 @@ std::string item::components_to_string() const

uint64_t item::make_component_hash() const
{
// First we need to ensure all the IDs are unique, to ensure we don't XOR identical hashes later.
std::set<std::string> id_set;
// First we need to sort the IDs so that identical ingredients give identical hashes.
std::multiset<std::string> id_set;
for( const item &it : components ) {
id_set.insert( it.typeId() );
}

uint64_t component_hash = 0;
std::hash<std::string> hasher;
std::string concatenated_ids;
for( std::string id : id_set ) {
component_hash ^= hasher( id );
concatenated_ids += id;
}

return component_hash;
std::hash<std::string> hasher;
return hasher( concatenated_ids );
}

bool item::needs_processing() const
Expand Down

0 comments on commit ef00aa2

Please sign in to comment.