Changeset 3224 for trunk/yat/utility/merge.h
- Timestamp:
- May 12, 2014, 6:30:09 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/merge.h
r2372 r3224 1 1 #ifndef _theplu_yat_utility_merge_ 2 #define _theplu_yat_utility_merge_ 2 #define _theplu_yat_utility_merge_ 3 3 4 4 // $Id$ 5 5 6 6 /* 7 Copyright (C) 2009, 2010 Peter Johansson7 Copyright (C) 2009, 2010, 2014 Peter Johansson 8 8 9 9 This file is part of the yat library, http://dev.thep.lu.se/yat … … 64 64 */ 65 65 template<class Container2D> 66 void merge(const Container2D& x, std::vector<std::string>& labels, Matrix& y); 66 void merge(const Container2D& x, std::vector<std::string>& labels, Matrix& y); 67 67 68 68 … … 70 70 \brief merge rows in a \ref concept_container_2d using a Functor 71 71 72 Same as utility::merge(const Container2D&, std::vector<std::string>&,72 Same as merge(const Container2D&, std::vector<std::string>&, 73 73 Matrix&) but instead of calculating the merged element as the 74 74 (weighted) arithmetic mean, the merged element is calculated as 75 75 defined by \a func. 76 76 77 \param x input container to be merged. 78 \param labels telling which rows to merge. 79 \param y resulting merged container. 80 \param func defines how data values are calculated 81 82 \c Functor \a func has an \c operator()(Iterator first, Iterator 83 last) \c const that calculates the merged value from range 84 [first, last). 85 86 Type Requirements: 87 - \c Container2D is a \ref concept_container_2d 88 - \c Functor is a <a href=http://www.sgi.com/tech/stl/BinaryFunction.html> 89 Binary Function</a>. 90 - \c Container2D::const_column_iterator is convertible to 91 argument types of \c Functor. 92 - \c Return type of \c Functor is convertible to \c double 93 77 94 If Functor works on both unweighted and weighted iterators, merge 78 95 works on both unweighted and weighted Container2D. 96 97 \note If \a x and \a y overlap in their underlying data 98 structures, the result is undefined. 79 99 80 100 \since New in yat 0.6 … … 82 102 template<class Container2D, class Functor> 83 103 void merge(const Container2D& x, std::vector<std::string>& labels, Matrix& y, 84 Functor func); 104 Functor func); 85 105 86 106 /** … … 106 126 \param weight_func defines how weight values are calculated 107 127 128 \c Functor1 and \c Functor2 have an \c operator()(Iterator first, 129 Iterator last) \c const that calculates the merged value and 130 weight, respectively, from range [first, last). 131 132 Type Requirements: 133 - \c Container2D is a \ref concept_container_2d 134 - \c Functor1 is a <a href=http://www.sgi.com/tech/stl/BinaryFunction.html> 135 Binary Function</a>. 136 - \c Functor2 is a <a href=http://www.sgi.com/tech/stl/BinaryFunction.html> 137 Binary Function</a>. 138 - \c Container2D::const_column_iterator is convertible to 139 argument types of \c Functor1 and \c Functor2. 140 - \c Return type of \c Functor1 is convertible to \c double 141 - \c Return type of \c Functor2 is convertible to \c double 142 143 If \c Functor1 and \c Functor2 work on both unweighted and 144 weighted iterators, merge works on both unweighted and weighted 145 Container2D. 146 108 147 \note If \a x and \a y overlap in their underlying data 109 148 structures, the result is undefined. … … 112 151 */ 113 152 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); 153 void merge(const Container2D& x, std::vector<std::string>& labels, 154 MatrixWeighted& y, Functor1 data_func, Functor2 weight_func); 116 155 117 156 namespace detail { 118 157 119 158 /** 120 159 assign x using func1 on range [first, last). … … 136 175 Functor2 func2); 137 176 138 void merge_labels(std::vector<std::string>&, 177 void merge_labels(std::vector<std::string>&, 139 178 std::map<std::string, std::vector<size_t> >&); 140 179 141 180 142 template<class Container2D, class MutableContainer2D, 181 template<class Container2D, class MutableContainer2D, 143 182 class Functor1, class Functor2> 144 void merge(const Container2D& x, 145 std::map<std::string, std::vector<size_t> >& label2index, 183 void merge(const Container2D& x, 184 std::map<std::string, std::vector<size_t> >& label2index, 146 185 MutableContainer2D& result, Functor1 func1, Functor2 func2); 147 186 … … 159 198 160 199 template<class Container2D, class Functor> 161 void merge(const Container2D& x, std::vector<std::string>& labels, 200 void merge(const Container2D& x, std::vector<std::string>& labels, 162 201 Matrix& result, Functor func) 163 202 { … … 170 209 171 210 template<class Container2D, class Functor1, class Functor2> 172 void merge(const Container2D& x, std::vector<std::string>& labels, 211 void merge(const Container2D& x, std::vector<std::string>& labels, 173 212 MatrixWeighted& result, Functor1 func1, Functor2 func2) 174 213 { … … 177 216 detail::merge_labels(labels, label2index); 178 217 detail::merge(x, label2index, result, func1, func2); 179 } 218 } 180 219 181 220 // implemantions of private functions … … 199 238 200 239 201 void merge_labels(std::vector<std::string>& labels, 240 void merge_labels(std::vector<std::string>& labels, 202 241 std::map<std::string, std::vector<size_t> >& label2index) 203 242 { … … 210 249 211 250 212 template<class Container2D, class MutableContainer2D, 251 template<class Container2D, class MutableContainer2D, 213 252 class Functor1, class Functor2> 214 void merge(const Container2D& x, 215 std::map<std::string, std::vector<size_t> >& label2index, 253 void merge(const Container2D& x, 254 std::map<std::string, std::vector<size_t> >& label2index, 216 255 MutableContainer2D& result, Functor1 func1, Functor2 func2) 217 256 { … … 220 259 result.resize(label2index.size(), x.columns()); 221 260 typedef std::map<std::string, std::vector<size_t> > Map; 222 Map::const_iterator iter=label2index.begin(); 261 Map::const_iterator iter=label2index.begin(); 223 262 224 263 for (size_t row=0; row<result.rows(); ++row) { 225 264 const std::vector<size_t>& index = iter->second; 226 265 for (size_t col=0; col<result.columns(); ++col) { 227 assign(result(row,col), 228 boost::make_permutation_iterator(x.begin_column(col), 266 assign(result(row,col), 267 boost::make_permutation_iterator(x.begin_column(col), 229 268 index.begin()), 230 boost::make_permutation_iterator(x.end_column(col), 269 boost::make_permutation_iterator(x.end_column(col), 231 270 index.end()), 232 271 func1, func2); … … 234 273 ++iter; 235 274 } 236 } 275 } 237 276 238 277 } // end of namespace detail
Note: See TracChangeset
for help on using the changeset viewer.