Skip to content

Commit

Permalink
Include the accessor in the SourcePath hash
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jul 15, 2024
1 parent cdc23b6 commit 550b347
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/libutil/source-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "canon-path.hh"
#include "source-accessor.hh"

#include <boost/functional/hash.hpp> // for boost::hash_combine

namespace nix {

/**
Expand Down Expand Up @@ -128,6 +130,8 @@ struct std::hash<nix::SourcePath>
{
std::size_t operator()(const nix::SourcePath & s) const noexcept
{
return std::hash<decltype(s.path)>{}(s.path);
std::size_t hash = 0;
hash_combine(hash, s.accessor->number, s.path);
return hash;
}
};
14 changes: 14 additions & 0 deletions src/libutil/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,18 @@ inline std::string operator + (std::string_view s1, const char * s2)
return s;
}

/**
* hash_combine() from Boost. Hash several hashable values together
* into a single hash.
*/
inline void hash_combine(std::size_t & seed) { }

template <typename T, typename... Rest>
inline void hash_combine(std::size_t & seed, const T & v, Rest... rest)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
hash_combine(seed, rest...);
}

}

0 comments on commit 550b347

Please sign in to comment.