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 #ifndef _GLIBCXX_PARALLEL_BASE_H
00033 #define _GLIBCXX_PARALLEL_BASE_H 1
00034
00035 #include <functional>
00036 #include <omp.h>
00037 #include <parallel/features.h>
00038 #include <parallel/basic_iterator.h>
00039 #include <parallel/parallel.h>
00040
00041
00042
00043
00044
00045
00046
00047
00048 namespace std
00049 {
00050 namespace __parallel { }
00051 }
00052
00053
00054
00055
00056
00057 namespace __gnu_parallel
00058 {
00059
00060 using namespace std::__parallel;
00061 }
00062
00063
00064
00065
00066
00067 namespace __gnu_sequential
00068 {
00069
00070 #ifdef _GLIBCXX_PARALLEL
00071 using namespace std::__norm;
00072 #else
00073 using namespace std;
00074 #endif
00075 }
00076
00077
00078 namespace __gnu_parallel
00079 {
00080
00081
00082
00083
00084 inline int
00085 get_max_threads()
00086 {
00087 int __i = omp_get_max_threads();
00088 return __i > 1 ? __i : 1;
00089 }
00090
00091
00092 inline bool
00093 is_parallel(const _Parallelism __p) { return __p != sequential; }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 template<typename Size>
00104 inline Size
00105 __log2(Size n)
00106 {
00107 Size k;
00108 for (k = 0; n > 1; n >>= 1)
00109 ++k;
00110 return k;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 inline lcas_t
00122 encode2(int a, int b)
00123 {
00124 return (((lcas_t)a) << (lcas_t_bits / 2)) | (((lcas_t)b) << 0);
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 inline void
00136 decode2(lcas_t x, int& a, int& b)
00137 {
00138 a = (int)((x >> (lcas_t_bits / 2)) & lcas_t_mask);
00139 b = (int)((x >> 0 ) & lcas_t_mask);
00140 }
00141
00142
00143 template<typename T>
00144 const T&
00145 min(const T& a, const T& b)
00146 { return (a < b) ? a : b; }
00147
00148
00149 template<typename T>
00150 const T&
00151 max(const T& a, const T& b)
00152 { return (a > b) ? a : b; }
00153
00154
00155
00156
00157
00158 template<typename Comparator, typename T1, typename T2>
00159 class equal_from_less : public std::binary_function<T1, T2, bool>
00160 {
00161 private:
00162 Comparator& comp;
00163
00164 public:
00165 equal_from_less(Comparator& _comp) : comp(_comp) { }
00166
00167 bool operator()(const T1& a, const T2& b)
00168 {
00169 return !comp(a, b) && !comp(b, a);
00170 }
00171 };
00172
00173
00174
00175
00176 template<typename _Predicate, typename argument_type>
00177 class unary_negate
00178 : public std::unary_function<argument_type, bool>
00179 {
00180 protected:
00181 _Predicate _M_pred;
00182
00183 public:
00184 explicit
00185 unary_negate(const _Predicate& __x) : _M_pred(__x) { }
00186
00187 bool
00188 operator()(const argument_type& __x)
00189 { return !_M_pred(__x); }
00190 };
00191
00192
00193
00194 template<typename _Operation, typename first_argument_type,
00195 typename second_argument_type, typename result_type>
00196 class binder1st
00197 : public std::unary_function<second_argument_type, result_type>
00198 {
00199 protected:
00200 _Operation op;
00201 first_argument_type value;
00202
00203 public:
00204 binder1st(const _Operation& __x,
00205 const first_argument_type& __y)
00206 : op(__x), value(__y) { }
00207
00208 result_type
00209 operator()(const second_argument_type& __x)
00210 { return op(value, __x); }
00211
00212
00213
00214 result_type
00215 operator()(second_argument_type& __x) const
00216 { return op(value, __x); }
00217 };
00218
00219
00220
00221
00222
00223 template<typename _Operation, typename first_argument_type,
00224 typename second_argument_type, typename result_type>
00225 class binder2nd
00226 : public std::unary_function<first_argument_type, result_type>
00227 {
00228 protected:
00229 _Operation op;
00230 second_argument_type value;
00231
00232 public:
00233 binder2nd(const _Operation& __x,
00234 const second_argument_type& __y)
00235 : op(__x), value(__y) { }
00236
00237 result_type
00238 operator()(const first_argument_type& __x) const
00239 { return op(__x, value); }
00240
00241
00242
00243 result_type
00244 operator()(first_argument_type& __x)
00245 { return op(__x, value); }
00246 };
00247
00248
00249 template<typename T1, typename T2>
00250 struct equal_to : std::binary_function<T1, T2, bool>
00251 {
00252 bool operator()(const T1& t1, const T2& t2) const
00253 { return t1 == t2; }
00254 };
00255
00256
00257 template<typename T1, typename T2>
00258 struct less : std::binary_function<T1, T2, bool>
00259 {
00260 bool
00261 operator()(const T1& t1, const T2& t2) const
00262 { return t1 < t2; }
00263
00264 bool
00265 operator()(const T2& t2, const T1& t1) const
00266 { return t2 < t1; }
00267 };
00268
00269
00270 template<typename _Tp>
00271 struct less<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, bool>
00272 {
00273 bool
00274 operator()(const _Tp& __x, const _Tp& __y) const
00275 { return __x < __y; }
00276 };
00277
00278
00279
00280 template<typename _Tp1, typename _Tp2>
00281 struct plus : public std::binary_function<_Tp1, _Tp2, _Tp1>
00282 {
00283 typedef __typeof__(*static_cast<_Tp1*>(NULL)
00284 + *static_cast<_Tp2*>(NULL)) result;
00285
00286 result
00287 operator()(const _Tp1& __x, const _Tp2& __y) const
00288 { return __x + __y; }
00289 };
00290
00291
00292 template<typename _Tp>
00293 struct plus<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, _Tp>
00294 {
00295 typedef __typeof__(*static_cast<_Tp*>(NULL)
00296 + *static_cast<_Tp*>(NULL)) result;
00297
00298 result
00299 operator()(const _Tp& __x, const _Tp& __y) const
00300 { return __x + __y; }
00301 };
00302
00303
00304
00305 template<typename _Tp1, typename _Tp2>
00306 struct multiplies : public std::binary_function<_Tp1, _Tp2, _Tp1>
00307 {
00308 typedef __typeof__(*static_cast<_Tp1*>(NULL)
00309 * *static_cast<_Tp2*>(NULL)) result;
00310
00311 result
00312 operator()(const _Tp1& __x, const _Tp2& __y) const
00313 { return __x * __y; }
00314 };
00315
00316
00317 template<typename _Tp>
00318 struct multiplies<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, _Tp>
00319 {
00320 typedef __typeof__(*static_cast<_Tp*>(NULL)
00321 * *static_cast<_Tp*>(NULL)) result;
00322
00323 result
00324 operator()(const _Tp& __x, const _Tp& __y) const
00325 { return __x * __y; }
00326 };
00327
00328
00329 template<typename T, typename _DifferenceTp>
00330 class pseudo_sequence;
00331
00332
00333
00334
00335
00336
00337 template<typename T, typename _DifferenceTp>
00338 class pseudo_sequence_iterator
00339 {
00340 public:
00341 typedef _DifferenceTp difference_type;
00342
00343 private:
00344 typedef pseudo_sequence_iterator<T, _DifferenceTp> type;
00345
00346 const T& val;
00347 difference_type pos;
00348
00349 public:
00350 pseudo_sequence_iterator(const T& val, difference_type pos)
00351 : val(val), pos(pos) { }
00352
00353
00354 type&
00355 operator++()
00356 {
00357 ++pos;
00358 return *this;
00359 }
00360
00361
00362 const type
00363 operator++(int)
00364 { return type(pos++); }
00365
00366 const T&
00367 operator*() const
00368 { return val; }
00369
00370 const T&
00371 operator[](difference_type) const
00372 { return val; }
00373
00374 bool
00375 operator==(const type& i2)
00376 { return pos == i2.pos; }
00377
00378 difference_type
00379 operator!=(const type& i2)
00380 { return pos != i2.pos; }
00381
00382 difference_type
00383 operator-(const type& i2)
00384 { return pos - i2.pos; }
00385 };
00386
00387
00388
00389
00390
00391
00392
00393 template<typename T, typename _DifferenceTp>
00394 class pseudo_sequence
00395 {
00396 typedef pseudo_sequence<T, _DifferenceTp> type;
00397
00398 public:
00399 typedef _DifferenceTp difference_type;
00400
00401
00402 typedef pseudo_sequence_iterator<T, uint64> iterator;
00403
00404
00405
00406
00407
00408 pseudo_sequence(const T& val, difference_type count)
00409 : val(val), count(count) { }
00410
00411
00412 iterator
00413 begin() const
00414 { return iterator(val, 0); }
00415
00416
00417 iterator
00418 end() const
00419 { return iterator(val, count); }
00420
00421 private:
00422 const T& val;
00423 difference_type count;
00424 };
00425
00426
00427 template<typename _ValueTp>
00428 class void_functor
00429 {
00430 inline void
00431 operator()(const _ValueTp& v) const { }
00432 };
00433
00434
00435
00436
00437
00438
00439
00440
00441 template<typename RandomAccessIterator, typename Comparator>
00442 RandomAccessIterator
00443 median_of_three_iterators(RandomAccessIterator a, RandomAccessIterator b,
00444 RandomAccessIterator c, Comparator& comp)
00445 {
00446 if (comp(*a, *b))
00447 if (comp(*b, *c))
00448 return b;
00449 else
00450 if (comp(*a, *c))
00451 return c;
00452 else
00453 return a;
00454 else
00455 {
00456
00457 if (comp(*a, *c))
00458 return a;
00459 else
00460 if (comp(*b, *c))
00461 return c;
00462 else
00463 return b;
00464 }
00465 }
00466
00467 #define _GLIBCXX_PARALLEL_ASSERT(_Condition) __glibcxx_assert(_Condition)
00468
00469 }
00470
00471 #endif