3 // Copyright (C) 2007, 2008, 2009, 2010 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file parallel/algobase.h
26 * @brief Parallel STL function calls corresponding to the
27 * stl_algobase.h header. The functions defined here mainly do case
28 * switches and call the actual parallelized versions in other files.
29 * Inlining policy: Functions that basically only contain one
30 * function call, are declared inline.
31 * This file is a GNU parallel extension to the Standard C++ Library.
34 // Written by Johannes Singler and Felix Putze.
36 #ifndef _GLIBCXX_PARALLEL_ALGOBASE_H
37 #define _GLIBCXX_PARALLEL_ALGOBASE_H 1
39 #include <bits/stl_algobase.h>
40 #include <parallel/base.h>
41 #include <parallel/tags.h>
42 #include <parallel/settings.h>
43 #include <parallel/find.h>
44 #include <parallel/find_selectors.h>
46 namespace std
_GLIBCXX_VISIBILITY(default)
50 // NB: equal and lexicographical_compare require mismatch.
52 // Sequential fallback
53 template<typename _IIter1
, typename _IIter2
>
54 inline pair
<_IIter1
, _IIter2
>
55 mismatch(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
,
56 __gnu_parallel::sequential_tag
)
57 { return _GLIBCXX_STD_A::mismatch(__begin1
, __end1
, __begin2
); }
59 // Sequential fallback
60 template<typename _IIter1
, typename _IIter2
, typename _Predicate
>
61 inline pair
<_IIter1
, _IIter2
>
62 mismatch(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
,
63 _Predicate __pred
, __gnu_parallel::sequential_tag
)
64 { return _GLIBCXX_STD_A::mismatch(__begin1
, __end1
, __begin2
, __pred
); }
66 // Sequential fallback for input iterator case
67 template<typename _IIter1
, typename _IIter2
,
68 typename _Predicate
, typename _IteratorTag1
, typename _IteratorTag2
>
69 inline pair
<_IIter1
, _IIter2
>
70 __mismatch_switch(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
,
71 _Predicate __pred
, _IteratorTag1
, _IteratorTag2
)
72 { return _GLIBCXX_STD_A::mismatch(__begin1
, __end1
, __begin2
, __pred
); }
74 // Parallel mismatch for random access iterators
75 template<typename _RAIter1
, typename _RAIter2
, typename _Predicate
>
76 pair
<_RAIter1
, _RAIter2
>
77 __mismatch_switch(_RAIter1 __begin1
, _RAIter1 __end1
,
78 _RAIter2 __begin2
, _Predicate __pred
,
79 random_access_iterator_tag
, random_access_iterator_tag
)
81 if (_GLIBCXX_PARALLEL_CONDITION(true))
84 __gnu_parallel::__find_template(__begin1
, __end1
, __begin2
, __pred
,
86 __mismatch_selector()).first
;
87 return make_pair(__res
, __begin2
+ (__res
- __begin1
));
90 return _GLIBCXX_STD_A::mismatch(__begin1
, __end1
, __begin2
, __pred
);
94 template<typename _IIter1
, typename _IIter2
>
95 inline pair
<_IIter1
, _IIter2
>
96 mismatch(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
)
98 typedef std::iterator_traits
<_IIter1
> _Iterator1Traits
;
99 typedef std::iterator_traits
<_IIter2
> _Iterator2Traits
;
100 typedef typename
_Iterator1Traits::value_type _ValueType1
;
101 typedef typename
_Iterator2Traits::value_type _ValueType2
;
102 typedef typename
_Iterator1Traits::iterator_category _IteratorCategory1
;
103 typedef typename
_Iterator2Traits::iterator_category _IteratorCategory2
;
105 typedef __gnu_parallel::_EqualTo
<_ValueType1
, _ValueType2
> _EqualTo
;
107 return __mismatch_switch(__begin1
, __end1
, __begin2
, _EqualTo(),
108 _IteratorCategory1(), _IteratorCategory2());
112 template<typename _IIter1
, typename _IIter2
, typename _Predicate
>
113 inline pair
<_IIter1
, _IIter2
>
114 mismatch(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
,
117 typedef std::iterator_traits
<_IIter1
> _Iterator1Traits
;
118 typedef std::iterator_traits
<_IIter2
> _Iterator2Traits
;
119 typedef typename
_Iterator1Traits::iterator_category _IteratorCategory1
;
120 typedef typename
_Iterator2Traits::iterator_category _IteratorCategory2
;
122 return __mismatch_switch(__begin1
, __end1
, __begin2
, __pred
,
123 _IteratorCategory1(), _IteratorCategory2());
126 // Sequential fallback
127 template<typename _IIter1
, typename _IIter2
>
129 equal(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
,
130 __gnu_parallel::sequential_tag
)
131 { return _GLIBCXX_STD_A::equal(__begin1
, __end1
, __begin2
); }
133 // Sequential fallback
134 template<typename _IIter1
, typename _IIter2
, typename _Predicate
>
136 equal(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
,
137 _Predicate __pred
, __gnu_parallel::sequential_tag
)
138 { return _GLIBCXX_STD_A::equal(__begin1
, __end1
, __begin2
, __pred
); }
141 template<typename _IIter1
, typename _IIter2
>
143 equal(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
)
145 return __gnu_parallel::mismatch(__begin1
, __end1
, __begin2
).first
150 template<typename _IIter1
, typename _IIter2
, typename _Predicate
>
152 equal(_IIter1 __begin1
, _IIter1 __end1
, _IIter2 __begin2
,
155 return __gnu_parallel::mismatch(__begin1
, __end1
, __begin2
, __pred
).first
159 // Sequential fallback
160 template<typename _IIter1
, typename _IIter2
>
162 lexicographical_compare(_IIter1 __begin1
, _IIter1 __end1
,
163 _IIter2 __begin2
, _IIter2 __end2
,
164 __gnu_parallel::sequential_tag
)
165 { return _GLIBCXX_STD_A::lexicographical_compare(__begin1
, __end1
,
168 // Sequential fallback
169 template<typename _IIter1
, typename _IIter2
, typename _Predicate
>
171 lexicographical_compare(_IIter1 __begin1
, _IIter1 __end1
,
172 _IIter2 __begin2
, _IIter2 __end2
,
173 _Predicate __pred
, __gnu_parallel::sequential_tag
)
174 { return _GLIBCXX_STD_A::lexicographical_compare(
175 __begin1
, __end1
, __begin2
, __end2
, __pred
); }
177 // Sequential fallback for input iterator case
178 template<typename _IIter1
, typename _IIter2
,
179 typename _Predicate
, typename _IteratorTag1
, typename _IteratorTag2
>
181 __lexicographical_compare_switch(_IIter1 __begin1
, _IIter1 __end1
,
182 _IIter2 __begin2
, _IIter2 __end2
,
184 _IteratorTag1
, _IteratorTag2
)
185 { return _GLIBCXX_STD_A::lexicographical_compare(
186 __begin1
, __end1
, __begin2
, __end2
, __pred
); }
188 // Parallel lexicographical_compare for random access iterators
189 // Limitation: Both valuetypes must be the same
190 template<typename _RAIter1
, typename _RAIter2
, typename _Predicate
>
192 __lexicographical_compare_switch(_RAIter1 __begin1
, _RAIter1 __end1
,
193 _RAIter2 __begin2
, _RAIter2 __end2
,
195 random_access_iterator_tag
,
196 random_access_iterator_tag
)
198 if (_GLIBCXX_PARALLEL_CONDITION(true))
200 typedef iterator_traits
<_RAIter1
> _TraitsType1
;
201 typedef typename
_TraitsType1::value_type _ValueType1
;
203 typedef iterator_traits
<_RAIter2
> _TraitsType2
;
204 typedef typename
_TraitsType2::value_type _ValueType2
;
206 typedef __gnu_parallel::
207 _EqualFromLess
<_ValueType1
, _ValueType2
, _Predicate
>
208 _EqualFromLessCompare
;
210 // Longer sequence in first place.
211 if ((__end1
- __begin1
) < (__end2
- __begin2
))
213 typedef pair
<_RAIter1
, _RAIter2
> _SpotType
;
214 _SpotType __mm
= __mismatch_switch(__begin1
, __end1
, __begin2
,
215 _EqualFromLessCompare(__pred
),
216 random_access_iterator_tag(),
217 random_access_iterator_tag());
219 return (__mm
.first
== __end1
)
220 || bool(__pred(*__mm
.first
, *__mm
.second
));
224 typedef pair
<_RAIter2
, _RAIter1
> _SpotType
;
225 _SpotType __mm
= __mismatch_switch(__begin2
, __end2
, __begin1
,
226 _EqualFromLessCompare(__pred
),
227 random_access_iterator_tag(),
228 random_access_iterator_tag());
230 return (__mm
.first
!= __end2
)
231 && bool(__pred(*__mm
.second
, *__mm
.first
));
235 return _GLIBCXX_STD_A::lexicographical_compare(
236 __begin1
, __end1
, __begin2
, __end2
, __pred
);
240 template<typename _IIter1
, typename _IIter2
>
242 lexicographical_compare(_IIter1 __begin1
, _IIter1 __end1
,
243 _IIter2 __begin2
, _IIter2 __end2
)
245 typedef iterator_traits
<_IIter1
> _TraitsType1
;
246 typedef typename
_TraitsType1::value_type _ValueType1
;
247 typedef typename
_TraitsType1::iterator_category _IteratorCategory1
;
249 typedef iterator_traits
<_IIter2
> _TraitsType2
;
250 typedef typename
_TraitsType2::value_type _ValueType2
;
251 typedef typename
_TraitsType2::iterator_category _IteratorCategory2
;
252 typedef __gnu_parallel::_Less
<_ValueType1
, _ValueType2
> _LessType
;
254 return __lexicographical_compare_switch(
255 __begin1
, __end1
, __begin2
, __end2
, _LessType(),
256 _IteratorCategory1(), _IteratorCategory2());
260 template<typename _IIter1
, typename _IIter2
, typename _Predicate
>
262 lexicographical_compare(_IIter1 __begin1
, _IIter1 __end1
,
263 _IIter2 __begin2
, _IIter2 __end2
,
266 typedef iterator_traits
<_IIter1
> _TraitsType1
;
267 typedef typename
_TraitsType1::iterator_category _IteratorCategory1
;
269 typedef iterator_traits
<_IIter2
> _TraitsType2
;
270 typedef typename
_TraitsType2::iterator_category _IteratorCategory2
;
272 return __lexicographical_compare_switch(
273 __begin1
, __end1
, __begin2
, __end2
, __pred
,
274 _IteratorCategory1(), _IteratorCategory2());
279 #endif /* _GLIBCXX_PARALLEL_ALGOBASE_H */