1 #ifndef _theplu_yat_utility_merge_
2 #define _theplu_yat_utility_merge_
25 #include "concept_check.h"
27 #include "MatrixWeighted.h"
30 #include "yat/statistics/Average.h"
32 #include <boost/concept_check.hpp>
33 #include <boost/iterator/permutation_iterator.hpp>
65 template<
class Container2D>
66 void merge(
const Container2D& x, std::vector<std::string>& labels, Matrix& y);
82 template<
class Container2D,
class Functor>
83 void merge(
const Container2D& x, std::vector<std::string>& labels, Matrix& y,
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);
125 template<
typename Iterator,
class Functor1,
class Functor2>
126 void assign(
double& x, Iterator first, Iterator last, Functor1 func1,
134 template<
typename Iterator,
class Functor1,
class Functor2>
135 void assign(DataWeight& x, Iterator first, Iterator last, Functor1 func1,
138 void merge_labels(std::vector<std::string>&,
139 std::map<std::string, std::vector<size_t> >&);
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);
153 template<
class Container2D>
160 template<
class Container2D,
class Functor>
162 Matrix& result, Functor func)
165 std::map<std::string, std::vector<size_t> > label2index;
166 detail::merge_labels(labels, label2index);
167 detail::merge(x, label2index, result, func, func);
171 template<
class Container2D,
class Functor1,
class Functor2>
176 std::map<std::string, std::vector<size_t> > label2index;
177 detail::merge_labels(labels, label2index);
178 detail::merge(x, label2index, result, func1, func2);
184 template<
typename Iterator,
class Functor1,
class Functor2>
185 void assign(
double& x, Iterator first, Iterator last, Functor1 func1,
188 x = func1(first, last);
192 template<
typename Iterator,
class Functor1,
class Functor2>
193 void assign(DataWeight& x, Iterator first, Iterator last, Functor1 func1,
196 x.data() = func1(first, last);
197 x.weight() = func2(first, last);
201 void merge_labels(std::vector<std::string>& labels,
202 std::map<std::string, std::vector<size_t> >& label2index)
204 inverse(labels.begin(), labels.end(), label2index);
205 labels.resize(label2index.size());
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)
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();
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),
230 boost::make_permutation_iterator(x.end_column(col),