Remove unused boost include and split out std-hash.hh

Splitting it out immediately answers questions like [this],
without increasing the number of compilation units.

I did consider using boost::hash_combine instead, but it doesn't seem
to be quite as capable, accepting only two arguments.

[this]: https://github.com/NixOS/nix/pull/11113#discussion_r1679991573
This commit is contained in:
Robert Hensing 2024-07-16 22:22:15 +02:00
parent 64b46000ad
commit d0e9878389
5 changed files with 27 additions and 17 deletions

View file

@ -2,7 +2,7 @@
#include <cinttypes> #include <cinttypes>
#include "util.hh" #include "std-hash.hh"
namespace nix { namespace nix {

View file

@ -216,6 +216,7 @@ headers = [config_h] + files(
'source-accessor.hh', 'source-accessor.hh',
'source-path.hh', 'source-path.hh',
'split.hh', 'split.hh',
'std-hash.hh',
'strings.hh', 'strings.hh',
'strings-inline.hh', 'strings-inline.hh',
'suggestions.hh', 'suggestions.hh',

View file

@ -8,8 +8,7 @@
#include "ref.hh" #include "ref.hh"
#include "canon-path.hh" #include "canon-path.hh"
#include "source-accessor.hh" #include "source-accessor.hh"
#include "std-hash.hh"
#include <boost/functional/hash.hpp> // for boost::hash_combine
namespace nix { namespace nix {

24
src/libutil/std-hash.hh Normal file
View file

@ -0,0 +1,24 @@
#pragma once
//!@file Hashing utilities for use with unordered_map, etc. (ie low level implementation logic, not domain logic like
//! Nix hashing)
#include <functional>
namespace nix {
/**
* 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...);
}
} // namespace nix

View file

@ -375,18 +375,4 @@ inline std::string operator + (std::string_view s1, const char * s2)
return s; 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...);
}
} }