00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_ALGORITHMS_INFILTRATION_HXX
00018 # define VCSN_ALGORITHMS_INFILTRATION_HXX
00019
00020 # include <set>
00021 # include <map>
00022 # include <queue>
00023 # include <stack>
00024
00025 # include <vaucanson/algorithms/infiltration.hh>
00026
00027 # ifndef VCSN_NDEBUG
00028 # include <vaucanson/algorithms/realtime.hh>
00029 # endif // ! VCSN_NDEBUG
00030
00031 # include <vaucanson/automata/concept/automata_base.hh>
00032 # include <vaucanson/misc/usual_macros.hh>
00033 # include <vaucanson/automata/implementation/geometry.hh>
00034 # include <vaucanson/misc/static.hh>
00035
00036 namespace vcsn
00037 {
00038
00039
00040
00041
00042 template<typename A, typename T, typename U>
00043 class Infiltration
00044 {
00045 public:
00046 typedef AutomataBase<A> structure_t;
00047 typedef Element<A, T> lhs_t;
00048 typedef Element<A, U> rhs_t;
00049 typedef lhs_t output_t;
00050 typedef std::map<typename output_t::hstate_t,
00051 std::pair<typename lhs_t::hstate_t, typename rhs_t::hstate_t> >
00052 pair_map_t;
00053
00054 Infiltration (const structure_t& structure,
00055 const bool use_geometry)
00056 : use_geometry_(use_geometry),
00057 series_(structure.series()),
00058 monoid_(series_.monoid()),
00059 semiring_zero_(series_.semiring().zero(SELECT(semiring_elt_value_t)))
00060 {
00061 }
00062
00063
00064 output_t&
00065 operator() (output_t& output,
00066 const lhs_t& lhs,
00067 const rhs_t& rhs,
00068 pair_map_t& m)
00069 {
00070 BENCH_TASK_SCOPED("infiltration");
00071 visited_.clear();
00072
00073 precondition(is_realtime(lhs));
00074 precondition(is_realtime(rhs));
00075
00076 this->initialize_queue(output, lhs, rhs, m);
00077
00078 while (not to_process_.empty())
00079 {
00080 const pair_hstate_t current_pair = to_process_.front();
00081 to_process_.pop();
00082
00083 const hstate_t lhs_s = current_pair.first;
00084 const hstate_t rhs_s = current_pair.second;
00085 const hstate_t current_state = visited_[current_pair];
00086
00087 output.set_initial(current_state,
00088 lhs.get_initial(lhs_s) * rhs.get_initial(rhs_s));
00089 output.set_final(current_state,
00090 lhs.get_final(lhs_s) * rhs.get_final(rhs_s));
00091
00092 for (typename lhs_t::delta_iterator l(lhs.value(), lhs_s);
00093 ! l.done();
00094 l.next())
00095 for (typename rhs_t::delta_iterator r(rhs.value(), rhs_s);
00096 ! r.done();
00097 r.next())
00098 {
00099 series_set_elt_t prod_series(series_);
00100
00101 if (is_product_not_null(lhs, rhs, l, r, prod_series))
00102 {
00103 const pair_hstate_t new_pair(lhs.dst_of(*l), rhs.dst_of(*r));
00104 typename visited_t::const_iterator found = visited_.find(new_pair);
00105
00106 hstate_t dst;
00107 if (found == visited_.end())
00108 {
00109 dst = output.add_state();
00110
00111 this->add_state_to_process(output, lhs, rhs, m, dst, new_pair);
00112 }
00113 else
00114 dst = found->second;
00115 output.add_series_transition(current_state, dst, prod_series);
00116 }
00117 }
00118 for (typename lhs_t::delta_iterator l(lhs.value(), lhs_s);
00119 ! l.done();
00120 l.next())
00121 {
00122 const pair_hstate_t new_pair(lhs.dst_of(*l), rhs_s);
00123 typename visited_t::const_iterator found = visited_.find(new_pair);
00124
00125 hstate_t dst;
00126 if (found == visited_.end())
00127 {
00128 dst = output.add_state();
00129
00130 this->add_state_to_process(output, lhs, rhs, m, dst, new_pair);
00131 }
00132 else
00133 dst = found->second;
00134 output.add_series_transition(current_state, dst, lhs.series_of(*l));
00135 }
00136 for (typename rhs_t::delta_iterator r(rhs.value(), rhs_s);
00137 ! r.done();
00138 r.next())
00139 {
00140 const pair_hstate_t new_pair(lhs_s, rhs.dst_of(*r));
00141 typename visited_t::const_iterator found = visited_.find(new_pair);
00142
00143 hstate_t dst;
00144 if (found == visited_.end())
00145 {
00146 dst = output.add_state();
00147
00148 this->add_state_to_process(output, lhs, rhs, m, dst, new_pair);
00149 }
00150 else
00151 dst = found->second;
00152 output.add_series_transition(current_state, dst, rhs.series_of(*r));
00153 }
00154
00155 }
00156 return output;
00157 }
00158
00159 private:
00160
00161 class grphx
00162 {
00163 public:
00164 template <typename Output, typename Lhs, typename Rhs>
00165 static void
00166 setcoordfrom (Output& a,
00167 const Lhs& lhs,
00168 const Rhs& rhs,
00169 const typename Output::hstate_t state,
00170 const typename Lhs::hstate_t x_state,
00171 const typename Rhs::hstate_t y_state)
00172 {
00173 typename std::map<typename Lhs::hstate_t,
00174 typename Lhs::geometry_t::coords_t>::const_iterator iter;
00175 typename std::map<typename Rhs::hstate_t,
00176 typename Rhs::geometry_t::coords_t>::const_iterator iter2;
00177 double x = 0, y = 0;
00178
00179 iter = lhs.geometry().states().find(x_state);
00180 if (iter != lhs.geometry().states().end())
00181 x = iter->second.first;
00182
00183 iter2 = rhs.geometry().states().find(y_state);
00184 if (iter2 != rhs.geometry().states().end())
00185 y = iter2->second.second;
00186
00187 a.geometry().states()[state] = std::make_pair(x, y);
00188 }
00189 private:
00190
00191 template<typename I>
00192 void
00193 align (const I& a)
00194 {
00195 AUTOMATON_TYPES(I);
00196 std::map<hstate_t,bool> visited;
00197 std::stack<hstate_t> stack;
00198
00199 for_all_const_states(i, a)
00200 {
00201 visited[*i] = false;
00202
00203 stack.push(*i);
00204 }
00205
00206 for_all_const_initial_states(i, a)
00207 stack.push(*i);
00208
00209 int x = 0;
00210 while (!stack.empty())
00211 {
00212 hstate_t i = stack.top();
00213 stack.pop();
00214
00215 if (!visited[i])
00216 {
00217 visited[i] = true;
00218
00219 a.geometry()[i] = std::make_pair(x, x);
00220 x++;
00221
00222 for (delta_iterator j(a.value(), i);
00223 ! j.done();
00224 j.next())
00225 stack.push(a.dst_of(*j));
00226 }
00227 }
00228 }
00229
00230 };
00231 class no_grphx
00232 {
00233 public:
00234 template <typename Output, typename Lhs, typename Rhs>
00235 static void
00236 setcoordfrom (Output& a,
00237 const Lhs& lhs,
00238 const Rhs& rhs,
00239 const typename Output::hstate_t state,
00240 const typename Lhs::hstate_t x_state,
00241 const typename Rhs::hstate_t y_state) {};
00242 };
00243
00244
00245 AUTOMATON_TYPES(output_t);
00246
00247 typedef std::pair<typename lhs_t::hstate_t, typename rhs_t::hstate_t>
00248 pair_hstate_t;
00249 typedef std::list<htransition_t> delta_ret_t;
00250 typedef std::map<pair_hstate_t, hstate_t> visited_t;
00251 typedef typename series_set_elt_t::support_t support_t;
00252
00253
00254 inline void
00255 add_state_to_process (output_t& output,
00256 const lhs_t& lhs,
00257 const rhs_t& rhs,
00258 pair_map_t& m,
00259 const hstate_t& new_state,
00260 const pair_hstate_t& new_pair)
00261 {
00262 m[new_state] = new_pair;
00263 visited_[new_pair] = new_state;
00264 to_process_.push(new_pair);
00265
00266 # define if_(Cond, ThenClause, ElseClause) \
00267 misc::static_if_simple<Cond, ThenClause, ElseClause>::t
00268 # define eq_(Type1, Type2) \
00269 misc::static_eq<Type1, Type2>::value
00270 # define DECLARE_GEOMETRY(Type) \
00271 typedef geometry<typename Type::hstate_t, typename Type::htransition_t, typename Type::geometry_coords_t> geometry_ ## Type ;
00272
00273 DECLARE_GEOMETRY(output_t)
00274 DECLARE_GEOMETRY(lhs_t)
00275 DECLARE_GEOMETRY(rhs_t)
00276 if (use_geometry_)
00277 if_(eq_(typename output_t::geometry_t, geometry_output_t) and \
00278 eq_(typename rhs_t::geometry_t, geometry_rhs_t) and \
00279 eq_(typename lhs_t::geometry_t, geometry_lhs_t), \
00280 grphx, no_grphx)
00281 ::setcoordfrom(output, lhs, rhs,
00282 new_state, new_pair.first, new_pair.second);
00283 # undef if_
00284 # undef eq_
00285 }
00286
00287
00288 inline void
00289 initialize_queue (output_t& output,
00290 const lhs_t& lhs,
00291 const rhs_t& rhs,
00292 pair_map_t& m)
00293 {
00294 for_all_const_initial_states(lhs_s, lhs)
00295 for_all_const_initial_states(rhs_s, rhs)
00296 {
00297 const pair_hstate_t new_pair(*lhs_s, *rhs_s);
00298 const hstate_t new_state = output.add_state();
00299
00300 this->add_state_to_process(output, lhs, rhs, m, new_state, new_pair);
00301 }
00302 }
00303
00304 inline bool
00305 is_product_not_null (const lhs_t& lhs,
00306 const rhs_t& rhs,
00307 const typename lhs_t::delta_iterator& l,
00308 const typename rhs_t::delta_iterator& r,
00309 series_set_elt_t& prod_series) const
00310 {
00311 const series_set_elt_t left_series = lhs.series_of(*l);
00312 const series_set_elt_t right_series = rhs.series_of(*r);
00313
00314 bool prod_is_not_null = false;
00315 for_all_(support_t, supp, left_series.supp())
00316 {
00317 const monoid_elt_t supp_elt (monoid_, *supp);
00318 const semiring_elt_t l = left_series.get(supp_elt);
00319 const semiring_elt_t r = right_series.get(supp_elt);
00320 const semiring_elt_t p = l * r;
00321 if (p != semiring_zero_)
00322 {
00323 prod_series.assoc(*supp, p.value());
00324 prod_is_not_null = true;
00325 }
00326 }
00327 return (prod_is_not_null);
00328 }
00329
00330
00331 const bool use_geometry_;
00332
00333
00334 visited_t visited_;
00335
00336 std::queue<pair_hstate_t> to_process_;
00337
00338
00339 const series_set_t& series_;
00340 const monoid_t& monoid_;
00341
00342 const semiring_elt_t semiring_zero_;
00343 };
00344
00345
00346
00347
00348
00349 template<typename A, typename T, typename U>
00350 Element<A, T>
00351 infiltration (const Element<A, T>& lhs, const Element<A, U>& rhs,
00352 std::map<typename T::hstate_t,
00353 std::pair<typename T::hstate_t, typename U::hstate_t> >& m,
00354 const bool use_geometry)
00355 {
00356 Element<A, T> ret(rhs.structure());
00357 Infiltration<A, T, U> do_infiltration(ret.structure(), use_geometry);
00358 return do_infiltration (ret, lhs, rhs, m);
00359 }
00360
00361 template<typename A, typename T, typename U>
00362 Element<A, T>
00363 infiltration (const Element<A, T>& lhs, const Element<A, U>& rhs,
00364 const bool use_geometry)
00365 {
00366 std::map<typename T::hstate_t,
00367 std::pair<typename T::hstate_t, typename U::hstate_t> > m;
00368 return infiltration (lhs, rhs, m, use_geometry);
00369 }
00370
00371 }
00372
00373 #endif // ! VCSN_ALGORITHMS_INFILTRATION_HXX