Wildmeshing Toolkit
Loading...
Searching...
No Matches
vector_hash.cpp
Go to the documentation of this file.
1#include "vector_hash.hpp"
2#include <algorithm>
3#include <functional>
4#include <string>
5#include <string_view>
7
8namespace wmtk::utils {
9namespace {
10template <typename T>
11std::size_t _vector_hash_impl(const std::vector<T>& data)
12{
13 // wraps the passed data as the contents of a std::string_view to take
14 // advantage of std::string_view's hash function. NOTE that
15 // std::string_view's hash function is probably not consistent between
16 // platforms. In fact, on libstdcxx depends on <bits/hash_bytes.h> which
17 // states that the STL _Hash_bytes is not a constant API per release.
18 // as such TODO: find a platform-agnostic hash function
19 using SVType = std::string_view::value_type;
20 static_assert(sizeof(T) % sizeof(SVType) == 0);
21 constexpr static size_t size_ratio = sizeof(T) / sizeof(SVType);
22
23 std::string_view view(reinterpret_cast<const SVType*>(data.data()), size_ratio * data.size());
24 return std::hash<std::string_view>{}(view);
25}
26} // namespace
27
28std::size_t vector_hash(const std::vector<size_t>& data)
29{
30 return _vector_hash_impl(data);
31}
32std::size_t vector_hash(const std::vector<int64_t>& data)
33{
34 return _vector_hash_impl(data);
35}
36std::size_t vector_hash(const std::vector<double>& data)
37{
38 return _vector_hash_impl(data);
39}
40std::size_t vector_hash(const std::vector<char>& data)
41{
42 return _vector_hash_impl(data);
43}
44std::size_t vector_hash(const std::vector<Rational>& data)
45{
46 std::vector<size_t> hashes;
47 std::transform(data.begin(), data.end(), std::back_inserter(hashes), [](const Rational& r) {
48 std::vector<size_t> v;
49 std::hash<std::string> h;
50 v.emplace_back(h(r.serialize()));
51
52 return vector_hash(v);
53 });
54 return vector_hash(hashes);
55}
56} // namespace wmtk::utils
std::size_t vector_hash(const std::vector< size_t > &data)