Vaucanson  1.4.1
krat_exp_support.hxx
1 // krat_exp_support.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_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_SUPPORT_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_SUPPORT_HXX
19 
20 # include <utility>
21 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
22 # include <vaucanson/algebra/implementation/series/krat_exp_is_finite_app.hxx>
23 
24 namespace vcsn {
25 
26  template <class Series, class T, class Dispatch>
27  class SupportMatcher : public algebra::KRatExpMatcher<
28  SupportMatcher<Series, T, Dispatch>,
29  T,
30  int,
31  Dispatch
32  >
33  {
34  public:
35  typedef IsFiniteAppMatcher<Series, T, Dispatch> self_t;
36  typedef int return_type;
37  typedef Series series_set_t;
38  typedef Element<Series, T> series_set_elt_t;
39  typedef typename series_set_elt_t::monoid_elt_t monoid_elt_t;
40  typedef typename monoid_elt_t::value_t monoid_elt_value_t;
41  typedef typename series_set_elt_t::semiring_elt_t semiring_elt_t;
42  typedef typename semiring_elt_t::value_t semiring_elt_value_t;
43  typedef std::list<monoid_elt_value_t> support_t;
44  typedef std::list<std::pair<semiring_elt_value_t, monoid_elt_value_t> >
45  ext_support_t;
46  INHERIT_CONSTRUCTORS(self_t, T, return_type, Dispatch);
47 
48  SupportMatcher(const series_set_t& s):
49  series_(s)
50  {}
51 
52  MATCH__(Product, lhs, rhs)
53  {
54  ext_support_t old_supp_ = supp_;
55  supp_.clear();
56  this->match(lhs);
57  ext_support_t lhs_s = supp_;
58  supp_.clear();
59  this->match(rhs);
60  ext_support_t ret;
61  for_all_const_(ext_support_t, c, lhs_s)
62  for_all_const_(ext_support_t, d, supp_)
63  {
64  monoid_elt_t mc(series_.monoid(), c->second);
65  monoid_elt_t md(series_.monoid(), d->second);
66  semiring_elt_t wc(series_.semiring(), c->first);
67  semiring_elt_t wd(series_.semiring(), d->first);
68  ret.push_back(std::make_pair((wc * wd).value(),
69  (mc * md).value()));
70  }
71  supp_ = ret;
72  supp_.insert(supp_.begin(), old_supp_.begin(), old_supp_.end());
73  return 0;
74  }
75  END
76 
77  MATCH__(Sum, lhs, rhs)
78  {
79  this->match(lhs);
80  this->match(rhs);
81  return 0;
82  }
83  END
84 
85  MATCH_(Star, node)
86  {
87  result_not_computable("undefined case (star) in krat_exp_support");
88  return 0;
89  }
90  END
91 
92  MATCH__(LeftWeight, w, node)
93  {
94  ext_support_t old_supp_ = supp_;
95  supp_.clear();
96  this->match(node);
97  for_all_(ext_support_t, c, supp_)
98  c->first = op_mul(series_.semiring(), w, c->first);
99  supp_.insert(supp_.begin(), old_supp_.begin(), old_supp_.end());
100  return 0;
101  }
102  END
103 
104  MATCH__(RightWeight, node, w)
105  {
106  ext_support_t old_supp_ = supp_;
107  supp_.clear();
108  this->match(node);
109  for_all_(ext_support_t, c, supp_)
110  c->first = op_mul(series_.semiring(), c->first, w);
111  supp_.insert(supp_.begin(), old_supp_.begin(), old_supp_.end());
112  return 0;
113  }
114  END
115 
116  MATCH_(Constant, m)
117  {
118  supp_.push_back(std::make_pair
119  (identity_value(series_.semiring(),
120  SELECT(semiring_elt_value_t)),
121  m));
122  return 0;
123  }
124  END
125 
126  MATCH(Zero)
127  {
128  return 0;
129  }
130  END
131 
132  MATCH(One)
133  {
134  supp_.push_back(std::make_pair
135  (identity_value(series_.semiring(),
136  SELECT(semiring_elt_value_t)),
137  identity_value(series_.monoid(),
138  SELECT(monoid_elt_value_t))));
139  return 0;
140  }
141  END
142 
143  support_t get()
144  {
145  support_t ret;
146  ext_support_t s = ext_get();
147  for_all_const_(ext_support_t, c, s)
148  ret.push_back(c->second);
149  return ret;
150  }
151 
152  ext_support_t& ext_get()
153  {
154  // Now join same words.
155  typedef std::map<monoid_elt_value_t, semiring_elt_value_t> tmap_t;
156  tmap_t v;
157  typename tmap_t::iterator f;
158  for_all_const_(ext_support_t, c, supp_)
159  {
160  if ((f = v.find(c->second)) == v.end())
161  v[c->second] = c->first;
162  else
163  v[c->second] = op_add(series_.semiring(), v[c->second], c->first);
164  }
165  supp_.clear();
166  for_all_const_(tmap_t, m, v)
167  supp_.push_back(std::make_pair(m->second, m->first));
168  return supp_;
169  }
170 
171  private:
172  ext_support_t supp_;
173  const series_set_t& series_;
174  };
175 
176 } // vcsn
177 
178 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_SUPPORT_HXX