Awali
Another Weighted Automata library
random.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_RANDOM_HH
18 # define AWALI_MISC_RANDOM_HH
19 
20 # include <random>
21 # include <iterator>
22 
24 
25 namespace awali { namespace sttc {
26  namespace internal {
27 
28  // Based on https://gist.github.com/cbsmith/5538174.
29  template <typename RandomGenerator = std::default_random_engine>
31  {
32  random_selector(const RandomGenerator& g = RandomGenerator())
33  : gen_(g)
34  {}
35 
37  template <typename Iter>
38  Iter select(Iter start, Iter end)
39  {
40  std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
41  std::advance(start, dis(gen_));
42  return start;
43  }
44 
46  template <typename Iter>
47  Iter operator()(Iter start, Iter end)
48  {
49  return select(start, end);
50  }
51 
53  template <typename Container>
54  auto operator()(const Container& c) -> decltype(*std::begin(c))
55  {
56  return *select(std::begin(c), std::end(c));
57  }
58 
60  template <typename Container>
61  auto pop(Container& c) -> typename Container::value_type
62  {
63  auto i = select(std::begin(c), std::end(c));
64  auto res = *i;
65  c.erase(i);
66  return res;
67  }
68 
69  private:
70  RandomGenerator gen_;
71  };
72 
73 
74  template <typename RandomGenerator = std::default_random_engine>
75  struct random_selector<RandomGenerator>
76  make_random_selector(const RandomGenerator& g)
77  ATTRIBUTE_PURE;
78 
79  template <typename RandomGenerator>
80  struct random_selector<RandomGenerator>
81  make_random_selector(const RandomGenerator& g)
82  {
83  return g;
84  }
85 
86  }}}//end of ns awali::stc
87 
88 #endif // !AWALI_MISC_RANDOM_HH
The semiring of complex numbers.
Definition: c.hh:44
struct random_selector< RandomGenerator > make_random_selector(const RandomGenerator &g) ATTRIBUTE_PURE
Definition: random.hh:81
Main namespace of Awali.
Definition: ato.hh:22
Iter select(Iter start, Iter end)
A randomly selected iterator in [start, end).
Definition: random.hh:38
Iter operator()(Iter start, Iter end)
A randomly selected iterator in [start, end).
Definition: random.hh:47
auto operator()(const Container &c) -> decltype(*std::begin(c))
A randomly selected member of c.
Definition: random.hh:54
random_selector(const RandomGenerator &g=RandomGenerator())
Definition: random.hh:32
auto pop(Container &c) -> typename Container::value_type
A randomly selected member of c. Remove it from c.
Definition: random.hh:61