VCCC  2024.05
VisualCamp Common C++ library
pick.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2020/12/08.
3 # */
4 #
5 # ifndef VCCC_RANDOM_PICK_HPP
6 # define VCCC_RANDOM_PICK_HPP
7 #
8 # include <unordered_set>
9 # include <random>
10 # include <type_traits>
11 
12 namespace vccc {
13 
16 
32 template<typename T, typename Container = std::unordered_set<T>, typename RandomGenerator>
33 Container
34 pick_k(T a, T b, T k, RandomGenerator&& gen)
35 {
36  static_assert(std::is_integral<T>::value, "type must be integer (vccc::pick_k)");
37  std::unordered_set<int> elems;
38  elems.reserve(k);
39 
40  for (auto r = b - k + 1; r <= b; ++r) {
41  auto v = std::uniform_int_distribution<>(a, r)(gen);
42 
43  if (!elems.emplace(v).second) {
44  elems.emplace(r);
45  }
46  }
47  return elems;
48 }
49 
51 
52 } // namespace vccc
53 
54 # endif // VCCC_RANDOM_INCLUDE_VCCC_RANDOM_DETAIL_PICK_HPP
Container pick_k(T a, T b, T k, RandomGenerator &&gen)
pick k from int range [a, b] (no duplicates)
Definition: pick.hpp:34
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35