Sprout/sprout/random/unique_seed.hpp

60 lines
1.6 KiB
C++
Raw Normal View History

2011-10-12 20:28:33 +00:00
#ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP
#define SPROUT_RANDOM_UNIQUE_SEED_HPP
2011-10-29 03:02:21 +00:00
#include <cstddef>
#include <type_traits>
2011-10-12 20:28:33 +00:00
#include <sprout/config.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/array/array.hpp>
2013-02-04 14:10:24 +00:00
#include <sprout/functional/hash.hpp>
2011-10-12 20:28:33 +00:00
#include <sprout/preprocessor/unique_string.hpp>
2011-10-29 03:02:21 +00:00
namespace sprout {
//
// make_seed
//
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR std::size_t
make_seed(T const& v) {
2011-10-29 03:02:21 +00:00
return sprout::to_hash(v);
}
namespace detail {
template<std::size_t N, typename T, typename... Args>
SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Args) + 1 == N,
sprout::array<std::size_t, N>
2012-10-05 15:58:56 +00:00
>::type
make_seed_seq_impl(T const& v, std::size_t seed, Args const&... args) {
2011-10-29 03:02:21 +00:00
return sprout::array<std::size_t, N>{{args..., seed}};
}
template<std::size_t N, typename T, typename... Args>
SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Args) + 1 != N,
sprout::array<std::size_t, N>
2012-10-05 15:58:56 +00:00
>::type
make_seed_seq_impl(T const& v, std::size_t seed, Args const&... args) {
2011-10-29 03:02:21 +00:00
return sprout::detail::make_seed_seq_impl<N>(v, sprout::hash_combine(seed, v), args..., seed);
}
} // namespace detail
//
// make_seed_seq
//
template<std::size_t N, typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::array<std::size_t, N>
make_seed_seq(T const& v) {
2011-10-29 03:02:21 +00:00
return sprout::detail::make_seed_seq_impl<N>(v, sprout::to_hash(v));
}
} // namespace sprout
2011-10-12 20:28:33 +00:00
//
// SPROUT_UNIQUE_SEED
//
2011-10-29 03:02:21 +00:00
#define SPROUT_UNIQUE_SEED (::sprout::make_seed(SPROUT_PP_UNIQUE_STRING))
//
// SPROUT_UNIQUE_SEED_SEQ
//
#define SPROUT_UNIQUE_SEED_SEQ(N) (::sprout::make_seed_seq<N>(SPROUT_PP_UNIQUE_STRING))
2011-10-12 20:28:33 +00:00
#endif // #ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP