3 // Copyright (C) 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file parallel/base.h
32 * @brief Sequential helper functions.
33 * This file is a GNU parallel extension to the Standard C++ Library.
36 // Written by Johannes Singler.
38 #ifndef _GLIBCXX_PARALLEL_BASE_H
39 #define _GLIBCXX_PARALLEL_BASE_H 1
41 #include <parallel/features.h>
43 #include <parallel/basic_iterator.h>
44 #include <parallel/parallel.h>
47 namespace __gnu_parallel
49 // XXX remove std::duplicates from here if possible,
50 // XXX but keep minimal dependencies.
52 /** @brief Calculates the rounded-down logarithm of @c n for base 2.
54 * @return Returns 0 for argument 0.
56 template<typename Size
>
61 for (k
= 0; n
!= 1; n
>>= 1)
66 /** @brief Encode two integers into one __gnu_parallel::lcas_t.
67 * @param a First integer, to be encoded in the most-significant @c
69 * @param b Second integer, to be encoded in the least-significant
70 * @c lcas_t_bits/2 bits.
71 * @return __gnu_parallel::lcas_t value encoding @c a and @c b.
75 encode2(int a
, int b
) //must all be non-negative, actually
77 return (((lcas_t
)a
) << (lcas_t_bits
/ 2)) | (((lcas_t
)b
) << 0);
80 /** @brief Decode two integers from one __gnu_parallel::lcas_t.
81 * @param x __gnu_parallel::lcas_t to decode integers from.
82 * @param a First integer, to be decoded from the most-significant
83 * @c lcas_t_bits/2 bits of @c x.
84 * @param b Second integer, to be encoded in the least-significant
85 * @c lcas_t_bits/2 bits of @c x.
89 decode2(lcas_t x
, int& a
, int& b
)
91 a
= (int)((x
>> (lcas_t_bits
/ 2)) & lcas_t_mask
);
92 b
= (int)((x
>> 0 ) & lcas_t_mask
);
95 /** @brief Constructs predicate for equality from strict weak
98 // XXX comparator at the end, as per others
99 template<typename Comparator
, typename T1
, typename T2
>
100 class equal_from_less
: public std::binary_function
<T1
, T2
, bool>
106 equal_from_less(Comparator
& _comp
) : comp(_comp
) { }
108 bool operator()(const T1
& a
, const T2
& b
)
110 return !comp(a
, b
) && !comp(b
, a
);
115 /** @brief Similar to std::equal_to, but allows two different types. */
116 template<typename T1
, typename T2
>
117 struct equal_to
: std::binary_function
<T1
, T2
, bool>
119 bool operator()(const T1
& t1
, const T2
& t2
) const
123 /** @brief Similar to std::binder1st, but giving the argument types explicitly. */
124 template<typename _Predicate
, typename argument_type
>
126 : public std::unary_function
<argument_type
, bool>
133 unary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
136 operator()(const argument_type
& __x
)
137 { return !_M_pred(__x
); }
140 /** @brief Similar to std::binder1st, but giving the argument types explicitly. */
141 template<typename _Operation
, typename first_argument_type
, typename second_argument_type
, typename result_type
>
143 : public std::unary_function
<second_argument_type
, result_type
>
147 first_argument_type value
;
150 binder1st(const _Operation
& __x
,
151 const first_argument_type
& __y
)
152 : op(__x
), value(__y
) { }
155 operator()(const second_argument_type
& __x
)
156 { return op(value
, __x
); }
158 // _GLIBCXX_RESOLVE_LIB_DEFECTS
159 // 109. Missing binders for non-const sequence elements
161 operator()(second_argument_type
& __x
) const
162 { return op(value
, __x
); }
166 * @brief Similar to std::binder2nd, but giving the argument types
169 template<typename _Operation
, typename first_argument_type
, typename second_argument_type
, typename result_type
>
171 : public std::unary_function
<first_argument_type
, result_type
>
175 second_argument_type value
;
178 binder2nd(const _Operation
& __x
,
179 const second_argument_type
& __y
)
180 : op(__x
), value(__y
) { }
183 operator()(const first_argument_type
& __x
) const
184 { return op(__x
, value
); }
186 // _GLIBCXX_RESOLVE_LIB_DEFECTS
187 // 109. Missing binders for non-const sequence elements
189 operator()(first_argument_type
& __x
)
190 { return op(__x
, value
); }
193 /** @brief Similar to std::less, but allows two different types. */
194 template<typename T1
, typename T2
>
195 struct less
: std::binary_function
<T1
, T2
, bool>
198 operator()(const T1
& t1
, const T2
& t2
) const
202 operator()(const T2
& t2
, const T1
& t1
) const
206 // Partial specialization for one type. Same as std::less.
207 template<typename _Tp
>
208 struct less
<_Tp
, _Tp
> : public std::binary_function
<_Tp
, _Tp
, bool>
211 operator()(const _Tp
& __x
, const _Tp
& __y
) const
212 { return __x
< __y
; }
215 template<typename T
, typename _DifferenceTp
>
216 class pseudo_sequence
;
218 /** @brief Iterator associated with __gnu_parallel::pseudo_sequence.
219 * If features the usual random-access iterator functionality.
220 * @param T Sequence value type.
221 * @param difference_type Sequence difference type.
223 template<typename T
, typename _DifferenceTp
>
224 class pseudo_sequence_iterator
227 typedef _DifferenceTp difference_type
;
230 typedef pseudo_sequence_iterator
<T
, _DifferenceTp
> type
;
236 pseudo_sequence_iterator(const T
& val
, difference_type pos
)
237 : val(val
), pos(pos
) { }
239 // Pre-increment operator.
247 // Post-increment operator.
250 { return type(pos
++); }
257 operator[](difference_type
) const
261 operator==(const type
& i2
)
262 { return pos
== i2
.pos
; }
265 operator!=(const type
& i2
)
266 { return pos
!= i2
.pos
; }
269 operator-(const type
& i2
)
270 { return pos
- i2
.pos
; }
273 /** @brief Sequence that conceptually consists of multiple copies of
275 * The copies are not stored explicitly, of course.
276 * @param T Sequence value type.
277 * @param difference_type Sequence difference type.
279 template<typename T
, typename _DifferenceTp
>
280 class pseudo_sequence
282 typedef pseudo_sequence
<T
, _DifferenceTp
> type
;
285 typedef _DifferenceTp difference_type
;
287 // Better case down to uint64, than up to _DifferenceTp.
288 typedef pseudo_sequence_iterator
<T
, uint64
> iterator
;
290 /** @brief Constructor.
291 * @param val Element of the sequence.
292 * @param count Number of (virtual) copies.
294 pseudo_sequence(const T
& val
, difference_type count
)
295 : val(val
), count(count
) { }
297 /** @brief Begin iterator. */
300 { return iterator(val
, 0); }
302 /** @brief End iterator. */
305 { return iterator(val
, count
); }
309 difference_type count
;
312 /** @brief Functor that does nothing */
313 template<typename _ValueTp
>
317 operator()(const _ValueTp
& v
) const { }
320 /** @brief Compute the median of three referenced elements,
321 according to @c comp.
322 * @param a First iterator.
323 * @param b Second iterator.
324 * @param c Third iterator.
325 * @param comp Comparator.
327 template<typename RandomAccessIterator
, typename Comparator
>
329 median_of_three_iterators(RandomAccessIterator a
, RandomAccessIterator b
,
330 RandomAccessIterator c
, Comparator
& comp
)
342 // Just swap a and b.
353 // Avoid the use of assert, because we're trying to keep the <cassert>
354 // include out of the mix. (Same as debug mode).
356 __replacement_assert(const char* __file
, int __line
,
357 const char* __function
, const char* __condition
)
359 std::printf("%s:%d: %s: Assertion '%s' failed.\n", __file
, __line
,
360 __function
, __condition
);
364 #define _GLIBCXX_PARALLEL_ASSERT(_Condition) \
368 __gnu_parallel::__replacement_assert(__FILE__, __LINE__, \
369 __PRETTY_FUNCTION__, #_Condition); \
372 } //namespace __gnu_parallel