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-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_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:
52  history_kind_t get_nature() const override
53  {
54  return history_kind_t::TUPLE;
55  }
56 
58  using automata_t = Auts;
61 
63  template <size_t I>
66 
68  : auts_(auts)
69  {}
70 
71  std::ostream&
72  print_state_name(state_t s, std::ostream& o,
73  const std::string& fmt) const override
74  {
75  o << '(';
76  print_state_name_(s, o, fmt, indices);
77  o << ')';
78  return o;
79  }
80 
81 
83  using origins_t = std::map<state_t, tuple_t>;
84 
85  const origins_t& origins() const
86  {
87  return origins_;
88  }
89 
90  // FIXME: protected:
92  template <std::size_t... I>
94 
97  static constexpr indices_t indices{};
98 
99  template <size_t... I>
100  std::ostream&
101  print_state_name_(state_t s, std::ostream& o,
102  const std::string& fmt,
103  seq<I...>) const
104  {
105  auto i = origins().find(s);
106  const char* sep = "";
107  using swallow = int[];
108  (void) swallow
109  {
110  (o << sep,
111  std::get<I>(auts_)->print_state_history(std::get<I>(i->second),
112  o, fmt),
113  sep = ", ",
114  0)...
115  };
116  return o;
117  }
118 
119  bool has_history(state_t s) const override {
120  return (origins_.find(s)!=origins_.end());
121  }
122 
123  bool remove_history(state_t s) override {
124  return origins_.erase(s);
125  };
126 
127  void
128  add_state(state_t s,const tuple_t& set)
129  {
130  origins_[s] = set;
131  }
132 
135 
137 
139  throw std::runtime_error("Origin state not available");
140  }
141 
142  template <size_t... I>
143  std::vector<state_t> get_state_set(state_t s, seq<I...>) {
144  return {std::get<I>(origins_[s])...};
145  }
146 
147  std::vector<state_t>
148  get_state_set(state_t s) override
149  {
150  return get_state_set(s,indices);
151  }
152  };
153 
154  }
155 }//end of ns awali::stc
156 
157 #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
history_kind_t get_nature() const override
The type of the resulting automaton.
Definition: tuple_history.hh:52
bool remove_history(state_t s) override
Definition: tuple_history.hh:123
tuple_history(const automata_t &auts)
Definition: tuple_history.hh:67
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &fmt) const override
Definition: tuple_history.hh:72
void add_state(state_t s, const tuple_t &set)
Definition: tuple_history.hh:128
typename std::cst_tuple< state_t, std::tuple_size< Auts >::value >::type tuple_t
Tuple of states of input automata.
Definition: tuple_history.hh:60
static constexpr indices_t indices
Definition: tuple_history.hh:97
bool has_history(state_t s) const override
Definition: tuple_history.hh:119
std::ostream & print_state_name_(state_t s, std::ostream &o, const std::string &fmt, seq< I... >) const
Definition: tuple_history.hh:101
state_t get_state(state_t) override
Definition: tuple_history.hh:138
Auts automata_t
The type of origin automata.
Definition: tuple_history.hh:58
automata_t auts_
Origin automata, supplied at construction time.
Definition: tuple_history.hh:134
origins_t origins_
Definition: tuple_history.hh:136
std::map< state_t, tuple_t > origins_t
A map from result state to tuple of original states.
Definition: tuple_history.hh:83
std::vector< state_t > get_state_set(state_t s, seq< I... >)
Definition: tuple_history.hh:143
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:65
const origins_t & origins() const
Definition: tuple_history.hh:85
std::vector< state_t > get_state_set(state_t s) override
Definition: tuple_history.hh:148
history_kind_t
The different kinds of history.
Definition: enums.hh:178
@ 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