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