Vcsn  2.8
Be Rational
algos.hxx
Go to the documentation of this file.
1 #include <sstream>
2 
3 #include <vcsn/dyn/automaton.hh>
4 #include <vcsn/dyn/context.hh>
5 #include <vcsn/dyn/value.hh>
7 
8 namespace vcsn
9 {
10  namespace dyn LIBVCSN_API
11  {
12  inline
13  bool are_equal(const automaton& lhs, const automaton& rhs)
14  {
15  return compare(lhs, rhs) == 0;
16  }
17 
18  inline
19  bool are_equal(const expression& lhs, const expression& rhs)
20  {
21  return compare(lhs, rhs) == 0;
22  }
23 
24  inline
25  std::string configuration(const std::string& key)
26  {
27  return vcsn::configuration(key);
28  }
29 
30  template <typename Value>
31  std::string format(const Value& v,
32  const std::string& format = "default")
33  {
34  std::ostringstream os;
35  print(v, os, format);
36  return os.str();
37  }
38 
39  inline
40  bool less_than(const automaton& lhs, const automaton& rhs)
41  {
42  return compare(lhs, rhs) < 0;
43  }
44 
45  inline
46  bool less_than(const expression& lhs, const expression& rhs)
47  {
48  return compare(lhs, rhs) < 0;
49  }
50 
51  inline
52  automaton make_automaton(const std::string& data,
53  const std::string& format,
54  bool strip)
55  {
56  auto&& is = std::istringstream{data};
57  try
58  {
59  auto res = read_automaton(is, format, strip);
60  require(is.peek() == EOF,
61  "unexpected trailing characters: ", is);
62  return res;
63  }
64  catch (const std::runtime_error& e)
65  {
66  raise(e, " while reading automaton");
67  }
68  }
69 
70  inline
72  const std::string& s, identities ids,
73  const std::string& format)
74  {
75  std::istringstream is{s};
76  try
77  {
78  auto res = read_expression(ctx, ids, is, format);
79  require(is.peek() == EOF,
80  "unexpected trailing characters: ", is);
81  return res;
82  }
83  catch (const std::runtime_error& e)
84  {
85  raise(e, " while reading expression: ", str_quote(s));
86  }
87  }
88 
89  inline
90  label make_label(const context& ctx, const std::string& s,
91  const std::string& format)
92  {
93  auto&& is = std::istringstream{s};
94  try
95  {
96  auto res = read_label(ctx, is, format);
97  require(is.peek() == EOF,
98  "unexpected trailing characters: ", is);
99  return res;
100  }
101  catch (const std::runtime_error& e)
102  {
103  raise(e, " while reading label: ", str_quote(s));
104  }
105  }
106 
107  inline
108  polynomial make_polynomial(const context& ctx, const std::string& s)
109  {
110  auto&& is = std::istringstream{s};
111  try
112  {
113  auto res = read_polynomial(ctx, is);
114  require(is.peek() == EOF,
115  "unexpected trailing characters: ", is);
116  return res;
117  }
118  catch (const std::runtime_error& e)
119  {
120  raise(e, " while reading polynomial: ", str_quote(s));
121  }
122  }
123 
124  inline
125  weight make_weight(const context& ctx, const std::string& s)
126  {
127  auto&& is = std::istringstream{s};
128  try
129  {
130  auto res = read_weight(ctx, is);
131  require(is.peek() == EOF,
132  "unexpected trailing characters: ", is);
133  return res;
134  }
135  catch (const std::runtime_error& e)
136  {
137  raise(e, " while reading weight: ", str_quote(s));
138  }
139  }
140 
141  inline
142  label make_word(const context& ctx, const std::string& s,
143  const std::string& format)
144  {
145  return make_label(make_word_context(ctx), s, format);
146  }
147 
148 
149  /*---------------.
150  | Comparisons. |
151  `---------------*/
152 
153  template <typename Lhs, typename Rhs>
154  auto operator==(const Lhs& l, const Rhs& r)
155  -> decltype(l->vname(), r->vname(), bool())
156  {
157  return compare(l, r) == 0;
158  }
159 
160  template <typename Lhs, typename Rhs>
161  auto operator!=(const Lhs& l, const Rhs& r)
162  -> decltype(l->vname(), r->vname(), bool())
163  {
164  return !(l == r);
165  }
166 
167  template <typename Lhs, typename Rhs>
168  auto operator<(const Lhs& l, const Rhs& r)
169  -> decltype(compare(l, r) < 0)
170  {
171  return compare(l, r) < 0;
172  }
173 
174  template <typename Lhs, typename Rhs>
175  auto operator<=(const Lhs& l, const Rhs& r)
176  -> decltype(compare(l, r) <= 0)
177  {
178  return compare(l, r) <= 0;
179  }
180 
181  template <typename Lhs, typename Rhs>
182  auto operator>(const Lhs& l, const Rhs& r)
183  -> decltype(compare(l, r) > 0)
184  {
185  return compare(l, r) > 0;
186  }
187 
188  template <typename Lhs, typename Rhs>
189  auto operator>=(const Lhs& l, const Rhs& r)
190  -> decltype(compare(l, r) >= 0)
191  {
192  return compare(l, r) >= 0;
193  }
194 
195 
196  /*--------------.
197  | Operations. |
198  `--------------*/
199 
200  template <typename Lhs, typename Rhs>
201  auto operator+(const Lhs& l, const Rhs& r)
202  -> decltype(add(l, r))
203  {
204  return add(l, r);
205  }
206 
207  template <typename Lhs, typename Rhs>
208  auto operator&(const Lhs& l, const Rhs& r)
209  -> decltype(conjunction(l, r))
210  {
211  return conjunction(l, r);
212  }
213 
214  template <typename Lhs, typename Rhs>
215  auto operator*(const Lhs& l, const Rhs& r)
216  -> decltype(multiply(l, r))
217  {
218  return multiply(l, r);
219  }
220  }
221 }
A dyn automaton.
Definition: automaton.hh:17
std::string configuration(const std::string &key)
Access a configuration value.
Definition: algos.hxx:25
label read_label(const context &ctx, std::istream &is, const std::string &format="default")
Read a label from a stream.
Definition: read.cc:126
polynomial make_polynomial(const context &ctx, const std::string &s)
Build a polynomial from a string.
Definition: algos.hxx:108
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
bool are_equal(const automaton &lhs, const automaton &rhs)
Whether are the same automaton.
Definition: algos.hxx:13
automaton make_automaton(const std::string &data, const std::string &format="default", bool strip=true)
Read an automaton from a string.
Definition: algos.hxx:52
bool less_than(const automaton &lhs, const automaton &rhs)
Ordering between automata.
Definition: algos.hxx:40
context make_word_context(const context &ctx)
The context for words.
auto operator<=(const Lhs &l, const Rhs &r) -> decltype(compare(l, r)<=0)
Definition: algos.hxx:175
expression make_expression(const context &ctx, const std::string &s, identities ids={}, const std::string &format="default")
Read an expression from a stream.
Definition: algos.hxx:71
#define LIBVCSN_API
Definition: export.hh:8
auto operator>(const Lhs &l, const Rhs &r) -> decltype(compare(l, r) > 0)
Definition: algos.hxx:182
expression read_expression(const context &ctx, identities ids, std::istream &is, const std::string &format="default", const location &loc=location{})
Read an expression from a stream.
Definition: read.cc:100
automaton read_automaton(std::istream &is, const std::string &format="default", bool strip=true, const location &loc=location{})
Read an automaton from a stream.
Definition: read.cc:73
weight read_weight(const context &ctx, std::istream &is)
Read a weight from a stream.
Definition: read.hh:89
An expressionset can implement several different sets of identities on expressions.
Definition: identities.hh:20
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
label make_label(const context &ctx, const std::string &s, const std::string &format="default")
Build a label from a string.
Definition: algos.hxx:90
auto operator &(const Lhs &l, const Rhs &r) -> decltype(conjunction(l, r))
Definition: algos.hxx:208
auto operator==(const Lhs &l, const Rhs &r) -> decltype(l->vname(), r->vname(), bool())
Definition: algos.hxx:154
A dyn Value/ValueSet.
Definition: fwd.hh:29
automaton conjunction(const automaton &lhs, const automaton &rhs, bool lazy=false)
The conjunction (aka synchronized product) of automata.
Definition: others.cc:23
auto operator!=(const Lhs &l, const Rhs &r) -> decltype(l->vname(), r->vname(), bool())
Definition: algos.hxx:161
std::ostringstream os
The output stream: the corresponding C++ snippet to compile.
Definition: translate.cc:404
Template-less root for contexts.
Definition: context.hh:16
auto operator>=(const Lhs &l, const Rhs &r) -> decltype(compare(l, r) >=0)
Definition: algos.hxx:189
std::string configuration(const std::string &key)
Get the string mapped by key (e.g., "configuration.version", "dot.styles").
Definition: a-star.hh:8
auto operator*(const Lhs &l, const Rhs &r) -> decltype(multiply(l, r))
Definition: algos.hxx:215
automaton strip(const automaton &a)
The automaton in a with its metadata layers removed.
Definition: strip.hh:46
weight make_weight(const context &ctx, const std::string &s)
Build a weight from a string.
Definition: algos.hxx:125
automaton multiply(const automaton &lhs, const automaton &rhs, const std::string &algo="auto")
Multiply (concatenate) two automata.
Definition: multiply.hh:169
return v
Definition: multiply.hh:362
label make_word(const context &ctx, const std::string &s, const std::string &format="default")
Build a word from a string.
Definition: algos.hxx:142
std::string str_quote(Args &&... args)
Convert to a string, in quotes.
Definition: escape.hh:49
static identities ids(const driver &d)
Get the identities of the driver.
Definition: parse.cc:91
std::string format(const Value &v, const std::string &format="default")
Definition: algos.hxx:31
std::ostream & print(const automaton &aut, std::ostream &out=std::cout, const std::string &format="default")
Print automaton a on out using format format.
Definition: print.hh:121
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:87
auto operator+(const Lhs &l, const Rhs &r) -> decltype(add(l, r))
Definition: algos.hxx:201
auto operator<(const Lhs &l, const Rhs &r) -> decltype(compare(l, r)< 0)
Definition: algos.hxx:168
int compare(const automaton &lhs, const automaton &rhs)
Three-way comparison between automata.
return res
Definition: multiply.hh:399
automaton add(const automaton &lhs, const automaton &rhs, const std::string &algo="auto")
Sum of two automata.
Definition: add.hh:124
polynomial read_polynomial(const context &ctx, std::istream &is)
Read a polynomial from a stream.
Definition: read.hh:59