2007-09-12 00:32:51 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
|
2007-09-12 00:32:51 +02:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
// software; you can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU General Public License as published by the Free Software
|
2009-04-09 17:00:19 +02:00
|
|
|
// Foundation; either version 3, or (at your option) any later
|
2007-09-12 00:32:51 +02:00
|
|
|
// version.
|
|
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
2007-09-12 00:32:51 +02:00
|
|
|
|
|
|
|
/** @file parallel/merge.h
|
|
|
|
* @brief Parallel implementation of std::merge().
|
|
|
|
* This file is a GNU parallel extension to the Standard C++ Library.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Written by Johannes Singler.
|
|
|
|
|
|
|
|
#ifndef _GLIBCXX_PARALLEL_MERGE_H
|
|
|
|
#define _GLIBCXX_PARALLEL_MERGE_H 1
|
|
|
|
|
|
|
|
#include <parallel/basic_iterator.h>
|
|
|
|
#include <bits/stl_algo.h>
|
|
|
|
|
|
|
|
namespace __gnu_parallel
|
|
|
|
{
|
2009-11-06 23:15:17 +01:00
|
|
|
/** @brief Merge routine being able to merge only the @c __max_length
|
2007-09-12 00:32:51 +02:00
|
|
|
* smallest elements.
|
|
|
|
*
|
2009-11-06 23:15:17 +01:00
|
|
|
* The @c __begin iterators are advanced accordingly, they might not
|
|
|
|
* reach @c __end, in contrast to the usual variant.
|
2009-09-16 11:47:25 +02:00
|
|
|
* @param __begin1 Begin iterator of first sequence.
|
|
|
|
* @param __end1 End iterator of first sequence.
|
|
|
|
* @param __begin2 Begin iterator of second sequence.
|
|
|
|
* @param __end2 End iterator of second sequence.
|
|
|
|
* @param __target Target begin iterator.
|
|
|
|
* @param __max_length Maximum number of elements to merge.
|
|
|
|
* @param __comp Comparator.
|
2007-09-12 00:32:51 +02:00
|
|
|
* @return Output end iterator. */
|
2009-09-16 11:47:25 +02:00
|
|
|
template<typename _RAIter1, typename _RAIter2,
|
2009-09-17 10:42:39 +02:00
|
|
|
typename _OutputIterator, typename _DifferenceTp,
|
|
|
|
typename _Compare>
|
2009-09-16 11:47:25 +02:00
|
|
|
_OutputIterator
|
2009-11-06 12:39:35 +01:00
|
|
|
__merge_advance_usual(_RAIter1& __begin1, _RAIter1 __end1,
|
|
|
|
_RAIter2& __begin2, _RAIter2 __end2,
|
|
|
|
_OutputIterator __target,
|
|
|
|
_DifferenceTp __max_length, _Compare __comp)
|
2008-01-09 19:27:27 +01:00
|
|
|
{
|
2009-09-16 11:47:25 +02:00
|
|
|
typedef _DifferenceTp _DifferenceType;
|
|
|
|
while (__begin1 != __end1 && __begin2 != __end2 && __max_length > 0)
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
|
|
|
// array1[__i1] < array0[i0]
|
|
|
|
if (__comp(*__begin2, *__begin1))
|
|
|
|
*__target++ = *__begin2++;
|
|
|
|
else
|
|
|
|
*__target++ = *__begin1++;
|
|
|
|
--__max_length;
|
|
|
|
}
|
2008-01-09 19:27:27 +01:00
|
|
|
|
2009-09-16 11:47:25 +02:00
|
|
|
if (__begin1 != __end1)
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
|
|
|
__target = std::copy(__begin1, __begin1 + __max_length, __target);
|
|
|
|
__begin1 += __max_length;
|
|
|
|
}
|
2008-01-09 19:27:27 +01:00
|
|
|
else
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
|
|
|
__target = std::copy(__begin2, __begin2 + __max_length, __target);
|
|
|
|
__begin2 += __max_length;
|
|
|
|
}
|
2009-09-16 11:47:25 +02:00
|
|
|
return __target;
|
2008-01-09 19:27:27 +01:00
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2009-11-06 23:15:17 +01:00
|
|
|
/** @brief Merge routine being able to merge only the @c __max_length
|
2007-09-12 00:32:51 +02:00
|
|
|
* smallest elements.
|
|
|
|
*
|
2009-11-06 23:15:17 +01:00
|
|
|
* The @c __begin iterators are advanced accordingly, they might not
|
|
|
|
* reach @c __end, in contrast to the usual variant.
|
2007-09-12 00:32:51 +02:00
|
|
|
* Specially designed code should allow the compiler to generate
|
|
|
|
* conditional moves instead of branches.
|
2009-09-16 11:47:25 +02:00
|
|
|
* @param __begin1 Begin iterator of first sequence.
|
|
|
|
* @param __end1 End iterator of first sequence.
|
|
|
|
* @param __begin2 Begin iterator of second sequence.
|
|
|
|
* @param __end2 End iterator of second sequence.
|
|
|
|
* @param __target Target begin iterator.
|
|
|
|
* @param __max_length Maximum number of elements to merge.
|
|
|
|
* @param __comp Comparator.
|
2007-09-12 00:32:51 +02:00
|
|
|
* @return Output end iterator. */
|
2009-09-16 11:47:25 +02:00
|
|
|
template<typename _RAIter1, typename _RAIter2,
|
2009-09-17 10:42:39 +02:00
|
|
|
typename _OutputIterator, typename _DifferenceTp,
|
|
|
|
typename _Compare>
|
2009-09-16 11:47:25 +02:00
|
|
|
_OutputIterator
|
2009-11-06 12:39:35 +01:00
|
|
|
__merge_advance_movc(_RAIter1& __begin1, _RAIter1 __end1,
|
|
|
|
_RAIter2& __begin2, _RAIter2 __end2,
|
|
|
|
_OutputIterator __target,
|
|
|
|
_DifferenceTp __max_length, _Compare __comp)
|
2008-01-09 19:27:27 +01:00
|
|
|
{
|
2009-09-16 11:47:25 +02:00
|
|
|
typedef _DifferenceTp _DifferenceType;
|
|
|
|
typedef typename std::iterator_traits<_RAIter1>::value_type
|
2009-09-23 10:09:50 +02:00
|
|
|
_ValueType1;
|
2009-09-16 11:47:25 +02:00
|
|
|
typedef typename std::iterator_traits<_RAIter2>::value_type
|
2009-09-23 10:09:50 +02:00
|
|
|
_ValueType2;
|
2007-09-12 00:32:51 +02:00
|
|
|
|
|
|
|
#if _GLIBCXX_ASSERTIONS
|
2009-09-16 11:47:25 +02:00
|
|
|
_GLIBCXX_PARALLEL_ASSERT(__max_length >= 0);
|
2007-09-12 00:32:51 +02:00
|
|
|
#endif
|
|
|
|
|
2009-09-16 11:47:25 +02:00
|
|
|
while (__begin1 != __end1 && __begin2 != __end2 && __max_length > 0)
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
|
|
|
_RAIter1 __next1 = __begin1 + 1;
|
|
|
|
_RAIter2 __next2 = __begin2 + 1;
|
2009-09-23 10:09:50 +02:00
|
|
|
_ValueType1 __element1 = *__begin1;
|
|
|
|
_ValueType2 __element2 = *__begin2;
|
2008-01-09 19:27:27 +01:00
|
|
|
|
2009-09-17 10:42:39 +02:00
|
|
|
if (__comp(__element2, __element1))
|
|
|
|
{
|
|
|
|
__element1 = __element2;
|
|
|
|
__begin2 = __next2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__begin1 = __next1;
|
2008-01-09 19:27:27 +01:00
|
|
|
|
2009-09-17 10:42:39 +02:00
|
|
|
*__target = __element1;
|
2008-01-09 19:27:27 +01:00
|
|
|
|
2009-09-17 10:42:39 +02:00
|
|
|
++__target;
|
|
|
|
--__max_length;
|
|
|
|
}
|
2009-09-16 11:47:25 +02:00
|
|
|
if (__begin1 != __end1)
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
|
|
|
__target = std::copy(__begin1, __begin1 + __max_length, __target);
|
|
|
|
__begin1 += __max_length;
|
|
|
|
}
|
2008-01-09 19:27:27 +01:00
|
|
|
else
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
|
|
|
__target = std::copy(__begin2, __begin2 + __max_length, __target);
|
|
|
|
__begin2 += __max_length;
|
|
|
|
}
|
2009-09-16 11:47:25 +02:00
|
|
|
return __target;
|
2008-01-09 19:27:27 +01:00
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2009-11-06 23:15:17 +01:00
|
|
|
/** @brief Merge routine being able to merge only the @c __max_length
|
2007-09-12 00:32:51 +02:00
|
|
|
* smallest elements.
|
|
|
|
*
|
2009-11-06 23:15:17 +01:00
|
|
|
* The @c __begin iterators are advanced accordingly, they might not
|
|
|
|
* reach @c __end, in contrast to the usual variant.
|
2007-09-12 00:32:51 +02:00
|
|
|
* Static switch on whether to use the conditional-move variant.
|
2009-09-16 11:47:25 +02:00
|
|
|
* @param __begin1 Begin iterator of first sequence.
|
|
|
|
* @param __end1 End iterator of first sequence.
|
|
|
|
* @param __begin2 Begin iterator of second sequence.
|
|
|
|
* @param __end2 End iterator of second sequence.
|
|
|
|
* @param __target Target begin iterator.
|
|
|
|
* @param __max_length Maximum number of elements to merge.
|
|
|
|
* @param __comp Comparator.
|
2007-09-12 00:32:51 +02:00
|
|
|
* @return Output end iterator. */
|
2009-09-16 11:47:25 +02:00
|
|
|
template<typename _RAIter1, typename _RAIter2,
|
2009-09-17 10:42:39 +02:00
|
|
|
typename _OutputIterator, typename _DifferenceTp,
|
|
|
|
typename _Compare>
|
2009-09-16 11:47:25 +02:00
|
|
|
inline _OutputIterator
|
|
|
|
__merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
|
2009-11-06 12:39:35 +01:00
|
|
|
_RAIter2& __begin2, _RAIter2 __end2,
|
|
|
|
_OutputIterator __target, _DifferenceTp __max_length,
|
|
|
|
_Compare __comp)
|
2008-01-09 19:27:27 +01:00
|
|
|
{
|
2009-09-16 11:47:25 +02:00
|
|
|
_GLIBCXX_CALL(__max_length)
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2009-11-06 12:39:35 +01:00
|
|
|
return __merge_advance_movc(__begin1, __end1, __begin2, __end2,
|
|
|
|
__target, __max_length, __comp);
|
2008-01-09 19:27:27 +01:00
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
|
|
|
|
/** @brief Merge routine fallback to sequential in case the
|
|
|
|
iterators of the two input sequences are of different type.
|
2009-09-16 11:47:25 +02:00
|
|
|
* @param __begin1 Begin iterator of first sequence.
|
|
|
|
* @param __end1 End iterator of first sequence.
|
|
|
|
* @param __begin2 Begin iterator of second sequence.
|
|
|
|
* @param __end2 End iterator of second sequence.
|
|
|
|
* @param __target Target begin iterator.
|
|
|
|
* @param __max_length Maximum number of elements to merge.
|
|
|
|
* @param __comp Comparator.
|
2007-09-12 00:32:51 +02:00
|
|
|
* @return Output end iterator. */
|
2009-09-16 11:47:25 +02:00
|
|
|
template<typename _RAIter1, typename _RAIter2,
|
2009-09-17 10:42:39 +02:00
|
|
|
typename _RAIter3, typename _Compare>
|
2009-09-16 11:47:25 +02:00
|
|
|
inline _RAIter3
|
2009-11-06 12:39:35 +01:00
|
|
|
__parallel_merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
|
|
|
|
_RAIter2& __begin2,
|
|
|
|
// different iterators, parallel implementation
|
|
|
|
// not available
|
|
|
|
_RAIter2 __end2, _RAIter3 __target, typename
|
|
|
|
std::iterator_traits<_RAIter1>::
|
|
|
|
difference_type __max_length, _Compare __comp)
|
2009-09-16 11:47:25 +02:00
|
|
|
{ return __merge_advance(__begin1, __end1, __begin2, __end2, __target,
|
2009-11-06 12:39:35 +01:00
|
|
|
__max_length, __comp); }
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2009-11-06 23:15:17 +01:00
|
|
|
/** @brief Parallel merge routine being able to merge only the @c
|
2009-09-16 11:47:25 +02:00
|
|
|
* __max_length smallest elements.
|
2007-09-12 00:32:51 +02:00
|
|
|
*
|
2009-11-06 23:15:17 +01:00
|
|
|
* The @c __begin iterators are advanced accordingly, they might not
|
|
|
|
* reach @c __end, in contrast to the usual variant.
|
2007-09-12 00:32:51 +02:00
|
|
|
* The functionality is projected onto parallel_multiway_merge.
|
2009-09-16 11:47:25 +02:00
|
|
|
* @param __begin1 Begin iterator of first sequence.
|
|
|
|
* @param __end1 End iterator of first sequence.
|
|
|
|
* @param __begin2 Begin iterator of second sequence.
|
|
|
|
* @param __end2 End iterator of second sequence.
|
|
|
|
* @param __target Target begin iterator.
|
|
|
|
* @param __max_length Maximum number of elements to merge.
|
|
|
|
* @param __comp Comparator.
|
2007-09-12 00:32:51 +02:00
|
|
|
* @return Output end iterator.
|
|
|
|
*/
|
2009-09-16 11:47:25 +02:00
|
|
|
template<typename _RAIter1, typename _RAIter3,
|
2009-09-17 10:42:39 +02:00
|
|
|
typename _Compare>
|
2009-09-16 11:47:25 +02:00
|
|
|
inline _RAIter3
|
2009-11-06 12:39:35 +01:00
|
|
|
__parallel_merge_advance(_RAIter1& __begin1, _RAIter1 __end1,
|
|
|
|
_RAIter1& __begin2, _RAIter1 __end2,
|
|
|
|
_RAIter3 __target, typename
|
|
|
|
std::iterator_traits<_RAIter1>::
|
|
|
|
difference_type __max_length, _Compare __comp)
|
2008-01-09 19:27:27 +01:00
|
|
|
{
|
2008-04-07 10:27:34 +02:00
|
|
|
typedef typename
|
2009-09-16 11:47:25 +02:00
|
|
|
std::iterator_traits<_RAIter1>::value_type _ValueType;
|
|
|
|
typedef typename std::iterator_traits<_RAIter1>::
|
2009-09-17 10:42:39 +02:00
|
|
|
difference_type _DifferenceType1 /* == difference_type2 */;
|
2009-09-16 11:47:25 +02:00
|
|
|
typedef typename std::iterator_traits<_RAIter3>::
|
2009-09-17 10:42:39 +02:00
|
|
|
difference_type _DifferenceType3;
|
2009-09-16 11:47:25 +02:00
|
|
|
typedef typename std::pair<_RAIter1, _RAIter1>
|
|
|
|
_IteratorPair;
|
2008-01-09 19:27:27 +01:00
|
|
|
|
2009-11-06 12:39:35 +01:00
|
|
|
_IteratorPair __seqs[2] = { std::make_pair(__begin1, __end1),
|
|
|
|
std::make_pair(__begin2, __end2) };
|
|
|
|
_RAIter3 __target_end = parallel_multiway_merge
|
|
|
|
< /* __stable = */ true, /* __sentinels = */ false>
|
|
|
|
(__seqs, __seqs + 2, __target, multiway_merge_exact_splitting
|
|
|
|
< /* __stable = */ true, _IteratorPair*,
|
|
|
|
_Compare, _DifferenceType1>, __max_length, __comp,
|
|
|
|
omp_get_max_threads());
|
2008-01-09 19:27:27 +01:00
|
|
|
|
2009-09-16 11:47:25 +02:00
|
|
|
return __target_end;
|
2008-01-09 19:27:27 +01:00
|
|
|
}
|
2009-09-17 10:42:39 +02:00
|
|
|
} //namespace __gnu_parallel
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2008-07-15 12:06:58 +02:00
|
|
|
#endif /* _GLIBCXX_PARALLEL_MERGE_H */
|