00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_ALGORITHMS_SHUFFLE_HXX
00018 # define VCSN_ALGORITHMS_SHUFFLE_HXX
00019
00020 # include <set>
00021 # include <map>
00022 # include <queue>
00023 # include <stack>
00024
00025 # include <vaucanson/algorithms/shuffle.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 Shuffle
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 Shuffle (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("shuffle");
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 {
00096 const pair_hstate_t new_pair(lhs.dst_of(*l), rhs_s);
00097 typename visited_t::const_iterator found = visited_.find(new_pair);
00098
00099 hstate_t dst;
00100 if (found == visited_.end())
00101 {
00102 dst = output.add_state();
00103
00104 this->add_state_to_process(output, lhs, rhs, m, dst, new_pair);
00105 }
00106 else
00107 dst = found->second;
00108 output.add_series_transition(current_state, dst, lhs.series_of(*l));
00109 }
00110 for (typename rhs_t::delta_iterator r(rhs.value(), rhs_s);
00111 ! r.done();
00112 r.next())
00113 {
00114 const pair_hstate_t new_pair(lhs_s, rhs.dst_of(*r));
00115 typename visited_t::const_iterator found = visited_.find(new_pair);
00116
00117 hstate_t dst;
00118 if (found == visited_.end())
00119 {
00120 dst = output.add_state();
00121
00122 this->add_state_to_process(output, lhs, rhs, m, dst, new_pair);
00123 }
00124 else
00125 dst = found->second;
00126 output.add_series_transition(current_state, dst, rhs.series_of(*r));
00127 }
00128
00129
00130
00131 }
00132 merge_transitions(output);
00133 return output;
00134 }
00135
00136 private:
00137
00138 void merge_transitions(output_t& a)
00139 {
00140 typedef std::map<hstate_t, series_set_elt_t> map_t;
00141 for_all_states(s, a)
00142 {
00143 map_t map;
00144 std::list<htransition_t> transitions;
00145 for (delta_iterator e(a.value(), *s); ! e.done(); e.next())
00146 {
00147 hstate_t target = a.dst_of(*e);
00148 transitions.push_back(*e);
00149 typename map_t::iterator it = map.find(target);
00150 if (it == map.end())
00151 map.insert(std::pair<hstate_t, series_set_elt_t>(target,
00152 a.series_of(*e)));
00153 else
00154 it->second += a.series_of(*e);
00155 }
00156 for_all_(std::list<htransition_t>, e, transitions)
00157 a.del_transition(*e);
00158 for_all_(map_t, it, map)
00159 a.add_series_transition(*s, it->first, it->second);
00160 }
00161 }
00162
00163
00164 class grphx
00165 {
00166 public:
00167 template <typename Output, typename Lhs, typename Rhs>
00168 static void
00169 setcoordfrom (Output& a,
00170 const Lhs& lhs,
00171 const Rhs& rhs,
00172 const typename Output::hstate_t state,
00173 const typename Lhs::hstate_t x_state,
00174 const typename Rhs::hstate_t y_state)
00175 {
00176 typename std::map<typename Lhs::hstate_t,
00177 typename Lhs::geometry_t::coords_t>::const_iterator iter;
00178 typename std::map<typename Rhs::hstate_t,
00179 typename Rhs::geometry_t::coords_t>::const_iterator iter2;
00180 double x = 0, y = 0;
00181
00182 iter = lhs.geometry().states().find(x_state);
00183 if (iter != lhs.geometry().states().end())
00184 x = iter->second.first;
00185
00186 iter2 = rhs.geometry().states().find(y_state);
00187 if (iter2 != rhs.geometry().states().end())
00188 y = iter2->second.second;
00189
00190 a.geometry().states()[state] = std::make_pair(x, y);
00191 }
00192 private:
00193
00194 template<typename I>
00195 void
00196 align (const I& a)
00197 {
00198 AUTOMATON_TYPES(I);
00199 std::map<hstate_t,bool> visited;
00200 std::stack<hstate_t> stack;
00201
00202 for_all_const_states(i, a)
00203 {
00204 visited[*i] = false;
00205
00206 stack.push(*i);
00207 }
00208
00209 for_all_const_initial_states(i, a)
00210 stack.push(*i);
00211
00212 int x = 0;
00213 while (!stack.empty())
00214 {
00215 hstate_t i = stack.top();
00216 stack.pop();
00217
00218 if (!visited[i])
00219 {
00220 visited[i] = true;
00221
00222 a.geometry()[i] = std::make_pair(x, x);
00223 x++;
00224
00225 for (delta_iterator j(a.value(), i);
00226 ! j.done();
00227 j.next())
00228 stack.push(a.dst_of(*j));
00229 }
00230 }
00231 }
00232
00233 };
00234 class no_grphx
00235 {
00236 public:
00237 template <typename Output, typename Lhs, typename Rhs>
00238 static void
00239 setcoordfrom (Output& a,
00240 const Lhs& lhs,
00241 const Rhs& rhs,
00242 const typename Output::hstate_t state,
00243 const typename Lhs::hstate_t x_state,
00244 const typename Rhs::hstate_t y_state) {};
00245 };
00246
00247
00248 AUTOMATON_TYPES(output_t);
00249
00250 typedef std::pair<typename lhs_t::hstate_t, typename rhs_t::hstate_t>
00251 pair_hstate_t;
00252 typedef std::list<htransition_t> delta_ret_t;
00253 typedef std::map<pair_hstate_t, hstate_t> visited_t;
00254 typedef typename series_set_elt_t::support_t support_t;
00255
00256
00257 inline void
00258 add_state_to_process (output_t& output,
00259 const lhs_t& lhs,
00260 const rhs_t& rhs,
00261 pair_map_t& m,
00262 const hstate_t& new_state,
00263 const pair_hstate_t& new_pair)
00264 {
00265 m[new_state] = new_pair;
00266 visited_[new_pair] = new_state;
00267 to_process_.push(new_pair);
00268
00269 # define if_(Cond, ThenClause, ElseClause) \
00270 misc::static_if_simple<Cond, ThenClause, ElseClause>::t
00271 # define eq_(Type1, Type2) \
00272 misc::static_eq<Type1, Type2>::value
00273 # define DECLARE_GEOMETRY(Type) \
00274 typedef geometry<typename Type::hstate_t, typename Type::htransition_t, typename Type::geometry_coords_t> geometry_ ## Type ;
00275
00276 DECLARE_GEOMETRY(output_t)
00277 DECLARE_GEOMETRY(lhs_t)
00278 DECLARE_GEOMETRY(rhs_t)
00279 if (use_geometry_)
00280 if_(eq_(typename output_t::geometry_t, geometry_output_t) and \
00281 eq_(typename rhs_t::geometry_t, geometry_rhs_t) and \
00282 eq_(typename lhs_t::geometry_t, geometry_lhs_t), \
00283 grphx, no_grphx)
00284 ::setcoordfrom(output, lhs, rhs,
00285 new_state, new_pair.first, new_pair.second);
00286 # undef if_
00287 # undef eq_
00288 }
00289
00290
00291 inline void
00292 initialize_queue (output_t& output,
00293 const lhs_t& lhs,
00294 const rhs_t& rhs,
00295 pair_map_t& m)
00296 {
00297 for_all_const_initial_states(lhs_s, lhs)
00298 for_all_const_initial_states(rhs_s, rhs)
00299 {
00300 const pair_hstate_t new_pair(*lhs_s, *rhs_s);
00301 const hstate_t new_state = output.add_state();
00302
00303 this->add_state_to_process(output, lhs, rhs, m, new_state, new_pair);
00304 }
00305 }
00306
00307 inline bool
00308 is_shuffle_not_null (const lhs_t& lhs,
00309 const rhs_t& rhs,
00310 const typename lhs_t::delta_iterator& l,
00311 const typename rhs_t::delta_iterator& r,
00312 series_set_elt_t& prod_series) const
00313 {
00314 const series_set_elt_t left_series = lhs.series_of(*l);
00315 const series_set_elt_t right_series = rhs.series_of(*r);
00316
00317 bool prod_is_not_null = false;
00318 for_all_(support_t, supp, left_series.supp())
00319 {
00320 const monoid_elt_t supp_elt (monoid_, *supp);
00321 const semiring_elt_t l = left_series.get(supp_elt);
00322 const semiring_elt_t r = right_series.get(supp_elt);
00323 const semiring_elt_t p = l * r;
00324 if (p != semiring_zero_)
00325 {
00326 prod_series.assoc(*supp, p.value());
00327 prod_is_not_null = true;
00328 }
00329 }
00330 return (prod_is_not_null);
00331 }
00332
00333
00334 const bool use_geometry_;
00335
00336
00337 visited_t visited_;
00338
00339 std::queue<pair_hstate_t> to_process_;
00340
00341
00342 const series_set_t& series_;
00343 const monoid_t& monoid_;
00344
00345 const semiring_elt_t semiring_zero_;
00346 };
00347
00348
00349
00350
00351
00352 template<typename A, typename T, typename U>
00353 Element<A, T>
00354 shuffle (const Element<A, T>& lhs, const Element<A, U>& rhs,
00355 std::map<typename T::hstate_t,
00356 std::pair<typename T::hstate_t, typename U::hstate_t> >& m,
00357 const bool use_geometry)
00358 {
00359 Element<A, T> ret(rhs.structure());
00360 Shuffle<A, T, U> do_shuffle(ret.structure(), use_geometry);
00361 return do_shuffle (ret, lhs, rhs, m);
00362 }
00363
00364 template<typename A, typename T, typename U>
00365 Element<A, T>
00366 shuffle (const Element<A, T>& lhs, const Element<A, U>& rhs,
00367 const bool use_geometry)
00368 {
00369 std::map<typename T::hstate_t,
00370 std::pair<typename T::hstate_t, typename U::hstate_t> > m;
00371 return shuffle (lhs, rhs, m, use_geometry);
00372 }
00373
00374 }
00375
00376 #endif // ! VCSN_ALGORITHMS_SHUFFLE_HXX