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 if(it->second != a.series().zero_)
00160 a.add_series_transition(*s, it->first, it->second);
00161 }
00162 }
00163
00164
00165 class grphx
00166 {
00167 public:
00168 template <typename Output, typename Lhs, typename Rhs>
00169 static void
00170 setcoordfrom (Output& a,
00171 const Lhs& lhs,
00172 const Rhs& rhs,
00173 const typename Output::hstate_t state,
00174 const typename Lhs::hstate_t x_state,
00175 const typename Rhs::hstate_t y_state)
00176 {
00177 typename std::map<typename Lhs::hstate_t,
00178 typename Lhs::geometry_t::coords_t>::const_iterator iter;
00179 typename std::map<typename Rhs::hstate_t,
00180 typename Rhs::geometry_t::coords_t>::const_iterator iter2;
00181 double x = 0, y = 0;
00182
00183 iter = lhs.geometry().states().find(x_state);
00184 if (iter != lhs.geometry().states().end())
00185 x = iter->second.first;
00186
00187 iter2 = rhs.geometry().states().find(y_state);
00188 if (iter2 != rhs.geometry().states().end())
00189 y = iter2->second.second;
00190
00191 a.geometry().states()[state] = std::make_pair(x, y);
00192 }
00193 private:
00194
00195 template<typename I>
00196 void
00197 align (const I& a)
00198 {
00199 AUTOMATON_TYPES(I);
00200 std::map<hstate_t,bool> visited;
00201 std::stack<hstate_t> stack;
00202
00203 for_all_const_states(i, a)
00204 {
00205 visited[*i] = false;
00206
00207 stack.push(*i);
00208 }
00209
00210 for_all_const_initial_states(i, a)
00211 stack.push(*i);
00212
00213 int x = 0;
00214 while (!stack.empty())
00215 {
00216 hstate_t i = stack.top();
00217 stack.pop();
00218
00219 if (!visited[i])
00220 {
00221 visited[i] = true;
00222
00223 a.geometry()[i] = std::make_pair(x, x);
00224 x++;
00225
00226 for (delta_iterator j(a.value(), i);
00227 ! j.done();
00228 j.next())
00229 stack.push(a.dst_of(*j));
00230 }
00231 }
00232 }
00233
00234 };
00235 class no_grphx
00236 {
00237 public:
00238 template <typename Output, typename Lhs, typename Rhs>
00239 static void
00240 setcoordfrom (Output& a,
00241 const Lhs& lhs,
00242 const Rhs& rhs,
00243 const typename Output::hstate_t state,
00244 const typename Lhs::hstate_t x_state,
00245 const typename Rhs::hstate_t y_state) {};
00246 };
00247
00248
00249 AUTOMATON_TYPES(output_t);
00250
00251 typedef std::pair<typename lhs_t::hstate_t, typename rhs_t::hstate_t>
00252 pair_hstate_t;
00253 typedef std::list<htransition_t> delta_ret_t;
00254 typedef std::map<pair_hstate_t, hstate_t> visited_t;
00255 typedef typename series_set_elt_t::support_t support_t;
00256
00257
00258 inline void
00259 add_state_to_process (output_t& output,
00260 const lhs_t& lhs,
00261 const rhs_t& rhs,
00262 pair_map_t& m,
00263 const hstate_t& new_state,
00264 const pair_hstate_t& new_pair)
00265 {
00266 m[new_state] = new_pair;
00267 visited_[new_pair] = new_state;
00268 to_process_.push(new_pair);
00269
00270 # define if_(Cond, ThenClause, ElseClause) \
00271 misc::static_if_simple<Cond, ThenClause, ElseClause>::t
00272 # define eq_(Type1, Type2) \
00273 misc::static_eq<Type1, Type2>::value
00274 # define DECLARE_GEOMETRY(Type) \
00275 typedef geometry<typename Type::hstate_t, typename Type::htransition_t, typename Type::geometry_coords_t> geometry_ ## Type ;
00276
00277 DECLARE_GEOMETRY(output_t)
00278 DECLARE_GEOMETRY(lhs_t)
00279 DECLARE_GEOMETRY(rhs_t)
00280 if (use_geometry_)
00281 if_(eq_(typename output_t::geometry_t, geometry_output_t) and \
00282 eq_(typename rhs_t::geometry_t, geometry_rhs_t) and \
00283 eq_(typename lhs_t::geometry_t, geometry_lhs_t), \
00284 grphx, no_grphx)
00285 ::setcoordfrom(output, lhs, rhs,
00286 new_state, new_pair.first, new_pair.second);
00287 # undef if_
00288 # undef eq_
00289 }
00290
00291
00292 inline void
00293 initialize_queue (output_t& output,
00294 const lhs_t& lhs,
00295 const rhs_t& rhs,
00296 pair_map_t& m)
00297 {
00298 for_all_const_initial_states(lhs_s, lhs)
00299 for_all_const_initial_states(rhs_s, rhs)
00300 {
00301 const pair_hstate_t new_pair(*lhs_s, *rhs_s);
00302 const hstate_t new_state = output.add_state();
00303
00304 this->add_state_to_process(output, lhs, rhs, m, new_state, new_pair);
00305 }
00306 }
00307
00308 inline bool
00309 is_shuffle_not_null (const lhs_t& lhs,
00310 const rhs_t& rhs,
00311 const typename lhs_t::delta_iterator& l,
00312 const typename rhs_t::delta_iterator& r,
00313 series_set_elt_t& prod_series) const
00314 {
00315 const series_set_elt_t left_series = lhs.series_of(*l);
00316 const series_set_elt_t right_series = rhs.series_of(*r);
00317
00318 bool prod_is_not_null = false;
00319 for_all_(support_t, supp, left_series.supp())
00320 {
00321 const monoid_elt_t supp_elt (monoid_, *supp);
00322 const semiring_elt_t l = left_series.get(supp_elt);
00323 const semiring_elt_t r = right_series.get(supp_elt);
00324 const semiring_elt_t p = l * r;
00325 if (p != semiring_zero_)
00326 {
00327 prod_series.assoc(*supp, p.value());
00328 prod_is_not_null = true;
00329 }
00330 }
00331 return (prod_is_not_null);
00332 }
00333
00334
00335 const bool use_geometry_;
00336
00337
00338 visited_t visited_;
00339
00340 std::queue<pair_hstate_t> to_process_;
00341
00342
00343 const series_set_t& series_;
00344 const monoid_t& monoid_;
00345
00346 const semiring_elt_t semiring_zero_;
00347 };
00348
00349
00350
00351
00352
00353 template<typename A, typename T, typename U>
00354 Element<A, T>
00355 shuffle (const Element<A, T>& lhs, const Element<A, U>& rhs,
00356 std::map<typename T::hstate_t,
00357 std::pair<typename T::hstate_t, typename U::hstate_t> >& m,
00358 const bool use_geometry)
00359 {
00360 Element<A, T> ret(rhs.structure());
00361 Shuffle<A, T, U> do_shuffle(ret.structure(), use_geometry);
00362 return do_shuffle (ret, lhs, rhs, m);
00363 }
00364
00365 template<typename A, typename T, typename U>
00366 Element<A, T>
00367 shuffle (const Element<A, T>& lhs, const Element<A, U>& rhs,
00368 const bool use_geometry)
00369 {
00370 std::map<typename T::hstate_t,
00371 std::pair<typename T::hstate_t, typename U::hstate_t> > m;
00372 return shuffle (lhs, rhs, m, use_geometry);
00373 }
00374
00375 }
00376
00377 #endif // ! VCSN_ALGORITHMS_SHUFFLE_HXX