Awali
Another Weighted Automata library
int.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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  static std::string sname()
38  {
39  return "int";
40  }
41 
42  static std::ostream& jsname(std::ostream& o)
43  {
44  o << '"' << "Integer" << '"';
45  return o;
46  }
47 
48  virtual std::string vname(bool = true) const
49  {
50  return sname();
51  }
52 
53  static letter_t
55  {
56  return l;
57  }
58 
59  static bool
60  equals(const letter_t& l1, const letter_t& l2)
61  {
62  return l1 == l2;
63  }
64 
65  static bool
67  {
68  return true;
69  }
70 
73  static constexpr letter_t one_letter() { return std::numeric_limits<letter_t>::min(); }
74 
78 
79  static const std::string& separation_mark(){
80  static const std::string sep{":"};
81  return sep;
82  }
83 
84  static std::ostream&
85  print(const letter_t& l, std::ostream& o)
86  {
87  if (l != one_letter() && l != special_letter())
88  o << l;
89  return o;
90  }
91 
92  static letter_t
93  parse_one_letter(const std::string& s, size_t& p) {
94  letter_t r=z::parse(s,p);
95  /* Warning
96  * In order to avoid ambiguity in the parsing of ratexp,
97  * the '+' is excluded from the int
98  */
99  if(s[p]=='+')
100  ++p;
101  return r;
102  }
103 
104 
105  static letter_t
106  conv_one_letter(std::istream& i)
107  {
108  return z::conv(i);
109  }
110 
111  static std::string
112  format(const letter_t l)
113  {
114  if (l == one_letter() || l == special_letter())
115  return {};
116  else
117  return std::to_string(l);
118  }
119 
120 
121  template<unsigned version = version::fsm_json>
123  const
124  {
125  switch (version) {
126  case 0:
127  throw parse_exception("[char] Unsupported fsm-json version:"
128  + std::to_string(version));
129  case 1:
130  default:
131  return new json::string_t("Integer");
132 
133  }
134  }
135 
136 
137  template<unsigned version = version::fsm_json>
138  letter_t
140  const
141  {
142  switch (version) {
143  case 0:
144  throw parse_exception("[int] Unsupported fsm-json version:"
145  + std::to_string(version));
146  case 1:
147  default:
148  return p->to_int();
149  }
150  }
151 
152 
153  template<unsigned version = version::fsm_json>
155  const
156  {
157  switch (version) {
158  case 0:
159  throw parse_exception("[int] Unsupported fsm-json version:"
160  + std::to_string(version));
161  case 1:
162  default:
163  return new json::int_t(l);
164  }
165  }
166 
167  };
168 
169  }
170 }//end of ns awali::stc
171 
172 #endif // !AWALI_ALPHABETS_INT_HH
Definition: node.hh:483
Definition: node.hh:191
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:319
Definition: node.hh:526
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:77
int letter_t
Definition: int.hh:34
static bool is_letter(const letter_t &)
Definition: int.hh:66
static letter_t transpose(letter_t l)
Definition: int.hh:54
static letter_t parse_one_letter(const std::string &s, size_t &p)
Definition: int.hh:93
json::node_t * to_json() const
Definition: int.hh:122
static std::string format(const letter_t l)
Definition: int.hh:112
static constexpr letter_t one_letter()
The reserved letter used to forge the "one" label (the unit, the identity).
Definition: int.hh:73
virtual std::string vname(bool=true) const
Definition: int.hh:48
json::int_t * letter_to_json(letter_t const &l) const
Definition: int.hh:154
static const std::string & separation_mark()
Definition: int.hh:79
std::basic_string< int > word_t
Definition: int.hh:35
static bool equals(const letter_t &l1, const letter_t &l2)
Definition: int.hh:60
static std::ostream & jsname(std::ostream &o)
Definition: int.hh:42
static std::ostream & print(const letter_t &l, std::ostream &o)
Definition: int.hh:85
static letter_t conv_one_letter(std::istream &i)
Definition: int.hh:106
static std::string sname()
Definition: int.hh:37
letter_t letter_from_json(json::node_t const *p) const
Definition: int.hh:139
The semiring of floating Numbers.
Definition: r.hh:34
static value_t parse(const std::string &s, size_t &p)
Definition: z.hh:177
static value_t conv(self_type, value_t v)
Definition: z.hh:157
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