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