libstdc++
merge.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file parallel/merge.h
00026  *  @brief Parallel implementation of std::merge().
00027  *  This file is a GNU parallel extension to the Standard C++ Library.
00028  */
00029 
00030 // Written by Johannes Singler.
00031 
00032 #ifndef _GLIBCXX_PARALLEL_MERGE_H
00033 #define _GLIBCXX_PARALLEL_MERGE_H 1
00034 
00035 #include <parallel/basic_iterator.h>
00036 #include <bits/stl_algo.h>
00037 
00038 namespace __gnu_parallel
00039 {
00040   /** @brief Merge routine being able to merge only the @c __max_length
00041    * smallest elements.
00042    *
00043    * The @c __begin iterators are advanced accordingly, they might not
00044    * reach @c __end, in contrast to the usual variant.
00045    * @param __begin1 Begin iterator of first sequence.
00046    * @param __end1 End iterator of first sequence.
00047    * @param __begin2 Begin iterator of second sequence.
00048    * @param __end2 End iterator of second sequence.
00049    * @param __target Target begin iterator.
00050    * @param __max_length Maximum number of elements to merge.
00051    * @param __comp Comparator.
00052    * @return Output end iterator. */
00053   template<typename _RAIter1, typename _RAIter2,
00054            typename _OutputIterator, typename _DifferenceTp,
00055            typename _Compare>
00056     _OutputIterator
00057     __merge_advance_usual(_RAIter1& __begin1, _RAIter1 __end1,
00058               _RAIter2& __begin2, _RAIter2 __end2,
00059               _OutputIterator __target,
00060               _DifferenceTp __max_length, _Compare __comp)
00061     {
00062       typedef _DifferenceTp _DifferenceType;
00063       while (__begin1 != __end1 && __begin2 != __end2 && __max_length > 0)
00064         {
00065           // array1[__i1] < array0[i0]
00066           if (__comp(*__begin2, *__begin1))
00067             *__target++ = *__begin2++;
00068           else
00069             *__target++ = *__begin1++;
00070           --__max_length;
00071         }
00072 
00073       if (__begin1 != __end1)
00074         {
00075           __target = std::copy(__begin1, __begin1 + __max_length, __target);
00076           __begin1 += __max_length;
00077         }
00078       else
00079         {
00080           __target = std::copy(__begin2, __begin2 + __max_length, __target);
00081           __begin2 += __max_length;
00082         }
00083       return __target;
00084     }
00085 
00086   /** @brief Merge routine being able to merge only the @c __max_length
00087    * smallest elements.
00088    *
00089    * The @c __begin iterators are advanced accordingly, they might not
00090    * reach @c __end, in contrast to the usual variant.
00091    * Specially designed code should allow the compiler to generate
00092    * conditional moves instead of branches.
00093    * @param __begin1 Begin iterator of first sequence.
00094    * @param __end1 End iterator of first sequence.
00095    * @param __begin2 Begin iterator of second sequence.
00096    * @param __end2 End iterator of second sequence.
00097    * @param __target Target begin iterator.
00098    * @param __max_length Maximum number of elements to merge.
00099    * @param __comp Comparator.
00100    * @return Output end iterator. */
00101   template<typename _RAIter1, typename _RAIter2,
00102            typename _OutputIterator, typename _DifferenceTp,
00103            typename _Compare>
00104     _OutputIterator
00105     __merge_advance_movc(_RAIter1& __begin1, _RAIter1 __end1,
00106              _RAIter2& __begin2, _RAIter2 __end2,
00107              _OutputIterator __target,
00108              _DifferenceTp __max_length, _Compare __comp)
00109     {
00110       typedef _DifferenceTp _DifferenceType;
00111       typedef typename std::iterator_traits<_RAIter1>::value_type
00112         _ValueType1;
00113       typedef typename std::iterator_traits<_RAIter2>::value_type
00114         _ValueType2;
00115 
00116 #if _GLIBCXX_ASSERTIONS
00117       _GLIBCXX_PARALLEL_ASSERT(__max_length >= 0);
00118 #endif
00119 
00120       while (__begin1 != __end1 && __begin2 != __end2 && __max_length > 0)
00121         {
00122           _RAIter1 __next1 = __begin1 + 1;
00123           _RAIter2 __next2 = __begin2 + 1;
00124           _ValueType1 __element1 = *__begin1;
00125           _ValueType2 __element2 = *__begin2;
00126 
00127           if (__comp(__element2, __element1))
00128             {
00129               __element1 = __element2;
00130               __begin2 = __next2;
00131             }
00132           else
00133             __begin1 = __next1;
00134 
00135           *__target = __element1;
00136 
00137           ++__target;
00138           --__max_length;
00139         }
00140       if (__begin1 != __end1)
00141         {
00142           __target = std::copy(__begin1, __begin1 + __max_length, __target);
00143           __begin1 += __max_length;
00144         }
00145       else
00146         {
00147           __target = std::copy(__begin2, __begin2 + __max_length, __target);
00148           __begin2 += __max_length;
00149         }
00150       return __target;
00151     }
00152 
00153   /** @brief Merge routine being able to merge only the @c __max_length
00154    * smallest elements.
00155    *
00156    *  The @c __begin iterators are advanced accordingly, they might not
00157    *  reach @c __end, in contrast to the usual variant.
00158    *  Static switch on whether to use the conditional-move variant.
00159    *  @param __begin1 Begin iterator of first sequence.
00160    *  @param __end1 End iterator of first sequence.
00161    *  @param __begin2 Begin iterator of second sequence.
00162    *  @param __end2 End iterator of second sequence.
00163    *  @param __target Target begin iterator.
00164    *  @param __max_length Maximum number of elements to merge.
00165    *  @param __comp Comparator.
00166    *  @return Output end iterator. */
00167   template<typename _RAIter1, typename _RAIter2,
00168            typename _OutputIterator, typename _DifferenceTp,
00169            typename _Compare>
00170     inline _OutputIterator
00171     __merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
00172             _RAIter2& __begin2, _RAIter2 __end2,
00173             _OutputIterator __target, _DifferenceTp __max_length,
00174             _Compare __comp)
00175     {
00176       _GLIBCXX_CALL(__max_length)
00177 
00178       return __merge_advance_movc(__begin1, __end1, __begin2, __end2,
00179                   __target, __max_length, __comp);
00180     }
00181 
00182   /** @brief Merge routine fallback to sequential in case the
00183       iterators of the two input sequences are of different type.
00184       *  @param __begin1 Begin iterator of first sequence.
00185       *  @param __end1 End iterator of first sequence.
00186       *  @param __begin2 Begin iterator of second sequence.
00187       *  @param __end2 End iterator of second sequence.
00188       *  @param __target Target begin iterator.
00189       *  @param __max_length Maximum number of elements to merge.
00190       *  @param __comp Comparator.
00191       *  @return Output end iterator. */
00192   template<typename _RAIter1, typename _RAIter2,
00193            typename _RAIter3, typename _Compare>
00194     inline _RAIter3
00195     __parallel_merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
00196                  _RAIter2& __begin2,
00197                  // different iterators, parallel implementation
00198                  // not available
00199                  _RAIter2 __end2, _RAIter3 __target, typename
00200                  std::iterator_traits<_RAIter1>::
00201                  difference_type __max_length, _Compare __comp)
00202     { return __merge_advance(__begin1, __end1, __begin2, __end2, __target,
00203                  __max_length, __comp); }
00204 
00205   /** @brief Parallel merge routine being able to merge only the @c
00206    * __max_length smallest elements.
00207    *
00208    *  The @c __begin iterators are advanced accordingly, they might not
00209    *  reach @c __end, in contrast to the usual variant.
00210    *  The functionality is projected onto parallel_multiway_merge.
00211    *  @param __begin1 Begin iterator of first sequence.
00212    *  @param __end1 End iterator of first sequence.
00213    *  @param __begin2 Begin iterator of second sequence.
00214    *  @param __end2 End iterator of second sequence.
00215    *  @param __target Target begin iterator.
00216    *  @param __max_length Maximum number of elements to merge.
00217    *  @param __comp Comparator.
00218    *  @return Output end iterator.
00219    */
00220   template<typename _RAIter1, typename _RAIter3,
00221            typename _Compare>
00222     inline _RAIter3
00223     __parallel_merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
00224                  _RAIter1& __begin2, _RAIter1 __end2,
00225                  _RAIter3 __target, typename
00226                  std::iterator_traits<_RAIter1>::
00227                  difference_type __max_length, _Compare __comp)
00228     {
00229       typedef typename
00230           std::iterator_traits<_RAIter1>::value_type _ValueType;
00231       typedef typename std::iterator_traits<_RAIter1>::
00232         difference_type _DifferenceType1 /* == difference_type2 */;
00233       typedef typename std::iterator_traits<_RAIter3>::
00234         difference_type _DifferenceType3;
00235       typedef typename std::pair<_RAIter1, _RAIter1>
00236         _IteratorPair;
00237 
00238       _IteratorPair __seqs[2] = { std::make_pair(__begin1, __end1),
00239                   std::make_pair(__begin2, __end2) };
00240       _RAIter3 __target_end = parallel_multiway_merge
00241     < /* __stable = */ true, /* __sentinels = */ false>
00242     (__seqs, __seqs + 2, __target, multiway_merge_exact_splitting
00243      < /* __stable = */ true, _IteratorPair*,
00244      _Compare, _DifferenceType1>, __max_length, __comp,
00245      omp_get_max_threads());
00246 
00247       return __target_end;
00248     }
00249 }       //namespace __gnu_parallel
00250 
00251 #endif /* _GLIBCXX_PARALLEL_MERGE_H */