Vcsn  2.3
Be Rational
weightset.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <memory> // std::make_shared
5 
6 #include <vcsn/misc/type_traits.hh> // detect
7 
8 // It is much simpler and saner in C++ to put types and functions on
9 // these types in the same namespace. Since "using q =
10 // detail::weightset_mixin<q_impl>" would just create an alias of
11 // q, its original namespace, detail::, would still be the namespace
12 // used in ADL.
13 //
14 // This is really troublesome to implement free-functions such as join.
15 //
16 // Therefore, although this wrapper should be hidden as a detail::, it
17 // will remain in vcsn::, where join and the like will find it.
18 
19 namespace vcsn
20 {
21  namespace detail
22  {
24  template <typename T>
25  using power_mem_fn_t
26  = decltype(std::declval<T>()
27  .power(std::declval<typename T::value_t>(), 0));
28 
30  template <typename T>
32  }
33 
35  template <typename WeightSet>
36  struct weightset_mixin : WeightSet
37  {
38  using super_t = WeightSet;
39  using typename super_t::value_t;
40 
42  using super_t::super_t;
43 
45  using super_t::mul;
46 
48  template <typename... Ts>
49  value_t mul(const Ts&... ts) const
50  {
51  value_t res = this->one();
52  // FIXME: Remove once GCC is fixed.
53  using swallow = int[];
54  (void) swallow
55  {
56  ((res = super_t::mul(res, ts)), 0)...
57  };
58  return res;
59  }
60 
61  private:
64  template <typename WS = super_t>
65  auto power_(value_t e, unsigned n) const
66  -> std::enable_if_t<detail::has_power_mem_fn<WS>{}, value_t>
67  {
68  return super_t::power(e, n);
69  }
70 
73  template <typename WS = super_t>
74  auto power_(value_t e, unsigned n) const
75  -> std::enable_if_t<!detail::has_power_mem_fn<WS>{}, value_t>
76  {
78  if (!super_t::is_one(e))
79  while (n--)
80  res = mul(res, e);
81  return res;
82  }
83 
84  public:
85 
87  value_t power(value_t e, unsigned n) const
88  {
89  return power_<WeightSet>(e, n);
90  }
91  };
92 
93  // FIXME: find generic implementation for min-plus.
94  template <typename T>
95  struct is_tropical : std::false_type
96  {};
97 
98 }
value_t mul(const Ts &...ts) const
A variadic multiplication.
Definition: weightset.hh:49
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.
Definition: weightset.hh:74
constant< type_t::one, Context > one
Definition: fwd.hh:113
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.
Definition: weightset.hh:65
value_t power(value_t e, unsigned n) const
Repeated multiplication.
Definition: weightset.hh:87
Definition: a-star.hh:8
variadic< type_t::mul, Context > mul
Definition: fwd.hh:151
decltype(std::declval< T >().power(std::declval< typename T::value_t >(), 0)) power_mem_fn_t
The signature of power.
Definition: weightset.hh:27
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46