Awali
Another Weighted Automata library
tuple_history.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_TUPLE_HISTORY_HH
18 # define AWALI_TUPLE_HISTORY_HH
19 
20 # include <deque>
21 # include <iostream>
22 # include <map>
23 # include <utility>
24 
26 #include <awali/sttc/ctx/traits.hh>
27 #include <awali/sttc/misc/map.hh> // has
28 #include <awali/common/tuple.hh>
30 #include <awali/common/tuple.hh>
31 
32 # include <stdexcept>
33 
34 namespace awali {
35  namespace sttc {
36 
37  /*------------------------------.
38  | tuple_history<Aut, Auts...>. |
39  `------------------------------*/
40 
47  template <typename Auts>
48  class tuple_history : public history_base
49  {
51  public:
53  {
54  return history_kind_t::TUPLE;
55  }
56 
57  public:
59  using automata_t = Auts;
62 
64  template <size_t I>
67 
69  : auts_(auts)
70  {}
71 
72  std::ostream&
73  print_state_name(state_t s, std::ostream& o,
74  const std::string& fmt) const
75  {
76  o << '(';
77  print_state_name_(s, o, fmt, indices);
78  o << ')';
79  return o;
80  }
81 
82 
84  using origins_t = std::map<state_t, tuple_t>;
85 
86  const origins_t& origins() const
87  {
88  return origins_;
89  }
90 
91  // FIXME: protected:
93  template <std::size_t... I>
95 
98  static constexpr indices_t indices{};
99 
100  template <size_t... I>
101  std::ostream&
102  print_state_name_(state_t s, std::ostream& o,
103  const std::string& fmt,
104  seq<I...>) const
105  {
106  auto i = origins().find(s);
107  const char* sep = "";
108  using swallow = int[];
109  (void) swallow
110  {
111  (o << sep,
112  std::get<I>(auts_)->print_state_history(std::get<I>(i->second),
113  o, fmt),
114  sep = ", ",
115  0)...
116  };
117  return o;
118  }
119 
120  bool has_history(state_t s) const {
121  return (origins_.find(s)!=origins_.end());
122  }
123 
125  return origins_.erase(s);
126  };
127 
128  void
129  add_state(state_t s,const tuple_t& set)
130  {
131  origins_[s] = set;
132  }
133 
136 
138 
140  throw std::runtime_error("Origin state not available");
141  }
142 
143  template <size_t... I>
144  std::vector<state_t> get_state_set(state_t s, seq<I...>) {
145  return {std::get<I>(origins_[s])...};
146  }
147 
148  std::vector<state_t> get_state_set(state_t s) {
149  return get_state_set(s,indices);
150  }
151  };
152 
153  }
154 }//end of ns awali::stc
155 
156 #endif // !AWALI_TUPLE_HISTORY_HH
base type for history of automata
Definition: history.hh:40
An automaton whose states are tuples of states of automata.
Definition: tuple_history.hh:49
bool remove_history(state_t s)
Definition: tuple_history.hh:124
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &fmt) const
Definition: tuple_history.hh:73
tuple_history(const automata_t &auts)
Definition: tuple_history.hh:68
void add_state(state_t s, const tuple_t &set)
Definition: tuple_history.hh:129
typename std::cst_tuple< state_t, std::tuple_size< Auts >::value >::type tuple_t
Tuple of states of input automata.
Definition: tuple_history.hh:61
static constexpr indices_t indices
Definition: tuple_history.hh:98
std::ostream & print_state_name_(state_t s, std::ostream &o, const std::string &fmt, seq< I... >) const
Definition: tuple_history.hh:102
Auts automata_t
The type of origin automata.
Definition: tuple_history.hh:59
automata_t auts_
Origin automata, supplied at construction time.
Definition: tuple_history.hh:135
origins_t origins_
Definition: tuple_history.hh:137
state_t get_state(state_t s)
Definition: tuple_history.hh:139
bool has_history(state_t s) const
Definition: tuple_history.hh:120
std::map< state_t, tuple_t > origins_t
A map from result state to tuple of original states.
Definition: tuple_history.hh:84
std::vector< state_t > get_state_set(state_t s)
Definition: tuple_history.hh:148
history_kind_t get_nature() const
The type of the resulting automaton.
Definition: tuple_history.hh:52
std::vector< state_t > get_state_set(state_t s, seq< I... >)
Definition: tuple_history.hh:144
internal::base_t< typename std::tuple_element< I, automata_t >::type > input_automaton_t
The type of the Ith origin automaton, unqualified.
Definition: tuple_history.hh:66
const origins_t & origins() const
Definition: tuple_history.hh:86
history_kind_t
The different kinds of history.
Definition: enums.hh:176
@ TUPLE
The states comes from a tuple of state.
Definition: tuple.hh:43
typename std::remove_cv< typename std::remove_reference< T >::type >::type base_t
T without reference or const/volatile qualifiers.
Definition: traits.hh:30
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
Definition: tuple.hh:372