Awali
Another Weighted Automata library
int.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_ALPHABETS_INT_HH
18 # define AWALI_ALPHABETS_INT_HH
19 
20 # include <cassert>
21 # include <string>
22 # include <iostream>
23 #include <awali/common/json/node.cc>
24 # include <awali/sttc/weightset/z.hh>
25 
27 
28 namespace awali {
29  namespace sttc {
32  {
33  public:
34  using letter_t = int;
35  using word_t = std::basic_string<int>;
36 
37  virtual ~int_letters() = default;
38 
39  static std::string sname()
40  {
41  return "int";
42  }
43 
44  static std::ostream& jsname(std::ostream& o)
45  {
46  o << '"' << "Integer" << '"';
47  return o;
48  }
49 
50  virtual std::string vname(bool = true) const
51  {
52  return sname();
53  }
54 
55  static letter_t
57  {
58  return l;
59  }
60 
61  static bool
62  equals(const letter_t& l1, const letter_t& l2)
63  {
64  return l1 == l2;
65  }
66 
67  static bool
69  {
70  return true;
71  }
72 
75  static constexpr letter_t one_letter() { return std::numeric_limits<letter_t>::min(); }
76 
80 
81  static const std::string& separation_mark(){
82  static const std::string sep{":"};
83  return sep;
84  }
85 
86  static std::ostream&
87  print(const letter_t& l, std::ostream& o)
88  {
89  if (l != one_letter() && l != special_letter())
90  o << l;
91  return o;
92  }
93 
94  static letter_t
95  parse_one_letter(const std::string& s, size_t& p) {
96  letter_t r=z::parse(s,p);
97  /* Warning
98  * In order to avoid ambiguity in the parsing of ratexp,
99  * the '+' is excluded from the int
100  */
101  if(s[p]=='+')
102  ++p;
103  return r;
104  }
105 
106 
107  static letter_t
108  conv_one_letter(std::istream& i)
109  {
110  return z::conv(i);
111  }
112 
113  static std::string
114  format(const letter_t l)
115  {
116  if (l == one_letter() || l == special_letter())
117  return {};
118  else
119  return std::to_string(l);
120  }
121 
122 
123  template<unsigned version = version::fsm_json>
125  const
126  {
127  switch (version) {
128  case 0:
129  throw parse_exception("[char] Unsupported fsm-json version:"
130  + std::to_string(version));
131  case 1:
132  default:
133  return new json::string_t("Integer");
134 
135  }
136  }
137 
138 
139  template<unsigned version = version::fsm_json>
140  letter_t
142  const
143  {
144  switch (version) {
145  case 0:
146  throw parse_exception("[int] Unsupported fsm-json version:"
147  + std::to_string(version));
148  case 1:
149  default:
150  return p->to_int();
151  }
152  }
153 
154 
155  template<unsigned version = version::fsm_json>
157  const
158  {
159  switch (version) {
160  case 0:
161  throw parse_exception("[int] Unsupported fsm-json version:"
162  + std::to_string(version));
163  case 1:
164  default:
165  return new json::int_t(l);
166  }
167  }
168 
169  };
170 
171  }
172 }//end of ns awali::stc
173 
174 #endif // !AWALI_ALPHABETS_INT_HH
Definition: node.hh:488
Definition: node.hh:193
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:321
Definition: node.hh:529
helper for manipulations of int letters
Definition: int.hh:32
static constexpr letter_t special_letter()
The reserved letter used to forge the labels for initial and final transitions.
Definition: int.hh:79
int letter_t
Definition: int.hh:34
static bool is_letter(const letter_t &)
Definition: int.hh:68
static letter_t transpose(letter_t l)
Definition: int.hh:56
static letter_t parse_one_letter(const std::string &s, size_t &p)
Definition: int.hh:95
json::node_t * to_json() const
Definition: int.hh:124
static std::string format(const letter_t l)
Definition: int.hh:114
static constexpr letter_t one_letter()
The reserved letter used to forge the "one" label (the unit, the identity).
Definition: int.hh:75
virtual std::string vname(bool=true) const
Definition: int.hh:50
json::int_t * letter_to_json(letter_t const &l) const
Definition: int.hh:156
static const std::string & separation_mark()
Definition: int.hh:81
std::basic_string< int > word_t
Definition: int.hh:35
static bool equals(const letter_t &l1, const letter_t &l2)
Definition: int.hh:62
static std::ostream & jsname(std::ostream &o)
Definition: int.hh:44
virtual ~int_letters()=default
static std::ostream & print(const letter_t &l, std::ostream &o)
Definition: int.hh:87
static letter_t conv_one_letter(std::istream &i)
Definition: int.hh:108
static std::string sname()
Definition: int.hh:39
letter_t letter_from_json(json::node_t const *p) const
Definition: int.hh:141
The semiring of floating Numbers.
Definition: r.hh:35
static value_t parse(const std::string &s, size_t &p)
Definition: z.hh:178
static value_t conv(self_type, value_t v)
Definition: z.hh:158
std::string to_string(identities i)
ATTRIBUTE_CONST int max(int a, int b)
Definition: arith.hh:54
ATTRIBUTE_CONST int min(int a, int b)
Definition: arith.hh:48
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26