#include <boost/random/uniform_real.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <vector>
#include <iostream>
#include <numeric>
struct wac
{
double operator()(float const& e) const
{
return e;
}
};
int main()
{
#define C_ARRAY_ITERATOR_SAMPLING
#if defined(C_ARRAY_ITERATOR_SAMPLING)
float population[] = { 0, 1, 4, 3, 5, 8, 2 };
const size_t POPULATION_SIZE = sizeof(population)/sizeof(float);
const size_t SAMPLE_SIZE = 3;
typedef float* population_iterator;
population_iterator populationIteratorBegin(population),
populationIteratorEnd(population + POPULATION_SIZE);
#elif defined(STD_VECTOR_ITERATOR_SAMPLING)
boost::mt19937 rng;
boost::uniform_real<float> dist(0, 100);
boost::variate_generator<
boost::mt19937&,
boost::uniform_real<float>
> uni(rng, dist);
const size_t POPULATION_SIZE = 100;
std::vector<float> population(POPULATION_SIZE);
std::generate(population.begin(), population.end(), uni);
const size_t SAMPLE_SIZE = 10;
typedef std::vector<float>::iterator population_iterator;
population_iterator populationIteratorBegin = population.begin(),
populationIteratorEnd = population.end();
#else
# error Please choose an example.
#endif
std::cout << "Population ("
<< std::distance(populationIteratorBegin,
populationIteratorEnd)
<< " elements):" << std::endl;
std::copy(populationIteratorBegin,
populationIteratorEnd,
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
{
float,
double,
wac
> is_picked;
<is_picked, population_iterator> sample_iterator;
is_picked predicate(SAMPLE_SIZE,
std::accumulate(populationIteratorBegin,
populationIteratorEnd,
float(0)),
wac());
sample_iterator sampleIteratorBegin(predicate,
populationIteratorBegin,
populationIteratorEnd);
sample_iterator sampleIteratorEnd(predicate,
populationIteratorEnd,
populationIteratorEnd);
std::cout << "Sample of " << SAMPLE_SIZE << " elements:" << std::endl;
std::copy(sampleIteratorBegin,
sampleIteratorEnd,
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
float,
double,
wac
> is_picked;
<is_picked, population_iterator> sample_iterator;
is_picked predicate(SAMPLE_SIZE,
std::accumulate(populationIteratorBegin,
populationIteratorEnd,
float(0)),
wac());
sample_iterator sampleIteratorBegin(predicate,
populationIteratorBegin,
populationIteratorEnd);
sample_iterator sampleIteratorEnd = sampleIteratorBegin.end();
std::cout << "Probabilistic sample of " << SAMPLE_SIZE << " elements:" << std::endl;
std::copy(sampleIteratorBegin,
sampleIteratorEnd,
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
<population_iterator> permutation_iterator;
{
permutation_iterator pi =
populationIteratorEnd);
std::cout << "Permutation:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
permutation_iterator pi =
populationIteratorEnd,
SAMPLE_SIZE);
std::cout << "Permutation of a subset of " << SAMPLE_SIZE
<< " elements:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
}
{
<population_iterator> permutation_iterator;
{
permutation_iterator pi =
populationIteratorEnd);
std::cout << "Sorted population:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
permutation_iterator pi =
populationIteratorEnd,
std::less<float>(), SAMPLE_SIZE);
std::cout << "Sorted " << SAMPLE_SIZE
<< " smallest elements:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
}
return 0;
}