yat  0.12.3pre
merge.h
1 #ifndef _theplu_yat_utility_merge_
2 #define _theplu_yat_utility_merge_
3 
4 // $Id: merge.h 2372 2010-12-12 07:18:51Z peter $
5 
6 /*
7  Copyright (C) 2009, 2010 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "concept_check.h"
26 #include "Matrix.h"
27 #include "MatrixWeighted.h"
28 #include "stl_utility.h"
29 
30 #include "yat/statistics/Average.h"
31 
32 #include <boost/concept_check.hpp>
33 #include <boost/iterator/permutation_iterator.hpp>
34 
35 #include <algorithm>
36 #include <map>
37 #include <string>
38 #include <vector>
39 
40 namespace theplu {
41 namespace yat {
42 namespace utility {
43 
65  template<class Container2D>
66  void merge(const Container2D& x, std::vector<std::string>& labels, Matrix& y);
67 
68 
82  template<class Container2D, class Functor>
83  void merge(const Container2D& x, std::vector<std::string>& labels, Matrix& y,
84  Functor func);
85 
113  template<class Container2D, class Functor1, class Functor2>
114  void merge(const Container2D& x, std::vector<std::string>& labels,
115  MatrixWeighted& y, Functor1 data_func, Functor2 weight_func);
116 
117  namespace detail {
118 
125  template<typename Iterator, class Functor1, class Functor2>
126  void assign(double& x, Iterator first, Iterator last, Functor1 func1,
127  Functor2 func2);
128 
134  template<typename Iterator, class Functor1, class Functor2>
135  void assign(DataWeight& x, Iterator first, Iterator last, Functor1 func1,
136  Functor2 func2);
137 
138  void merge_labels(std::vector<std::string>&,
139  std::map<std::string, std::vector<size_t> >&);
140 
141 
142  template<class Container2D, class MutableContainer2D,
143  class Functor1, class Functor2>
144  void merge(const Container2D& x,
145  std::map<std::string, std::vector<size_t> >& label2index,
146  MutableContainer2D& result, Functor1 func1, Functor2 func2);
147 
148  } // end of private namespace detail
149 
150 
151  // template implementations //
152 
153  template<class Container2D>
154  void merge(const Container2D& x, std::vector<std::string>& labels, Matrix& y)
155  {
156  BOOST_CONCEPT_ASSERT((utility::Container2D<Container2D>));
157  merge(x, labels, y, statistics::Average());
158  }
159 
160  template<class Container2D, class Functor>
161  void merge(const Container2D& x, std::vector<std::string>& labels,
162  Matrix& result, Functor func)
163  {
164  BOOST_CONCEPT_ASSERT((utility::Container2D<Container2D>));
165  std::map<std::string, std::vector<size_t> > label2index;
166  detail::merge_labels(labels, label2index);
167  detail::merge(x, label2index, result, func, func);
168  }
169 
170 
171  template<class Container2D, class Functor1, class Functor2>
172  void merge(const Container2D& x, std::vector<std::string>& labels,
173  MatrixWeighted& result, Functor1 func1, Functor2 func2)
174  {
175  BOOST_CONCEPT_ASSERT((utility::Container2D<Container2D>));
176  std::map<std::string, std::vector<size_t> > label2index;
177  detail::merge_labels(labels, label2index);
178  detail::merge(x, label2index, result, func1, func2);
179  }
180 
181  // implemantions of private functions
182 
183  namespace detail {
184  template<typename Iterator, class Functor1, class Functor2>
185  void assign(double& x, Iterator first, Iterator last, Functor1 func1,
186  Functor2 func)
187  {
188  x = func1(first, last);
189  }
190 
191 
192  template<typename Iterator, class Functor1, class Functor2>
193  void assign(DataWeight& x, Iterator first, Iterator last, Functor1 func1,
194  Functor2 func2)
195  {
196  x.data() = func1(first, last);
197  x.weight() = func2(first, last);
198  }
199 
200 
201  void merge_labels(std::vector<std::string>& labels,
202  std::map<std::string, std::vector<size_t> >& label2index)
203  {
204  inverse(labels.begin(), labels.end(), label2index);
205  labels.resize(label2index.size());
206  std::copy(pair_first_iterator(label2index.begin()),
207  pair_first_iterator(label2index.end()),
208  labels.begin());
209  }
210 
211 
212  template<class Container2D, class MutableContainer2D,
213  class Functor1, class Functor2>
214  void merge(const Container2D& x,
215  std::map<std::string, std::vector<size_t> >& label2index,
216  MutableContainer2D& result, Functor1 func1, Functor2 func2)
217  {
218  BOOST_CONCEPT_ASSERT((utility::Mutable_Container2D<MutableContainer2D>));
219  BOOST_CONCEPT_ASSERT((utility::Container2D<Container2D>));
220  result.resize(label2index.size(), x.columns());
221  typedef std::map<std::string, std::vector<size_t> > Map;
222  Map::const_iterator iter=label2index.begin();
223 
224  for (size_t row=0; row<result.rows(); ++row) {
225  const std::vector<size_t>& index = iter->second;
226  for (size_t col=0; col<result.columns(); ++col) {
227  assign(result(row,col),
228  boost::make_permutation_iterator(x.begin_column(col),
229  index.begin()),
230  boost::make_permutation_iterator(x.end_column(col),
231  index.end()),
232  func1, func2);
233  }
234  ++iter;
235  }
236  }
237 
238  } // end of namespace detail
239 
240 }}} // of namespace utility, yat, and theplu
241 
242 #endif
void inverse(InputIterator first, InputIterator last, std::map< Key, std::vector< size_t >, Comp > &m)
Definition: stl_utility.h:493
void merge(const Container2D &x, std::vector< std::string > &labels, Matrix &y)
merge rows in a Container2D
Definition: merge.h:154
Functor to take average of a range.
Definition: Average.h:36
Concept check for Container2D.
Definition: concept_check.h:56
boost::transform_iterator< PairFirst< typename boost::remove_reference< typename std::iterator_traits< Iter >::reference >::type >, Iter > pair_first_iterator(Iter i)
Definition: stl_utility.h:785
Interface to GSL matrix.
Definition: Matrix.h:63
Weighted Matrix.
Definition: MatrixWeighted.h:44

Generated on Mon Jun 1 2015 12:29:52 for yat by  doxygen 1.8.5