xref: /dflybsd-src/contrib/gcc-4.7/libstdc++-v3/include/parallel/find_selectors.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino // -*- C++ -*-
2*e4b17023SJohn Marino 
3*e4b17023SJohn Marino // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4*e4b17023SJohn Marino //
5*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library.  This library is free
6*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the terms
7*e4b17023SJohn Marino // of the GNU General Public License as published by the Free Software
8*e4b17023SJohn Marino // Foundation; either version 3, or (at your option) any later
9*e4b17023SJohn Marino // version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful, but
12*e4b17023SJohn Marino // WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*e4b17023SJohn Marino // General Public License for more details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino /** @file parallel/find_selectors.h
26*e4b17023SJohn Marino  *  @brief _Function objects representing different tasks to be plugged
27*e4b17023SJohn Marino  *  into the parallel find algorithm.
28*e4b17023SJohn Marino  *  This file is a GNU parallel extension to the Standard C++ Library.
29*e4b17023SJohn Marino  */
30*e4b17023SJohn Marino 
31*e4b17023SJohn Marino // Written by Felix Putze.
32*e4b17023SJohn Marino 
33*e4b17023SJohn Marino #ifndef _GLIBCXX_PARALLEL_FIND_SELECTORS_H
34*e4b17023SJohn Marino #define _GLIBCXX_PARALLEL_FIND_SELECTORS_H 1
35*e4b17023SJohn Marino 
36*e4b17023SJohn Marino #include <parallel/tags.h>
37*e4b17023SJohn Marino #include <parallel/basic_iterator.h>
38*e4b17023SJohn Marino #include <bits/stl_pair.h>
39*e4b17023SJohn Marino 
40*e4b17023SJohn Marino namespace __gnu_parallel
41*e4b17023SJohn Marino {
42*e4b17023SJohn Marino   /** @brief Base class of all __gnu_parallel::__find_template selectors. */
43*e4b17023SJohn Marino   struct __generic_find_selector
44*e4b17023SJohn Marino   { };
45*e4b17023SJohn Marino 
46*e4b17023SJohn Marino   /**
47*e4b17023SJohn Marino    *  @brief Test predicate on a single element, used for std::find()
48*e4b17023SJohn Marino    *  and std::find_if ().
49*e4b17023SJohn Marino    */
50*e4b17023SJohn Marino   struct __find_if_selector : public __generic_find_selector
51*e4b17023SJohn Marino   {
52*e4b17023SJohn Marino     /** @brief Test on one position.
53*e4b17023SJohn Marino      * @param __i1 _Iterator on first sequence.
54*e4b17023SJohn Marino      * @param __i2 _Iterator on second sequence (unused).
55*e4b17023SJohn Marino      * @param __pred Find predicate.
56*e4b17023SJohn Marino      */
57*e4b17023SJohn Marino     template<typename _RAIter1, typename _RAIter2,
58*e4b17023SJohn Marino              typename _Pred>
59*e4b17023SJohn Marino       bool
operator__find_if_selector60*e4b17023SJohn Marino       operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred)
61*e4b17023SJohn Marino       { return __pred(*__i1); }
62*e4b17023SJohn Marino 
63*e4b17023SJohn Marino     /** @brief Corresponding sequential algorithm on a sequence.
64*e4b17023SJohn Marino      *  @param __begin1 Begin iterator of first sequence.
65*e4b17023SJohn Marino      *  @param __end1 End iterator of first sequence.
66*e4b17023SJohn Marino      *  @param __begin2 Begin iterator of second sequence.
67*e4b17023SJohn Marino      *  @param __pred Find predicate.
68*e4b17023SJohn Marino      */
69*e4b17023SJohn Marino     template<typename _RAIter1, typename _RAIter2,
70*e4b17023SJohn Marino              typename _Pred>
71*e4b17023SJohn Marino       std::pair<_RAIter1, _RAIter2>
_M_sequential_algorithm__find_if_selector72*e4b17023SJohn Marino       _M_sequential_algorithm(_RAIter1 __begin1,
73*e4b17023SJohn Marino                            _RAIter1 __end1,
74*e4b17023SJohn Marino                            _RAIter2 __begin2, _Pred __pred)
75*e4b17023SJohn Marino       { return std::make_pair(find_if(__begin1, __end1, __pred,
76*e4b17023SJohn Marino                                       sequential_tag()), __begin2); }
77*e4b17023SJohn Marino   };
78*e4b17023SJohn Marino 
79*e4b17023SJohn Marino   /** @brief Test predicate on two adjacent elements. */
80*e4b17023SJohn Marino   struct __adjacent_find_selector : public __generic_find_selector
81*e4b17023SJohn Marino   {
82*e4b17023SJohn Marino     /** @brief Test on one position.
83*e4b17023SJohn Marino      *  @param __i1 _Iterator on first sequence.
84*e4b17023SJohn Marino      *  @param __i2 _Iterator on second sequence (unused).
85*e4b17023SJohn Marino      *  @param __pred Find predicate.
86*e4b17023SJohn Marino      */
87*e4b17023SJohn Marino     template<typename _RAIter1, typename _RAIter2,
88*e4b17023SJohn Marino              typename _Pred>
89*e4b17023SJohn Marino       bool
operator__adjacent_find_selector90*e4b17023SJohn Marino       operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred)
91*e4b17023SJohn Marino       {
92*e4b17023SJohn Marino         // Passed end iterator is one short.
93*e4b17023SJohn Marino         return __pred(*__i1, *(__i1 + 1));
94*e4b17023SJohn Marino       }
95*e4b17023SJohn Marino 
96*e4b17023SJohn Marino     /** @brief Corresponding sequential algorithm on a sequence.
97*e4b17023SJohn Marino      *  @param __begin1 Begin iterator of first sequence.
98*e4b17023SJohn Marino      *  @param __end1 End iterator of first sequence.
99*e4b17023SJohn Marino      *  @param __begin2 Begin iterator of second sequence.
100*e4b17023SJohn Marino      *  @param __pred Find predicate.
101*e4b17023SJohn Marino      */
102*e4b17023SJohn Marino     template<typename _RAIter1, typename _RAIter2,
103*e4b17023SJohn Marino              typename _Pred>
104*e4b17023SJohn Marino       std::pair<_RAIter1, _RAIter2>
_M_sequential_algorithm__adjacent_find_selector105*e4b17023SJohn Marino       _M_sequential_algorithm(_RAIter1 __begin1,
106*e4b17023SJohn Marino 			      _RAIter1 __end1,
107*e4b17023SJohn Marino 			      _RAIter2 __begin2, _Pred __pred)
108*e4b17023SJohn Marino       {
109*e4b17023SJohn Marino         // Passed end iterator is one short.
110*e4b17023SJohn Marino         _RAIter1 __spot = adjacent_find(__begin1, __end1 + 1,
111*e4b17023SJohn Marino 					__pred, sequential_tag());
112*e4b17023SJohn Marino         if (__spot == (__end1 + 1))
113*e4b17023SJohn Marino           __spot = __end1;
114*e4b17023SJohn Marino         return std::make_pair(__spot, __begin2);
115*e4b17023SJohn Marino       }
116*e4b17023SJohn Marino   };
117*e4b17023SJohn Marino 
118*e4b17023SJohn Marino   /** @brief Test inverted predicate on a single element. */
119*e4b17023SJohn Marino   struct __mismatch_selector : public __generic_find_selector
120*e4b17023SJohn Marino   {
121*e4b17023SJohn Marino     /**
122*e4b17023SJohn Marino      *  @brief Test on one position.
123*e4b17023SJohn Marino      *  @param __i1 _Iterator on first sequence.
124*e4b17023SJohn Marino      *  @param __i2 _Iterator on second sequence (unused).
125*e4b17023SJohn Marino      *  @param __pred Find predicate.
126*e4b17023SJohn Marino      */
127*e4b17023SJohn Marino     template<typename _RAIter1, typename _RAIter2,
128*e4b17023SJohn Marino              typename _Pred>
129*e4b17023SJohn Marino       bool
operator__mismatch_selector130*e4b17023SJohn Marino       operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred)
131*e4b17023SJohn Marino       { return !__pred(*__i1, *__i2); }
132*e4b17023SJohn Marino 
133*e4b17023SJohn Marino     /**
134*e4b17023SJohn Marino      *  @brief Corresponding sequential algorithm on a sequence.
135*e4b17023SJohn Marino      *  @param __begin1 Begin iterator of first sequence.
136*e4b17023SJohn Marino      *  @param __end1 End iterator of first sequence.
137*e4b17023SJohn Marino      *  @param __begin2 Begin iterator of second sequence.
138*e4b17023SJohn Marino      *  @param __pred Find predicate.
139*e4b17023SJohn Marino      */
140*e4b17023SJohn Marino     template<typename _RAIter1, typename _RAIter2,
141*e4b17023SJohn Marino              typename _Pred>
142*e4b17023SJohn Marino       std::pair<_RAIter1, _RAIter2>
_M_sequential_algorithm__mismatch_selector143*e4b17023SJohn Marino       _M_sequential_algorithm(_RAIter1 __begin1,
144*e4b17023SJohn Marino 			      _RAIter1 __end1,
145*e4b17023SJohn Marino 			      _RAIter2 __begin2, _Pred __pred)
146*e4b17023SJohn Marino       { return mismatch(__begin1, __end1, __begin2,
147*e4b17023SJohn Marino 			__pred, sequential_tag()); }
148*e4b17023SJohn Marino   };
149*e4b17023SJohn Marino 
150*e4b17023SJohn Marino 
151*e4b17023SJohn Marino   /** @brief Test predicate on several elements. */
152*e4b17023SJohn Marino   template<typename _FIterator>
153*e4b17023SJohn Marino     struct __find_first_of_selector : public __generic_find_selector
154*e4b17023SJohn Marino     {
155*e4b17023SJohn Marino       _FIterator _M_begin;
156*e4b17023SJohn Marino       _FIterator _M_end;
157*e4b17023SJohn Marino 
__find_first_of_selector__find_first_of_selector158*e4b17023SJohn Marino       explicit __find_first_of_selector(_FIterator __begin,
159*e4b17023SJohn Marino 					_FIterator __end)
160*e4b17023SJohn Marino       : _M_begin(__begin), _M_end(__end) { }
161*e4b17023SJohn Marino 
162*e4b17023SJohn Marino       /** @brief Test on one position.
163*e4b17023SJohn Marino        *  @param __i1 _Iterator on first sequence.
164*e4b17023SJohn Marino        *  @param __i2 _Iterator on second sequence (unused).
165*e4b17023SJohn Marino        *  @param __pred Find predicate. */
166*e4b17023SJohn Marino       template<typename _RAIter1, typename _RAIter2,
167*e4b17023SJohn Marino 	       typename _Pred>
168*e4b17023SJohn Marino         bool
operator__find_first_of_selector169*e4b17023SJohn Marino         operator()(_RAIter1 __i1, _RAIter2 __i2, _Pred __pred)
170*e4b17023SJohn Marino         {
171*e4b17023SJohn Marino 	  for (_FIterator __pos_in_candidates = _M_begin;
172*e4b17023SJohn Marino 	       __pos_in_candidates != _M_end; ++__pos_in_candidates)
173*e4b17023SJohn Marino 	    if (__pred(*__i1, *__pos_in_candidates))
174*e4b17023SJohn Marino 	      return true;
175*e4b17023SJohn Marino 	  return false;
176*e4b17023SJohn Marino 	}
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino       /** @brief Corresponding sequential algorithm on a sequence.
179*e4b17023SJohn Marino        *  @param __begin1 Begin iterator of first sequence.
180*e4b17023SJohn Marino        *  @param __end1 End iterator of first sequence.
181*e4b17023SJohn Marino        *  @param __begin2 Begin iterator of second sequence.
182*e4b17023SJohn Marino        *  @param __pred Find predicate. */
183*e4b17023SJohn Marino       template<typename _RAIter1, typename _RAIter2,
184*e4b17023SJohn Marino 	       typename _Pred>
185*e4b17023SJohn Marino         std::pair<_RAIter1, _RAIter2>
_M_sequential_algorithm__find_first_of_selector186*e4b17023SJohn Marino         _M_sequential_algorithm(_RAIter1 __begin1,
187*e4b17023SJohn Marino 				_RAIter1 __end1,
188*e4b17023SJohn Marino 				_RAIter2 __begin2, _Pred __pred)
189*e4b17023SJohn Marino         {
190*e4b17023SJohn Marino 	  return std::make_pair(find_first_of(__begin1, __end1,
191*e4b17023SJohn Marino 					      _M_begin, _M_end, __pred,
192*e4b17023SJohn Marino 					      sequential_tag()), __begin2);
193*e4b17023SJohn Marino 	}
194*e4b17023SJohn Marino      };
195*e4b17023SJohn Marino }
196*e4b17023SJohn Marino 
197*e4b17023SJohn Marino #endif /* _GLIBCXX_PARALLEL_FIND_SELECTORS_H */
198