random/normal_distribution.hpp 追加

numeric/iota.hpp 追加
algorithm/shuffle.hpp 追加
This commit is contained in:
bolero-MURAKAMI 2011-10-19 22:47:59 +09:00
parent 8ebe6ea878
commit 44b67d28e2
19 changed files with 570 additions and 18 deletions

View file

@ -25,6 +25,7 @@
#include <sprout/algorithm/fit/reverse_copy.hpp>
#include <sprout/algorithm/fit/rotate.hpp>
#include <sprout/algorithm/fit/rotate_copy.hpp>
#include <sprout/algorithm/fit/shuffle.hpp>
#include <sprout/algorithm/fit/partition.hpp>
#include <sprout/algorithm/fit/partition_copy.hpp>
#include <sprout/algorithm/fit/stable_partition.hpp>

View file

@ -0,0 +1,43 @@
#ifndef SPROUT_ALGORITHM_FIT_SHUFFLE_HPP
#define SPROUT_ALGORITHM_FIT_SHUFFLE_HPP
#include <sprout/config.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/algorithm/fixed/shuffle.hpp>
#include <sprout/algorithm/fit/result_of.hpp>
#include <sprout/sub_array.hpp>
namespace sprout {
namespace fit {
namespace detail {
template<typename Container, typename UniformRandomNumberGenerator>
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Container>::type shuffle_impl(
Container const& cont,
UniformRandomNumberGenerator&& g,
typename sprout::fixed_container_traits<Container>::difference_type offset
)
{
return sprout::sub_copy(
sprout::get_fixed(sprout::fixed::shuffle(cont, sprout::forward<UniformRandomNumberGenerator>(g))),
offset,
offset + sprout::size(cont)
);
}
} // namespace detail
//
// shuffle
//
template<typename Container, typename UniformRandomNumberGenerator>
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Container>::type shuffle(
Container const& cont,
UniformRandomNumberGenerator&& g
)
{
return sprout::fit::detail::shuffle_impl(cont, sprout::forward<UniformRandomNumberGenerator>(g), sprout::fixed_begin_offset(cont));
}
} // namespace fit
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_FIT_SHUFFLE_HPP

View file

@ -25,6 +25,7 @@
#include <sprout/algorithm/fixed/reverse_copy.hpp>
#include <sprout/algorithm/fixed/rotate.hpp>
#include <sprout/algorithm/fixed/rotate_copy.hpp>
#include <sprout/algorithm/fixed/shuffle.hpp>
#include <sprout/algorithm/fixed/partition.hpp>
#include <sprout/algorithm/fixed/partition_copy.hpp>
#include <sprout/algorithm/fixed/stable_partition.hpp>

View file

@ -0,0 +1,118 @@
#ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP
#define SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP
#include <cstddef>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/array.hpp>
#include <sprout/null_array.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/algorithm/fixed/result_of.hpp>
#include <sprout/algorithm/fixed/swap_element.hpp>
#include <sprout/numeric/fixed/iota.hpp>
#include <sprout/random/uniform_int_distribution.hpp>
namespace sprout {
namespace fixed {
namespace detail {
template<std::size_t N, typename Random>
SPROUT_CONSTEXPR inline sprout::array<std::ptrdiff_t, N> make_shuffle_indexes_1(
std::ptrdiff_t n,
Random const& rnd,
sprout::array<std::ptrdiff_t, N> const& arr,
std::ptrdiff_t i
)
{
return i < n
? sprout::fixed::detail::make_shuffle_indexes_1(
n,
rnd(),
sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()),
i + 1
)
: arr
;
}
template<std::size_t N, typename UniformRandomNumberGenerator>
SPROUT_CONSTEXPR inline sprout::array<std::ptrdiff_t, N> make_shuffle_indexes(
std::ptrdiff_t n,
UniformRandomNumberGenerator&& g
)
{
return n > 0
? sprout::fixed::detail::make_shuffle_indexes_1(
n,
sprout::random::uniform_int_distribution<std::ptrdiff_t>(0, n - 1)(sprout::forward<UniformRandomNumberGenerator>(g)),
sprout::fixed::iota(sprout::null_array<sprout::array<std::ptrdiff_t, N> >(), 0),
0
)
: sprout::array<std::ptrdiff_t, N>{{}}
;
}
template<typename Container, typename Shuffled, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type shuffle_impl_1(
Container const& cont,
sprout::index_tuple<Indexes...>,
Shuffled const& shuffled,
typename sprout::fixed_container_traits<Container>::difference_type offset,
typename sprout::fixed_container_traits<Container>::size_type size
)
{
return sprout::remake_clone<Container, Container>(
cont,
sprout::size(cont),
(Indexes >= offset && Indexes < offset + size
? *sprout::next(sprout::begin(cont), shuffled[Indexes - offset])
: *sprout::next(sprout::fixed_begin(cont), Indexes)
)...
);
}
template<typename Container, typename UniformRandomNumberGenerator, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type shuffle_impl(
Container const& cont,
sprout::index_tuple<Indexes...> indexes,
UniformRandomNumberGenerator&& g,
typename sprout::fixed_container_traits<Container>::difference_type offset,
typename sprout::fixed_container_traits<Container>::size_type size
)
{
return sprout::fixed::detail::shuffle_impl_1(
cont,
indexes,
sprout::fixed::detail::make_shuffle_indexes<sprout::fixed_container_traits<Container>::fixed_size>(
size,
sprout::forward<UniformRandomNumberGenerator>(g)
),
offset,
size
);
}
} // namespace detail
//
// shuffle
//
template<typename Container, typename UniformRandomNumberGenerator>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type shuffle(
Container const& cont,
UniformRandomNumberGenerator&& g
)
{
return sprout::fixed::detail::shuffle_impl(
cont,
typename sprout::index_range<0, sprout::fixed_container_traits<Container>::fixed_size>::type(),
sprout::forward<UniformRandomNumberGenerator>(g),
sprout::fixed_begin_offset(cont),
sprout::size(cont)
);
}
} // namespace fixed
using sprout::fixed::shuffle;
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_ALGORITHM_SHUFFLE_HPP
#define SPROUT_ALGORITHM_SHUFFLE_HPP
#include <sprout/config.hpp>
#include <sprout/algorithm/fixed/shuffle.hpp>
#include <sprout/algorithm/fit/shuffle.hpp>
#endif // #ifndef SPROUT_ALGORITHM_SHUFFLE_HPP