Awali
Another Weighted Automata library
mutable_automaton.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 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_CORE_MUTABLE_AUTOMATON_HH
18 # define AWALI_CORE_MUTABLE_AUTOMATON_HH
19 
20 # include <algorithm>
21 # include <cassert>
22 # include <vector>
23 # include <sstream>
24 # include <string>
25 
26 //#include <awali/sttc/core/crange.hh>
27 #include <awali/common/types.hh>
30 #include <awali/sttc/ctx/traits.hh>
35 
36 namespace awali {
37  namespace sttc {
38 
39  namespace internal {
40  template <typename Context>
41  class mutable_automaton_impl;
42  }
43  template <typename Context>
45  = std::shared_ptr<internal::mutable_automaton_impl<Context>>;
46 
47  namespace internal
48  {
49  template <typename Context>
51  {
52  public:
53  using context_t = Context;
59  using kind_t = typename context_t::kind_t;
60 
61  using labelset_ptr = typename context_t::labelset_ptr;
62  using weightset_ptr = typename context_t::weightset_ptr;
63 
65  using label_t = typename labelset_t::value_t;
67  using weight_t = typename weightset_t::value_t;
69  using history_t = std::shared_ptr<history_base>;
70  using names_t = std::shared_ptr<string_history>;
71 
74 
77 
79  using tr_cont_t = std::vector<transition_t>;
80 
83  {
86  };
88  using st_store_t = std::vector<stored_state_t>;
90  using tr_store_t = std::vector<stored_transition_t>;
92  using free_store_t = std::vector<state_t>;
93  private:
94 
95  st_store_t states_;
97  free_store_t states_fs_;
98  tr_store_t transitions_;
100  free_store_t transitions_fs_;
102  label_t prepost_label_;
104  history_t history_;
105  // State names
106  names_t names_;
107  public:
111  : ctx_{ctx}
112  , states_{2}
113  , prepost_label_(ctx.labelset()->special())
114  , history_(std::make_shared<no_history>())
115  , names_(std::make_shared<string_history>())
116  {}
117 
119  : ctx_(that.ctx_)
120  , prepost_label_(that.prepost_label_) {
121  *this = std::move(that);
122  }
123 
125  if (this != &that) {
126  ctx_ = std::move(that.ctx_);
127  prepost_label_ = std::move(that.prepost_label_);
128  std::swap(states_, that.states_);
129  std::swap(states_fs_, that.states_fs_);
130  std::swap(transitions_, that.transitions_);
131  std::swap(transitions_fs_, that.transitions_fs_);
132  history_ = that.history_;
133  names_ = that.names_;
134  }
135  return *this;
136  }
137 
138  // Related sets
140 
141  static std::string sname() {
142  return "mutable_automaton<" + context_t::sname() + ">";
143  }
144 
145  std::string vname(bool full = true) const {
146  return "mutable_automaton<" + context().vname(full) + ">";
147  }
148 
149  const context_t& context() const { return ctx_; }
150  const weightset_ptr& weightset() const { return ctx_.weightset(); }
151  const labelset_ptr& labelset() const { return ctx_.labelset(); }
152 
153  // Special states and transitions
155 
156  // pseudo-initial and final states.
157  // The code below assumes that pre() and post() are the first
158  // two states of the automaton. In other words, all other states
159  // have greater numbers. We also assume that pre()<post().
160  static constexpr state_t pre() { return 0U; }
161  static constexpr state_t post() { return 1U; }
162  // Invalid transition or state.
163  static constexpr state_t null_state() { return -1U; }
164  static constexpr transition_t null_transition() { return -1U; }
165 
167  return prepost_label_;
168  }
169 
170  // Statistics
172 
173  size_t num_all_states() const { return states_.size() - states_fs_.size(); }
174  size_t num_states() const { return num_all_states() - 2; }
175  size_t num_initials() const { return states_[pre()].succ.size(); }
176  size_t num_finals() const { return states_[post()].pred.size(); }
177  size_t num_transitions() const {
178  return (transitions_.size()
179  - transitions_fs_.size() - num_initials() - num_finals());
180  }
181 
182  // Queries on states
184 
185  bool
186  has_state(state_t s) const {
187  // Any number outside our container is not a state.
188  // (This includes "null_state()".)
189  if (s >= states_.size())
190  return false;
191  const stored_state_t& ss = states_[s];
192 
193  // Erased states have 'invalid' as their first successor.
194  return ss.succ.empty() || ss.succ.front() != null_transition();
195  }
196 
197  state_t max_state() const {
198  state_t s=states_.size();
199  for(; !has_state(s); --s)
200  ;
201  return s;
202  }
203 
204  bool
205  is_initial(state_t s) const {
206  return has_transition(pre(), s, prepost_label_);
207  }
208 
209  bool
210  is_final(state_t s) const {
211  return has_transition(s, post(), prepost_label_);
212  }
213 
214  ATTRIBUTE_PURE
215  weight_t
217  transition_t t = get_transition(pre(), s, prepost_label_);
218  if (t == null_transition())
219  return weightset()->zero();
220  else
221  return weight_of(t);
222  }
223 
224  ATTRIBUTE_PURE
225  weight_t
227  transition_t t = get_transition(s, post(), prepost_label_);
228  if (t == null_transition())
229  return weightset()->zero();
230  else
231  return weight_of(t);
232  }
233 
234  // Queries on transitions
236 
238  get_transition(state_t src, state_t dst, label_t l) const {
239  assert(has_state(src));
240  assert(has_state(dst));
241  const tr_cont_t& succ = states_[src].succ;
242  const tr_cont_t& pred = states_[dst].pred;
243  const auto& ls = *this->labelset();
244  if (succ.size() <= pred.size()) {
245  auto i =
246  std::find_if(begin(succ), end(succ),
247  [this,l,ls,dst] (transition_t t) -> bool {
248  const stored_transition_t& st = transitions_[t];
249  return (st.dst == dst
250  && ls.equals(st.get_label(), l));
251  });
252  if (i != end(succ))
253  return *i;
254  }
255  else {
256  auto i =
257  std::find_if(begin(pred), end(pred),
258  [this,l,ls,src] (transition_t t) -> bool {
259  const stored_transition_t& st = transitions_[t];
260  return (st.src == src
261  && ls.equals(st.get_label(), l));
262  });
263  if (i != end(pred))
264  return *i;
265  }
266  return null_transition();
267  }
268 
269  bool
270  has_transition(state_t src, state_t dst, label_t l) const {
271  return get_transition(src, dst, l) != null_transition();
272  }
273 
274  bool
276  // Any number outside our container is not a transition.
277  // (This includes "null_transition()".)
278  if (t >= transitions_.size())
279  return false;
280 
281  // Erased transition have invalid source state.
282  return transitions_[t].src != null_state();
283  }
284 
285  state_t src_of(transition_t t) const { return transitions_[t].src; }
286  state_t dst_of(transition_t t) const { return transitions_[t].dst; }
288  return transitions_[t].get_label();
289  }
290 
292  return transitions_[t].get_weight();
293  }
294 
295  // Edition of states
297 
298  protected:
300  void
302  stored_transition_t& st = transitions_[t];
303  auto& succ = states_[st.src].succ;
304  auto tsucc = std::find(succ.begin(), succ.end(), t);
305  assert(tsucc != succ.end());
306  *tsucc = std::move(succ.back());
307  succ.pop_back();
308  }
309 
311  void
313  stored_transition_t& st = transitions_[t];
314  auto& pred = states_[st.dst].pred;
315  auto tpred = std::find(pred.begin(), pred.end(), t);
316  assert(tpred != pred.end());
317  *tpred = std::move(pred.back());
318  pred.pop_back();
319  }
320 
321  void
322  del_transition_container(tr_cont_t& tc, bool from_succ) {
323  for (auto t: tc) {
324  if (from_succ)
326  else
328  transitions_[t].src = null_state();
329  }
330  transitions_fs_.insert(transitions_fs_.end(), tc.begin(), tc.end());
331  tc.clear();
332  }
333 
334  public:
335  state_t
337  state_t s;
338  if (states_fs_.empty()) {
339  s = states_.size();
340  states_.resize(s + 1);
341  }
342  else {
343  s = states_fs_.back();
344  states_fs_.pop_back();
345  }
346  stored_state_t& ss = states_[s];
347  // De-invalidate this state: remove the invalid transition.
348  ss.succ.clear();
349  return s;
350  }
351 
352  void
354  state_t s = states_.size();
355  assert(!has_state(index));
356  if (index >= s) {
357  states_.resize(index + 1);
358  for(state_t t=s; t< index; t++) {
359  states_[t].succ.emplace_back(null_transition()); // So has_state() can work.
360  states_fs_.emplace_back(t);
361  }
362  }
363  stored_state_t& ss = states_[index];
364  // De-invalidate this state: remove the invalid transition.
365  ss.succ.clear();
366  }
367 
368  history_t history() const {
369  return history_;
370  }
371 
373  history_ = h;
374  }
375 
376  void strip_history() {
377  history_ = std::make_shared<no_history>();
378  }
379 
380  void strip_names() {
381  names_ = std::make_shared<string_history>();
382  }
383 
384  std::ostream& print_state(state_t s, std::ostream& o) const {
385  if(names_->has_history(s))
386  return names_->print_state_name(s, o, "text");
387  if(s == pre())
388  return o << "_";
389  if(s == post())
390  return o << "_";
391  return o << '$' << (s-2);
392  }
393 
394  std::ostream& print_state_name(state_t s, std::ostream& o,
395  const std::string& = "text") const {
396  return print_state(s, o);
397  }
398 
399  std::string get_state_name(state_t s) const {
400  if(names_->has_history(s))
401  return names_->get_state_name(s);
402  std::ostringstream o;
403  print_state(s, o);
404  return o.str();
405  }
406 
407  std::ostream& print_state_history(state_t s, std::ostream& o,
408  const std::string& fmt = "text") const {
409  if(history_->has_history(s))
410  return history_->print_state_name(s, o, fmt);
411  return print_state_name(s, o, fmt);
412  }
413 
414  bool has_history(state_t s) const {
415  return history_->has_history(s);
416  }
417 
418  bool has_name(state_t s) const {
419  return names_->has_history(s);
420  }
421 
423  for(auto s : states())
424  if(history_->has_history(s)) {
425  std::ostringstream o;
426  print_state_history(s, o);
427  set_state_name(s,o.str());
428  }
429  }
430 
431  bool has_explicit_name(state_t s) const {
432  return names_->has_history(s);
433  }
434 
435  void set_state_name(state_t s, const std::string& n) {
436  names_->add_state(s, n);
437  }
438 
439  state_t get_state_by_name(const std::string& name) const {
440  for(auto i : states()) {
441  std::ostringstream os;
442  print_state_name(i,os);
443  if(os.str()==name)
444  return i;
445  }
446  return null_state();
447  }
448 
449  bool set_name(const std::string& n) {
450  if(n.length()>0 && n[0]=='$')
451  return false;
452  names_->set_name(n);
453  return true;
454  }
455 
456  void set_desc(const std::string& d) {
457  names_->set_desc(d);
458  }
459 
460  const std::string& get_name() const {
461  return names_->get_name();
462  }
463 
464  const std::string& get_desc() const {
465  return names_->get_desc();
466  }
467 
468 
469  void del_state(state_t s) {
470  assert(has_state(s));
471  assert(s > post()); // Cannot erase pre() and post().
472  stored_state_t& ss = states_[s];
473  del_transition_container(ss.pred, false);
474  del_transition_container(ss.succ, true);
475  history_->remove_history(s);
476  names_->remove_history(s);
477  ss.succ.emplace_back(null_transition()); // So has_state() can work.
478  states_fs_.emplace_back(s);
479  }
480 
482  set_transition(pre(), s, prepost_label_, w);
483  }
484 
486  set_initial(s, weightset()->one());
487  }
488 
490  return add_transition(pre(), s, prepost_label_, w);
491  }
492 
494  set_initial(s, weightset()->zero());
495  }
496 
498  set_transition(s, post(), prepost_label_, w);
499  }
500 
501  void set_final(state_t s) {
502  set_final(s, weightset()->one());
503  }
504 
506  return add_transition(s, post(), prepost_label_, w);
507  }
508 
510  set_final(s, weightset()->zero());
511  }
512 
513  // Edition of transitions
515 
516  void
518  {
519  assert(has_transition(t));
520  // Remove transition from source and destination.
523  // Actually erase the transition.
524  transitions_[t].src = null_state();
525  transitions_fs_.emplace_back(t);
526  }
527 
531  {
532  transition_t t = get_transition(src, dst, l);
533  if (t != null_transition())
534  del_transition(t);
535  return t;
536  }
537 
539  void
541  {
542  // Make a copy of the transition indexes, as the iterators are
543  // invalidated by del_transition(t).
544  auto ts = outin(s, d);
545  for (auto t: tr_cont_t{ts.begin(), ts.end()})
546  del_transition(t);
547  }
548 
561  {
562  assert(!has_transition(src, dst, l));
563  if (weightset()->is_zero(w))
564  return null_transition();
565  else
566  {
567  transition_t t;
568  if (transitions_fs_.empty())
569  {
570  t = transitions_.size();
571  transitions_.resize(t + 1);
572  }
573  else
574  {
575  t = transitions_fs_.back();
576  transitions_fs_.pop_back();
577  }
578  stored_transition_t& st = transitions_[t];
579  st.src = src;
580  st.dst = dst;
581  // FIXME: When src == pre() || dst == post(), label must be empty.
582  st.set_label(l);
583  st.set_weight(w);
584  states_[src].succ.emplace_back(t);
585  states_[dst].pred.emplace_back(t);
586  return t;
587  }
588  }
589 
603  template <typename A>
606  const A& aut, transition_t t,
607  bool transpose = false)
608  {
609  auto l = aut->label_of(t);
610  auto w = aut->weight_of(t);
611  if (transpose)
612  {
613  l = aut->labelset()->transpose(l);
614  w = aut->weightset()->transpose(w);
615  }
616  return new_transition(src, dst,
617  labelset()->conv(*aut->labelset(), l),
618  weightset()->conv(*aut->weightset(), w));
619  }
620 
624  {
625  return new_transition(src, dst, l, weightset()->one());
626  }
627 
640  {
641  // It's illegal to connect pre() to post().
642  // FIXME: reenable except for labels_are_one.
643  // assert(src != pre() || dst != post());
644  // It's illegal to leave post().
645  assert(src != post());
646  // It's illegal to go to pre().
647  assert(dst != pre());
648  transition_t t = get_transition(src, dst, l);
649  if (t == null_transition())
650  t = new_transition(src, dst, l, w);
651  else if (weightset()->is_zero(w))
652  {
653  del_transition(t);
654  t = null_transition();
655  }
656  else
657  {
658  stored_transition_t& st = transitions_[t];
659  st.set_label(l);
660  st.set_weight(w);
661  }
662  return t;
663  }
664 
668  {
669  return set_transition(src, dst, l, weightset()->one());
670  }
671 
672  template <typename A>
675  const A& aut, transition_t t,
676  bool transpose = false)
677  {
678  auto l = aut->label_of(t);
679  auto w = aut->weight_of(t);
680  if (transpose)
681  {
682  l = aut->labelset()->transpose(l);
683  w = aut->weightset()->transpose(w);
684  }
685  return set_transition(src, dst,
686  labelset()->conv(*aut->labelset(), l),
687  weightset()->conv(*aut->weightset(), w));
688  }
689 
700  weight_t
702  {
703  transition_t t = get_transition(src, dst, l);
704  if (t == null_transition())
705  new_transition(src, dst, l, w);
706  else
707  {
708  w = weightset()->add(weight_of(t), w);
709  set_weight(t, w);
710  }
711  return w;
712  }
713 
715  weight_t
717  {
718  return add_transition(src, dst, l, weightset()->one());
719  }
720 
732  template <typename A>
733  weight_t
735  const A& aut, transition_t t,
736  bool transpose = false)
737  {
738  auto l = aut->label_of(t);
739  auto w = aut->weight_of(t);
740  if (transpose)
741  {
742  l = aut->labelset()->transpose(l);
743  w = aut->weightset()->transpose(w);
744  }
745  return add_transition(src, dst,
746  labelset()->conv(*aut->labelset(), l),
747  weightset()->conv(*aut->weightset(), w));
748  }
749 
750  weight_t
752  {
753  if (weightset()->is_zero(w))
754  del_transition(t);
755  else
756  transitions_[t].set_weight(w);
757  return w;
758  }
759 
760  weight_t
762  {
763  return set_weight(t, weightset()->add(weight_of(t), w));
764  }
765 
766  weight_t
768  {
769  return set_weight(t, weightset()->mul(w, weight_of(t)));
770  }
771 
772  weight_t
774  {
775  return set_weight(t, weightset()->mul(weight_of(t), w));
776  }
777 
778  // Iteration on states and transitions
780 
782 
786  states() const {
787  return states_output_t(states_, [this] (const stored_state_t& ss) -> bool
788  {
789  return (ss.succ.empty()
790  || ss.succ.front() != this->null_transition());
791  }, 2);
792  }
796  all_states() const {
797  return states_output_t(states_, [this] (const stored_state_t& ss) -> bool
798  {
799  return (ss.succ.empty()
800  || ss.succ.front() != this->null_transition());
801  });
802  }
803 
805 
808  transitions() const
809  {
810  return transitions_output_t (transitions_, [this] (const stored_transition_t& tr) -> bool
811  {
812  state_t src = tr.src;
813  if (src == this->null_state() || src == this->pre())
814  return false;
815  return tr.dst != this->post();
816  });
817  }
818 
822  {
823  return transitions_output_t(transitions_, [this] (const stored_transition_t& tr) -> bool
824  {
825  return tr.src != this->null_state();
826  });
827  }
828 
830 
834  {
835  return out(pre());
836  }
837 
841  {
842  return in(post());
843  }
844 
848  out(state_t s) const
849  {
850  assert(has_state(s));
851  return transitions_s_output_t(states_[s].succ, [this] (const transition_t& i) -> bool
852  {
853  return this->transitions_[i].dst != this->post();
854  });
855  }
856 
859  const tr_cont_t&
860  all_out(state_t s) const
861  {
862  assert(has_state(s));
863  return states_[s].succ;
864  }
865 
866  tr_cont_t&
868  {
869  assert(has_state(s));
870  return states_[s].succ;
871  }
872 
876  out(state_t s, const label_t& l) const
877  {
878  assert(has_state(s));
879  return transitions_s_output_t(states_[s].succ, [this,l] (const transition_t& i) -> bool
880  {
881  return this->labelset()->equals(this->transitions_[i].get_label(), l);
882  });
883  }
884 
888  in(state_t s) const
889  {
890  assert(has_state(s));
891  return transitions_s_output_t(states_[s].pred, [this] (const transition_t& i) -> bool
892  { return this->transitions_[i].src != this->pre(); });
893  }
894 
897  const tr_cont_t&
898  all_in(state_t s) const
899  {
900  assert(has_state(s));
901  return states_[s].pred;
902  }
903 
907  in(state_t s, const label_t& l) const
908  {
909  assert(has_state(s));
910  return transitions_s_output_t(states_[s].pred, [this,l] (const transition_t& i) -> bool
911  {
912  return this->labelset()->equals(this->transitions_[i].get_label(), l);
913  });
914  }
915 
919  outin(state_t s, state_t d) const
920  {
921  assert(has_state(s));
922  assert(has_state(d));
923  return transitions_s_output_t(states_[s].succ, [this,d] (const transition_t& i) -> bool
924  { return this->transitions_[i].dst == d; });
925  }
926  };
927  }
928 
929  template <typename Context>
930  mutable_automaton<Context>
931  make_mutable_automaton(const Context& ctx)
932  {
933  return make_shared_ptr<mutable_automaton<Context>>(ctx);
934  }
935  }
936 }//end of ns awali::stc
937 
938 #endif // !AWALI_CORE_MUTABLE_AUTOMATON_HH
Definition: mutable_automaton.hh:51
cont_filter< tr_cont_t > transitions_s_output_t
Definition: mutable_automaton.hh:829
void add_state(state_t index)
Definition: mutable_automaton.hh:353
std::vector< stored_state_t > st_store_t
All the automaton's states.
Definition: mutable_automaton.hh:88
std::string get_state_name(state_t s) const
Definition: mutable_automaton.hh:399
void set_desc(const std::string &d)
Definition: mutable_automaton.hh:456
indice_filter< tr_store_t > transitions_output_t
Definition: mutable_automaton.hh:804
transitions_s_output_t final_transitions() const
Indexes of transitions from visible final states.
Definition: mutable_automaton.hh:840
std::shared_ptr< string_history > names_t
Definition: mutable_automaton.hh:70
void set_initial(state_t s, weight_t w)
Definition: mutable_automaton.hh:481
bool is_final(state_t s) const
Definition: mutable_automaton.hh:210
transitions_s_output_t out(state_t s) const
Indexes of visible transitions leaving state s.
Definition: mutable_automaton.hh:848
const tr_cont_t & all_out(state_t s) const
Indexes of all transitions leaving state s.
Definition: mutable_automaton.hh:860
bool has_history(state_t s) const
Definition: mutable_automaton.hh:414
state_t src_of(transition_t t) const
Definition: mutable_automaton.hh:285
void unset_initial(state_t s)
Definition: mutable_automaton.hh:493
weight_t add_transition(state_t src, state_t dst, label_t l, weight_t w)
Add a transition between two states.
Definition: mutable_automaton.hh:701
states_output_t states() const
All states excluding pre()/post().
Definition: mutable_automaton.hh:786
indice_filter< st_store_t > states_output_t
Definition: mutable_automaton.hh:781
states_output_t all_states() const
All states including pre()/post().
Definition: mutable_automaton.hh:796
transitions_output_t all_transitions() const
All the transition indexes between all states (including pre and post).
Definition: mutable_automaton.hh:821
std::vector< transition_t > tr_cont_t
All the incoming/outgoing transition handles of a state.
Definition: mutable_automaton.hh:79
weight_t weight_of(transition_t t) const
Definition: mutable_automaton.hh:291
bool has_explicit_name(state_t s) const
Definition: mutable_automaton.hh:431
std::ostream & print_state(state_t s, std::ostream &o) const
Definition: mutable_automaton.hh:384
context_t ctx_
The algebraic type of this automaton.
Definition: mutable_automaton.hh:73
void set_final(state_t s)
Definition: mutable_automaton.hh:501
void set_final(state_t s, weight_t w)
Definition: mutable_automaton.hh:497
static constexpr state_t null_state()
Definition: mutable_automaton.hh:163
state_t get_state_by_name(const std::string &name) const
Definition: mutable_automaton.hh:439
ATTRIBUTE_PURE weight_t get_initial_weight(state_t s) const
Definition: mutable_automaton.hh:216
transitions_s_output_t initial_transitions() const
Indexes of transitions to visible initial states.
Definition: mutable_automaton.hh:833
size_t num_transitions() const
Definition: mutable_automaton.hh:177
void del_transition_container(tr_cont_t &tc, bool from_succ)
Definition: mutable_automaton.hh:322
void del_transition(state_t s, state_t d)
Remove all the transitions between s and d.
Definition: mutable_automaton.hh:540
void del_state(state_t s)
Definition: mutable_automaton.hh:469
state_t dst_of(transition_t t) const
Definition: mutable_automaton.hh:286
mutable_automaton_impl(mutable_automaton_impl &&that)
Definition: mutable_automaton.hh:118
transition_t set_transition(state_t src, state_t dst, label_t l)
Same as above, with unit weight.
Definition: mutable_automaton.hh:667
ATTRIBUTE_PURE weight_t get_final_weight(state_t s) const
Definition: mutable_automaton.hh:226
void strip_history()
Definition: mutable_automaton.hh:376
const context_t & context() const
Definition: mutable_automaton.hh:149
static std::string sname()
Definition: mutable_automaton.hh:141
const labelset_ptr & labelset() const
Definition: mutable_automaton.hh:151
typename context_t::kind_t kind_t
Definition: mutable_automaton.hh:59
bool has_transition(transition_t t) const
Definition: mutable_automaton.hh:275
mutable_automaton_impl(const context_t &ctx)
Definition: mutable_automaton.hh:110
state_t max_state() const
Definition: mutable_automaton.hh:197
const std::string & get_name() const
Definition: mutable_automaton.hh:460
weight_t lmul_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:767
static constexpr transition_t null_transition()
Definition: mutable_automaton.hh:164
transition_t new_transition(state_t src, state_t dst, label_t l)
Same as above, with weight one.
Definition: mutable_automaton.hh:623
transition_t get_transition(state_t src, state_t dst, label_t l) const
Definition: mutable_automaton.hh:238
transitions_s_output_t outin(state_t s, state_t d) const
Indexes of visible transitions from state s to state d.
Definition: mutable_automaton.hh:919
size_t num_all_states() const
Definition: mutable_automaton.hh:173
weight_t rmul_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:773
transition_t del_transition(state_t src, state_t dst, label_t l)
Remove the transition (src, l, dst).
Definition: mutable_automaton.hh:530
weight_t add_final(state_t s, weight_t w)
Definition: mutable_automaton.hh:505
bool set_name(const std::string &n)
Definition: mutable_automaton.hh:449
std::vector< state_t > free_store_t
A list of unused indexes in the states/transitions tables.
Definition: mutable_automaton.hh:92
labelset_t_of< context_t > labelset_t
Definition: mutable_automaton.hh:57
label_t label_of(transition_t t) const
Definition: mutable_automaton.hh:287
weight_t add_transition_copy(state_t src, state_t dst, const A &aut, transition_t t, bool transpose=false)
Add a transition between two states, copying the label from the given transition.
Definition: mutable_automaton.hh:734
typename context_t::labelset_ptr labelset_ptr
Definition: mutable_automaton.hh:61
Context context_t
Definition: mutable_automaton.hh:53
void strip_names()
Definition: mutable_automaton.hh:380
transitions_s_output_t in(state_t s) const
Indexes of visible transitions arriving to state s.
Definition: mutable_automaton.hh:888
std::vector< stored_transition_t > tr_store_t
All the automaton's transitions.
Definition: mutable_automaton.hh:90
typename weightset_t::value_t weight_t
Transition weight.
Definition: mutable_automaton.hh:67
mutable_automaton< context_t > automaton_nocv_t
The (shared pointer) type to use it we have to create an automaton of the same (underlying) type.
Definition: mutable_automaton.hh:56
typename labelset_t::value_t label_t
Transition label.
Definition: mutable_automaton.hh:65
size_t num_initials() const
Definition: mutable_automaton.hh:175
mutable_automaton_impl(const mutable_automaton_impl &)=delete
bool is_initial(state_t s) const
Definition: mutable_automaton.hh:205
void set_state_name(state_t s, const std::string &n)
Definition: mutable_automaton.hh:435
history_t history() const
Definition: mutable_automaton.hh:368
weightset_t_of< context_t > weightset_t
Definition: mutable_automaton.hh:58
void set_state_names_from_history()
Definition: mutable_automaton.hh:422
transitions_output_t transitions() const
All the transition indexes between visible states.
Definition: mutable_automaton.hh:808
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &="text") const
Definition: mutable_automaton.hh:394
const std::string & get_desc() const
Definition: mutable_automaton.hh:464
bool has_transition(state_t src, state_t dst, label_t l) const
Definition: mutable_automaton.hh:270
transition_t new_transition_copy(state_t src, state_t dst, const A &aut, transition_t t, bool transpose=false)
Copy the label of a transition between two states, creating a new transition.
Definition: mutable_automaton.hh:605
size_t num_finals() const
Definition: mutable_automaton.hh:176
void set_history(history_t h)
Definition: mutable_automaton.hh:372
static constexpr state_t pre()
Definition: mutable_automaton.hh:160
tr_cont_t succ
Definition: mutable_automaton.hh:84
state_t add_state()
Definition: mutable_automaton.hh:336
const weightset_ptr & weightset() const
Definition: mutable_automaton.hh:150
void del_transition(transition_t t)
Definition: mutable_automaton.hh:517
std::shared_ptr< history_base > history_t
History.
Definition: mutable_automaton.hh:69
bool has_state(state_t s) const
Definition: mutable_automaton.hh:186
void del_transition_from_dst(transition_t t)
Remove t from the ingoing transition of the destination state.
Definition: mutable_automaton.hh:312
transitions_s_output_t out(state_t s, const label_t &l) const
Indexes of all transitions leaving state s on label l.
Definition: mutable_automaton.hh:876
label_t prepost_label() const
Definition: mutable_automaton.hh:166
weight_t add_initial(state_t s, weight_t w)
Definition: mutable_automaton.hh:489
size_t num_states() const
Definition: mutable_automaton.hh:174
transition_t set_transition(state_t src, state_t dst, label_t l, weight_t w)
Set a transition between two states.
Definition: mutable_automaton.hh:639
const tr_cont_t & all_in(state_t s) const
Indexes of all transitions arriving to state s.
Definition: mutable_automaton.hh:898
void unset_final(state_t s)
Definition: mutable_automaton.hh:509
bool has_name(state_t s) const
Definition: mutable_automaton.hh:418
weight_t add_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:761
void set_initial(state_t s)
Definition: mutable_automaton.hh:485
transition_t new_transition(state_t src, state_t dst, label_t l, weight_t w)
Create a transition between two states.
Definition: mutable_automaton.hh:560
mutable_automaton_impl & operator=(mutable_automaton_impl &&that)
Definition: mutable_automaton.hh:124
transitions_s_output_t in(state_t s, const label_t &l) const
Indexes of visible transitions arriving to state s on label l.
Definition: mutable_automaton.hh:907
static constexpr state_t post()
Definition: mutable_automaton.hh:161
tr_cont_t pred
Definition: mutable_automaton.hh:85
std::string vname(bool full=true) const
Definition: mutable_automaton.hh:145
std::ostream & print_state_history(state_t s, std::ostream &o, const std::string &fmt="text") const
Definition: mutable_automaton.hh:407
weight_t add_transition(state_t src, state_t dst, label_t l)
Same as above, with weight one.
Definition: mutable_automaton.hh:716
weight_t set_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:751
tr_cont_t & all_out_(state_t s)
Definition: mutable_automaton.hh:867
typename context_t::weightset_ptr weightset_ptr
Definition: mutable_automaton.hh:62
transition_t set_transition_copy(state_t src, state_t dst, const A &aut, transition_t t, bool transpose=false)
Definition: mutable_automaton.hh:674
void del_transition_from_src(transition_t t)
Remove t from the outgoing transitions of the source state.
Definition: mutable_automaton.hh:301
Data stored for each state.
Definition: mutable_automaton.hh:83
The semiring of Natural numbers.
Definition: n.hh:34
specialisation of history_base
Definition: no_history.hh:28
Definition: string_history.hh:29
ATTRIBUTE_PURE auto find(const std::vector< T, Alloc > &s, const T &e) -> typename std::vector< T, Alloc >::const_iterator
Convenience wrapper around std::find.
Definition: vector.hh:81
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
AutOut transpose(Aut &aut, bool keep_history=true)
Definition: transpose.hh:79
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:931
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
auto conv(const ValueSet &vs, const std::string &str, Args &&... args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
Definition: stream.hh:45
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:42
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22
Definition: cont_filter.hh:145
Definition: cont_filter.hh:69
label_t get_label() const
Definition: transition.hh:39
void set_label(label_t &l)
Definition: transition.hh:40
Definition: transition.hh:65
void set_weight(weight_t &k)
Definition: transition.hh:68