]>
Commit | Line | Data |
---|---|---|
c2ba9709 JS |
1 | // -*- C++ -*- |
2 | ||
748086b7 | 3 | // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. |
c2ba9709 JS |
4 | // |
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 | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
c2ba9709 JS |
9 | // version. |
10 | ||
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. | |
15 | ||
748086b7 JJ |
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. | |
19 | ||
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/>. | |
c2ba9709 JS |
24 | |
25 | /** @file parallel/find_selectors.h | |
1acba85b | 26 | * @brief _Function objects representing different tasks to be plugged |
c2ba9709 JS |
27 | * into the parallel find algorithm. |
28 | * This file is a GNU parallel extension to the Standard C++ Library. | |
29 | */ | |
30 | ||
31 | // Written by Felix Putze. | |
32 | ||
cbcd1e45 JS |
33 | #ifndef _GLIBCXX_PARALLEL_FIND_SELECTORS_H |
34 | #define _GLIBCXX_PARALLEL_FIND_SELECTORS_H 1 | |
c2ba9709 JS |
35 | |
36 | #include <parallel/tags.h> | |
37 | #include <parallel/basic_iterator.h> | |
38 | #include <bits/stl_pair.h> | |
39 | ||
40 | namespace __gnu_parallel | |
41 | { | |
1acba85b JS |
42 | /** @brief Base class of all __gnu_parallel::__find_template selectors. */ |
43 | struct __generic_find_selector | |
c2ba9709 JS |
44 | { }; |
45 | ||
46 | /** | |
47 | * @brief Test predicate on a single element, used for std::find() | |
48 | * and std::find_if (). | |
49 | */ | |
1acba85b | 50 | struct __find_if_selector : public __generic_find_selector |
c2ba9709 | 51 | { |
721641c4 | 52 | /** @brief Test on one position. |
1acba85b JS |
53 | * @param __i1 _Iterator on first sequence. |
54 | * @param __i2 _Iterator on second sequence (unused). | |
55 | * @param __pred Find predicate. | |
c2ba9709 | 56 | */ |
1acba85b | 57 | template<typename _RAIter1, typename _RAIter2, |
15ac3c72 | 58 | typename _Pred> |
531898c3 | 59 | bool |
1acba85b JS |
60 | operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred) |
61 | { return __pred(*__i1); } | |
c2ba9709 JS |
62 | |
63 | /** @brief Corresponding sequential algorithm on a sequence. | |
1acba85b JS |
64 | * @param __begin1 Begin iterator of first sequence. |
65 | * @param __end1 End iterator of first sequence. | |
66 | * @param __begin2 Begin iterator of second sequence. | |
67 | * @param __pred Find predicate. | |
c2ba9709 | 68 | */ |
1acba85b | 69 | template<typename _RAIter1, typename _RAIter2, |
15ac3c72 | 70 | typename _Pred> |
1acba85b JS |
71 | std::pair<_RAIter1, _RAIter2> |
72 | _M_sequential_algorithm(_RAIter1 __begin1, | |
15ac3c72 JS |
73 | _RAIter1 __end1, |
74 | _RAIter2 __begin2, _Pred __pred) | |
1acba85b | 75 | { return std::make_pair(find_if(__begin1, __end1, __pred, |
15ac3c72 | 76 | sequential_tag()), __begin2); } |
c2ba9709 JS |
77 | }; |
78 | ||
721641c4 | 79 | /** @brief Test predicate on two adjacent elements. */ |
1acba85b | 80 | struct __adjacent_find_selector : public __generic_find_selector |
c2ba9709 | 81 | { |
721641c4 | 82 | /** @brief Test on one position. |
1acba85b JS |
83 | * @param __i1 _Iterator on first sequence. |
84 | * @param __i2 _Iterator on second sequence (unused). | |
85 | * @param __pred Find predicate. | |
c2ba9709 | 86 | */ |
1acba85b | 87 | template<typename _RAIter1, typename _RAIter2, |
15ac3c72 | 88 | typename _Pred> |
531898c3 | 89 | bool |
1acba85b | 90 | operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred) |
531898c3 | 91 | { |
15ac3c72 JS |
92 | // Passed end iterator is one short. |
93 | return __pred(*__i1, *(__i1 + 1)); | |
531898c3 | 94 | } |
c2ba9709 JS |
95 | |
96 | /** @brief Corresponding sequential algorithm on a sequence. | |
1acba85b JS |
97 | * @param __begin1 Begin iterator of first sequence. |
98 | * @param __end1 End iterator of first sequence. | |
99 | * @param __begin2 Begin iterator of second sequence. | |
100 | * @param __pred Find predicate. | |
c2ba9709 | 101 | */ |
1acba85b | 102 | template<typename _RAIter1, typename _RAIter2, |
15ac3c72 | 103 | typename _Pred> |
1acba85b JS |
104 | std::pair<_RAIter1, _RAIter2> |
105 | _M_sequential_algorithm(_RAIter1 __begin1, | |
15ac3c72 JS |
106 | _RAIter1 __end1, |
107 | _RAIter2 __begin2, _Pred __pred) | |
531898c3 | 108 | { |
15ac3c72 JS |
109 | // Passed end iterator is one short. |
110 | _RAIter1 spot = adjacent_find(__begin1, __end1 + 1, | |
111 | __pred, sequential_tag()); | |
112 | if (spot == (__end1 + 1)) | |
113 | spot = __end1; | |
114 | return std::make_pair(spot, __begin2); | |
531898c3 | 115 | } |
c2ba9709 JS |
116 | }; |
117 | ||
118 | /** @brief Test inverted predicate on a single element. */ | |
1acba85b | 119 | struct __mismatch_selector : public __generic_find_selector |
c2ba9709 JS |
120 | { |
121 | /** | |
721641c4 | 122 | * @brief Test on one position. |
1acba85b JS |
123 | * @param __i1 _Iterator on first sequence. |
124 | * @param __i2 _Iterator on second sequence (unused). | |
125 | * @param __pred Find predicate. | |
c2ba9709 | 126 | */ |
1acba85b | 127 | template<typename _RAIter1, typename _RAIter2, |
15ac3c72 | 128 | typename _Pred> |
531898c3 | 129 | bool |
1acba85b JS |
130 | operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred) |
131 | { return !__pred(*__i1, *__i2); } | |
c2ba9709 JS |
132 | |
133 | /** | |
134 | * @brief Corresponding sequential algorithm on a sequence. | |
1acba85b JS |
135 | * @param __begin1 Begin iterator of first sequence. |
136 | * @param __end1 End iterator of first sequence. | |
137 | * @param __begin2 Begin iterator of second sequence. | |
138 | * @param __pred Find predicate. | |
c2ba9709 | 139 | */ |
1acba85b | 140 | template<typename _RAIter1, typename _RAIter2, |
15ac3c72 | 141 | typename _Pred> |
1acba85b JS |
142 | std::pair<_RAIter1, _RAIter2> |
143 | _M_sequential_algorithm(_RAIter1 __begin1, | |
15ac3c72 JS |
144 | _RAIter1 __end1, |
145 | _RAIter2 __begin2, _Pred __pred) | |
146 | { return mismatch(__begin1, __end1, __begin2, __pred, sequential_tag()); | |
147 | } | |
c2ba9709 JS |
148 | }; |
149 | ||
150 | ||
721641c4 | 151 | /** @brief Test predicate on several elements. */ |
15ac3c72 | 152 | template<typename _FIterator> |
1acba85b | 153 | struct __find_first_of_selector : public __generic_find_selector |
c2ba9709 | 154 | { |
15ac3c72 JS |
155 | _FIterator _M_begin; |
156 | _FIterator _M_end; | |
1acba85b | 157 | |
15ac3c72 | 158 | explicit __find_first_of_selector(_FIterator __begin, _FIterator __end) |
54384f7f | 159 | : _M_begin(__begin), _M_end(__end) { } |
1acba85b | 160 | |
721641c4 | 161 | /** @brief Test on one position. |
1acba85b JS |
162 | * @param __i1 _Iterator on first sequence. |
163 | * @param __i2 _Iterator on second sequence (unused). | |
164 | * @param __pred Find predicate. */ | |
165 | template<typename _RAIter1, typename _RAIter2, | |
15ac3c72 | 166 | typename _Pred> |
531898c3 | 167 | bool |
1acba85b | 168 | operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred) |
531898c3 | 169 | { |
15ac3c72 JS |
170 | for (_FIterator __pos_in_candidates = _M_begin; |
171 | __pos_in_candidates != _M_end; ++__pos_in_candidates) | |
172 | if (__pred(*__i1, *__pos_in_candidates)) | |
173 | return true; | |
174 | return false; | |
531898c3 | 175 | } |
c2ba9709 JS |
176 | |
177 | /** @brief Corresponding sequential algorithm on a sequence. | |
1acba85b JS |
178 | * @param __begin1 Begin iterator of first sequence. |
179 | * @param __end1 End iterator of first sequence. | |
180 | * @param __begin2 Begin iterator of second sequence. | |
181 | * @param __pred Find predicate. */ | |
182 | template<typename _RAIter1, typename _RAIter2, | |
15ac3c72 | 183 | typename _Pred> |
1acba85b JS |
184 | std::pair<_RAIter1, _RAIter2> |
185 | _M_sequential_algorithm(_RAIter1 __begin1, | |
15ac3c72 JS |
186 | _RAIter1 __end1, |
187 | _RAIter2 __begin2, _Pred __pred) | |
188 | { | |
189 | return std::make_pair( | |
190 | find_first_of(__begin1, __end1, _M_begin, _M_end, __pred, | |
191 | sequential_tag()), __begin2); | |
192 | } | |
c2ba9709 JS |
193 | }; |
194 | } | |
195 | ||
cbcd1e45 | 196 | #endif /* _GLIBCXX_PARALLEL_FIND_SELECTORS_H */ |