for_each_selectors.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef _GLIBCXX_PARALLEL_FOR_EACH_SELECTORS_H
00040 #define _GLIBCXX_PARALLEL_FOR_EACH_SELECTORS_H 1
00041
00042 #include <parallel/basic_iterator.h>
00043
00044 namespace __gnu_parallel
00045 {
00046
00047
00048 template<typename It>
00049 struct generic_for_each_selector
00050 {
00051
00052
00053
00054 It finish_iterator;
00055 };
00056
00057
00058
00059 template<typename It>
00060 struct for_each_selector : public generic_for_each_selector<It>
00061 {
00062
00063
00064
00065 template<typename Op>
00066 bool
00067 operator()(Op& o, It i)
00068 {
00069 o(*i);
00070 return true;
00071 }
00072 };
00073
00074
00075 template<typename It>
00076 struct generate_selector : public generic_for_each_selector<It>
00077 {
00078
00079
00080
00081 template<typename Op>
00082 bool
00083 operator()(Op& o, It i)
00084 {
00085 *i = o();
00086 return true;
00087 }
00088 };
00089
00090
00091 template<typename It>
00092 struct fill_selector : public generic_for_each_selector<It>
00093 {
00094
00095
00096
00097 template<typename Val>
00098 bool
00099 operator()(Val& v, It i)
00100 {
00101 *i = v;
00102 return true;
00103 }
00104 };
00105
00106
00107 template<typename It>
00108 struct transform1_selector : public generic_for_each_selector<It>
00109 {
00110
00111
00112
00113 template<typename Op>
00114 bool
00115 operator()(Op& o, It i)
00116 {
00117 *i.second = o(*i.first);
00118 return true;
00119 }
00120 };
00121
00122
00123 template<typename It>
00124 struct transform2_selector : public generic_for_each_selector<It>
00125 {
00126
00127
00128
00129 template<typename Op>
00130 bool
00131 operator()(Op& o, It i)
00132 {
00133 *i.third = o(*i.first, *i.second);
00134 return true;
00135 }
00136 };
00137
00138
00139 template<typename It, typename T>
00140 struct replace_selector : public generic_for_each_selector<It>
00141 {
00142
00143 const T& new_val;
00144
00145
00146
00147 explicit
00148 replace_selector(const T &new_val) : new_val(new_val) {}
00149
00150
00151
00152
00153 bool
00154 operator()(T& v, It i)
00155 {
00156 if (*i == v)
00157 *i = new_val;
00158 return true;
00159 }
00160 };
00161
00162
00163 template<typename It, typename Op, typename T>
00164 struct replace_if_selector : public generic_for_each_selector<It>
00165 {
00166
00167 const T& new_val;
00168
00169
00170
00171 explicit
00172 replace_if_selector(const T &new_val) : new_val(new_val) { }
00173
00174
00175
00176
00177 bool
00178 operator()(Op& o, It i)
00179 {
00180 if (o(*i))
00181 *i = new_val;
00182 return true;
00183 }
00184 };
00185
00186
00187 template<typename It, typename Diff>
00188 struct count_selector : public generic_for_each_selector<It>
00189 {
00190
00191
00192
00193
00194 template<typename Val>
00195 Diff
00196 operator()(Val& v, It i)
00197 { return (v == *i) ? 1 : 0; }
00198 };
00199
00200
00201 template<typename It, typename Diff>
00202 struct count_if_selector : public generic_for_each_selector<It>
00203 {
00204
00205
00206
00207
00208 template<typename Op>
00209 Diff
00210 operator()(Op& o, It i)
00211 { return (o(*i)) ? 1 : 0; }
00212 };
00213
00214
00215 template<typename It>
00216 struct accumulate_selector : public generic_for_each_selector<It>
00217 {
00218
00219
00220
00221
00222 template<typename Op>
00223 typename std::iterator_traits<It>::value_type operator()(Op o, It i)
00224 { return *i; }
00225 };
00226
00227
00228 template<typename It, typename It2, typename T>
00229 struct inner_product_selector : public generic_for_each_selector<It>
00230 {
00231
00232 It begin1_iterator;
00233
00234
00235 It2 begin2_iterator;
00236
00237
00238
00239
00240 explicit
00241 inner_product_selector(It b1, It2 b2)
00242 : begin1_iterator(b1), begin2_iterator(b2) { }
00243
00244
00245
00246
00247
00248 template<typename Op>
00249 T
00250 operator()(Op mult, It current)
00251 {
00252 typename std::iterator_traits<It>::difference_type position
00253 = current - begin1_iterator;
00254 return mult(*current, *(begin2_iterator + position));
00255 }
00256 };
00257
00258
00259 template<typename It>
00260 struct identity_selector : public generic_for_each_selector<It>
00261 {
00262
00263
00264
00265
00266 template<typename Op>
00267 It
00268 operator()(Op o, It i)
00269 { return i; }
00270 };
00271
00272
00273
00274
00275 template<typename It>
00276 struct adjacent_difference_selector : public generic_for_each_selector<It>
00277 {
00278 template<typename Op>
00279 bool
00280 operator()(Op& o, It i)
00281 {
00282 typename It::first_type go_back_one = i.first;
00283 --go_back_one;
00284 *i.second = o(*i.first, *go_back_one);
00285 return true;
00286 }
00287 };
00288
00289
00290
00291
00292
00293
00294
00295 struct nothing
00296 {
00297
00298
00299 template<typename It>
00300 void
00301 operator()(It i) { }
00302 };
00303
00304
00305 struct dummy_reduct
00306 {
00307 bool
00308 operator()(bool , bool ) const
00309 { return true; }
00310 };
00311
00312
00313 template<typename Comp, typename It>
00314 struct min_element_reduct
00315 {
00316 Comp& comp;
00317
00318 explicit
00319 min_element_reduct(Comp &c) : comp(c) { }
00320
00321 It
00322 operator()(It x, It y)
00323 {
00324 if (comp(*x, *y))
00325 return x;
00326 else
00327 return y;
00328 }
00329 };
00330
00331
00332 template<typename Comp, typename It>
00333 struct max_element_reduct
00334 {
00335 Comp& comp;
00336
00337 explicit
00338 max_element_reduct(Comp& c) : comp(c) { }
00339
00340 It
00341 operator()(It x, It y)
00342 {
00343 if (comp(*x, *y))
00344 return y;
00345 else
00346 return x;
00347 }
00348 };
00349
00350
00351 template<typename BinOp>
00352 struct accumulate_binop_reduct
00353 {
00354 BinOp& binop;
00355
00356 explicit
00357 accumulate_binop_reduct(BinOp& b) : binop(b) { }
00358
00359 template<typename Result, typename Addend>
00360 Result
00361 operator()(const Result& x, const Addend& y)
00362 { return binop(x, y); }
00363 };
00364 }
00365
00366 #endif