Awali
Another Weighted Automata library
oneset.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_ONESET_HH
18 # define AWALI_LABELSET_ONESET_HH
19 
20 # include <iostream>
21 # include <set>
22 # include <stdexcept>
23 
24 #include <awali/common/version.hh>
25 #include <awali/common/json/node.cc>
26 #include <awali/common/parse_exception.cc>
27 #include <awali/sttc/core/kind.hh>
28 #include <awali/sttc/empty.hh>
29 //#include <awali/sttc/labelset/labelset.hh>
30 #include <awali/utils/hash.hh>
31 #include <awali/sttc/misc/raise.hh>
32 
33 namespace awali { namespace sttc {
34 
37  class oneset
38  {
39  public:
40  using self_type = oneset;
41  using value_t = empty_t;
42 
44 
45  oneset() = default;
46 
47  static std::string sname()
48  {
49  return "lao";
50  }
51 
52  static std::string vname(bool = true)
53  {
54  return sname();
55  }
56 
58  static oneset make(std::istream& is)
59  {
60  // name: lao_ratexpset<law_char(xyz)_b>
61  // ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
62  // kind weightset
63  //
64  // There is no "char(...)_".
65  kind_t::make(is);
66  return {};
67  }
68 
70  bool open(bool o) const
71  {
72  return !o;
73  }
74 
75  static constexpr bool is_free()
76  {
77  // This is debatable. However, in Vaucanson, if a labelset
78  // is_free, then we expect to be able to iterate on its genset,
79  // and I don't plan to provide a genset here.
80  return false;
81  }
82 
84  template <typename... Args>
85  value_t value(Args&&... args) const
86  {
87  return value_t{std::forward<Args>(args)...};
88  }
89 
91  static bool
92  equals(const value_t, const value_t)
93  {
94  return true;
95  }
96 
98  static bool less_than(const value_t, const value_t)
99  {
100  return false;
101  }
102 
103  static value_t special()
104  {
105  return {};
106  }
107 
109  constexpr static bool
111  {
112  return true;
113  }
114 
115  static constexpr bool
117  {
118  return false;
119  }
120 
121  static constexpr bool
123  {
124  return true;
125  }
126 
127  static constexpr bool
129  {
130  return true;
131  }
132 
133  static empty_t one()
134  {
135  return {};
136  }
137 
138  static bool is_one(empty_t)
139  {
140  return true;
141  }
142 
144  {
145  return {};
146  }
147 
148  static bool is_letter(empty_t)
149  {
150  return false;
151  }
152 
153  template<typename T>
154  static bool has(const T&)
155  {
156  return false;
157  }
158 
160  {
161  return {};
162  }
163 
164  static std::ostream& print(empty_t, std::ostream& o,
165  const std::string& = "text")
166  {
167  return o;
168  }
169 
170  static size_t size(value_t)
171  {
172  return 0;
173  }
174 
175  static size_t hash(value_t v)
176  {
177  return utils::hash_value(v);
178  }
179 
180  static value_t
182  {
183  return v;
184  }
185 
186  static value_t conv(std::istream& i)
187  {
188  if (i.peek() == '\\')
189  {
190  i.ignore();
191  char c = i.peek();
192  require(c == 'e',
193  "invalid label: unexpected \\", c);
194  i.ignore();
195  }
196  return {};
197  }
198 
199  static std::set<value_t> convs(std::istream&)
200  {
201  raise("oneset: ranges not implemented");
202  }
203 
204  std::ostream&
205  print_set(std::ostream& o, const std::string& format = "text") const
206  {
207  if (format == "latex")
208  o << "1";
209  else if (format == "text")
210  o << vname(true);
211  else
212  raise("invalid format: ", format);
213  return o;
214  }
215 
216  std::set<value_t> genset() const {
217  return std::set<value_t>();
218  }
219 
220 
221  template<unsigned version = version::fsm_json>
222  static
224  {
225  switch (version) {
226  case 0:
227  throw parse_exception("[One] Unsupported fsm-json version: "
228  + std::to_string(version));
229  case 1:
230  default:
231  json::object_t* obj = new json::object_t();
232  obj->push_back("labelKind", new json::string_t("No Label"));
233  return obj;
234  }
235  }
236 
237  template<unsigned version = version::fsm_json>
239  {
240  switch (version) {
241  case 0:
242  throw parse_exception("[One] Unsupported fsm-json version:"
243  + std::to_string(version));
244  case 1:
245  default:
246  return new json::string_t("");
247  }
248  }
249 
250  template<unsigned version = version::fsm_json>
252  switch (version) {
253  case 0:
254  throw parse_exception("[One] Unsupported fsm-json version:"
255  + std::to_string(version));
256  case 1:
257  default:
258  std::string s = p->to_string();
259  require(s.empty(),"json parser Letterset","empty string expected");
260  return one();
261  }
262  }
263 
264  value_t
265  parse(const std::string & s, size_t& p, bool fixed_alphabet=true) const {
266  if ((p>=2) && s[p-2] == '\\' && s[p-1] == 'e')
267  p -= 2;
268  return one();
269  }
270 
271  };
272 
274  inline
275  oneset
276  meet(const oneset&, const oneset&)
277  {
278  return {};
279  }
280 
282  inline
283  oneset
284  join(const oneset&, const oneset&)
285  {
286  return {};
287  }
288 
289 
290 }}//end of ns awali::stc
291 
292 #endif // !AWALI_LABELSET_ONESET_HH
Definition: node.hh:193
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:331
Definition: node.hh:367
object_t * push_back(std::string key, node_t *node)
Definition: node.hh:529
The semiring of complex numbers.
Definition: c.hh:44
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:38
constexpr static bool is_special(value_t)
The special label is indistinguishable for the others.
Definition: oneset.hh:110
empty_t value_t
Definition: oneset.hh:41
static constexpr bool has_one()
Definition: oneset.hh:122
static bool is_one(empty_t)
Definition: oneset.hh:138
value_t parse(const std::string &s, size_t &p, bool fixed_alphabet=true) const
Definition: oneset.hh:265
static value_t conv(self_type, value_t v)
Definition: oneset.hh:181
static size_t hash(value_t v)
Definition: oneset.hh:175
static std::set< value_t > convs(std::istream &)
Definition: oneset.hh:199
static bool less_than(const value_t, const value_t)
Whether l < r.
Definition: oneset.hh:98
static value_t conv(std::istream &i)
Definition: oneset.hh:186
value_t value_from_json(json::node_t const *p) const
Definition: oneset.hh:251
static std::string sname()
Definition: oneset.hh:47
static constexpr bool is_letterized()
Definition: oneset.hh:128
static bool equals(const value_t, const value_t)
Whether l == r.
Definition: oneset.hh:92
static bool is_letter(empty_t)
Definition: oneset.hh:148
std::set< value_t > genset() const
Definition: oneset.hh:216
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: oneset.hh:205
static std::ostream & print(empty_t, std::ostream &o, const std::string &="text")
Definition: oneset.hh:164
static value_t special()
Definition: oneset.hh:103
static constexpr bool is_free()
Definition: oneset.hh:75
static empty_t one()
Definition: oneset.hh:133
static constexpr bool is_ratexpset()
Definition: oneset.hh:116
bool open(bool o) const
Does not make a lot of sense.
Definition: oneset.hh:70
value_t value(Args &&... args) const
Value constructor.
Definition: oneset.hh:85
static empty_t concat(empty_t, empty_t)
Definition: oneset.hh:159
static empty_t transpose(empty_t)
Definition: oneset.hh:143
json::node_t * value_to_json(value_t) const
Definition: oneset.hh:238
static oneset make(std::istream &is)
Build from the description in is.
Definition: oneset.hh:58
static std::string vname(bool=true)
Definition: oneset.hh:52
static bool has(const T &)
Definition: oneset.hh:154
static json::node_t * to_json()
Definition: oneset.hh:223
static size_t size(value_t)
Definition: oneset.hh:170
labelset_description oneset()
std::string to_string(identities i)
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
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
Empty labels, for LAO.
Definition: empty.hh:28
std::size_t hash_value(const T &v)
Definition: hash.hh:76
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26
marker type for labelsets where labels are one
Definition: kind.hh:80
static void make(std::istream &is)
Definition: kind.hh:80