xref: /dflybsd-src/contrib/gcc-4.7/libstdc++-v3/include/parallel/merge.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/merge.h
26*e4b17023SJohn Marino  *  @brief Parallel implementation of std::merge().
27*e4b17023SJohn Marino  *  This file is a GNU parallel extension to the Standard C++ Library.
28*e4b17023SJohn Marino  */
29*e4b17023SJohn Marino 
30*e4b17023SJohn Marino // Written by Johannes Singler.
31*e4b17023SJohn Marino 
32*e4b17023SJohn Marino #ifndef _GLIBCXX_PARALLEL_MERGE_H
33*e4b17023SJohn Marino #define _GLIBCXX_PARALLEL_MERGE_H 1
34*e4b17023SJohn Marino 
35*e4b17023SJohn Marino #include <parallel/basic_iterator.h>
36*e4b17023SJohn Marino #include <bits/stl_algo.h>
37*e4b17023SJohn Marino 
38*e4b17023SJohn Marino namespace __gnu_parallel
39*e4b17023SJohn Marino {
40*e4b17023SJohn Marino   /** @brief Merge routine being able to merge only the @c __max_length
41*e4b17023SJohn Marino    * smallest elements.
42*e4b17023SJohn Marino    *
43*e4b17023SJohn Marino    * The @c __begin iterators are advanced accordingly, they might not
44*e4b17023SJohn Marino    * reach @c __end, in contrast to the usual variant.
45*e4b17023SJohn Marino    * @param __begin1 Begin iterator of first sequence.
46*e4b17023SJohn Marino    * @param __end1 End iterator of first sequence.
47*e4b17023SJohn Marino    * @param __begin2 Begin iterator of second sequence.
48*e4b17023SJohn Marino    * @param __end2 End iterator of second sequence.
49*e4b17023SJohn Marino    * @param __target Target begin iterator.
50*e4b17023SJohn Marino    * @param __max_length Maximum number of elements to merge.
51*e4b17023SJohn Marino    * @param __comp Comparator.
52*e4b17023SJohn Marino    * @return Output end iterator. */
53*e4b17023SJohn Marino   template<typename _RAIter1, typename _RAIter2,
54*e4b17023SJohn Marino            typename _OutputIterator, typename _DifferenceTp,
55*e4b17023SJohn Marino            typename _Compare>
56*e4b17023SJohn Marino     _OutputIterator
__merge_advance_usual(_RAIter1 & __begin1,_RAIter1 __end1,_RAIter2 & __begin2,_RAIter2 __end2,_OutputIterator __target,_DifferenceTp __max_length,_Compare __comp)57*e4b17023SJohn Marino     __merge_advance_usual(_RAIter1& __begin1, _RAIter1 __end1,
58*e4b17023SJohn Marino 			  _RAIter2& __begin2, _RAIter2 __end2,
59*e4b17023SJohn Marino 			  _OutputIterator __target,
60*e4b17023SJohn Marino 			  _DifferenceTp __max_length, _Compare __comp)
61*e4b17023SJohn Marino     {
62*e4b17023SJohn Marino       typedef _DifferenceTp _DifferenceType;
63*e4b17023SJohn Marino       while (__begin1 != __end1 && __begin2 != __end2 && __max_length > 0)
64*e4b17023SJohn Marino         {
65*e4b17023SJohn Marino           // array1[__i1] < array0[i0]
66*e4b17023SJohn Marino           if (__comp(*__begin2, *__begin1))
67*e4b17023SJohn Marino             *__target++ = *__begin2++;
68*e4b17023SJohn Marino           else
69*e4b17023SJohn Marino             *__target++ = *__begin1++;
70*e4b17023SJohn Marino           --__max_length;
71*e4b17023SJohn Marino         }
72*e4b17023SJohn Marino 
73*e4b17023SJohn Marino       if (__begin1 != __end1)
74*e4b17023SJohn Marino         {
75*e4b17023SJohn Marino           __target = std::copy(__begin1, __begin1 + __max_length, __target);
76*e4b17023SJohn Marino           __begin1 += __max_length;
77*e4b17023SJohn Marino         }
78*e4b17023SJohn Marino       else
79*e4b17023SJohn Marino         {
80*e4b17023SJohn Marino           __target = std::copy(__begin2, __begin2 + __max_length, __target);
81*e4b17023SJohn Marino           __begin2 += __max_length;
82*e4b17023SJohn Marino         }
83*e4b17023SJohn Marino       return __target;
84*e4b17023SJohn Marino     }
85*e4b17023SJohn Marino 
86*e4b17023SJohn Marino   /** @brief Merge routine being able to merge only the @c __max_length
87*e4b17023SJohn Marino    * smallest elements.
88*e4b17023SJohn Marino    *
89*e4b17023SJohn Marino    * The @c __begin iterators are advanced accordingly, they might not
90*e4b17023SJohn Marino    * reach @c __end, in contrast to the usual variant.
91*e4b17023SJohn Marino    * Specially designed code should allow the compiler to generate
92*e4b17023SJohn Marino    * conditional moves instead of branches.
93*e4b17023SJohn Marino    * @param __begin1 Begin iterator of first sequence.
94*e4b17023SJohn Marino    * @param __end1 End iterator of first sequence.
95*e4b17023SJohn Marino    * @param __begin2 Begin iterator of second sequence.
96*e4b17023SJohn Marino    * @param __end2 End iterator of second sequence.
97*e4b17023SJohn Marino    * @param __target Target begin iterator.
98*e4b17023SJohn Marino    * @param __max_length Maximum number of elements to merge.
99*e4b17023SJohn Marino    * @param __comp Comparator.
100*e4b17023SJohn Marino    * @return Output end iterator. */
101*e4b17023SJohn Marino   template<typename _RAIter1, typename _RAIter2,
102*e4b17023SJohn Marino            typename _OutputIterator, typename _DifferenceTp,
103*e4b17023SJohn Marino            typename _Compare>
104*e4b17023SJohn Marino     _OutputIterator
__merge_advance_movc(_RAIter1 & __begin1,_RAIter1 __end1,_RAIter2 & __begin2,_RAIter2 __end2,_OutputIterator __target,_DifferenceTp __max_length,_Compare __comp)105*e4b17023SJohn Marino     __merge_advance_movc(_RAIter1& __begin1, _RAIter1 __end1,
106*e4b17023SJohn Marino 			 _RAIter2& __begin2, _RAIter2 __end2,
107*e4b17023SJohn Marino 			 _OutputIterator __target,
108*e4b17023SJohn Marino 			 _DifferenceTp __max_length, _Compare __comp)
109*e4b17023SJohn Marino     {
110*e4b17023SJohn Marino       typedef _DifferenceTp _DifferenceType;
111*e4b17023SJohn Marino       typedef typename std::iterator_traits<_RAIter1>::value_type
112*e4b17023SJohn Marino         _ValueType1;
113*e4b17023SJohn Marino       typedef typename std::iterator_traits<_RAIter2>::value_type
114*e4b17023SJohn Marino         _ValueType2;
115*e4b17023SJohn Marino 
116*e4b17023SJohn Marino #if _GLIBCXX_ASSERTIONS
117*e4b17023SJohn Marino       _GLIBCXX_PARALLEL_ASSERT(__max_length >= 0);
118*e4b17023SJohn Marino #endif
119*e4b17023SJohn Marino 
120*e4b17023SJohn Marino       while (__begin1 != __end1 && __begin2 != __end2 && __max_length > 0)
121*e4b17023SJohn Marino         {
122*e4b17023SJohn Marino           _RAIter1 __next1 = __begin1 + 1;
123*e4b17023SJohn Marino           _RAIter2 __next2 = __begin2 + 1;
124*e4b17023SJohn Marino           _ValueType1 __element1 = *__begin1;
125*e4b17023SJohn Marino           _ValueType2 __element2 = *__begin2;
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino           if (__comp(__element2, __element1))
128*e4b17023SJohn Marino             {
129*e4b17023SJohn Marino               __element1 = __element2;
130*e4b17023SJohn Marino               __begin2 = __next2;
131*e4b17023SJohn Marino             }
132*e4b17023SJohn Marino           else
133*e4b17023SJohn Marino             __begin1 = __next1;
134*e4b17023SJohn Marino 
135*e4b17023SJohn Marino           *__target = __element1;
136*e4b17023SJohn Marino 
137*e4b17023SJohn Marino           ++__target;
138*e4b17023SJohn Marino           --__max_length;
139*e4b17023SJohn Marino         }
140*e4b17023SJohn Marino       if (__begin1 != __end1)
141*e4b17023SJohn Marino         {
142*e4b17023SJohn Marino           __target = std::copy(__begin1, __begin1 + __max_length, __target);
143*e4b17023SJohn Marino           __begin1 += __max_length;
144*e4b17023SJohn Marino         }
145*e4b17023SJohn Marino       else
146*e4b17023SJohn Marino         {
147*e4b17023SJohn Marino           __target = std::copy(__begin2, __begin2 + __max_length, __target);
148*e4b17023SJohn Marino           __begin2 += __max_length;
149*e4b17023SJohn Marino         }
150*e4b17023SJohn Marino       return __target;
151*e4b17023SJohn Marino     }
152*e4b17023SJohn Marino 
153*e4b17023SJohn Marino   /** @brief Merge routine being able to merge only the @c __max_length
154*e4b17023SJohn Marino    * smallest elements.
155*e4b17023SJohn Marino    *
156*e4b17023SJohn Marino    *  The @c __begin iterators are advanced accordingly, they might not
157*e4b17023SJohn Marino    *  reach @c __end, in contrast to the usual variant.
158*e4b17023SJohn Marino    *  Static switch on whether to use the conditional-move variant.
159*e4b17023SJohn Marino    *  @param __begin1 Begin iterator of first sequence.
160*e4b17023SJohn Marino    *  @param __end1 End iterator of first sequence.
161*e4b17023SJohn Marino    *  @param __begin2 Begin iterator of second sequence.
162*e4b17023SJohn Marino    *  @param __end2 End iterator of second sequence.
163*e4b17023SJohn Marino    *  @param __target Target begin iterator.
164*e4b17023SJohn Marino    *  @param __max_length Maximum number of elements to merge.
165*e4b17023SJohn Marino    *  @param __comp Comparator.
166*e4b17023SJohn Marino    *  @return Output end iterator. */
167*e4b17023SJohn Marino   template<typename _RAIter1, typename _RAIter2,
168*e4b17023SJohn Marino            typename _OutputIterator, typename _DifferenceTp,
169*e4b17023SJohn Marino            typename _Compare>
170*e4b17023SJohn Marino     inline _OutputIterator
__merge_advance(_RAIter1 & __begin1,_RAIter1 __end1,_RAIter2 & __begin2,_RAIter2 __end2,_OutputIterator __target,_DifferenceTp __max_length,_Compare __comp)171*e4b17023SJohn Marino     __merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
172*e4b17023SJohn Marino 		    _RAIter2& __begin2, _RAIter2 __end2,
173*e4b17023SJohn Marino 		    _OutputIterator __target, _DifferenceTp __max_length,
174*e4b17023SJohn Marino 		    _Compare __comp)
175*e4b17023SJohn Marino     {
176*e4b17023SJohn Marino       _GLIBCXX_CALL(__max_length)
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino       return __merge_advance_movc(__begin1, __end1, __begin2, __end2,
179*e4b17023SJohn Marino 				  __target, __max_length, __comp);
180*e4b17023SJohn Marino     }
181*e4b17023SJohn Marino 
182*e4b17023SJohn Marino   /** @brief Merge routine fallback to sequential in case the
183*e4b17023SJohn Marino       iterators of the two input sequences are of different type.
184*e4b17023SJohn Marino       *  @param __begin1 Begin iterator of first sequence.
185*e4b17023SJohn Marino       *  @param __end1 End iterator of first sequence.
186*e4b17023SJohn Marino       *  @param __begin2 Begin iterator of second sequence.
187*e4b17023SJohn Marino       *  @param __end2 End iterator of second sequence.
188*e4b17023SJohn Marino       *  @param __target Target begin iterator.
189*e4b17023SJohn Marino       *  @param __max_length Maximum number of elements to merge.
190*e4b17023SJohn Marino       *  @param __comp Comparator.
191*e4b17023SJohn Marino       *  @return Output end iterator. */
192*e4b17023SJohn Marino   template<typename _RAIter1, typename _RAIter2,
193*e4b17023SJohn Marino            typename _RAIter3, typename _Compare>
194*e4b17023SJohn Marino     inline _RAIter3
__parallel_merge_advance(_RAIter1 & __begin1,_RAIter1 __end1,_RAIter2 & __begin2,_RAIter2 __end2,_RAIter3 __target,typename std::iterator_traits<_RAIter1>::difference_type __max_length,_Compare __comp)195*e4b17023SJohn Marino     __parallel_merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
196*e4b17023SJohn Marino 			     _RAIter2& __begin2,
197*e4b17023SJohn Marino 			     // different iterators, parallel implementation
198*e4b17023SJohn Marino 			     // not available
199*e4b17023SJohn Marino 			     _RAIter2 __end2, _RAIter3 __target, typename
200*e4b17023SJohn Marino 			     std::iterator_traits<_RAIter1>::
201*e4b17023SJohn Marino 			     difference_type __max_length, _Compare __comp)
202*e4b17023SJohn Marino     { return __merge_advance(__begin1, __end1, __begin2, __end2, __target,
203*e4b17023SJohn Marino 			     __max_length, __comp); }
204*e4b17023SJohn Marino 
205*e4b17023SJohn Marino   /** @brief Parallel merge routine being able to merge only the @c
206*e4b17023SJohn Marino    * __max_length smallest elements.
207*e4b17023SJohn Marino    *
208*e4b17023SJohn Marino    *  The @c __begin iterators are advanced accordingly, they might not
209*e4b17023SJohn Marino    *  reach @c __end, in contrast to the usual variant.
210*e4b17023SJohn Marino    *  The functionality is projected onto parallel_multiway_merge.
211*e4b17023SJohn Marino    *  @param __begin1 Begin iterator of first sequence.
212*e4b17023SJohn Marino    *  @param __end1 End iterator of first sequence.
213*e4b17023SJohn Marino    *  @param __begin2 Begin iterator of second sequence.
214*e4b17023SJohn Marino    *  @param __end2 End iterator of second sequence.
215*e4b17023SJohn Marino    *  @param __target Target begin iterator.
216*e4b17023SJohn Marino    *  @param __max_length Maximum number of elements to merge.
217*e4b17023SJohn Marino    *  @param __comp Comparator.
218*e4b17023SJohn Marino    *  @return Output end iterator.
219*e4b17023SJohn Marino    */
220*e4b17023SJohn Marino   template<typename _RAIter1, typename _RAIter3,
221*e4b17023SJohn Marino            typename _Compare>
222*e4b17023SJohn Marino     inline _RAIter3
__parallel_merge_advance(_RAIter1 & __begin1,_RAIter1 __end1,_RAIter1 & __begin2,_RAIter1 __end2,_RAIter3 __target,typename std::iterator_traits<_RAIter1>::difference_type __max_length,_Compare __comp)223*e4b17023SJohn Marino     __parallel_merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
224*e4b17023SJohn Marino 			     _RAIter1& __begin2, _RAIter1 __end2,
225*e4b17023SJohn Marino 			     _RAIter3 __target, typename
226*e4b17023SJohn Marino 			     std::iterator_traits<_RAIter1>::
227*e4b17023SJohn Marino 			     difference_type __max_length, _Compare __comp)
228*e4b17023SJohn Marino     {
229*e4b17023SJohn Marino       typedef typename
230*e4b17023SJohn Marino           std::iterator_traits<_RAIter1>::value_type _ValueType;
231*e4b17023SJohn Marino       typedef typename std::iterator_traits<_RAIter1>::
232*e4b17023SJohn Marino         difference_type _DifferenceType1 /* == difference_type2 */;
233*e4b17023SJohn Marino       typedef typename std::iterator_traits<_RAIter3>::
234*e4b17023SJohn Marino         difference_type _DifferenceType3;
235*e4b17023SJohn Marino       typedef typename std::pair<_RAIter1, _RAIter1>
236*e4b17023SJohn Marino         _IteratorPair;
237*e4b17023SJohn Marino 
238*e4b17023SJohn Marino       _IteratorPair __seqs[2] = { std::make_pair(__begin1, __end1),
239*e4b17023SJohn Marino 				  std::make_pair(__begin2, __end2) };
240*e4b17023SJohn Marino       _RAIter3 __target_end = parallel_multiway_merge
241*e4b17023SJohn Marino 	< /* __stable = */ true, /* __sentinels = */ false>
242*e4b17023SJohn Marino 	(__seqs, __seqs + 2, __target, multiway_merge_exact_splitting
243*e4b17023SJohn Marino 	 < /* __stable = */ true, _IteratorPair*,
244*e4b17023SJohn Marino 	 _Compare, _DifferenceType1>, __max_length, __comp,
245*e4b17023SJohn Marino 	 omp_get_max_threads());
246*e4b17023SJohn Marino 
247*e4b17023SJohn Marino       return __target_end;
248*e4b17023SJohn Marino     }
249*e4b17023SJohn Marino }       //namespace __gnu_parallel
250*e4b17023SJohn Marino 
251*e4b17023SJohn Marino #endif /* _GLIBCXX_PARALLEL_MERGE_H */
252