I've got a variable size binary data blob that is accessible through a std::byte pointer and a byte size. Is there a way to call std::hash on it? (I can't find any example for this)
std::hash with a pointer and a byte size
361 Views Asked by moala At
2
There are 2 best solutions below
0
On
Here's an example based on my comment:
#include <span>
#include <iostream>
#include <string_view>
using bytes = std::span<const std::byte>;
template <>
struct std::hash<bytes>
{
std::size_t operator()(const bytes& x) const noexcept
{
return std::hash<std::string_view>{}({reinterpret_cast<const char*>(x.data()), x.size()});
}
};
int main()
{
auto integers = std::array { 1, 2, 3, 4, 5 };
auto doubles = std::array { 1.2, 3.4, 5.6, 7.8, 9.0 };
std::string string = "A string";
auto hash = std::hash<bytes>{};
std::cout << std::hex;
std::cout << hash(std::as_bytes(std::span(integers))) << "\n";
std::cout << hash(std::as_bytes(std::span(doubles))) << "\n";
std::cout << hash(std::as_bytes(std::span(string))) << "\n";
}
5ae7ce2c85367f67
42515290ba6efcdb
d760fec69e30b9e1
Of course, I used std::span, but you could use anything else (e.g. std::pair<const std::byte*, std::size_t>).
Alternatively, you could use MurmurHash (I believe that's what libstdc++ uses internally anyway).
As far as I can tell, you have to create a custom hash function:
Output:
The types can be adjusted as necessary for your use case.
Edit: I added an alternative hash combiner (that is commented out) that in theory may produce better results.
Edit2: Added additional hash functor that takes a pointer and size.