17 #ifndef __MASTER_ALLOCATOR_SORTER_RANDOM_UTILS_HPP__ 18 #define __MASTER_ALLOCATOR_SORTER_RANDOM_UTILS_HPP__ 42 template <
class RandomAccessIterator,
class URBG>
44 RandomAccessIterator begin,
45 RandomAccessIterator end,
46 const std::vector<double>& weights,
49 CHECK_EQ(end - begin, (
int) weights.size());
51 std::vector<double> keys(weights.size());
53 for (
size_t i = 0; i < weights.size(); ++i) {
54 CHECK_GT(weights[i], 0.0);
57 double random = std::uniform_real_distribution<>(0.0, 1.0)(urbg);
58 keys[i] = 0.0 - std::pow(random, (1.0 / weights[i]));
63 std::vector<size_t> permutation(keys.size());
64 std::iota(permutation.begin(), permutation.end(), 0);
66 std::sort(permutation.begin(), permutation.end(),
67 [&](
size_t i,
size_t j){
return keys[i] < keys[j]; });
70 std::vector<typename std::iterator_traits<RandomAccessIterator>::value_type>
71 shuffled(end - begin);
77 [&](
size_t i){
return begin[i]; });
80 std::move(shuffled.begin(), shuffled.end(), begin);
88 #endif // __MASTER_ALLOCATOR_SORTER_RANDOM_UTILS_HPP__ Definition: master.hpp:27
int random()
Definition: os.hpp:580
void weightedShuffle(RandomAccessIterator begin, RandomAccessIterator end, const std::vector< double > &weights, URBG &&urbg)
Definition: utils.hpp:43
process::Future< Nothing > transform(process::Owned< Reader< T >> &&reader, const std::function< std::string(const T &)> &func, process::http::Pipe::Writer writer)
This is a helper function that reads records from a Reader, applies a transformation to the records a...
Definition: recordio.hpp:112
Definition: attributes.hpp:24