18 #ifndef VCSN_AUTOMATA_IMPLEMENTATION_BMIG_GRAPH_IMPL_HXX_
19 # define VCSN_AUTOMATA_IMPLEMENTATION_BMIG_GRAPH_IMPL_HXX_
21 # include <vaucanson/automata/implementation/bmig_graph_impl.hh>
32 # define BMIGRAPH_TPARAM \
33 template <class Kind, class WordValue, class WeightValue, \
34 class SeriesValue, class Letter, class Tag, class GeometryCoords>
37 Graph<Kind, WordValue, WeightValue, SeriesValue, Letter, Tag, GeometryCoords>
51 number_of_epsilon_(0),
66 BMIGRAPH::Graph (
unsigned initial_number_of_states,
68 : initial_bitset_(initial_number_of_states),
69 final_bitset_(initial_number_of_states),
70 number_of_epsilon_(0),
71 number_of_state_(initial_number_of_states),
72 states_(initial_number_of_states)
74 for (
unsigned i = 0; i < initial_number_of_states; ++i)
76 boost::shared_ptr<std::size_t> p(
new std::size_t(i));
82 BMIGRAPH::Graph (
const self_t& g)
85 initial_bitset_ = g.initial_bitset_;
86 final_bitset_ = g.final_bitset_;
87 number_of_epsilon_ = g.number_of_epsilon_;
88 number_of_state_ = g.number_of_state_;
89 label_container_ = g.label_container_;
92 states_.resize(g.number_of_state_);
93 for (
unsigned i = 0; i < g.number_of_state_; ++i)
95 boost::shared_ptr<std::size_t> p(
new std::size_t(i));
99 for (
typename graph_data_t::const_iterator i = g.graph_.begin();
102 graph_.insert(edge_data_t(states_[*i->from_],
106 # define VCSN_COPY_I_T(Set) \
107 for (typename Set##t::const_iterator i = g.Set.begin(); \
110 Set.insert(initial_value_t (states_[*i->first], i->second));
111 VCSN_COPY_I_T(initial_)
112 VCSN_COPY_I_T(final_)
113 # undef VCSN_COPY_I_T
115 # define VCSN_COPY_GEOMETRY(Type) \
117 typename geometry_t::Type##_geometry_map_t& map_##Type = geometry_.Type(); \
118 map_##Type.clear(); \
119 for (typename geometry_t::Type##_geometry_map_t::const_iterator i = g.geometry_.Type().begin(); \
120 i != g.geometry_.Type().end(); \
122 map_##Type[i->first] = i->second; \
124 VCSN_COPY_GEOMETRY(states)
125 VCSN_COPY_GEOMETRY(initials)
126 VCSN_COPY_GEOMETRY(finals)
127 # undef VCSN_COPY_GEOMETRY
129 typename geometry_t::transitions_geometry_map_t& map_transitions = geometry_.transitions();
130 map_transitions.clear();
131 for (
typename geometry_t::transitions_geometry_map_t::const_iterator i = g.geometry_.transitions().begin();
132 i != g.geometry_.transitions().end();
135 typename graph_data_t::const_iterator tmp = find_edge(states_[*i->first.value()->from_],
136 states_[*i->first.value()->to_],
137 i->first.value()->label_);
139 if (tmp != graph_.end())
140 map_transitions[htransition_t(tmp)] = i->second;
155 typename BMIGRAPH::self_t&
156 BMIGRAPH::operator= (
const self_t& g)
161 initial_bitset_ = g.initial_bitset_;
162 final_bitset_ = g.final_bitset_;
163 number_of_epsilon_ = g.number_of_epsilon_;
164 number_of_state_ = g.number_of_state_;
165 label_container_ = g.label_container_;
168 states_.resize(g.number_of_state_);
169 for (
unsigned i = 0; i < g.number_of_state_; ++i)
171 boost::shared_ptr<std::size_t> p(
new std::size_t(i));
175 for (
typename graph_data_t::const_iterator i = g.graph_.begin();
178 graph_.insert(edge_data_t(states_[*i->from_],
181 # define VCSN_COPY_I_T(Set) \
182 for (typename Set##t::const_iterator i = g.Set.begin(); \
185 Set.insert(initial_value_t (states_[*i->first], i->second));
186 VCSN_COPY_I_T(initial_)
187 VCSN_COPY_I_T(final_)
188 # undef VCSN_COPY_I_T
190 # define VCSN_COPY_GEOMETRY(Type) \
192 typename geometry_t::Type##_geometry_map_t& map_##Type = geometry_.Type(); \
193 map_##Type.clear(); \
194 for (typename geometry_t::Type##_geometry_map_t::const_iterator i = g.geometry_.Type().begin(); \
195 i != g.geometry_.Type().end(); \
197 map_##Type[i->first] = i->second; \
199 VCSN_COPY_GEOMETRY(states)
200 VCSN_COPY_GEOMETRY(initials)
201 VCSN_COPY_GEOMETRY(finals)
202 # undef VCSN_COPY_GEOMETRY
204 typename geometry_t::transitions_geometry_map_t& map_transitions = geometry_.transitions();
205 map_transitions.clear();
206 for (
typename geometry_t::transitions_geometry_map_t::const_iterator i = g.geometry_.transitions().begin();
207 i != g.geometry_.transitions().end();
210 typename graph_data_t::const_iterator tmp = find_edge(states_[*i->first.value()->from_],
211 states_[*i->first.value()->to_],
212 i->first.value()->label_);
214 if (tmp != graph_.end())
215 map_transitions[htransition_t(tmp)] = i->second;
226 typename BMIGRAPH::states_t
227 BMIGRAPH::states()
const
229 return misc::Support<states_data_t>(states_);
233 typename BMIGRAPH::edges_t
234 BMIGRAPH::edges()
const
236 return edges_t(graph_);
240 typename BMIGRAPH::initial_support_t
241 BMIGRAPH::initial ()
const
243 return initial_support_t(initial_);
247 typename BMIGRAPH::final_support_t
248 BMIGRAPH::final ()
const
250 return final_support_t(final_);
260 BMIGRAPH::has_state (
const hstate_t& s)
const
265 for_all_iterator(states_data_t::const_iterator, i, states_)
272 typename BMIGRAPH::hstate_t
273 BMIGRAPH::add_state ()
275 initial_bitset_.append(
false);
276 final_bitset_.append(
false);
277 boost::shared_ptr<std::size_t> p(
new std::size_t(number_of_state_++));
279 states_.push_back(h);
284 typename BMIGRAPH::hstate_t
285 BMIGRAPH::get_state (
unsigned s)
const
287 precondition(s < states_.size());
288 return hstate_t(states_[s]);
293 BMIGRAPH::del_state (
const hstate_t& s)
295 precondition (has_state(s));
298 graph_.template get<src>().erase(s.value());
299 graph_.template get<dst>().erase(s.value());
301 if (s != (number_of_state_ - 1) && (number_of_state_ - 1))
303 state_t lastone = states_.back();
304 states_[s] = lastone;
305 # define VCSN_UPDATE(Set) \
306 if (Set##bitset_[*lastone]) \
308 if (Set##bitset_[s]) \
309 Set.erase(s.value()); \
311 Set##bitset_[s] = true; \
315 if (Set##bitset_[s]) \
317 Set##bitset_[s] = false; \
318 Set.erase(s.value()); \
323 VCSN_UPDATE(initial_)
326 *lastone = *s.value();
330 initial_.erase(s.value());
331 final_.erase(s.value());
336 postcondition(states_.size() == number_of_state_);
338 initial_bitset_.resize(number_of_state_);
339 final_bitset_.resize(number_of_state_);
347 BMIGRAPH::set_initial (
const hstate_t& s,
348 const series_set_elt_value_t& v,
349 const series_set_elt_value_t& z)
351 precondition(has_state(s));
354 initial_.erase (s.value());
355 initial_bitset_[s] =
false;
357 else if (!initial_bitset_[s])
359 initial_.insert (initial_value_t (s.value(), v));
360 initial_bitset_[s] =
true;
363 initial_.modify(initial_.find(s.value()), update_label<initial_value_t>(v));
367 const typename BMIGRAPH::series_set_elt_value_t&
368 BMIGRAPH::get_initial(
const hstate_t& s,
const series_set_elt_value_t &zero)
const
370 precondition(has_state(s));
371 typename initial_t::const_iterator it = initial_.find(s.value());
373 if (it == initial_.end())
380 BMIGRAPH::is_initial(
const hstate_t& s,
const series_set_elt_value_t&)
const
382 precondition(has_state(s));
383 return initial_bitset_[s];
388 BMIGRAPH::clear_initial()
391 initial_bitset_.reset();
396 BMIGRAPH::set_final(
const hstate_t& s,
397 const series_set_elt_value_t& v,
398 const series_set_elt_value_t& z)
400 precondition(has_state(s));
403 final_.erase (s.value());
404 final_bitset_[s] =
false;
406 else if (!final_bitset_[s])
408 final_.insert (initial_value_t (s.value(), v));
409 final_bitset_[s] =
true;
412 final_.modify(final_.find(s.value()), update_label<final_value_t>(v));
416 const typename BMIGRAPH::series_set_elt_value_t&
417 BMIGRAPH::get_final(
const hstate_t& s,
const series_set_elt_value_t &zero)
const
419 precondition(has_state(s));
420 typename final_t::const_iterator it = final_.find(s.value());
422 if (it == final_.end())
429 BMIGRAPH::is_final(
const hstate_t& s,
const series_set_elt_value_t&)
const
431 precondition(has_state(s));
432 return final_bitset_[s];
437 BMIGRAPH::clear_final()
440 final_bitset_.reset();
449 BMIGRAPH::has_edge (
const hedge_t& h)
const
451 succ_range r = graph_.equal_range(boost::make_tuple(h.value()->from_,
454 state_t to = h.value()->to_;
455 for (it = r.first; it != r.second && it->to_ != to; ++it)
458 return it != r.second;
462 typename BMIGRAPH::hedge_t
463 BMIGRAPH::add_edge (
const hstate_t& from,
const hstate_t& to,
const label_t& l)
466 return hedge_t (graph_.insert (edge_data_t (from.value(), to.value(), l)).first);
471 BMIGRAPH::del_edge (
const hedge_t& h)
473 precondition (has_edge(h));
475 hlabel_t l = h.value()->label_;
476 graph_.erase(h.value());
485 typename BMIGRAPH::hstate_t
486 BMIGRAPH::src_of (
const hedge_t& h)
const
488 return hstate_t(h.value()->from_);
492 typename BMIGRAPH::hstate_t
493 BMIGRAPH::dst_of (
const hedge_t& h)
const
495 return hstate_t(h.value()->to_);
499 const typename BMIGRAPH::label_t&
500 BMIGRAPH::label_of (
const hedge_t& h)
const
502 return h.value()->label_;
508 BMIGRAPH::update(
const hedge_t& h,
const label_t& l)
510 label_container_.update(h->label_, l);
511 graph_.modify(h.value(), update_hlabel<hlabel_t>(h->label_));
517 BMIGRAPH::exists (
const AutomataBase<S>& s)
const
519 typename WordValue::iterator it;
520 typename label_t::const_iterator r;
524 for (
typename graph_data_t::iterator i = graph_.begin(); i != graph_.end(); ++i)
528 if (!has_state(dst_of(hedge_t(i))) ||
529 !has_state(src_of(hedge_t(i))))
533 l = label_of(hedge_t(i));
534 for (r = l.begin(); r != l.end(); ++r)
537 for (it = w.begin(); it != w.end(); ++it)
538 if (!s.series().monoid().alphabet().contains(*it))
547 typename BMIGRAPH::tag_t&
555 const typename BMIGRAPH::tag_t&
556 BMIGRAPH::tag ()
const
563 typename BMIGRAPH::geometry_t&
564 BMIGRAPH::geometry ()
571 const typename BMIGRAPH::geometry_t&
572 BMIGRAPH::geometry ()
const
577 template <
class Kind,
class WordValue,
class WeightValue,
class SeriesValue,
578 class Letter,
class Tag,
class GeometryCoords,
class I>
580 op_tag(
const AutomataBase<I>&, BMIGRAPH &g)
585 template <
class Kind,
class WordValue,
class WeightValue,
class SeriesValue,
586 class Letter,
class Tag,
class GeometryCoords,
class I>
588 op_tag(
const AutomataBase<I>&, BMIGRAPH &g)
593 template <
class Kind,
class WordValue,
class WeightValue,
class SeriesValue,
594 class Letter,
class Tag,
class GeometryCoords,
class I>
595 typename BMIGRAPH::geometry_t&
596 op_geometry(
const AutomataBase<I>&, BMIGRAPH &g)
601 template <
class Kind,
class WordValue,
class WeightValue,
class SeriesValue,
602 class Letter,
class Tag,
class GeometryCoords,
class I>
603 const typename BMIGRAPH::geometry_t&
604 op_geometry(
const AutomataBase<I>&,
const BMIGRAPH &g)
610 typename BMIGRAPH::graph_data_t::const_iterator
611 BMIGRAPH::find_edge(
const state_t& from,
const state_t& to,
612 const hlabel_t& label)
const
614 succ_range r = graph_.equal_range(::boost::make_tuple(from, label));
615 for (succ_iterator i = r.first; i != r.second; ++i)
625 # define DEFINE_DELTAI_FUNCTION(DeltaKind) \
627 std::pair<typename BMIGRAPH::DeltaKind##_iterator, \
628 typename BMIGRAPH::DeltaKind##_iterator> \
629 BMIGRAPH::deltai(const hstate_t& s, DeltaKind##_iterator) const \
631 assertion(has_state(s)); \
632 return graph_.template get<DeltaKind>().equal_range(s.value()); \
635 DEFINE_DELTAI_FUNCTION(src);
636 DEFINE_DELTAI_FUNCTION(dst);
637 # undef DEFINE_DELTAI_FUNCTION
640 template <
typename I>
641 typename BMIGRAPH::htransition_t
642 BMIGRAPH::get_htransition(
const I& i)
const
644 return htransition_t(graph_.project<0>(i));
648 # undef BMIGRAPH_TPARAM
653 #endif // !VCSN_AUTOMATA_IMPLEMENTATION_BMIG_GRAPH_IMPL_HXX_ //