3 // Copyright (C) 2007, 2008, 2009 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>
50 // NB: equal and lexicographical_compare require mismatch.
52 // Sequential fallback
53 template<typename InputIterator1, typename InputIterator2>
54 inline pair<InputIterator1, InputIterator2>
55 mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
56 __gnu_parallel::sequential_tag)
57 { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2); }
59 // Sequential fallback
60 template<typename InputIterator1, typename InputIterator2,
62 inline pair<InputIterator1, InputIterator2>
63 mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
64 Predicate pred, __gnu_parallel::sequential_tag)
65 { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
67 // Sequential fallback for input iterator case
68 template<typename InputIterator1, typename InputIterator2,
69 typename Predicate, typename IteratorTag1, typename IteratorTag2>
70 inline pair<InputIterator1, InputIterator2>
71 mismatch_switch(InputIterator1 begin1, InputIterator1 end1,
72 InputIterator2 begin2, Predicate pred, IteratorTag1,
74 { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
76 // Parallel mismatch for random access iterators
77 template<typename RandomAccessIterator1, typename RandomAccessIterator2,
79 pair<RandomAccessIterator1, RandomAccessIterator2>
80 mismatch_switch(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
81 RandomAccessIterator2 begin2, Predicate pred,
82 random_access_iterator_tag, random_access_iterator_tag)
84 if (_GLIBCXX_PARALLEL_CONDITION(true))
86 RandomAccessIterator1 res =
87 __gnu_parallel::find_template(begin1, end1, begin2, pred,
89 mismatch_selector()).first;
90 return make_pair(res , begin2 + (res - begin1));
93 return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred);
97 template<typename InputIterator1, typename InputIterator2>
98 inline pair<InputIterator1, InputIterator2>
99 mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
101 typedef std::iterator_traits<InputIterator1> iterator1_traits;
102 typedef std::iterator_traits<InputIterator2> iterator2_traits;
103 typedef typename iterator1_traits::value_type value1_type;
104 typedef typename iterator2_traits::value_type value2_type;
105 typedef typename iterator1_traits::iterator_category iterator1_category;
106 typedef typename iterator2_traits::iterator_category iterator2_category;
108 typedef __gnu_parallel::equal_to<value1_type, value2_type> equal_to_type;
110 return mismatch_switch(begin1, end1, begin2, equal_to_type(),
111 iterator1_category(), iterator2_category());
115 template<typename InputIterator1, typename InputIterator2,
117 inline pair<InputIterator1, InputIterator2>
118 mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
121 typedef std::iterator_traits<InputIterator1> iterator1_traits;
122 typedef std::iterator_traits<InputIterator2> iterator2_traits;
123 typedef typename iterator1_traits::iterator_category iterator1_category;
124 typedef typename iterator2_traits::iterator_category iterator2_category;
126 return mismatch_switch(begin1, end1, begin2, pred, iterator1_category(),
127 iterator2_category());
130 // Sequential fallback
131 template<typename InputIterator1, typename InputIterator2>
133 equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
134 __gnu_parallel::sequential_tag)
135 { return _GLIBCXX_STD_P::equal(begin1, end1, begin2); }
137 // Sequential fallback
138 template<typename InputIterator1, typename InputIterator2,
141 equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
142 Predicate pred, __gnu_parallel::sequential_tag)
143 { return _GLIBCXX_STD_P::equal(begin1, end1, begin2, pred); }
146 template<typename InputIterator1, typename InputIterator2>
148 equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
149 { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2).first == end1; }
152 template<typename InputIterator1, typename InputIterator2,
155 equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
158 return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred).first
162 // Sequential fallback
163 template<typename InputIterator1, typename InputIterator2>
165 lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
166 InputIterator2 begin2, InputIterator2 end2,
167 __gnu_parallel::sequential_tag)
168 { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
171 // Sequential fallback
172 template<typename InputIterator1, typename InputIterator2,
175 lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
176 InputIterator2 begin2, InputIterator2 end2,
177 Predicate pred, __gnu_parallel::sequential_tag)
178 { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
179 begin2, end2, pred); }
181 // Sequential fallback for input iterator case
182 template<typename InputIterator1, typename InputIterator2,
183 typename Predicate, typename IteratorTag1, typename IteratorTag2>
185 lexicographical_compare_switch(InputIterator1 begin1, InputIterator1 end1,
186 InputIterator2 begin2, InputIterator2 end2,
187 Predicate pred, IteratorTag1, IteratorTag2)
188 { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
189 begin2, end2, pred); }
191 // Parallel lexicographical_compare for random access iterators
192 // Limitation: Both valuetypes must be the same
193 template<typename RandomAccessIterator1, typename RandomAccessIterator2,
196 lexicographical_compare_switch(RandomAccessIterator1 begin1,
197 RandomAccessIterator1 end1,
198 RandomAccessIterator2 begin2,
199 RandomAccessIterator2 end2, Predicate pred,
200 random_access_iterator_tag,
201 random_access_iterator_tag)
203 if (_GLIBCXX_PARALLEL_CONDITION(true))
205 typedef iterator_traits<RandomAccessIterator1> traits1_type;
206 typedef typename traits1_type::value_type value1_type;
208 typedef iterator_traits<RandomAccessIterator2> traits2_type;
209 typedef typename traits2_type::value_type value2_type;
211 typedef __gnu_parallel::equal_from_less<Predicate, value1_type,
212 value2_type> equal_type;
214 // Longer sequence in first place.
215 if ((end1 - begin1) < (end2 - begin2))
217 typedef pair<RandomAccessIterator1, RandomAccessIterator2>
219 pair_type mm = mismatch_switch(begin1, end1, begin2,
221 random_access_iterator_tag(),
222 random_access_iterator_tag());
224 return (mm.first == end1) || bool(pred(*mm.first, *mm.second));
228 typedef pair<RandomAccessIterator2, RandomAccessIterator1>
230 pair_type mm = mismatch_switch(begin2, end2, begin1,
232 random_access_iterator_tag(),
233 random_access_iterator_tag());
235 return (mm.first != end2) && bool(pred(*mm.second, *mm.first));
239 return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
244 template<typename InputIterator1, typename InputIterator2>
246 lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
247 InputIterator2 begin2, InputIterator2 end2)
249 typedef iterator_traits<InputIterator1> traits1_type;
250 typedef typename traits1_type::value_type value1_type;
251 typedef typename traits1_type::iterator_category iterator1_category;
253 typedef iterator_traits<InputIterator2> traits2_type;
254 typedef typename traits2_type::value_type value2_type;
255 typedef typename traits2_type::iterator_category iterator2_category;
256 typedef __gnu_parallel::less<value1_type, value2_type> less_type;
258 return lexicographical_compare_switch(begin1, end1, begin2, end2,
259 less_type(), iterator1_category(),
260 iterator2_category());
264 template<typename InputIterator1, typename InputIterator2,
267 lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
268 InputIterator2 begin2, InputIterator2 end2,
271 typedef iterator_traits<InputIterator1> traits1_type;
272 typedef typename traits1_type::iterator_category iterator1_category;
274 typedef iterator_traits<InputIterator2> traits2_type;
275 typedef typename traits2_type::iterator_category iterator2_category;
277 return lexicographical_compare_switch(begin1, end1, begin2, end2, pred,
278 iterator1_category(),
279 iterator2_category());
284 #endif /* _GLIBCXX_PARALLEL_ALGOBASE_H */