1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/algorithm/cxx14/sample.hpp

83 lines
3.1 KiB
C++
Raw Normal View History

2014-04-05 11:59:19 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2014-04-05 11:59:19 +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)
=============================================================================*/
#ifndef SPROUT_ALGORITHM_CXX14_SAMPLE_HPP
#define SPROUT_ALGORITHM_CXX14_SAMPLE_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/iterator/distance.hpp>
#include <sprout/workaround/detail/uniform_int_distribution.hpp>
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
2014-12-14 03:59:51 +00:00
template<typename PopulationIterator, typename SampleIterator, typename Distance, typename URNG>
2014-04-05 11:59:19 +00:00
inline SPROUT_CXX14_CONSTEXPR SampleIterator
sample_impl(
2014-12-14 03:59:51 +00:00
PopulationIterator first, PopulationIterator last, std::input_iterator_tag*,
2014-04-05 11:59:19 +00:00
SampleIterator out, std::random_access_iterator_tag*,
2014-12-14 03:59:51 +00:00
Distance n, URNG&& g
2014-04-05 11:59:19 +00:00
)
{
2014-12-14 03:59:51 +00:00
typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION<Distance> distribution_type;
2014-04-05 11:59:19 +00:00
typedef typename distribution_type::param_type param_type;
distribution_type dist = {};
2014-12-14 03:59:51 +00:00
Distance sample_size = 0;
2014-04-05 11:59:19 +00:00
while (first != last && sample_size != n) {
out[sample_size++] = *first++;
}
2014-12-14 03:59:51 +00:00
for (Distance pop_size = sample_size; first != last; ++first, ++pop_size) {
2014-04-05 11:59:19 +00:00
param_type const p(0, pop_size);
2014-12-14 03:59:51 +00:00
Distance const k = dist(g, p);
2014-04-05 11:59:19 +00:00
if (k < n) {
out[k] = *first;
}
}
return out + sample_size;
}
2014-12-14 03:59:51 +00:00
template<typename PopulationIterator, typename SampleIterator, typename Distance, typename URNG>
2014-04-05 11:59:19 +00:00
inline SPROUT_CXX14_CONSTEXPR SampleIterator
sample_impl(
2014-12-14 03:59:51 +00:00
PopulationIterator first, PopulationIterator last, std::forward_iterator_tag*,
2014-04-05 11:59:19 +00:00
SampleIterator out, std::output_iterator_tag*,
2014-12-14 03:59:51 +00:00
Distance n, URNG&& g
2014-04-05 11:59:19 +00:00
)
{
2014-12-14 03:59:51 +00:00
typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION<Distance> distribution_type;
2014-04-05 11:59:19 +00:00
typedef typename distribution_type::param_type param_type;
distribution_type dist = {};
2014-12-14 03:59:51 +00:00
Distance unsampled_size = sprout::distance(first, last);
2014-04-05 11:59:19 +00:00
for (n = NS_SSCRISK_CEL_OR_SPROUT::min(n, unsampled_size); n != 0; ++first ) {
param_type const p(0, --unsampled_size);
if (dist(g, p) < n) {
*out++ = *first;
--n;
}
}
return out;
}
} // namespace detail
//
// sample
//
2014-12-14 03:59:51 +00:00
template<typename PopulationIterator, typename SampleIterator, typename Distance, typename URNG>
2014-04-05 11:59:19 +00:00
inline SPROUT_CXX14_CONSTEXPR SampleIterator
2014-12-14 03:59:51 +00:00
sample(PopulationIterator first, PopulationIterator last, SampleIterator out, Distance n, URNG&& g) {
typedef typename std::iterator_traits<PopulationIterator>::iterator_category* pop_category;
2014-04-05 11:59:19 +00:00
typedef typename std::iterator_traits<SampleIterator>::iterator_category* sample_category;
return sprout::detail::sample_impl(
first, last, pop_category(),
out, sample_category(),
n, SPROUT_FORWARD(URNG, g)
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_SAMPLE_HPP