Awali
Another Weighted Automata library
pair.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_PAIR_HH
18 # define AWALI_ALPHABETS_PAIR_HH
19 
20 # include <cassert>
21 # include <string>
22 # include <vector>
23 # include <iostream>
24 # include <utility>
25 # include <functional>
26 #include <awali/common/json/node.cc>
27 # include <awali/sttc/weightset/z.hh>
28 
30 
31 namespace awali {
32  namespace sttc {
34  template<typename F, typename S>
35  class pair_letters {
36  public:
37  using first_letter_t = typename F::letter_t;
38  using second_letter_t = typename S::letter_t;
39  using letter_t = std::pair<first_letter_t, second_letter_t>;
40  using word_t = std::vector<letter_t>;
41 
42  static std::string sname()
43  {
44  return "lap<"+F::sname()+","+S::sname()+">";
45  }
46 
47  static std::ostream& jsname(std::ostream& o)
48  {
49  o << "\"Pair\", \"entryTypes\":[";
50  F::jsname(o) << ',';
51  S::jsname(o) << ']';
52 return o;
53  }
54 
55  virtual std::string vname(bool = true) const
56  {
57  return sname();
58  }
59 
60  static letter_t
62  {
63  l.first=F::transpose(l.first);
64  l.second=S::transpose(l.second);
65  return l;
66  }
67 
68  static bool
69  equals(const letter_t& l1, const letter_t& l2)
70  {
71  return F::equals(l1.first,l2.first) && S::equals(l1.second,l2.second);
72  }
73 
74  static bool
76  {
77  return true;
78  }
79 
82  static letter_t one_letter() { return {F::one_letter(),S::one_letter()};}
83 
86  static letter_t special_letter() { return {F::special_letter(),S::special_letter()};}
87 
88  static const std::string& separation_mark(){
89  static const std::string c;
90  return c;
91  }
92 
93  static std::ostream&
94  print(const letter_t& l, std::ostream& o)
95  {
96  if (l != one_letter() && l != special_letter()) {
97  o << '[';
98  F::print(l.first,o) << ',';
99  S::print(l.second,o) << ']';
100  }
101  return o;
102  }
103 
104  static letter_t
105  parse_one_letter(const std::string& s, size_t& p) {
106  std::string message="Tuple letter expected ] got .";
107  if(s[--p]!=']') {
108  message[28]=s[p];
109  throw parse_exception(p,message);
110  }
111  typename S::letter_t y=S::parse_one_letter(s,p);
112  if(s[--p]!=',') {
113  message[22]=',';
114  message[28]=s[p];
115  throw parse_exception(p,message);
116  }
117  typename F::letter_t x=F::parse_one_letter(s,p);
118  if(s[--p]!='[') {
119  message[22]='[';
120  message[28]=s[p];
121  throw parse_exception(p,message);
122  }
123  return {x,y};
124  }
125 
126 
127  static letter_t
128  conv_one_letter(std::istream& i) {
129  if(i.peek()!='[')
130  throw parse_exception(i,"incorrect tuple");
131  eat(i,'[');
132  typename F::letter_t x=F::conv_one_letter(i);
133  if(i.peek()!=',')
134  throw parse_exception(i,"incorrect tuple");
135  eat(i,',');
136  typename S::letter_t y=S::conv_one_letter(i);
137  if(i.peek()!=']')
138  throw parse_exception(i,"incorrect tuple");
139  eat(i,',');
140  return {x,y};
141  }
142 
143  static std::string
144  format(const letter_t l)
145  {
146  if (l == one_letter() || l == special_letter())
147  return {};
148  else
149  return "["+F::format(l.first)+","+S::format(l.second)+"]";
150  }
151 
152  };
153 
154  }
155 }//end of ns awali::stc
156 
157 #endif // !AWALI_ALPHABETS_PAIR_HH
The semiring of complex numbers.
Definition: c.hh:44
helper for manipulations of pair letters
Definition: pair.hh:35
static const std::string & separation_mark()
Definition: pair.hh:88
static bool is_letter(const letter_t &)
Definition: pair.hh:75
static std::string sname()
Definition: pair.hh:42
virtual std::string vname(bool=true) const
Definition: pair.hh:55
static std::ostream & jsname(std::ostream &o)
Definition: pair.hh:47
typename S::letter_t second_letter_t
Definition: pair.hh:38
static letter_t parse_one_letter(const std::string &s, size_t &p)
Definition: pair.hh:105
static letter_t transpose(letter_t l)
Definition: pair.hh:61
static letter_t conv_one_letter(std::istream &i)
Definition: pair.hh:128
static letter_t one_letter()
The reserved letter used to forge the "one" label (the unit, the identity).
Definition: pair.hh:82
typename F::letter_t first_letter_t
Definition: pair.hh:37
static letter_t special_letter()
The reserved letter used to forge the labels for initial and final transitions.
Definition: pair.hh:86
static std::string format(const letter_t l)
Definition: pair.hh:144
static bool equals(const letter_t &l1, const letter_t &l2)
Definition: pair.hh:69
std::vector< letter_t > word_t
Definition: pair.hh:40
std::pair< first_letter_t, second_letter_t > letter_t
Definition: pair.hh:39
static std::ostream & print(const letter_t &l, std::ostream &o)
Definition: pair.hh:94
automaton_t transpose(automaton_t aut, options_t opts={})
Transposes aut or returns a transposed copy of aut.
std::ostream & print(const std::tuple< Args... > &args, std::ostream &o)
Definition: tuple.hh:254
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
RatExpSet::ratexp_t equals(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: equal_visit.hh:153
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26