SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shuffle.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_SHUFFLE_HPP
9 #define SIG_UTIL_SHUFFLE_HPP
10 
11 #include "../sigutil.hpp"
12 #include "../tools/random.hpp"
13 
15 
16 namespace sig
17 {
19 
28 template <class C>
29 void shuffle(C& container)
30 {
31  static SimpleRandom<double> myrand(0.0, 1.0, false);
32 
33  std::random_shuffle(std::begin(container), std::end(container), [&](std::ptrdiff_t max){ return static_cast<std::ptrdiff_t>(myrand() * max); });
34 }
35 
36 
37 namespace impl
38 {
39 
40 template <class C>
41 void shuffle_impl(uint loop, C const& seq){}
42 
43 template <class C, class It, class... Its>
44 void shuffle_impl(uint loop, C const& seq, It iter, Its... iters)
45 {
46  auto ori_iter = iter;
47  std::unordered_map<uint, typename It::value_type> map;
48 
49  for (uint i=0; i<loop; ++i, ++iter){
50  map[seq[i]] = *iter;
51  }
52  for (uint i = 0; i < loop; ++i, ++ori_iter){
53  *ori_iter = map[i];
54  }
55 
56  shuffle_impl(loop, seq, iters...);
57 }
58 } //impl
59 
61 
75 template <class... Cs>
76 void shuffle(Cs&... containers)
77 {
78  uint size = min(containers.size()...);
79  auto rseq = make_unique_numbers(size, 0, size - 1, false);
80 
81  impl::shuffle_impl(size, rseq, std::begin(containers)...);
82 }
83 
84 }
85 #endif
C make_unique_numbers(uint n, int min, int max, bool debug)
重複の無い一様分布の整数乱数を生成
Definition: random.hpp:85
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
void shuffle(C &container)
コンテナの要素をシャッフル
Definition: shuffle.hpp:29
void shuffle_impl(uint loop, C const &seq)
Definition: shuffle.hpp:41
初期化時に指定した範囲の一様分布乱数を発生させるクラス
Definition: random.hpp:31
auto min(T v) -> T
auto max(T v) -> T
auto map(F &&func, C &&list)
1引数高階関数
Definition: high_order.hpp:62
Definition: array.hpp:15
auto begin(C &&c) -> std::move_iterator< typename RC::iterator >