Vcsn  2.3a
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 
89  static bool
90  equal(const value_t l, const value_t r)
91  {
92  return l == r;
93  }
94 
96  static bool less(value_t lhs, value_t rhs)
97  {
98  return lhs < rhs;
99  }
100 
101  constexpr static bool is_special(value_t)
102  {
103  return false;
104  }
105 
106  static bool
108  {
109  return v == zero();
110  }
111 
112  static bool
113  is_one(const value_t v)
114  {
115  return v == one();
116  }
117 
118  static constexpr bool is_commutative() { return true; }
119  static constexpr bool is_idempotent() { return true; }
120 
121  static constexpr bool show_one() { return true; }
122  static constexpr star_status_t star_status()
123  {
124  return star_status_t::TOPS;
125  }
126 
127  static value_t
129  {
130  return v;
131  }
132 
133  static size_t hash(value_t v)
134  {
135  return hash_value(v);
136  }
137 
138  static value_t
140  {
141  return v ? one() : zero();
142  }
143 
144  static value_t
145  conv(std::istream& is, bool = true)
146  {
147  value_t res;
148  if (is.peek() == 'o')
149  {
150  is.ignore();
151  int c = is.get();
152  VCSN_REQUIRE(c == 'o', "invalid value: o", str_escape(c));
153  res = zero();
154  }
155  else if (!(is >> res))
156  // FIXME: sname.
157  vcsn::fail_reading(is, "invalid value");
158  return res;
159  }
160 
161  static std::ostream&
162  print(const value_t v, std::ostream& o = std::cout,
163  format fmt = {})
164  {
165  if (is_zero(v))
166  return o << (fmt == format::latex ? "\\infty" : "oo");
167  else
168  return o << v;
169  }
170  };
171  }
172 }
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: raise.hh:100
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: min-plus.hh:96
static bool is_zero(const value_t v)
Definition: min-plus.hh:107
Print for LaTeX.
Definition: format.hh:22
value_t star(const value_t v) const
Definition: min-plus.hh:57
Definition: a-star.hh:8
return res
Definition: multiply.hh:398
return exp min
Definition: multiply.hh:361
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
static value_t conv(std::istream &is, bool=true)
Definition: min-plus.hh:145
static constexpr bool is_special(value_t)
Definition: min-plus.hh:101
An input/output format for valuesets.
Definition: format.hh:13
value_t rdivide(const value_t l, const value_t r) const
Definition: min-plus.hh:44
static bool is_one(const value_t v)
Definition: min-plus.hh:113
static constexpr star_status_t star_status()
Definition: min-plus.hh:122
static bool equal(const value_t l, const value_t r)
Definition: min-plus.hh:90
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
star_status_t
Definition: star-status.hh:5
static value_t one()
Definition: min-plus.hh:66
value_t ldivide(const value_t l, const value_t r) const
Definition: min-plus.hh:51
static value_t conv(b, b::value_t v)
Definition: min-plus.hh:139
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:51
static value_t transpose(const value_t v)
Definition: min-plus.hh:128
static constexpr bool show_one()
Definition: min-plus.hh:121
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:111
static constexpr bool is_commutative()
Definition: min-plus.hh:118
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format fmt={})
Definition: min-plus.hh:162
static value_t min()
Definition: min-plus.hh:78
static size_t hash(value_t v)
Definition: min-plus.hh:133
static value_t mul(const value_t l, const value_t r)
Definition: min-plus.hh:36
valid iff proper succeeds.
Definition: star-status.hh:20
static value_t add(const value_t l, const value_t r)
Definition: min-plus.hh:30
static constexpr bool is_idempotent()
Definition: min-plus.hh:119
static value_t zero()
Definition: min-plus.hh:72
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
Definition: stream.hh:68
static value_t max()
Definition: min-plus.hh:84
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46