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