10 #include <boost/algorithm/string/erase.hpp> 11 #include <boost/tokenizer.hpp> 40 = decltype(std::declval<T>()
41 .power(std::declval<typename T::value_t>(), 0));
49 template <
typename WeightSet>
53 using typename super_t::value_t;
56 using super_t::super_t;
62 template <
typename... Ts>
63 value_t
mul(
const Ts&... ts)
const 65 value_t
res = this->one();
67 using swallow =
int[];
78 template <
typename WS = super_t>
79 auto power_(value_t e,
unsigned n)
const 80 -> std::enable_if_t<detail::has_power_mem_fn<WS>{}, value_t>
82 return super_t::power(e, n);
87 template <
typename WS = super_t>
88 auto power_(value_t e,
unsigned n)
const 89 -> std::enable_if_t<!detail::has_power_mem_fn<WS>{}, value_t>
92 if (!super_t::is_one(e))
101 value_t
power(value_t e,
unsigned n)
const 103 return power_<WeightSet>(e, n);
117 typename RandomGenerator = std::default_random_engine>
123 using random_generator_t = RandomGenerator;
140 return print_random_weight_();
146 using tokenizer = boost::tokenizer<boost::char_separator<char>>;
147 using boost::algorithm::erase_all_copy;
148 auto sep = boost::char_separator<char>{
","};
149 for (
const auto& arg: tokenizer(weights, sep))
151 auto eq = arg.find(
'=');
152 auto weight = erase_all_copy(arg.substr(0, eq),
" ");
154 min_ =
conv(ws_, arg.substr(eq + 1));
156 max_ =
conv(ws_, arg.substr(eq + 1));
159 float value = (eq != std::string::npos)
160 ? detail::lexical_cast<float>(arg.substr(eq + 1))
171 virtual weight_t pick_value_()
const = 0;
180 auto it = chooser_it_(weight_weight_, weight_);
186 return pick_value_();
198 using weight_map_t = std::map<weight_t, float, vcsn::less<weightset_t>>;
207 template <
typename WeightSet,
typename RandomGenerator>
216 template <
typename T>
225 template <
typename ValueSet,
typename... Args>
229 raise(vs,
": invalid value: ", std::forward<Args>(args)...);
233 template <
typename WeightSet>
236 const typename WeightSet::value_t& w)
238 raise(ws,
": value is not starrable: ",
to_string(ws, w));
void parse_param(const std::string ¶m)
static bool choose_map(const std::vector< float > &map, RandomGenerator &gen=RandomGenerator())
Choose whether to pick an element from a map or not.
ATTRIBUTE_NORETURN void raise_invalid_value(const ValueSet &vs, Args &&... args)
Cannot make a value from this.
variadic< type_t::mul, Context > mul
constant< type_t::one, Context > one
auto power_(value_t e, unsigned n) const -> std::enable_if_t< detail::has_power_mem_fn< WS >
Case where the weightset T features a power(value_t, unsigned) member function.
value_t mul(const Ts &... ts) const
A variadic multiplication.
auto conv(const ValueSet &vs, const std::string &str, Args &&... args) -> decltype(vs.conv(std::declval< std::istream &>(), std::forward< Args >(args)...))
Parse str via vs.conv.
auto transform(const Container &c, Fun &&fun)
Map a unary function on a container of values, and return the vector the results. ...
Abstract class for random weight generation.
Provide a variadic mul on top of a binary mul(), and one().
auto power_(value_t e, unsigned n) const -> std::enable_if_t<!detail::has_power_mem_fn< WS >
Case where the weightset T does not feature a power(value_t, unsigned) member function.
weight_t min_
The min and the max given by the user.
weight_t generate_random_weight() const
std::vector< float > weight_weight_t
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
random_generator_t & gen_
value_t power(value_t e, unsigned n) const
Repeated multiplication.
Generic declaration of the class which is specialized in each weightset.
std::string to_string(direction d)
Conversion to string.
virtual void parse_param_(const std::string &weights)
decltype(std::declval< T >() .power(std::declval< typename T::value_t >(), 0)) power_mem_fn_t
The signature of power.
typename weightset_t::value_t weight_t
std::map< weight_t, float, vcsn::less< weightset_t > > weight_map_t
Elements given by the user and their associated probabilities (weight_weight_).
value_impl< detail::weight_tag > weight
weight_t print_random_weight_() const
A random weight.
random_weight_base(random_generator_t &gen, const weightset_t &ws)
weight_weight_t weight_weight_