Awali
Another Weighted Automata library
letterset.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_LABELSET_LETTERSET_HH
18 # define AWALI_LABELSET_LETTERSET_HH
19 
20 # include <memory>
21 
22 #include <awali/sttc/alphabets/setalpha.hh> // intersection
23 #include <awali/sttc/core/kind.hh>
24 #include <awali/common/json/node.cc>
27 #include <awali/sttc/misc/raise.hh>
28 #include <awali/common/parse_exception.cc>
29 # include <sstream>
30 #include <awali/sttc/misc/set.hh> // intersection
31 //#include <awali/sttc/labelset/genset_labelset.hh>
32 //#include <awali/sttc/labelset/labelset.hh>
34 
36 
37 namespace awali {
38  namespace sttc {
39 
41  template <typename GenSet>
42  class letterset
43  {
44  public:
45  using genset_t = GenSet;
46  using genset_ptr = std::shared_ptr<const genset_t>;
48 
49  using letter_t = typename genset_t::letter_t;
51  using letters_t = std::set<letter_t>;
52 
53  using value_t = letter_t;
54 
56 
57  private:
58  genset_ptr genset_;
59  public:
60  letterset(const genset_t& gs = {})
61  : genset_(std::make_shared<const genset_t>(gs))
62  {}
63 
64  letterset(const std::initializer_list<letter_t>& letters)
65  : genset_(std::make_shared<const genset_t>(letters))
66  {}
67 
68  letterset(const letters_t& letters)
69  : genset_(std::make_shared<const genset_t>(letters))
70  {}
71 
72  const genset_t& genset() const {
73  return *genset_;
74  }
75 
76  static std::string sname()
77  {
78  return "lal_" + genset_t::sname();
79  }
80 
81  std::string vname(bool full = true) const
82  {
83  return "lal_" + genset().vname(full);
84  }
85 
86 
88  static letterset make(std::istream& is)
89  {
90  // name: lal_char(abc).
91  // ^^^ ^^^^^^^^^
92  // kind genset
93  kind_t::make(is);
94  eat(is, '_');
95  auto gs = genset_t::make(is);
96  return gs;
97  }
98 
102  bool open(bool o) const
103  {
104  return this->genset().open(o);
105  }
106 
107  static constexpr bool is_free()
108  {
109  return true;
110  }
111 
113  template <typename... Args>
114  value_t value(Args&&... args) const
115  {
116  return value_t{std::forward<Args>(args)...};
117  }
118 
120  word_t word(value_t v) const
121  {
122  return {v};
123  }
124 
126  static word_t
128  {
129  return v;
130  }
131 
132  static value_t
133  special() ATTRIBUTE_PURE
134  {
135  return genset_t::special_letter();
136  }
137 
139  static bool
140  equals(const value_t l, const value_t r)
141  {
142  return l == r;
143  }
144 
146  static bool less_than(const value_t l, const value_t r)
147  {
148  return l < r;
149  }
150 
151  static constexpr bool
153  {
154  return false;
155  }
156 
157  static constexpr bool
159  {
160  return false;
161  }
162 
163  static constexpr bool
165  {
166  return true;
167  }
168 
169  static bool
170  is_special(value_t v) ATTRIBUTE_PURE
171  {
172  return v == special();
173  }
174 
175  static constexpr bool
177  {
178  return false;
179  }
180 
181  bool
182  is_valid(value_t v) const
183  {
184  return genset().has(v);
185  }
186 
187  static size_t size(value_t)
188  {
189  return 1;
190  }
191 
192  static size_t hash(value_t v)
193  {
194  return utils::hash_value(v);
195  }
196 
197  static value_t
199  {
200  return v;
201  }
202  /*
203  template<typename T, typename U>
204  static value_t
205  conv(T, U)
206  {
207  std::string message{"Impossible conversion : "};
208  message+=sname()+","+T::sname();
209  throw std::runtime_error(message);
210  }
211  */
213  value_t
214  conv(std::istream& i) const
215  {
216  value_t v=genset().conv_one_letter(i);
217  if(genset().has(v))
218  return v;
219  else
220  throw std::domain_error("invalid label");
221  // : unexpected " +this->genset().to_word (v));
222  }
223 
224  value_t
225  parse(const std::string& s, size_t& p, bool fixed_domain=true) const {
226  value_t v = this->genset().parse_one_letter(s,p);
227  if (genset().has(v))
228  return v;
229  if(!fixed_domain) {
230  const_cast<genset_t&>(*genset_).add_letter(v);
231  return v;
232  }
233  throw std::domain_error("invalid label");
234  }
235 
236  std::set<value_t>
237  convs(std::istream& i) const
238  {
239  std::set<value_t> res;
240  for (auto r : this->convs_(i))
241  res.insert(value_t{r});
242  return res;
243  }
244 
245  std::ostream&
246  print(const value_t& l, std::ostream& o,
247  const std::string& = "text") const
248  {
249  if (!is_special(l))
250  o << genset().format(l);
251  return o;
252  }
253 
254  std::ostream&
255  print_set(std::ostream& o, const std::string& format = "text") const
256  {
257  if (format == "latex")
258  genset().print_set(o, format);
259  else if (format == "text")
260  o << vname(true);
261  else
262  raise("invalid format: ", format);
263  return o;
264  }
265 
266 
267  template<unsigned version = version::fsm_json>
269  version::check_fsmjson<version>();
270  switch (version) {
271  case 0: /* Never occurs due to above check. */
272  case 1:
273  default:
274  json::object_t* obj
275  = this->genset().template to_json<version>()->object();
276  obj->push_front("labelKind", new json::string_t("Letters"));
277  return obj;
278  }
279  }
280 
281 
282  template<unsigned version = version::fsm_json>
284  {
285  return this->genset().template letter_to_json<version>(v);
286  }
287 
288 
289  template<unsigned version = version::fsm_json>
290  value_t
291  value_from_json(json::node_t const* p) const {
292  return genset().template letter_from_json<version>(p);
293  raise("json parser Letterset");
294  }
295 
296 
297  value_t
298  transpose(const value_t& v) const {
299  return genset().transpose(v);
300  }
301  };
302 
303  /*
304  namespace internal
305  {
306  template <typename GenSet>
307  struct law_traits<letterset<GenSet>>
308  {
309  using type = wordset<GenSet>;
310  static type value(const letterset<GenSet>& ls)
311  {
312  return ls.genset();
313  }
314  };
315  }
316  */
317 
319  template <typename GenSet>
320  letterset<GenSet>
321  meet(const letterset<GenSet>& lhs, const letterset<GenSet>& rhs)
322  {
323  return {intersection(lhs.genset(), rhs.genset())};
324  }
325 
327  template <typename GenSet>
328  letterset<GenSet>
329  join(const letterset<GenSet>& lhs, const letterset<GenSet>& rhs)
330  {
331  return {get_union(lhs.genset(), rhs.genset())};
332  }
333 
334  template<typename T, typename U>
335  T join(const T& l, const U&) {
336  std::string message{"Impossible join : "};
337  message+=T::sname()+","+U::sname();
338  throw std::runtime_error(message);
339  }
340  }
341 }//end of ns awali::stc
342 
343 #endif // !AWALI_LABELSET_LETTERSET_HH
Definition: node.hh:193
Definition: node.hh:367
object_t * push_front(std::string key, node_t *node)
Definition: node.hh:529
Implementation of labels are letters.
Definition: letterset.hh:43
std::set< letter_t > letters_t
Definition: letterset.hh:51
static value_t special() ATTRIBUTE_PURE
Definition: letterset.hh:133
value_t transpose(const value_t &v) const
Definition: letterset.hh:298
std::string vname(bool full=true) const
Definition: letterset.hh:81
static size_t hash(value_t v)
Definition: letterset.hh:192
value_t parse(const std::string &s, size_t &p, bool fixed_domain=true) const
Definition: letterset.hh:225
const genset_t & genset() const
Definition: letterset.hh:72
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: letterset.hh:255
static bool is_special(value_t v) ATTRIBUTE_PURE
Definition: letterset.hh:170
static bool equals(const value_t l, const value_t r)
Whether l == r.
Definition: letterset.hh:140
letterset(const genset_t &gs={})
Definition: letterset.hh:60
static constexpr bool has_one()
Definition: letterset.hh:152
bool open(bool o) const
Whether unknown letters should be added, or rejected.
Definition: letterset.hh:102
static size_t size(value_t)
Definition: letterset.hh:187
static letterset make(std::istream &is)
Build from the description in is.
Definition: letterset.hh:88
value_t conv(std::istream &i) const
Read one letter from i, return the corresponding label.
Definition: letterset.hh:214
std::ostream & print(const value_t &l, std::ostream &o, const std::string &="text") const
Definition: letterset.hh:246
bool is_valid(value_t v) const
Definition: letterset.hh:182
typename genset_t::letter_t letter_t
Definition: letterset.hh:49
typename wordset< genset_t >::word_t word_t
Definition: letterset.hh:50
static std::string sname()
Definition: letterset.hh:76
static constexpr bool is_letterized()
Definition: letterset.hh:164
std::shared_ptr< const genset_t > genset_ptr
Definition: letterset.hh:46
letter_t value_t
Definition: letterset.hh:53
static bool less_than(const value_t l, const value_t r)
Whether l < r.
Definition: letterset.hh:146
json::node_t * value_to_json(value_t v) const
Definition: letterset.hh:283
value_t value(Args &&... args) const
Value constructor.
Definition: letterset.hh:114
static constexpr bool is_free()
Definition: letterset.hh:107
word_t word(value_t v) const
Convert to a word.
Definition: letterset.hh:120
letterset(const std::initializer_list< letter_t > &letters)
Definition: letterset.hh:64
std::set< value_t > convs(std::istream &i) const
Definition: letterset.hh:237
static constexpr bool is_ratexpset()
Definition: letterset.hh:158
value_t value_from_json(json::node_t const *p) const
Definition: letterset.hh:291
static word_t letters_of(word_t v)
Prepare to iterate over the letters of v.
Definition: letterset.hh:127
json::node_t * to_json() const
Definition: letterset.hh:268
static constexpr bool is_one(value_t)
Definition: letterset.hh:176
GenSet genset_t
Definition: letterset.hh:45
letterset(const letters_t &letters)
Definition: letterset.hh:68
static value_t conv(self_type, value_t v)
Definition: letterset.hh:198
The semiring of floating Numbers.
Definition: r.hh:35
typename genset_t::word_t word_t
Definition: wordset.hh:42
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:53
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:445
auto meet(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< meet_t< Ctx1, Ctx2 >>
The meet of two ratexpsets.
Definition: ratexpset.hh:434
set_alphabet< L2 > get_union(const set_alphabet< L2 > &lhs, const set_alphabet< L2 > &rhs)
Definition: setalpha.hh:364
set_alphabet< L2 > intersection(const set_alphabet< L2 > &lhs, const set_alphabet< L2 > &rhs)
Definition: setalpha.hh:357
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
std::size_t hash_value(const T &v)
Definition: hash.hh:76
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:42
Main namespace of Awali.
Definition: ato.hh:22
marker type for labelsets where labels are letters
Definition: kind.hh:76
static void make(std::istream &is)
Definition: kind.hh:76