Sprout/sprout/random/unique_seed.hpp

68 lines
2.2 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2011-10-12 20:28:33 +00:00
#ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP
#define SPROUT_RANDOM_UNIQUE_SEED_HPP
2013-08-07 07:49:27 +00:00
#include <cstdint>
2011-10-29 03:02:21 +00:00
#include <type_traits>
2011-10-12 20:28:33 +00:00
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.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>
2013-08-07 07:49:27 +00:00
SPROUT_CONSTEXPR std::uint_least32_t
2012-10-05 15:58:56 +00:00
make_seed(T const& v) {
2013-08-07 07:49:27 +00:00
return static_cast<std::uint_least32_t>(sprout::to_hash(v));
2011-10-29 03:02:21 +00:00
}
namespace detail {
template<std::size_t N, typename T, typename... Args>
SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Args) + 1 == N,
2013-08-07 07:49:27 +00:00
sprout::array<std::uint_least32_t, N>
2012-10-05 15:58:56 +00:00
>::type
2013-07-22 13:00:09 +00:00
make_seed_seq_impl(T const&, std::size_t seed, Args const&... args) {
2013-08-07 07:49:27 +00:00
return sprout::array<std::uint_least32_t, N>{{args..., static_cast<std::uint_least32_t>(seed)}};
2011-10-29 03:02:21 +00:00
}
template<std::size_t N, typename T, typename... Args>
SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Args) + 1 != N,
2013-08-07 07:49:27 +00:00
sprout::array<std::uint_least32_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) {
2013-08-07 07:49:27 +00:00
return sprout::detail::make_seed_seq_impl<N>(v, sprout::hash_combine(seed, v), args..., static_cast<std::uint_least32_t>(seed));
2011-10-29 03:02:21 +00:00
}
} // namespace detail
//
// make_seed_seq
//
template<std::size_t N, typename T>
2013-08-07 07:49:27 +00:00
SPROUT_CONSTEXPR sprout::array<std::uint_least32_t, N>
2012-10-05 15:58:56 +00:00
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