Vcsn  2.2
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/fwd.hh>
10 #include <vcsn/weightset/b.hh>
12 
13 namespace vcsn
14 {
15  namespace detail
16  {
17  template <typename Value>
19  {
20  public:
21  using value_t = Value;
22 
23  static value_t
24  add(const value_t l, const value_t r)
25  {
26  return std::min(l, r);
27  }
28 
29  static value_t
30  mul(const value_t l, const value_t r)
31  {
32  return (is_zero(l) || is_zero(r)
33  ? zero()
34  : l + r);
35  }
36 
37  static value_t
38  rdiv(const value_t l, const value_t r)
39  {
40  require(!is_zero(r), "div: division by zero");
41  return is_zero(l) ? l : l - r;
42  }
43 
44  static value_t
45  ldiv(const value_t l, const value_t r)
46  {
47  return rdiv(r, l);
48  }
49 
50  value_t
51  star(const value_t v) const
52  {
53  if (0 <= v)
54  return one();
55  else
56  // FIXME: sname.
57  raise("star: invalid value: ", to_string(*this, v));
58  }
59 
60  static value_t
61  one()
62  {
63  return 0;
64  }
65 
66  static value_t
67  zero()
68  {
69  return std::numeric_limits<value_t>::max();
70  }
71 
72  static bool
73  equal(const value_t l, const value_t r)
74  {
75  return l == r;
76  }
77 
79  static bool less(value_t lhs, value_t rhs)
80  {
81  return lhs < rhs;
82  }
83 
84  constexpr static bool is_special(value_t)
85  {
86  return false;
87  }
88 
89  static bool
90  is_zero(const value_t v)
91  {
92  return v == zero();
93  }
94 
95  static bool
96  is_one(const value_t v)
97  {
98  return v == one();
99  }
100 
101  static constexpr bool is_commutative() { return true; }
102  static constexpr bool is_idempotent() { return true; }
103 
104  static constexpr bool show_one() { return true; }
105  static constexpr star_status_t star_status()
106  {
107  return star_status_t::TOPS;
108  }
109 
110  static value_t
112  {
113  return v;
114  }
115 
116  static size_t hash(value_t v)
117  {
118  return hash_value(v);
119  }
120 
121  static value_t
123  {
124  return v ? one() : zero();
125  }
126 
127  static value_t
128  conv(std::istream& is, bool = true)
129  {
130  value_t res;
131  if (is.peek() == 'o')
132  {
133  is.ignore();
134  int c = is.get();
135  VCSN_REQUIRE(c == 'o', "invalid value: o", str_escape(c));
136  res = zero();
137  }
138  else if (!(is >> res))
139  // FIXME: sname.
140  vcsn::fail_reading(is, "invalid value");
141  return res;
142  }
143 
144  static std::ostream&
145  print(const value_t v, std::ostream& o,
146  format fmt = {})
147  {
148  if (is_zero(v))
149  return o << (fmt == format::latex ? "\\infty" : "oo");
150  else
151  return o << v;
152  }
153  };
154  }
155 }
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:89
valid iff proper succeeds.
Definition: star-status.hh:20
static size_t hash(value_t v)
Definition: min-plus.hh:116
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:66
static constexpr bool is_special(value_t)
Definition: min-plus.hh:84
static constexpr bool show_one()
Definition: min-plus.hh:104
Definition: a-star.hh:8
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
static value_t zero()
Definition: min-plus.hh:67
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:54
Print for LaTeX.
Definition: format.hh:20
static bool is_zero(const value_t v)
Definition: min-plus.hh:90
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
An input/output format for valuesets.
Definition: format.hh:11
static value_t conv(std::istream &is, bool=true)
Definition: min-plus.hh:128
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static bool equal(const value_t l, const value_t r)
Definition: min-plus.hh:73
static constexpr bool is_commutative()
Definition: min-plus.hh:101
static bool is_one(const value_t v)
Definition: min-plus.hh:96
static value_t mul(const value_t l, const value_t r)
Definition: min-plus.hh:30
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
Definition: stream.hh:62
star_status_t
Definition: star-status.hh:5
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380
static value_t rdiv(const value_t l, const value_t r)
Definition: min-plus.hh:38
static constexpr star_status_t star_status()
Definition: min-plus.hh:105
value_t star(const value_t v) const
Definition: min-plus.hh:51
static value_t transpose(const value_t v)
Definition: min-plus.hh:111
static value_t add(const value_t l, const value_t r)
Definition: min-plus.hh:24
static constexpr bool is_idempotent()
Definition: min-plus.hh:102
static value_t one()
Definition: min-plus.hh:61
static std::ostream & print(const value_t v, std::ostream &o, format fmt={})
Definition: min-plus.hh:145
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: min-plus.hh:79
static value_t ldiv(const value_t l, const value_t r)
Definition: min-plus.hh:45
static value_t conv(b, b::value_t v)
Definition: min-plus.hh:122