Vaucanson  1.4.1
krat_exp_realtime.hxx
1 // krat_exp_realtime.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2011 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGORITHMS_KRAT_EXP_REALTIME_HXX
18 # define VCSN_ALGORITHMS_KRAT_EXP_REALTIME_HXX
19 
21 
22 # include <vaucanson/algebra/concept/series_base.hh>
23 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
24 
25 namespace vcsn {
26 
27  namespace algebra
28  {
29  template <class Exp_,
30  class Dispatch_>
31  class KRatExpIsRealtime :
32  public algebra::KRatExpMatcher<
33  KRatExpIsRealtime<Exp_, Dispatch_>,
34  Exp_,
35  bool,
36  Dispatch_
37  >
38  {
39  public :
40  typedef KRatExpIsRealtime<Exp_, Dispatch_> this_class;
41  typedef bool return_type;
42  typedef typename Exp_::monoid_elt_value_t monoid_elt_value_t;
43  typedef typename Exp_::semiring_elt_value_t semiring_elt_value_t;
44  INHERIT_CONSTRUCTORS(this_class, Exp_, bool, Dispatch_);
45 
46  MATCH__(Sum, l, r)
47  {
48  return this->match(l) and this->match(r);
49  }
50  END
51 
52  MATCH__(Product, l, r)
53  {
54  return this->match(l) and this->match(r);
55  }
56  END
57 
58  MATCH_(Star, e)
59  {
60  return this->match(e);
61  }
62  END
63 
64  MATCH__(LeftWeight, w, e)
65  {
66  (void)w; // Avoid warning.
67  return this->match(e);
68  }
69  END
70 
71  MATCH__(RightWeight, e, w)
72  {
73  (void)w; // Avoid warning.
74  return this->match(e);
75  }
76  END
77 
78  MATCH_(Constant, m)
79  {
80  typename monoid_elt_value_t::const_iterator i = m.begin();
81  return ++i == m.end();
82  }
83  END
84 
85  MATCH(Zero)
86  {
87  return true;
88  }
89  END
90 
91  MATCH(One)
92  {
93  return true;
94  }
95  END
96  };
97 
98  } // End of namespace algebra.
99 
100  template <class Exp_, class S_>
101  bool
102  do_is_realtime(const algebra::SeriesBase<S_>&, const Exp_& exp)
103  {
104  typedef S_ S;
105  typedef typename Exp_::value_t T;
106 
107  algebra::KRatExpIsRealtime< T, algebra::DispatchFunction<T> >
108  matcher;
109  return matcher.match(exp.value());
110  }
111 
112 
113  namespace algebra {
114 
115  template <class Series, class T, class Dispatch>
116  struct KRatExpRealtime : algebra::KRatExpIdentity<
117  KRatExpRealtime<Series, T, Dispatch>,
118  Series,
119  T,
120  Dispatch
121  >
122  {
123  typedef KRatExpRealtime<Series, T, Dispatch> self_t;
124  typedef Element<Series, T> return_type;
125  typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
126  typedef typename semiring_elt_t::value_t semiring_elt_value_t;
127  typedef typename Element<Series, T>::monoid_elt_t monoid_elt_t;
128  typedef typename monoid_elt_t::value_t monoid_elt_value_t;
129  typedef typename monoid_elt_t::set_t monoid_t;
130  typedef typename monoid_t::alphabet_t alphabet_t;
131  typedef typename alphabet_t::letter_t letter_t;
132  INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
133 
134  KRatExpRealtime(const Element<Series, T>& exp) :
135  KRatExpIdentity<KRatExpRealtime<Series, T, Dispatch>,
136  Series,
137  T,
138  Dispatch
139  >(exp)
140  {}
141 
142  MATCH_(Constant, m)
143  {
144  return_type exp = identity_as<T>::of(this->exp_.structure());
145  return_type tmp (this->exp_.structure());
146  monoid_elt_t letter (this->exp_.structure().monoid());
147  for (typename monoid_elt_value_t::const_iterator i = m.begin();
148  i != m.end(); ++i)
149  {
150  letter = *i;
151  tmp = letter;
152  exp *= tmp;
153  }
154  return exp;
155  }
156  END
157  };
158 
159  } // algebra
160 
161 
162  template <class Exp_, class S_>
163  Exp_
164  do_realtime(const algebra::SeriesBase<S_>&, const Exp_& exp)
165  {
166  typedef S_ S;
167  typedef typename Exp_::value_t T;
168 
169  algebra::KRatExpRealtime< S, T, algebra::DispatchFunction<T> >
170  matcher(exp);
171  return matcher.match(exp.value());
172  }
173 
174  template <class Exp_, class S_>
175  void
176  do_realtime_here(const algebra::SeriesBase<S_>&, Exp_& exp)
177  {
178  typedef S_ S;
179  typedef typename Exp_::value_t T;
180 
181  algebra::KRatExpRealtime< S, T, algebra::DispatchFunction<T> >
182  matcher(exp);
183  exp = matcher.match(exp.value());
184  }
185 
186 } // vcsn
187 
188 #endif // ! VCSN_ALGORITHMS_KRAT_EXP_REALTIME_HXX