CUBA
pda.hh
1 
8 #ifndef DS_PDA_HH_
9 #define DS_PDA_HH_
10 
11 #include "utilities.hh"
12 #include "prop.hh"
13 
14 namespace ruba {
15 
17 using pda_state = uint;
19 using pda_alpha = uint;
20 
29 class alphabet {
30 public:
31  alphabet() {
32  }
33  ~alphabet() {
34  }
35 
36  const static pda_alpha EPSILON;
37  const static pda_alpha NULLPTR;
38  const static char OPT_EPSILON;
39 };
40 
48  PUSH, POP, OVERWRITE
49 };
50 
57 inline ostream& operator<<(ostream& os, const type_stack_operation& t) {
58  switch (t) {
59  case type_stack_operation::PUSH:
60  os << '-';
61  break;
62  case type_stack_operation::POP:
63  os << '-';
64  break;
65  default:
66  os << '-';
67  break;
68  }
69  return os;
70 }
71 
79  FORK, NORM, BRCT
80 };
81 
88 inline ostream& operator<<(ostream& os, const type_synchronization& t) {
89  switch (t) {
90  case type_synchronization::FORK:
91  os << '+';
92  break;
93  case type_synchronization::BRCT:
94  os << '~';
95  break;
96  default:
97  os << '-';
98  break;
99  }
100  return os;
101 }
102 
106 template<typename T1, typename T2> class transition {
107 public:
108  transition(const T1& src, const T2& dst, const type_stack_operation& op) :
109  src(src), dst(dst), op(op) {
110  }
111  ~transition() {
112  }
113 
114  T1 get_src() const {
115  return src;
116  }
117 
118  T2 get_dst() const {
119  return dst;
120  }
121 
122  type_stack_operation get_oper_type() const {
123  return op;
124  }
125 
126 private:
127  T1 src;
128  T2 dst;
130 };
131 
138 template<typename T1, typename T2> inline ostream& operator<<(ostream& os,
139  const transition<T1, T2>& r) {
140  os << r.get_src() << " ";
141  os << "->";
142  os << " " << r.get_dst();
143  return os;
144 }
145 
154 
159 public:
161  thread_visible_state(const pda_state& s, const pda_alpha& l);
163 
164  static pda_state S;
165 
170  return l;
171  }
172 
177  return s;
178  }
179 
180 private:
181  pda_state s;
182  pda_alpha l;
183 };
184 
191 inline ostream& operator<<(ostream& os, const thread_visible_state& t) {
192  os << "(" << t.get_state() << ",";
193  if (t.get_alpha() == alphabet::EPSILON)
194  os << alphabet::OPT_EPSILON;
195  else
196  os << t.get_alpha();
197  os << ")";
198  return os;
199 }
200 
207 inline bool operator<(const thread_visible_state& t1,
208  const thread_visible_state& t2) {
209  if (t1.get_state() == t2.get_state())
210  return t1.get_alpha() < t2.get_alpha();
211  return t1.get_state() < t2.get_state();
212 }
213 
220 inline bool operator>(const thread_visible_state& t1,
221  const thread_visible_state& t2) {
222  return t2 < t1;
223 }
224 
231 inline bool operator==(const thread_visible_state& t1,
232  const thread_visible_state& t2) {
233  return (t1.get_state() == t2.get_state())
234  && (t1.get_alpha() == t2.get_alpha());
235 }
236 
243 inline bool operator!=(const thread_visible_state& t1,
244  const thread_visible_state& t2) {
245  return !(t1 == t2);
246 }
247 
251 template<typename T> class sstack {
252 public:
253  inline sstack() :
254  worklist() {
255  }
256 
257  inline sstack(const deque<T>& worklist) :
258  worklist(worklist) {
259  }
260 
261  inline ~sstack() {
262  }
263 
264  T top() {
265  return worklist.front();
266  }
267 
268  T top() const {
269  if (worklist.empty())
270  throw cuba_runtime_error("Stack is empty!");
271  return worklist.front();
272  }
273 
274  void push(const T& _value) {
275  worklist.emplace_front(_value);
276  }
277 
278  size_t size() const {
279  return worklist.size();
280  }
281 
282  bool pop() {
283  if (worklist.empty())
284  return false;
285  worklist.pop_front();
286  return true;
287  }
288 
289  bool overwrite(const T& _value) {
290  if (worklist.empty())
291  return false;
292  worklist[0] = _value;
293  return true;
294  }
295 
296  bool empty() const {
297  return worklist.empty();
298  }
299 
300  const deque<T>& get_worklist() const {
301  return worklist;
302  }
303 
304 private:
305  deque<T> worklist;
306 };
307 
314 template<typename T> inline ostream& operator<<(ostream& os,
315  const sstack<T>& a) {
316  if (a.size() == 0) {
317  os << alphabet::OPT_EPSILON;
318  } else {
319  for (const T& s : a.get_worklist()) {
320  if (s == alphabet::EPSILON)
321  os << alphabet::OPT_EPSILON;
322  else
323  os << s;
324  }
325  }
326  return os;
327 }
328 
335 template<typename T> inline bool operator<(const sstack<T>& a1,
336  const sstack<T>& a2) {
337  if (a1.get_worklist().size() == a2.get_worklist().size()) {
338  auto ia1 = a1.get_worklist().cbegin();
339  auto ia2 = a2.get_worklist().cbegin();
340  while (ia1 != a1.get_worklist().cend()) {
341  if (*ia1 < *ia2) {
342  return true;
343  } else if (*ia1 > *ia2) {
344  return false;
345  } else {
346  ++ia1, ++ia2;
347  }
348  }
349  return false;
350  }
351  return a1.get_worklist().size() < a2.get_worklist().size();
352 }
353 
360 template<typename T> inline bool operator>(const sstack<T>& a1,
361  const sstack<T>& a2) {
362  return a2 < a1;
363 }
364 
371 template<typename T> inline bool operator==(const sstack<T>& a1,
372  const sstack<T>& a2) {
373  if (a1.get_worklist().size() != a2.get_worklist().size())
374  return false;
375  auto ia1 = a1.get_worklist().cbegin();
376  auto ia2 = a2.get_worklist().cbegin();
377  while (ia1 != a1.get_worklist().cend()) {
378  if (*ia1 != *ia2)
379  return false;
380  ++ia1, ++ia2;
381  }
382  return true;
383 }
384 
391 template<typename T> inline bool operator!=(const sstack<T>& a1,
392  const sstack<T>& a2) {
393  return !(a1 == a2);
394 }
395 
400 
405 public:
406  thread_state();
407  thread_state(const pda_state& s, const pda_alpha& l);
409  thread_state(const pda_state& s, const pda_stack& w);
410  thread_state(const thread_state& c);
411  ~thread_state();
412 
413  pda_state get_state() const {
414  return s;
415  }
416 
417  const pda_stack& get_stack() const {
418  return w;
419  }
420 
421  thread_visible_state top() const {
422  return thread_visible_state(s, w.top());
423  }
424 
425 private:
426  pda_state s;
427  pda_stack w;
428 };
429 
437 inline ostream& operator<<(ostream& os, const thread_state& c) {
438  os << "(" << c.get_state() << "," << c.get_stack() << ")";
439  return os;
440 }
441 
448 inline bool operator<(const thread_state& c1, const thread_state& c2) {
449  if (c1.get_state() == c2.get_state())
450  return c1.get_stack() < c2.get_stack();
451  return c1.get_state() < c2.get_state();
452 }
453 
460 inline bool operator>(const thread_state& c1, const thread_state& c2) {
461  return c2 < c1;
462 }
463 
470 inline bool operator==(const thread_state& c1, const thread_state& c2) {
471  return (c1.get_state() == c2.get_state())
472  && (c1.get_stack() == c2.get_stack());
473 }
474 
481 inline bool operator!=(const thread_state& c1, const thread_state& c2) {
482  return !(c1 == c2);
483 }
484 
490 using id_action = uint;
491 using adj_list = map<thread_visible_state, deque<id_action>>;
493 
498 public:
500  pushdown_automaton(const set<pda_state>& states,
501  const set<pda_alpha>& alphas,
502  const vector<pda_action>& actions,
503  const adj_list& program);
505 
506  const set<pda_state>& get_states() const {
507  return states;
508  }
509 
510  const set<pda_alpha>& get_alphas() const {
511  return alphas;
512  }
513 
514  const vector<pda_action>& get_actions() const {
515  return actions;
516  }
517 
518  const adj_list& get_program() const {
519  return program;
520  }
521 
522 private:
523  set<pda_state> states;
524  set<pda_alpha> alphas;
525  vector<pda_action> actions;
526  adj_list program;
527 };
528 
535 inline ostream& operator<<(ostream& os, const pushdown_automaton& PDA) {
536  for (auto s : PDA.get_states()) {
537  os << s << " ";
538  }
539  os << "\n";
540  for (auto r : PDA.get_actions()) {
541  os << r << "\n";
542  }
543  os << "\n";
544  return os;
545 }
546 
547 } /* namespace ruba */
548 
549 #endif /* DS_PDA_HH_ */
uint pda_alpha
define the stack symbol of PDSs
Definition: pda.hh:19
~thread_visible_state()
Definition: cpda.cc:34
customized runtime error class
Definition: excep.hh:24
~thread_state()
Definition: cpda.cc:82
Definition: pda.hh:106
Definition: cpda.cc:10
uint id_action
Definition: pda.hh:490
bool operator>(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:108
bool operator==(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:118
pda_alpha get_alpha() const
Definition: pda.hh:169
pushdown_automaton()
Definition: pda.cc:19
bool operator!=(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:131
thread_state()
Definition: cpda.cc:40
Definition: pda.hh:404
pda_state get_state() const
Definition: pda.hh:176
ostream & operator<<(ostream &os, const visible_state &s)
Definition: cpda.hh:70
Definition: pda.hh:497
uint pda_state
define the control state of PDSs
Definition: pda.hh:17
bool operator<(const visible_state &s1, const visible_state &s2)
Definition: cpda.hh:95
Definition: pda.hh:251
Definition: pda.hh:29
type_synchronization
Definition: pda.hh:78
type_stack_operation
Definition: pda.hh:47
Definition: pda.hh:158
~pushdown_automaton()
Definition: pda.cc:41
thread_visible_state()
Definition: cpda.cc:17