Vcsn  2.5.dev
Be Rational
min-plus.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/misc/format.hh>
4 #include <vcsn/misc/functional.hh> // hash_value
5 #include <vcsn/misc/raise.hh>
7 #include <vcsn/misc/stream.hh> // eat
8 #include <vcsn/misc/symbol.hh>
9 #include <vcsn/weightset/b.hh>
10 #include <vcsn/weightset/fwd.hh>
12 
13 namespace vcsn
14 {
15  namespace detail
16  {
17  template <typename Derived, typename Value>
19  {
20  public:
21  using self_t = Derived;
22  using value_t = Value;
23 
24  const self_t& self() const
25  {
26  return static_cast<const self_t&>(*this);
27  }
28 
29  static value_t
30  add(const value_t l, const value_t r)
31  {
32  return std::min(l, r);
33  }
34 
35  static value_t
36  mul(const value_t l, const value_t r)
37  {
38  return (is_zero(l) || is_zero(r)
39  ? zero()
40  : l + r);
41  }
42 
43  value_t
44  rdivide(const value_t l, const value_t r) const
45  {
46  require(!is_zero(r), self(), "div: division by zero");
47  return is_zero(l) ? l : l - r;
48  }
49 
50  value_t
51  ldivide(const value_t l, const value_t r) const
52  {
53  return rdivide(r, l);
54  }
55 
56  value_t
57  star(const value_t v) const
58  {
59  if (0 <= v)
60  return one();
61  else
62  raise_not_starrable(self(), v);
63  }
64 
65  static value_t
66  one()
67  {
68  return 0;
69  }
70 
71  static value_t
72  zero()
73  {
74  return std::numeric_limits<value_t>::max();
75  }
76 
77  static value_t
78  min()
79  {
81  }
82 
83  static value_t
84  max()
85  {
86  return std::numeric_limits<value_t>::max();
87  }
88 
90  static int
91  compare(const value_t l, const value_t r)
92  {
93  return l - r;
94  }
95 
97  static bool
98  equal(const value_t l, const value_t r)
99  {
100  return l == r;
101  }
102 
104  static bool
106  {
107  return l < r;
108  }
109 
110  constexpr static bool
112  {
113  return false;
114  }
115 
116  static bool
118  {
119  return v == zero();
120  }
121 
122  static bool
123  is_one(const value_t v)
124  {
125  return v == one();
126  }
127 
128  static constexpr bool is_commutative() { return true; }
129  static constexpr bool is_idempotent() { return true; }
130 
131  static constexpr bool show_one() { return true; }
132  static constexpr star_status_t star_status()
133  {
134  return star_status_t::TOPS;
135  }
136 
137  static value_t
139  {
140  return v;
141  }
142 
143  static size_t hash(value_t v)
144  {
145  return hash_value(v);
146  }
147 
148  static value_t
150  {
151  return v ? one() : zero();
152  }
153 
154  value_t
155  conv(std::istream& is, bool = true) const
156  {
157  value_t res;
158  if (is.peek() == 'o')
159  {
160  is.ignore();
161  int c = is.get();
162  if (c == 'o')
163  res = zero();
164  else
165  raise_invalid_value(self(), "o", c);
166  }
167  else if (!(is >> res))
168  raise_invalid_value(self(), is);
169  return res;
170  }
171 
172  static std::ostream&
173  print(const value_t v, std::ostream& o = std::cout,
174  format fmt = {})
175  {
176  if (is_zero(v))
177  return o << (fmt == format::latex ? "\\infty" : "oo");
178  else
179  return o << v;
180  }
181  };
182  }
183 }
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static value_t transpose(const value_t v)
Definition: min-plus.hh:138
star_status_t
Definition: star-status.hh:5
static constexpr bool is_special(value_t)
Definition: min-plus.hh:111
static constexpr bool is_idempotent()
Definition: min-plus.hh:129
static bool less(value_t l, value_t r)
Whether l < r.
Definition: min-plus.hh:105
static value_t conv(b, b::value_t v)
Definition: min-plus.hh:149
static value_t zero()
Definition: min-plus.hh:72
value_t star(const value_t v) const
Definition: min-plus.hh:57
static int compare(const value_t l, const value_t r)
Three-way comparison between l and r.
Definition: min-plus.hh:91
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:45
static value_t max()
Definition: min-plus.hh:84
return res
Definition: multiply.hh:399
ATTRIBUTE_NORETURN void raise_invalid_value(const ValueSet &vs, Args &&... args)
Cannot make a value from this.
Definition: weightset.hh:227
static bool is_one(const value_t v)
Definition: min-plus.hh:123
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format fmt={})
Definition: min-plus.hh:173
static bool equal(const value_t l, const value_t r)
Whether l == r.
Definition: min-plus.hh:98
static value_t mul(const value_t l, const value_t r)
Definition: min-plus.hh:36
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: weightset.hh:235
static value_t add(const value_t l, const value_t r)
Definition: min-plus.hh:30
value_t ldivide(const value_t l, const value_t r) const
Definition: min-plus.hh:51
static value_t min()
Definition: min-plus.hh:78
static constexpr bool show_one()
Definition: min-plus.hh:131
static constexpr star_status_t star_status()
Definition: min-plus.hh:132
Print for LaTeX.
Definition: format.hh:22
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
Definition: a-star.hh:8
static constexpr bool is_commutative()
Definition: min-plus.hh:128
value_t rdivide(const value_t l, const value_t r) const
Definition: min-plus.hh:44
An input/output format for valuesets.
Definition: format.hh:13
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
return exp min
Definition: multiply.hh:362
value_t conv(std::istream &is, bool=true) const
Definition: min-plus.hh:155
static value_t one()
Definition: min-plus.hh:66
static size_t hash(value_t v)
Definition: min-plus.hh:143
static bool is_zero(const value_t v)
Definition: min-plus.hh:117
valid iff proper succeeds.
Definition: star-status.hh:20