17 #ifndef VCSN_ALGORITHMS_SEARCH_HXX
18 # define VCSN_ALGORITHMS_SEARCH_HXX
29 template <
typename InputIterator,
typename FoundFunctor,
typename Series,
typename Kind,
typename T>
32 const InputIterator& begin,
33 const InputIterator& end,
37 BENCH_TASK_SCOPED(
"search");
38 FindBestSearch::search(a, begin, end, eol, f);
41 template <
typename InputIterator,
typename FoundFunctor,
typename Series,
typename Kind,
typename T>
43 FindBestSearch::search(
const Element<Automata<Series, Kind>, T>& a,
44 const InputIterator& begin,
45 const InputIterator& end,
46 typename Element<Automata<Series, Kind>, T>::
50 WindowedBackSearch::search(a, begin, end, eol, f);
63 template <
typename Series,
typename Kind,
typename T,
typename StatesSet>
67 std::vector<StatesSet>& distances)
69 precondition(a.initial().size() > 0);
70 precondition(a.final().size() > 0);
71 precondition(distances.size() == 0);
75 AUTOMATON_TYPES(automaton_t);
78 StatesSet s_new (a.states().back() + 1);
79 StatesSet s_old (a.states().back() + 1);
81 s_old.insert(a.initial().begin(), a.initial().end());
82 for (
unsigned int i = 0;
true; ++i)
85 distances.push_back(s_old);
87 distances[i].insert(distances[i - 1].begin(),
88 distances[i - 1].end());
89 for (
typename StatesSet::const_iterator s = s_old.begin();
95 postcondition(i == distances.size());
98 for (delta_iterator i(a.value(), *s);
101 s_new.insert(a.dst_of(*i));
121 template <
typename InputIterator,
typename Series,
typename Kind,
typename T,
typename StatesSet>
123 std::pair<bool, unsigned int>
127 const std::vector<StatesSet>& distances)
129 precondition(w.size() > 0);
133 AUTOMATON_TYPES(automaton_t);
134 AUTOMATON_FREEMONOID_TYPES(automaton_t);
136 typedef typename window_t::length_t length_t;
140 bitset_t s_new (a.states().back() + 1);
141 bitset_t s_old (a.states().back() + 1);
143 length_t critpos = pos;
145 s_old.insert(distances[pos].begin(), distances[pos].end());
146 while ((--pos >= 0) && !s_old.empty())
149 for (bitset_t::const_iterator s = s_old.begin(); s != s_old.end(); ++s)
150 if (distances[pos + 1].find(*s) != distances[pos + 1].end())
152 if (a.is_initial(*s))
154 std::insert_iterator<bitset_t> i(s_new, s_new.begin());
155 for (delta_iterator t(a.value(), *s); ! t.done(); t.next())
157 monoid_elt_t w(a.series_of(*t).structure().monoid(), w[pos]);
158 if (a.series_of(*t).get(w) != a.series().semiring().wzero_)
165 for (bitset_t::const_iterator s = s_old.begin(); s != s_old.end(); ++s)
166 if (a.is_initial(*s))
167 return std::make_pair(
true, critpos);
168 return std::make_pair(
false, critpos);
172 template <
typename InputIterator,
typename FoundFunctor,
typename Series,
typename Kind,
typename T>
182 AUTOMATON_TYPES(automaton_t);
183 AUTOMATON_FREEMONOID_TYPES(automaton_t);
185 typedef typename window_t::length_t length_t;
186 typedef typename window_t::iterator_t iterator_t;
190 bitset_t s_old (a.states().back() + 1);
191 bitset_t s_new (a.states().back() + 1);
192 iterator_t pos = w.stream();
193 iterator_t last = pos;
195 s_old.
insert(a.initial().begin(), a.initial().end());
196 while (!s_old.empty() && (*pos != w.eol_value()))
199 for (bitset_t::const_iterator s = s_old.begin(); s != s_old.end(); ++s)
203 std::insert_iterator<bitset_t> i(s_new, s_new.begin());
204 for (delta_iterator t(a.value(), *s); ! t.done(); t.next())
206 monoid_elt_t w(a.series_of(*t).structure().monoid(), *pos);
207 if (a.series_of(*t).get(w) != a.series().semiring().wzero_)
214 for (bitset_t::const_iterator s = s_old.begin(); s != s_old.end(); ++s)
218 return f(w.begin(), w.stream(), last);
225 template <
typename InputIterator,
typename FoundFunctor,
typename Series,
typename Kind,
typename T>
227 WindowedBackSearch::search(
const Element<Automata<Series, Kind>, T>& a,
228 const InputIterator& begin,
229 const InputIterator& end,
230 typename Element<Automata<Series, Kind>, T>::
236 AUTOMATON_TYPES(automaton_t);
237 AUTOMATON_FREEMONOID_TYPES(automaton_t);
238 typedef typename misc::Window<InputIterator, letter_t> window_t;
239 typedef typename window_t::length_t length_t;
240 typedef misc::Bitset bitset_t;
243 std::vector<bitset_t> distances;
247 WARNING(
"Search match every position in the stream, ignored.");
250 window_t w (begin, end, eol, wl);
259 std::pair<bool, length_t> p =
273 #endif // ! VCSN_ALGORITHMS_SEARCH_HXX