Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ratexpset.hxx
Go to the documentation of this file.
1 #include <sstream>
2 
3 #include <vcsn/dyn/ratexp.hh> // dyn::make_ratexp
5 #include <vcsn/misc/cast.hh> // down_pointer_cast
6 #include <vcsn/misc/stream.hh> // conv
7 
8 namespace vcsn
9 {
10 namespace dyn
11 {
12  namespace detail
13  {
14  /*--------------------.
15  | ratexpset_wrapper. |
16  `--------------------*/
17 
18  template <typename RatExpSet>
19  inline
21  (const ratexpset_t& rs)
22  : super_t()
23  , rs_(rs)
24  {}
25 
26 #define DEFINE \
27  template <typename RatExpSet> \
28  inline \
29  auto \
30  ratexpset_wrapper<RatExpSet>
31 
32  DEFINE::down(const value_t& v) const
33  -> typename ratexpset_t::value_t
34  {
35  return down_pointer_cast<const typename ratexpset_t::value_t::element_type>(v);
36  }
37 
38  DEFINE::down(const std::string& w) const -> weight_t
39  {
40  return ::vcsn::conv(*rs_.weightset(), w);
41  }
42 
43  DEFINE::make_ratexp(const value_t& v) const
44  -> dyn::ratexp
45  {
46  return dyn::make_ratexp(rs_, down(v));
47  }
48 
50  {
51  return rs_.identities();
52  }
53 
54  DEFINE::zero() const -> value_t
55  {
56  return rs_.zero();
57  }
58 
59  DEFINE::one() const -> value_t
60  {
61  return rs_.one();
62  }
63 
64  DEFINE::atom(const std::string& w) const -> value_t
65  {
66  return rs_.atom(::vcsn::conv(*rs_.labelset(), w));
67  }
68 
69  DEFINE::add(value_t l, value_t r) const -> value_t
70  {
71  return rs_.add(down(l), down(r));
72  }
73 
74  DEFINE::mul(value_t l, value_t r) const -> value_t
75  {
76  return rs_.mul(down(l), down(r));
77  }
78 
80  {
81  return rs_.conjunction(down(l), down(r));
82  }
83 
85  {
86  return rs_.shuffle(down(l), down(r));
87  }
88 
90  {
91  return rs_.ldiv(down(l), down(r));
92  }
93 
94  DEFINE::rdiv(value_t l, value_t r) const -> value_t
95  {
96  return rs_.rdiv(down(l), down(r));
97  }
98 
99  DEFINE::concat(value_t l, value_t r) const -> value_t
100  {
101  return rs_.concat(down(l), down(r));
102  }
103 
105  {
106  return rs_.star(down(v));
107  }
108 
110  {
111  return rs_.complement(down(v));
112  }
113 
115  {
116  return rs_.transposition(down(v));
117  }
118 
119  DEFINE::lmul(const std::string& w, value_t v) const -> value_t
120  {
121  return rs_.lmul(down(w), down(v));
122  }
123 
124  DEFINE::rmul(value_t v, const std::string& w) const -> value_t
125  {
126  return rs_.rmul(down(v), down(w));
127  }
128 
129  DEFINE::letter_class(const letter_class_t& cs, bool accept) const -> value_t
130  {
131  using labelset_t = labelset_t_of<ratexpset_t>;
132  return letter_class_<labelset_t>(cs, accept,
133  std::is_same<labelset_t, vcsn::oneset>{},
134  std::integral_constant<bool,
135  labelset_t::is_ratexpset()>{});
136  }
137 
138  template <typename RatExpSet>
139  template <typename LabelSet_>
140  inline
141  auto
143  bool,
144  std::false_type,
145  std::true_type) const
146  -> value_t
147  {
148  raise("not implemented");
149  }
150 
151  template <typename RatExpSet>
152  template <typename LabelSet_>
153  inline
154  auto
156  bool accept,
157  std::false_type,
158  std::false_type) const
159  -> value_t
160  {
161  auto ls = *rs_.labelset();
162 
163  using labelset_t = decltype(ls);
164  using word_t = typename labelset_t::word_t;
165  using label_t = typename labelset_t::value_t;
166  using letter_t = typename labelset_t::letter_t;
167 
168  std::set<std::pair<letter_t, letter_t>> ccs;
169  for (auto cc: chars)
170  {
171  // Yes, this is ugly: to convert an std::string into a
172  // letter_t, we (i) parse the string into a label, (ii)
173  // convert it into a word_t, (iii) from which we extract its
174  // first letter.
175  label_t lbl1 = ::vcsn::conv(ls, cc.first);
176  label_t lbl2 = ::vcsn::conv(ls, cc.second);
177  word_t w1 = ls.word(lbl1);
178  word_t w2 = ls.word(lbl2);
179  letter_t l1 = *std::begin(ls.letters_of(w1));
180  letter_t l2 = *std::begin(ls.letters_of(w2));
181  ccs.emplace(l1, l2);
182  }
183  return rs_.letter_class(ccs, accept);
184  }
185 
186  template <typename RatExpSet>
187  template <typename LabelSet_, typename Bool>
188  inline
189  auto
191  bool,
192  std::true_type,
193  Bool) const
194  -> value_t
195  {
196  return one();
197  }
198 
199  DEFINE::conv(std::istream& is) const -> value_t
200  {
201  return rs_.conv(is);
202  }
203 
204  DEFINE::print(value_t v, std::ostream& o) const -> std::ostream&
205  {
206  return rs_.print(down(v), o);
207  }
208 
209 #undef DEFINE
210 
211  } // namespace detail
212 
213  template <typename RatExpSet>
214  inline
215  ratexpset
216  make_ratexpset(const RatExpSet& rs)
217  {
218  using wrapper_t = detail::ratexpset_wrapper<RatExpSet>;
219  return std::make_shared<wrapper_t>(rs);
220  }
221 
222 } // namespace dyn
223 } // namespace vcsn
variadic< type_t::ldiv, Context > ldiv
Definition: fwd.hh:132
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
Definition: ratexp.hh:85
unary< type_t::complement, Context > complement
Definition: fwd.hh:110
label_t_of< context_t > label_t
Definition: ratexpset.hh:105
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
ratexpset make_ratexpset(const context &ctx,::vcsn::rat::identities is)
Build an ratexpset from its context.
Definition: make-context.cc:57
unary< type_t::transposition, Context > transposition
Definition: fwd.hh:116
Wrapper around a ratexpset.
Definition: fwd.hh:71
rat::identities identities(const ratexp &exp)
Bridge.
Definition: identities.hh:26
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
auto conv(const ValueSet &vs, const std::string &str, Args &&...args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
Definition: stream.hh:29
std::ostream & print(const ValueSet &vs, const typename ValueSet::value_t &v, std::ostream &o, const std::string &format)
Applies to (ValueSet, Value, ostream, string): for expansionset, polynomialset, ratexpset, and weightset.
Definition: print.hh:51
#define down_pointer_cast
Definition: cast.hh:16
unary< type_t::star, Context > star
Definition: fwd.hh:113
std::shared_ptr< const detail::ratexpset_base > ratexpset
Definition: fwd.hh:73
ratexpset_wrapper(const ratexpset_t &rs)
Constructor.
Definition: ratexpset.hxx:21
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
constant< type_t::one, Context > one
Definition: fwd.hh:100
constant< type_t::zero, Context > zero
Definition: fwd.hh:97
weight_t_of< context_t > weight_t
Definition: ratexpset.hh:106
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
Abstract wrapper around a (typeful) ratexpset.
Definition: ratexpset.hh:25
variadic< type_t::shuffle, Context > shuffle
Definition: fwd.hh:135
std::set< std::pair< std::string, std::string >> letter_class_t
Definition: ratexpset.hh:78
identities
A ratexpset can implement several different sets of identities on expressions.
Definition: identities.hh:17
value_t letter_class_(const letter_class_t &chars, bool accept, std::true_type, Bool) const
If context is oneset.
variadic< type_t::conjunction, Context > conjunction
Definition: fwd.hh:129