tatami_test
Utilities for testing tatami libraries
Loading...
Searching...
No Matches
create_seed.hpp
Go to the documentation of this file.
1#ifndef TATAMI_TEST_CREATE_SEED_HPP
2#define TATAMI_TEST_CREATE_SEED_HPP
3
4#include <tuple>
5#include <cstdint>
6#include <limits>
7#include <type_traits>
8#include <algorithm>
9
10#include "utils.hpp"
11
17namespace tatami_test {
18
22// Copied from the old boost::hash_combine.
23// SeedType should be unsigned and at least 64 bits so this shouldn't overflow.
24inline SeedType combine_hash(SeedType old_seed, SeedType val) {
25 static_assert(std::numeric_limits<SeedType>::digits >= 64);
26 static_assert(std::is_unsigned<SeedType>::value);
27 return val + 0x9e3779b9 + (old_seed<<6) + (old_seed>>2);
28}
29
30// SeedType should also be unsigned, so integer casts to SeedType are always well-defined.
31template<typename Integer_>
32std::enable_if_t<std::is_integral_v<Integer_>, SeedType> combine_hash(SeedType old_seed, Integer_ val) {
33 return combine_hash(old_seed, static_cast<SeedType>(val));
34}
35
36template<typename Float_>
37std::enable_if_t<std::is_floating_point_v<Float_>, SeedType> combine_hash(SeedType old_seed, Float_ val) {
38 SeedType dummy;
39 std::copy_n(reinterpret_cast<const char*>(&val), std::min(sizeof(SeedType), sizeof(val)), reinterpret_cast<char*>(&dummy));
40 return combine_hash(old_seed, dummy);
41}
42
43inline SeedType combine_hash(SeedType old_seed, bool val) {
44 return combine_hash(old_seed, static_cast<SeedType>(val * 57));
45}
46
47template<typename Type_>
48SeedType combine_hash(SeedType old_seed, const std::vector<Type_>& val) {
49 for (const auto& x : val) {
50 old_seed = combine_hash(old_seed, x);
51 }
52 return old_seed;
53}
54
55template<typename First_, typename Second_>
56SeedType combine_hash(SeedType old_seed, const std::pair<First_, Second_>& val) {
57 return combine_hash(combine_hash(old_seed, val.first), val.second);
58}
59
60template<std::size_t member_ = 0, typename ... Args_>
61SeedType combine_hash(SeedType old_seed, const std::tuple<Args_...>& val) {
62 if constexpr(member_ == std::tuple_size_v<I<decltype(val)> >) {
63 return old_seed;
64 } else {
65 return combine_hash<member_ + 1>(
66 combine_hash(old_seed, std::get<member_>(val)),
67 val
68 );
69 }
70}
71
72template<typename First_, typename Second_, typename ... Rest_>
73SeedType combine_hash(SeedType old_seed, const First_& first, const Second_& second, Rest_&&... rest) {
74 return combine_hash(combine_hash(old_seed, first), second, std::forward<Rest_>(rest)...);
75}
92template<typename... Args_>
93SeedType create_seed(Args_&&... args) {
94 return combine_hash(6259, std::forward<Args_>(args)...);
95}
96
97}
98
99#endif
Utilities for testing tatami libraries.
Definition create_seed.hpp:17
SeedType create_seed(Args_ &&... args)
Definition create_seed.hpp:93
RngEngine::result_type SeedType
Definition utils.hpp:31
Miscellaneous utilities.