00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_TOOLS_IO_HXX
00019 # define VCSN_TOOLS_IO_HXX
00020
00021 # include <vaucanson/tools/io.hh>
00022 # include <sstream>
00023
00024 namespace vcsn
00025 {
00026
00027 namespace tools
00028 {
00029
00030
00031
00032
00033
00034 template<typename Auto, typename TransitionConverter, typename Format>
00035 Auto& automaton_saver_<Auto, TransitionConverter, Format>::automaton()
00036 {
00037 return a_;
00038 }
00039
00040 template<typename Auto, typename TransitionConverter, typename Format>
00041 const Auto& automaton_saver_<Auto, TransitionConverter, Format>::
00042 automaton() const
00043 {
00044 return a_;
00045 }
00046
00047 template<typename Auto, typename TransitionConverter, typename Format>
00048 automaton_saver_<Auto, TransitionConverter, Format>::
00049 automaton_saver_(const Auto& a,
00050 const TransitionConverter& c,
00051 const Format& f)
00052 : a_(a), conv_(c), format_(f)
00053 {}
00054
00055 template<typename Auto, typename TransitionConverter, typename Format>
00056 std::ostream&
00057 operator<<(std::ostream& o,
00058 const automaton_saver_<Auto, TransitionConverter, Format>& s)
00059 {
00060 TIMER_SCOPED ("automaton output");
00061 s.format_(o, s, s.conv_);
00062 return o;
00063 }
00064
00065 template<typename S, typename T>
00066 std::string string_out::operator()(const AutomataBase<S>&, const T& t) const
00067 {
00068 std::ostringstream os;
00069 os << t;
00070 return os.str();
00071 }
00072
00073 template<typename S, typename T>
00074 std::string string_out::operator()(const TransducerBase<S>&, const T& t) const
00075 {
00076 std::ostringstream os;
00077 os << t;
00078 return os.str();
00079 }
00080
00081 }
00082
00083 template<typename Auto, typename TransitionConverter, typename Format>
00084 tools::automaton_saver_<Auto, TransitionConverter, Format>
00085 automaton_saver(const Auto& a,
00086 const TransitionConverter& e,
00087 const Format& f)
00088 {
00089 return tools::automaton_saver_<Auto, TransitionConverter, Format>(a, e, f);
00090 }
00091
00092 namespace tools
00093 {
00094
00095
00096
00097
00098
00099 template<typename Auto, typename TransitionConverter, typename Format>
00100 Auto& automaton_loader_<Auto, TransitionConverter, Format>::automaton()
00101 {
00102 return a_;
00103 }
00104
00105 template<typename Auto, typename TransitionConverter, typename Format>
00106 const Auto&
00107 automaton_loader_<Auto, TransitionConverter, Format>::automaton() const
00108 {
00109 return a_;
00110 }
00111
00112 template<typename Auto, typename TransitionConverter, typename Format>
00113 automaton_loader_<Auto, TransitionConverter, Format>::
00114 automaton_loader_(Auto& a,
00115 const TransitionConverter& conv,
00116 const Format& format,
00117 bool merge_states)
00118 : a_(a),
00119 conv_(conv),
00120 format_(format),
00121 scount_(0),
00122 smap_(),
00123 merge_states_(merge_states)
00124 {}
00125
00126 template<typename Auto, typename TransitionConverter, typename Format>
00127 typename Auto::hstate_t
00128 automaton_loader_<Auto, TransitionConverter, Format>::
00129 add_state(unsigned s)
00130 {
00131 if (smap_.find(s) == smap_.end())
00132 {
00133 if (a_.has_state(s) && merge_states_)
00134 smap_[s] = hstate_t(s);
00135 else
00136 smap_[s] = a_.add_state();
00137 }
00138 return smap_[s];
00139 }
00140
00141 template<typename Auto, typename TransitionConverter, typename Format>
00142 void automaton_loader_<Auto, TransitionConverter, Format>::
00143 set_initial(unsigned s, const std::string& lbl)
00144 {
00145 a_.set_initial(add_state(s), conv_(a_, lbl));
00146 }
00147
00148 template<typename Auto, typename TransitionConverter, typename Format>
00149 void automaton_loader_<Auto, TransitionConverter, Format>::
00150 set_final(unsigned s, const std::string& lbl)
00151 {
00152 a_.set_final(add_state(s), conv_(a_, lbl));
00153 }
00154
00155 template<typename Auto, typename TransitionConverter, typename Format>
00156 void automaton_loader_<Auto, TransitionConverter, Format>::
00157 add_spontaneous(unsigned from, unsigned to)
00158 {
00159 a_.add_spontaneous(add_state(from), add_state(to));
00160 }
00161
00162 template<typename Auto, typename TransitionConverter, typename Format>
00163 void automaton_loader_<Auto, TransitionConverter, Format>::
00164 add_transition(unsigned from, unsigned to, const std::string& lbl)
00165 {
00166 a_.add_series_transition(add_state(from), add_state(to), conv_(a_, lbl));
00167 }
00168
00169 template<typename Auto, typename TransitionConverter, typename Format>
00170 std::istream&
00171 operator>>(std::istream& in,
00172 automaton_loader_<Auto, TransitionConverter, Format> l)
00173 {
00174 TIMER_SCOPED ("automaton input");
00175 l.format_(in, l);
00176 return in;
00177 }
00178
00179 }
00180
00181 template<typename Auto, typename TransitionConverter, typename Format>
00182 tools::automaton_loader_<Auto, TransitionConverter, Format>
00183 automaton_loader(Auto& a,
00184 const TransitionConverter& e,
00185 const Format& f,
00186 bool merge_states)
00187 {
00188 return tools::automaton_loader_<Auto, TransitionConverter, Format>
00189 (a, e, f, merge_states);
00190 }
00191
00192 }
00193
00194 #endif // ! VCSN_TOOLS_IO_HXX