iterator.h: Reformat to 80 columns; adjust some inline specifiers.
2008-01-09 Paolo Carlini <pcarlini@suse.de> * include/parallel/iterator.h: Reformat to 80 columns; adjust some inline specifiers. * include/parallel/find_selectors.h: Likewise. * include/parallel/losertree.h: Likewise. * include/parallel/list_partition.h: Likewise. * include/parallel/for_each.h: Likewise. * include/parallel/multiseq_selection.h: Likewise. * include/parallel/algorithmfwd.h: Likewise. * include/parallel/for_each_selectors.h: Likewise. * include/parallel/balanced_quicksort.h: Likewise. * include/parallel/merge.h: Likewise. * include/parallel/algobase.h: Likewise. * include/parallel/find.h: Likewise. * include/parallel/algo.h: Likewise. * include/parallel/checkers.h: Likewise. From-SVN: r131431
This commit is contained in:
parent
c3e203cf80
commit
531898c3c2
|
@ -1,3 +1,21 @@
|
|||
2008-01-09 Paolo Carlini <pcarlini@suse.de>
|
||||
|
||||
* include/parallel/iterator.h: Reformat to 80 columns; adjust some
|
||||
inline specifiers.
|
||||
* include/parallel/find_selectors.h: Likewise.
|
||||
* include/parallel/losertree.h: Likewise.
|
||||
* include/parallel/list_partition.h: Likewise.
|
||||
* include/parallel/for_each.h: Likewise.
|
||||
* include/parallel/multiseq_selection.h: Likewise.
|
||||
* include/parallel/algorithmfwd.h: Likewise.
|
||||
* include/parallel/for_each_selectors.h: Likewise.
|
||||
* include/parallel/balanced_quicksort.h: Likewise.
|
||||
* include/parallel/merge.h: Likewise.
|
||||
* include/parallel/algobase.h: Likewise.
|
||||
* include/parallel/find.h: Likewise.
|
||||
* include/parallel/algo.h: Likewise.
|
||||
* include/parallel/checkers.h: Likewise.
|
||||
|
||||
2008-01-09 Francesco Biscani <bluescarni@gmail.com>
|
||||
|
||||
* include/tr1_impl/hashtable (_Hashtable): Fix friend declaration
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -57,215 +57,230 @@ namespace __parallel
|
|||
|
||||
// Sequential fallback
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
__gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2); }
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
__gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2); }
|
||||
|
||||
// Sequential fallback
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred, __gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred, __gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
|
||||
|
||||
// Sequential fallback for input iterator case
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate, typename IteratorTag1, typename IteratorTag2>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch_switch(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, Predicate pred, IteratorTag1,
|
||||
IteratorTag2)
|
||||
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate, typename IteratorTag1, typename IteratorTag2>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch_switch(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, Predicate pred, IteratorTag1,
|
||||
IteratorTag2)
|
||||
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
|
||||
|
||||
// Parallel mismatch for random access iterators
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Predicate>
|
||||
pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
mismatch_switch(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Predicate pred,
|
||||
random_access_iterator_tag, random_access_iterator_tag)
|
||||
{
|
||||
if (_GLIBCXX_PARALLEL_CONDITION(true))
|
||||
{
|
||||
RandomAccessIterator1 res = __gnu_parallel::find_template(begin1, end1, begin2, pred, __gnu_parallel::mismatch_selector()).first;
|
||||
return make_pair(res , begin2 + (res - begin1));
|
||||
}
|
||||
else
|
||||
return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred);
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Predicate>
|
||||
pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
mismatch_switch(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Predicate pred,
|
||||
random_access_iterator_tag, random_access_iterator_tag)
|
||||
{
|
||||
if (_GLIBCXX_PARALLEL_CONDITION(true))
|
||||
{
|
||||
RandomAccessIterator1 res =
|
||||
__gnu_parallel::find_template(begin1, end1, begin2, pred,
|
||||
__gnu_parallel::
|
||||
mismatch_selector()).first;
|
||||
return make_pair(res , begin2 + (res - begin1));
|
||||
}
|
||||
else
|
||||
return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred);
|
||||
}
|
||||
|
||||
// Public interface
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
|
||||
{
|
||||
typedef std::iterator_traits<InputIterator1> iterator1_traits;
|
||||
typedef std::iterator_traits<InputIterator2> iterator2_traits;
|
||||
typedef typename iterator1_traits::value_type value1_type;
|
||||
typedef typename iterator2_traits::value_type value2_type;
|
||||
typedef typename iterator1_traits::iterator_category iterator1_category;
|
||||
typedef typename iterator2_traits::iterator_category iterator2_category;
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
|
||||
{
|
||||
typedef std::iterator_traits<InputIterator1> iterator1_traits;
|
||||
typedef std::iterator_traits<InputIterator2> iterator2_traits;
|
||||
typedef typename iterator1_traits::value_type value1_type;
|
||||
typedef typename iterator2_traits::value_type value2_type;
|
||||
typedef typename iterator1_traits::iterator_category iterator1_category;
|
||||
typedef typename iterator2_traits::iterator_category iterator2_category;
|
||||
|
||||
typedef __gnu_parallel::equal_to<value1_type, value2_type> equal_to_type;
|
||||
typedef __gnu_parallel::equal_to<value1_type, value2_type> equal_to_type;
|
||||
|
||||
return mismatch_switch(begin1, end1, begin2, equal_to_type(),
|
||||
iterator1_category(), iterator2_category());
|
||||
}
|
||||
return mismatch_switch(begin1, end1, begin2, equal_to_type(),
|
||||
iterator1_category(), iterator2_category());
|
||||
}
|
||||
|
||||
// Public interface
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred)
|
||||
{
|
||||
typedef std::iterator_traits<InputIterator1> iterator1_traits;
|
||||
typedef std::iterator_traits<InputIterator2> iterator2_traits;
|
||||
typedef typename iterator1_traits::iterator_category iterator1_category;
|
||||
typedef typename iterator2_traits::iterator_category iterator2_category;
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate>
|
||||
inline pair<InputIterator1, InputIterator2>
|
||||
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred)
|
||||
{
|
||||
typedef std::iterator_traits<InputIterator1> iterator1_traits;
|
||||
typedef std::iterator_traits<InputIterator2> iterator2_traits;
|
||||
typedef typename iterator1_traits::iterator_category iterator1_category;
|
||||
typedef typename iterator2_traits::iterator_category iterator2_category;
|
||||
|
||||
return mismatch_switch(begin1, end1, begin2, pred, iterator1_category(),
|
||||
iterator2_category());
|
||||
}
|
||||
return mismatch_switch(begin1, end1, begin2, pred, iterator1_category(),
|
||||
iterator2_category());
|
||||
}
|
||||
|
||||
// Sequential fallback
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
__gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::equal(begin1, end1, begin2); }
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
__gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::equal(begin1, end1, begin2); }
|
||||
|
||||
// Sequential fallback
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate>
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred, __gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::equal(begin1, end1, begin2, pred); }
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate>
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred, __gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::equal(begin1, end1, begin2, pred); }
|
||||
|
||||
// Public interface
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
|
||||
{ return mismatch(begin1, end1, begin2).first == end1; }
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
|
||||
{ return mismatch(begin1, end1, begin2).first == end1; }
|
||||
|
||||
// Public interface
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate>
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred)
|
||||
{ return mismatch(begin1, end1, begin2, pred).first == end1; }
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate>
|
||||
inline bool
|
||||
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
|
||||
Predicate pred)
|
||||
{ return mismatch(begin1, end1, begin2, pred).first == end1; }
|
||||
|
||||
// Sequential fallback
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2,
|
||||
__gnu_parallel::sequential_tag)
|
||||
{
|
||||
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1, begin2, end2);
|
||||
}
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2,
|
||||
__gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
|
||||
begin2, end2); }
|
||||
|
||||
// Sequential fallback
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate>
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2,
|
||||
Predicate pred, __gnu_parallel::sequential_tag)
|
||||
{
|
||||
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
|
||||
begin2, end2, pred);
|
||||
}
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate>
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2,
|
||||
Predicate pred, __gnu_parallel::sequential_tag)
|
||||
{ return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
|
||||
begin2, end2, pred); }
|
||||
|
||||
// Sequential fallback for input iterator case
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate, typename IteratorTag1, typename IteratorTag2>
|
||||
inline bool
|
||||
lexicographical_compare_switch(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2,
|
||||
Predicate pred, IteratorTag1, IteratorTag2)
|
||||
{
|
||||
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
|
||||
begin2, end2, pred);
|
||||
}
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate, typename IteratorTag1, typename IteratorTag2>
|
||||
inline bool
|
||||
lexicographical_compare_switch(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2,
|
||||
Predicate pred, IteratorTag1, IteratorTag2)
|
||||
{ return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
|
||||
begin2, end2, pred); }
|
||||
|
||||
// Parallel lexicographical_compare for random access iterators
|
||||
// Limitation: Both valuetypes must be the same
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Predicate>
|
||||
bool
|
||||
lexicographical_compare_switch(RandomAccessIterator1 begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2,
|
||||
RandomAccessIterator2 end2, Predicate pred,
|
||||
random_access_iterator_tag,
|
||||
random_access_iterator_tag)
|
||||
{
|
||||
if (_GLIBCXX_PARALLEL_CONDITION(true))
|
||||
{
|
||||
typedef iterator_traits<RandomAccessIterator1> traits1_type;
|
||||
typedef typename traits1_type::value_type value1_type;
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Predicate>
|
||||
bool
|
||||
lexicographical_compare_switch(RandomAccessIterator1 begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2,
|
||||
RandomAccessIterator2 end2, Predicate pred,
|
||||
random_access_iterator_tag,
|
||||
random_access_iterator_tag)
|
||||
{
|
||||
if (_GLIBCXX_PARALLEL_CONDITION(true))
|
||||
{
|
||||
typedef iterator_traits<RandomAccessIterator1> traits1_type;
|
||||
typedef typename traits1_type::value_type value1_type;
|
||||
|
||||
typedef iterator_traits<RandomAccessIterator2> traits2_type;
|
||||
typedef typename traits2_type::value_type value2_type;
|
||||
|
||||
typedef __gnu_parallel::equal_from_less<Predicate, value1_type, value2_type> equal_type;
|
||||
typedef iterator_traits<RandomAccessIterator2> traits2_type;
|
||||
typedef typename traits2_type::value_type value2_type;
|
||||
|
||||
// Longer sequence in first place.
|
||||
if ((end1 - begin1) < (end2 - begin2))
|
||||
{
|
||||
typedef pair<RandomAccessIterator1, RandomAccessIterator2> pair_type;
|
||||
pair_type mm = mismatch_switch(begin1, end1, begin2,
|
||||
equal_type(pred),
|
||||
random_access_iterator_tag(),
|
||||
random_access_iterator_tag());
|
||||
typedef __gnu_parallel::equal_from_less<Predicate, value1_type,
|
||||
value2_type> equal_type;
|
||||
|
||||
return (mm.first == end1) || bool(pred(*mm.first, *mm.second));
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef pair<RandomAccessIterator2, RandomAccessIterator1> pair_type;
|
||||
pair_type mm = mismatch_switch(begin2, end2, begin1,
|
||||
equal_type(pred),
|
||||
random_access_iterator_tag(),
|
||||
random_access_iterator_tag());
|
||||
// Longer sequence in first place.
|
||||
if ((end1 - begin1) < (end2 - begin2))
|
||||
{
|
||||
typedef pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
pair_type;
|
||||
pair_type mm = mismatch_switch(begin1, end1, begin2,
|
||||
equal_type(pred),
|
||||
random_access_iterator_tag(),
|
||||
random_access_iterator_tag());
|
||||
|
||||
return (mm.first != end2) && bool(pred(*mm.second, *mm.first));
|
||||
}
|
||||
}
|
||||
else
|
||||
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1, begin2, end2, pred);
|
||||
}
|
||||
return (mm.first == end1) || bool(pred(*mm.first, *mm.second));
|
||||
}
|
||||
else
|
||||
{
|
||||
typedef pair<RandomAccessIterator2, RandomAccessIterator1>
|
||||
pair_type;
|
||||
pair_type mm = mismatch_switch(begin2, end2, begin1,
|
||||
equal_type(pred),
|
||||
random_access_iterator_tag(),
|
||||
random_access_iterator_tag());
|
||||
|
||||
return (mm.first != end2) && bool(pred(*mm.second, *mm.first));
|
||||
}
|
||||
}
|
||||
else
|
||||
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
|
||||
begin2, end2, pred);
|
||||
}
|
||||
|
||||
// Public interface
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, InputIterator2 end2)
|
||||
{
|
||||
typedef iterator_traits<InputIterator1> traits1_type;
|
||||
typedef typename traits1_type::value_type value1_type;
|
||||
typedef typename traits1_type::iterator_category iterator1_category;
|
||||
|
||||
typedef iterator_traits<InputIterator2> traits2_type;
|
||||
typedef typename traits2_type::value_type value2_type;
|
||||
typedef typename traits2_type::iterator_category iterator2_category;
|
||||
typedef __gnu_parallel::less<value1_type, value2_type> less_type;
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2)
|
||||
{
|
||||
typedef iterator_traits<InputIterator1> traits1_type;
|
||||
typedef typename traits1_type::value_type value1_type;
|
||||
typedef typename traits1_type::iterator_category iterator1_category;
|
||||
|
||||
return lexicographical_compare_switch(begin1, end1, begin2, end2,
|
||||
less_type(), iterator1_category(),
|
||||
iterator2_category());
|
||||
}
|
||||
typedef iterator_traits<InputIterator2> traits2_type;
|
||||
typedef typename traits2_type::value_type value2_type;
|
||||
typedef typename traits2_type::iterator_category iterator2_category;
|
||||
typedef __gnu_parallel::less<value1_type, value2_type> less_type;
|
||||
|
||||
return lexicographical_compare_switch(begin1, end1, begin2, end2,
|
||||
less_type(), iterator1_category(),
|
||||
iterator2_category());
|
||||
}
|
||||
|
||||
// Public interface
|
||||
template<typename InputIterator1, typename InputIterator2, typename Predicate>
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, InputIterator2 end2, Predicate pred)
|
||||
{
|
||||
typedef iterator_traits<InputIterator1> traits1_type;
|
||||
typedef typename traits1_type::iterator_category iterator1_category;
|
||||
|
||||
typedef iterator_traits<InputIterator2> traits2_type;
|
||||
typedef typename traits2_type::iterator_category iterator2_category;
|
||||
template<typename InputIterator1, typename InputIterator2,
|
||||
typename Predicate>
|
||||
inline bool
|
||||
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
|
||||
InputIterator2 begin2, InputIterator2 end2,
|
||||
Predicate pred)
|
||||
{
|
||||
typedef iterator_traits<InputIterator1> traits1_type;
|
||||
typedef typename traits1_type::iterator_category iterator1_category;
|
||||
|
||||
return lexicographical_compare_switch(begin1, end1, begin2, end2, pred,
|
||||
iterator1_category(),
|
||||
iterator2_category());
|
||||
}
|
||||
typedef iterator_traits<InputIterator2> traits2_type;
|
||||
typedef typename traits2_type::iterator_category iterator2_category;
|
||||
|
||||
return lexicographical_compare_switch(begin1, end1, begin2, end2, pred,
|
||||
iterator1_category(),
|
||||
iterator2_category());
|
||||
}
|
||||
} // end namespace
|
||||
} // end namespace
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -102,7 +102,7 @@ template<typename RandomAccessIterator>
|
|||
* this part.
|
||||
* @pre @c (end-begin)>=1 */
|
||||
template<typename RandomAccessIterator, typename Comparator>
|
||||
inline typename std::iterator_traits<RandomAccessIterator>::difference_type
|
||||
typename std::iterator_traits<RandomAccessIterator>::difference_type
|
||||
qsb_divide(RandomAccessIterator begin, RandomAccessIterator end,
|
||||
Comparator comp, thread_index_t num_threads)
|
||||
{
|
||||
|
@ -164,7 +164,7 @@ template<typename RandomAccessIterator, typename Comparator>
|
|||
* @param num_threads
|
||||
* Number of threads that are allowed to work on this part. */
|
||||
template<typename RandomAccessIterator, typename Comparator>
|
||||
inline void
|
||||
void
|
||||
qsb_conquer(QSBThreadLocal<RandomAccessIterator>** tls,
|
||||
RandomAccessIterator begin, RandomAccessIterator end,
|
||||
Comparator comp,
|
||||
|
@ -240,7 +240,7 @@ template<typename RandomAccessIterator, typename Comparator>
|
|||
* @param iam Number of the thread processing this function.
|
||||
*/
|
||||
template<typename RandomAccessIterator, typename Comparator>
|
||||
inline void
|
||||
void
|
||||
qsb_local_sort_with_helping(QSBThreadLocal<RandomAccessIterator>** tls,
|
||||
Comparator& comp, int iam, bool wait)
|
||||
{
|
||||
|
@ -418,7 +418,7 @@ template<typename RandomAccessIterator, typename Comparator>
|
|||
* this part.
|
||||
*/
|
||||
template<typename RandomAccessIterator, typename Comparator>
|
||||
inline void
|
||||
void
|
||||
parallel_sort_qsb(RandomAccessIterator begin, RandomAccessIterator end,
|
||||
Comparator comp,
|
||||
typename std::iterator_traits<RandomAccessIterator>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -53,28 +53,32 @@ namespace __gnu_parallel
|
|||
*/
|
||||
// XXX Comparator default template argument
|
||||
template<typename InputIterator, typename Comparator>
|
||||
bool
|
||||
is_sorted(InputIterator begin, InputIterator end, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>())
|
||||
{
|
||||
if (begin == end)
|
||||
bool
|
||||
is_sorted(InputIterator begin, InputIterator end,
|
||||
Comparator comp
|
||||
= std::less<typename std::iterator_traits<InputIterator>::
|
||||
value_type>())
|
||||
{
|
||||
if (begin == end)
|
||||
return true;
|
||||
|
||||
InputIterator current(begin), recent(begin);
|
||||
|
||||
unsigned long long position = 1;
|
||||
for (current++; current != end; current++)
|
||||
{
|
||||
if (comp(*current, *recent))
|
||||
{
|
||||
printf("is_sorted: check failed before position %i.\n",
|
||||
position);
|
||||
return false;
|
||||
}
|
||||
recent = current;
|
||||
position++;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
InputIterator current(begin), recent(begin);
|
||||
|
||||
unsigned long long position = 1;
|
||||
for (current++; current != end; current++)
|
||||
{
|
||||
if (comp(*current, *recent))
|
||||
{
|
||||
printf("is_sorted: check failed before position %i.\n", position);
|
||||
return false;
|
||||
}
|
||||
recent = current;
|
||||
position++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check whether @c [begin, @c end) is sorted according to @c comp.
|
||||
|
@ -87,30 +91,35 @@ namespace __gnu_parallel
|
|||
*/
|
||||
// XXX Comparator default template argument
|
||||
template<typename InputIterator, typename Comparator>
|
||||
bool
|
||||
is_sorted_failure(InputIterator begin, InputIterator end, InputIterator& first_failure, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>())
|
||||
{
|
||||
if (begin == end)
|
||||
bool
|
||||
is_sorted_failure(InputIterator begin, InputIterator end,
|
||||
InputIterator& first_failure,
|
||||
Comparator comp
|
||||
= std::less<typename std::iterator_traits<InputIterator>::
|
||||
value_type>())
|
||||
{
|
||||
if (begin == end)
|
||||
return true;
|
||||
|
||||
InputIterator current(begin), recent(begin);
|
||||
|
||||
unsigned long long position = 1;
|
||||
for (current++; current != end; current++)
|
||||
{
|
||||
if (comp(*current, *recent))
|
||||
{
|
||||
first_failure = current;
|
||||
printf("is_sorted: check failed before position %lld.\n",
|
||||
position);
|
||||
return false;
|
||||
}
|
||||
recent = current;
|
||||
position++;
|
||||
}
|
||||
|
||||
first_failure = end;
|
||||
return true;
|
||||
|
||||
InputIterator current(begin), recent(begin);
|
||||
|
||||
unsigned long long position = 1;
|
||||
for (current++; current != end; current++)
|
||||
{
|
||||
if (comp(*current, *recent))
|
||||
{
|
||||
first_failure = current;
|
||||
printf("is_sorted: check failed before position %lld.\n", position);
|
||||
return false;
|
||||
}
|
||||
recent = current;
|
||||
position++;
|
||||
}
|
||||
|
||||
first_failure = end;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check whether @c [begin, @c end) is sorted according to @c comp.
|
||||
|
@ -121,28 +130,31 @@ namespace __gnu_parallel
|
|||
* @return @c true if sorted, @c false otherwise.
|
||||
*/
|
||||
template<typename InputIterator, typename Comparator>
|
||||
bool
|
||||
// XXX Comparator default template argument
|
||||
is_sorted_print_failures(InputIterator begin, InputIterator end, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>())
|
||||
{
|
||||
if (begin == end)
|
||||
return true;
|
||||
bool
|
||||
// XXX Comparator default template argument
|
||||
is_sorted_print_failures(InputIterator begin, InputIterator end,
|
||||
Comparator comp
|
||||
= std::less<typename std::iterator_traits
|
||||
<InputIterator>::value_type>())
|
||||
{
|
||||
if (begin == end)
|
||||
return true;
|
||||
|
||||
InputIterator recent(begin);
|
||||
bool ok = true;
|
||||
InputIterator recent(begin);
|
||||
bool ok = true;
|
||||
|
||||
for (InputIterator pos(begin + 1); pos != end; pos++)
|
||||
{
|
||||
if (comp(*pos, *recent))
|
||||
{
|
||||
printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2),
|
||||
*(pos- 1), *pos, *(pos + 1));
|
||||
ok = false;
|
||||
}
|
||||
recent = pos;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
for (InputIterator pos(begin + 1); pos != end; pos++)
|
||||
{
|
||||
if (comp(*pos, *recent))
|
||||
{
|
||||
printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2),
|
||||
*(pos- 1), *pos, *(pos + 1));
|
||||
ok = false;
|
||||
}
|
||||
recent = pos;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -58,11 +58,10 @@ namespace __gnu_parallel
|
|||
* @param selector Functionality (e. g. std::find_if (), std::equal(),...)
|
||||
* @return Place of finding in both sequences.
|
||||
*/
|
||||
template<
|
||||
typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
template<typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Pred pred, Selector selector)
|
||||
|
@ -96,11 +95,10 @@ template<
|
|||
* @param selector Functionality (e. g. std::find_if (), std::equal(),...)
|
||||
* @return Place of finding in both sequences.
|
||||
*/
|
||||
template<
|
||||
typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
template<typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
find_template(RandomAccessIterator1 begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
|
@ -190,11 +188,10 @@ template<
|
|||
* for CSB, the blocks are allocated in a predetermined manner,
|
||||
* namely spacial round-robin.
|
||||
*/
|
||||
template<
|
||||
typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
template<typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Pred pred, Selector selector,
|
||||
|
@ -311,11 +308,10 @@ template<
|
|||
* blocks are allocated in a predetermined manner, namely spacial
|
||||
* round-robin.
|
||||
*/
|
||||
template<
|
||||
typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
template<typename RandomAccessIterator1,
|
||||
typename RandomAccessIterator2,
|
||||
typename Pred,
|
||||
typename Selector>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Pred pred, Selector selector,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -60,10 +60,11 @@ namespace __gnu_parallel
|
|||
* @param i2 Iterator on second sequence (unused).
|
||||
* @param pred Find predicate.
|
||||
*/
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{ return pred(*i1); }
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{ return pred(*i1); }
|
||||
|
||||
/** @brief Corresponding sequential algorithm on a sequence.
|
||||
* @param begin1 Begin iterator of first sequence.
|
||||
|
@ -71,12 +72,14 @@ namespace __gnu_parallel
|
|||
* @param begin2 Begin iterator of second sequence.
|
||||
* @param pred Find predicate.
|
||||
*/
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred)
|
||||
{
|
||||
return std::make_pair(find_if(begin1, end1, pred, sequential_tag()), begin2);
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Pred pred)
|
||||
{ return std::make_pair(find_if(begin1, end1, pred,
|
||||
sequential_tag()), begin2); }
|
||||
};
|
||||
|
||||
/** @brief Test predicate on two adjacent elements. */
|
||||
|
@ -87,13 +90,14 @@ namespace __gnu_parallel
|
|||
* @param i2 Iterator on second sequence (unused).
|
||||
* @param pred Find predicate.
|
||||
*/
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{
|
||||
// Passed end iterator is one short.
|
||||
return pred(*i1, *(i1 + 1));
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{
|
||||
// Passed end iterator is one short.
|
||||
return pred(*i1, *(i1 + 1));
|
||||
}
|
||||
|
||||
/** @brief Corresponding sequential algorithm on a sequence.
|
||||
* @param begin1 Begin iterator of first sequence.
|
||||
|
@ -101,16 +105,20 @@ namespace __gnu_parallel
|
|||
* @param begin2 Begin iterator of second sequence.
|
||||
* @param pred Find predicate.
|
||||
*/
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred)
|
||||
{
|
||||
// Passed end iterator is one short.
|
||||
RandomAccessIterator1 spot = adjacent_find(begin1, end1 + 1, pred, sequential_tag());
|
||||
if (spot == (end1 + 1))
|
||||
spot = end1;
|
||||
return std::make_pair(spot, begin2);
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Pred pred)
|
||||
{
|
||||
// Passed end iterator is one short.
|
||||
RandomAccessIterator1 spot = adjacent_find(begin1, end1 + 1,
|
||||
pred, sequential_tag());
|
||||
if (spot == (end1 + 1))
|
||||
spot = end1;
|
||||
return std::make_pair(spot, begin2);
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief Test inverted predicate on a single element. */
|
||||
|
@ -122,10 +130,11 @@ namespace __gnu_parallel
|
|||
* @param i2 Iterator on second sequence (unused).
|
||||
* @param pred Find predicate.
|
||||
*/
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{ return !pred(*i1, *i2); }
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{ return !pred(*i1, *i2); }
|
||||
|
||||
/**
|
||||
* @brief Corresponding sequential algorithm on a sequence.
|
||||
|
@ -134,12 +143,13 @@ namespace __gnu_parallel
|
|||
* @param begin2 Begin iterator of second sequence.
|
||||
* @param pred Find predicate.
|
||||
*/
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred)
|
||||
{
|
||||
return mismatch(begin1, end1, begin2, pred, sequential_tag());
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Pred pred)
|
||||
{ return mismatch(begin1, end1, begin2, pred, sequential_tag()); }
|
||||
};
|
||||
|
||||
|
||||
|
@ -157,27 +167,31 @@ namespace __gnu_parallel
|
|||
* @param i1 Iterator on first sequence.
|
||||
* @param i2 Iterator on second sequence (unused).
|
||||
* @param pred Find predicate. */
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{
|
||||
for (ForwardIterator pos_in_candidates = begin; pos_in_candidates != end; pos_in_candidates++)
|
||||
if (pred(*i1, *pos_in_candidates))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
bool
|
||||
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
|
||||
{
|
||||
for (ForwardIterator pos_in_candidates = begin;
|
||||
pos_in_candidates != end; ++pos_in_candidates)
|
||||
if (pred(*i1, *pos_in_candidates))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @brief Corresponding sequential algorithm on a sequence.
|
||||
* @param begin1 Begin iterator of first sequence.
|
||||
* @param end1 End iterator of first sequence.
|
||||
* @param begin2 Begin iterator of second sequence.
|
||||
* @param pred Find predicate. */
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred>
|
||||
inline std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred)
|
||||
{
|
||||
return std::make_pair(find_first_of(begin1, end1, begin, end, pred, sequential_tag()), begin2);
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename Pred>
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator2>
|
||||
sequential_algorithm(RandomAccessIterator1 begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2 begin2, Pred pred)
|
||||
{ return std::make_pair(find_first_of(begin1, end1, begin, end, pred,
|
||||
sequential_tag()), begin2); }
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -61,22 +61,42 @@ namespace __gnu_parallel
|
|||
* @param output Output iterator.
|
||||
* @param bound Maximum number of elements processed.
|
||||
* @param parallelism_tag Parallelization method */
|
||||
template<typename InputIterator, typename UserOp, typename Functionality, typename Red, typename Result>
|
||||
UserOp
|
||||
for_each_template_random_access(InputIterator begin, InputIterator end,
|
||||
UserOp user_op, Functionality& functionality,
|
||||
Red reduction, Result reduction_start,
|
||||
Result& output,
|
||||
typename std::iterator_traits<InputIterator>::difference_type bound, parallelism parallelism_tag)
|
||||
{
|
||||
if (parallelism_tag == parallel_unbalanced)
|
||||
return for_each_template_random_access_ed(begin, end, user_op, functionality, reduction, reduction_start, output, bound);
|
||||
else if (parallelism_tag == parallel_omp_loop)
|
||||
return for_each_template_random_access_omp_loop(begin, end, user_op, functionality, reduction, reduction_start, output, bound);
|
||||
else if (parallelism_tag == parallel_omp_loop_static)
|
||||
return for_each_template_random_access_omp_loop(begin, end, user_op, functionality, reduction, reduction_start, output, bound);
|
||||
else //e. g. parallel_balanced
|
||||
return for_each_template_random_access_workstealing(begin, end, user_op, functionality, reduction, reduction_start, output, bound);
|
||||
template<typename InputIterator, typename UserOp,
|
||||
typename Functionality, typename Red, typename Result>
|
||||
UserOp
|
||||
for_each_template_random_access(InputIterator begin, InputIterator end,
|
||||
UserOp user_op,
|
||||
Functionality& functionality,
|
||||
Red reduction, Result reduction_start,
|
||||
Result& output, typename
|
||||
std::iterator_traits<InputIterator>::
|
||||
difference_type bound,
|
||||
parallelism parallelism_tag)
|
||||
{
|
||||
if (parallelism_tag == parallel_unbalanced)
|
||||
return for_each_template_random_access_ed(begin, end, user_op,
|
||||
functionality, reduction,
|
||||
reduction_start,
|
||||
output, bound);
|
||||
else if (parallelism_tag == parallel_omp_loop)
|
||||
return for_each_template_random_access_omp_loop(begin, end, user_op,
|
||||
functionality,
|
||||
reduction,
|
||||
reduction_start,
|
||||
output, bound);
|
||||
else if (parallelism_tag == parallel_omp_loop_static)
|
||||
return for_each_template_random_access_omp_loop(begin, end, user_op,
|
||||
functionality,
|
||||
reduction,
|
||||
reduction_start,
|
||||
output, bound);
|
||||
else //e. g. parallel_balanced
|
||||
return for_each_template_random_access_workstealing(begin, end,
|
||||
user_op,
|
||||
functionality,
|
||||
reduction,
|
||||
reduction_start,
|
||||
output, bound);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -63,11 +63,12 @@ namespace __gnu_parallel
|
|||
* @param o Operator.
|
||||
* @param i Iterator referencing object. */
|
||||
template<typename Op>
|
||||
inline bool operator()(Op& o, It i)
|
||||
{
|
||||
o(*i);
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
operator()(Op& o, It i)
|
||||
{
|
||||
o(*i);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief std::generate() selector. */
|
||||
|
@ -78,11 +79,12 @@ namespace __gnu_parallel
|
|||
* @param o Operator.
|
||||
* @param i Iterator referencing object. */
|
||||
template<typename Op>
|
||||
inline bool operator()(Op& o, It i)
|
||||
{
|
||||
*i = o();
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
operator()(Op& o, It i)
|
||||
{
|
||||
*i = o();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief std::fill() selector. */
|
||||
|
@ -93,11 +95,12 @@ namespace __gnu_parallel
|
|||
* @param v Current value.
|
||||
* @param i Iterator referencing object. */
|
||||
template<typename Val>
|
||||
inline bool operator()(Val& v, It i)
|
||||
{
|
||||
*i = v;
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
operator()(Val& v, It i)
|
||||
{
|
||||
*i = v;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief std::transform() selector, one input sequence variant. */
|
||||
|
@ -108,11 +111,12 @@ namespace __gnu_parallel
|
|||
* @param o Operator.
|
||||
* @param i Iterator referencing object. */
|
||||
template<typename Op>
|
||||
inline bool operator()(Op& o, It i)
|
||||
{
|
||||
*i.second = o(*i.first);
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
operator()(Op& o, It i)
|
||||
{
|
||||
*i.second = o(*i.first);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief std::transform() selector, two input sequences variant. */
|
||||
|
@ -123,11 +127,12 @@ namespace __gnu_parallel
|
|||
* @param o Operator.
|
||||
* @param i Iterator referencing object. */
|
||||
template<typename Op>
|
||||
inline bool operator()(Op& o, It i)
|
||||
{
|
||||
*i.third = o(*i.first, *i.second);
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
operator()(Op& o, It i)
|
||||
{
|
||||
*i.third = o(*i.first, *i.second);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief std::replace() selector. */
|
||||
|
@ -144,7 +149,8 @@ namespace __gnu_parallel
|
|||
/** @brief Functor execution.
|
||||
* @param v Current value.
|
||||
* @param i Iterator referencing object. */
|
||||
inline bool operator()(T& v, It i)
|
||||
bool
|
||||
operator()(T& v, It i)
|
||||
{
|
||||
if (*i == v)
|
||||
*i = new_val;
|
||||
|
@ -166,7 +172,8 @@ namespace __gnu_parallel
|
|||
/** @brief Functor execution.
|
||||
* @param o Operator.
|
||||
* @param i Iterator referencing object. */
|
||||
inline bool operator()(Op& o, It i)
|
||||
bool
|
||||
operator()(Op& o, It i)
|
||||
{
|
||||
if (o(*i))
|
||||
*i = new_val;
|
||||
|
@ -183,8 +190,9 @@ namespace __gnu_parallel
|
|||
* @param i Iterator referencing object.
|
||||
* @return 1 if count, 0 if does not count. */
|
||||
template<typename Val>
|
||||
inline Diff operator()(Val& v, It i)
|
||||
{ return (v == *i) ? 1 : 0; }
|
||||
Diff
|
||||
operator()(Val& v, It i)
|
||||
{ return (v == *i) ? 1 : 0; }
|
||||
};
|
||||
|
||||
/** @brief std::count_if () selector. */
|
||||
|
@ -196,8 +204,9 @@ namespace __gnu_parallel
|
|||
* @param i Iterator referencing object.
|
||||
* @return 1 if count, 0 if does not count. */
|
||||
template<typename Op>
|
||||
inline Diff operator()(Op& o, It i)
|
||||
{ return (o(*i)) ? 1 : 0; }
|
||||
Diff
|
||||
operator()(Op& o, It i)
|
||||
{ return (o(*i)) ? 1 : 0; }
|
||||
};
|
||||
|
||||
/** @brief std::accumulate() selector. */
|
||||
|
@ -209,8 +218,8 @@ namespace __gnu_parallel
|
|||
* @param i Iterator referencing object.
|
||||
* @return The current value. */
|
||||
template<typename Op>
|
||||
inline typename std::iterator_traits<It>::value_type operator()(Op o, It i)
|
||||
{ return *i; }
|
||||
typename std::iterator_traits<It>::value_type operator()(Op o, It i)
|
||||
{ return *i; }
|
||||
};
|
||||
|
||||
/** @brief std::inner_product() selector. */
|
||||
|
@ -226,18 +235,21 @@ namespace __gnu_parallel
|
|||
/** @brief Constructor.
|
||||
* @param b1 Begin iterator of first sequence.
|
||||
* @param b2 Begin iterator of second sequence. */
|
||||
explicit inner_product_selector(It b1, It2 b2) : begin1_iterator(b1), begin2_iterator(b2) { }
|
||||
explicit inner_product_selector(It b1, It2 b2)
|
||||
: begin1_iterator(b1), begin2_iterator(b2) { }
|
||||
|
||||
/** @brief Functor execution.
|
||||
* @param mult Multiplication functor.
|
||||
* @param current Iterator referencing object.
|
||||
* @return Inner product elemental result. */
|
||||
template<typename Op>
|
||||
inline T operator()(Op mult, It current)
|
||||
{
|
||||
typename std::iterator_traits<It>::difference_type position = current - begin1_iterator;
|
||||
return mult(*current, *(begin2_iterator + position));
|
||||
}
|
||||
T
|
||||
operator()(Op mult, It current)
|
||||
{
|
||||
typename std::iterator_traits<It>::difference_type position
|
||||
= current - begin1_iterator;
|
||||
return mult(*current, *(begin2_iterator + position));
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief Selector that just returns the passed iterator. */
|
||||
|
@ -249,8 +261,9 @@ namespace __gnu_parallel
|
|||
* @param i Iterator referencing object.
|
||||
* @return Passed iterator. */
|
||||
template<typename Op>
|
||||
inline It operator()(Op o, It i)
|
||||
{ return i; }
|
||||
It
|
||||
operator()(Op o, It i)
|
||||
{ return i; }
|
||||
};
|
||||
|
||||
/** @brief Selector that returns the difference between two adjacent
|
||||
|
@ -260,13 +273,14 @@ namespace __gnu_parallel
|
|||
struct adjacent_difference_selector : public generic_for_each_selector<It>
|
||||
{
|
||||
template<typename Op>
|
||||
inline bool operator()(Op& o, It i)
|
||||
{
|
||||
typename It::first_type go_back_one = i.first;
|
||||
--go_back_one;
|
||||
*i.second = o(*i.first, *go_back_one);
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
operator()(Op& o, It i)
|
||||
{
|
||||
typename It::first_type go_back_one = i.first;
|
||||
--go_back_one;
|
||||
*i.second = o(*i.first, *go_back_one);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// XXX move into type_traits?
|
||||
|
@ -280,14 +294,15 @@ namespace __gnu_parallel
|
|||
/** @brief Functor execution.
|
||||
* @param i Iterator referencing object. */
|
||||
template<typename It>
|
||||
inline void operator()(It i)
|
||||
{ }
|
||||
void
|
||||
operator()(It i) { }
|
||||
};
|
||||
|
||||
/** @brief Reduction function doing nothing. */
|
||||
struct dummy_reduct
|
||||
{
|
||||
inline bool operator()(bool /*x*/, bool /*y*/) const
|
||||
bool
|
||||
operator()(bool /*x*/, bool /*y*/) const
|
||||
{ return true; }
|
||||
};
|
||||
|
||||
|
@ -300,7 +315,8 @@ namespace __gnu_parallel
|
|||
explicit min_element_reduct(Comp &c) : comp(c)
|
||||
{ }
|
||||
|
||||
inline It operator()(It x, It y)
|
||||
It
|
||||
operator()(It x, It y)
|
||||
{
|
||||
if (comp(*x, *y))
|
||||
return x;
|
||||
|
@ -318,7 +334,8 @@ namespace __gnu_parallel
|
|||
explicit max_element_reduct(Comp& c) : comp(c)
|
||||
{ }
|
||||
|
||||
inline It operator()(It x, It y)
|
||||
It
|
||||
operator()(It x, It y)
|
||||
{
|
||||
if (comp(*x, *y))
|
||||
return y;
|
||||
|
@ -336,7 +353,9 @@ namespace __gnu_parallel
|
|||
explicit accumulate_binop_reduct(BinOp& b) : binop(b) {}
|
||||
|
||||
template<typename Result, typename Addend>
|
||||
Result operator()(const Result& x, const Addend& y) { return binop(x, y); }
|
||||
Result
|
||||
operator()(const Result& x, const Addend& y)
|
||||
{ return binop(x, y); }
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -47,156 +47,158 @@ namespace __gnu_parallel
|
|||
* applied to both child iterators.
|
||||
*/
|
||||
template<typename Iterator1, typename Iterator2, typename IteratorCategory>
|
||||
class iterator_pair : public std::pair<Iterator1, Iterator2>
|
||||
{
|
||||
private:
|
||||
typedef iterator_pair<Iterator1, Iterator2, IteratorCategory> type;
|
||||
typedef std::pair<Iterator1, Iterator2> base_type;
|
||||
|
||||
public:
|
||||
typedef IteratorCategory iterator_category;
|
||||
typedef void value_type;
|
||||
|
||||
typedef std::iterator_traits<Iterator1> traits_type;
|
||||
typedef typename traits_type::difference_type difference_type;
|
||||
typedef type* pointer;
|
||||
typedef type& reference;
|
||||
|
||||
iterator_pair() { }
|
||||
|
||||
iterator_pair(const Iterator1& first, const Iterator2& second)
|
||||
: base_type(first, second) { }
|
||||
|
||||
// Pre-increment operator.
|
||||
type&
|
||||
operator++()
|
||||
class iterator_pair : public std::pair<Iterator1, Iterator2>
|
||||
{
|
||||
++base_type::first;
|
||||
++base_type::second;
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
typedef iterator_pair<Iterator1, Iterator2, IteratorCategory> type;
|
||||
typedef std::pair<Iterator1, Iterator2> base_type;
|
||||
|
||||
// Post-increment operator.
|
||||
const type
|
||||
operator++(int)
|
||||
{ return type(base_type::first++, base_type::second++); }
|
||||
public:
|
||||
typedef IteratorCategory iterator_category;
|
||||
typedef void value_type;
|
||||
|
||||
// Pre-decrement operator.
|
||||
type&
|
||||
operator--()
|
||||
{
|
||||
--base_type::first;
|
||||
--base_type::second;
|
||||
return *this;
|
||||
}
|
||||
typedef std::iterator_traits<Iterator1> traits_type;
|
||||
typedef typename traits_type::difference_type difference_type;
|
||||
typedef type* pointer;
|
||||
typedef type& reference;
|
||||
|
||||
// Post-decrement operator.
|
||||
const type
|
||||
operator--(int)
|
||||
{ return type(base_type::first--, base_type::second--); }
|
||||
iterator_pair() { }
|
||||
|
||||
// Type conversion.
|
||||
operator Iterator2() const
|
||||
{ return base_type::second; }
|
||||
iterator_pair(const Iterator1& first, const Iterator2& second)
|
||||
: base_type(first, second) { }
|
||||
|
||||
type&
|
||||
operator=(const type& other)
|
||||
{
|
||||
base_type::first = other.first;
|
||||
base_type::second = other.second;
|
||||
return *this;
|
||||
}
|
||||
// Pre-increment operator.
|
||||
type&
|
||||
operator++()
|
||||
{
|
||||
++base_type::first;
|
||||
++base_type::second;
|
||||
return *this;
|
||||
}
|
||||
|
||||
type
|
||||
operator+(difference_type delta) const
|
||||
{ return type(base_type::first + delta, base_type::second + delta); }
|
||||
// Post-increment operator.
|
||||
const type
|
||||
operator++(int)
|
||||
{ return type(base_type::first++, base_type::second++); }
|
||||
|
||||
difference_type
|
||||
operator-(const type& other) const
|
||||
{ return base_type::first - other.first; }
|
||||
// Pre-decrement operator.
|
||||
type&
|
||||
operator--()
|
||||
{
|
||||
--base_type::first;
|
||||
--base_type::second;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Post-decrement operator.
|
||||
const type
|
||||
operator--(int)
|
||||
{ return type(base_type::first--, base_type::second--); }
|
||||
|
||||
// Type conversion.
|
||||
operator Iterator2() const
|
||||
{ return base_type::second; }
|
||||
|
||||
type&
|
||||
operator=(const type& other)
|
||||
{
|
||||
base_type::first = other.first;
|
||||
base_type::second = other.second;
|
||||
return *this;
|
||||
}
|
||||
|
||||
type
|
||||
operator+(difference_type delta) const
|
||||
{ return type(base_type::first + delta, base_type::second + delta); }
|
||||
|
||||
difference_type
|
||||
operator-(const type& other) const
|
||||
{ return base_type::first - other.first; }
|
||||
};
|
||||
|
||||
|
||||
/** @brief A triple of iterators. The usual iterator operations are
|
||||
applied to all three child iterators.
|
||||
*/
|
||||
template<typename Iterator1, typename Iterator2, typename Iterator3, typename IteratorCategory>
|
||||
class iterator_triple
|
||||
{
|
||||
private:
|
||||
typedef iterator_triple<Iterator1, Iterator2, Iterator3, IteratorCategory> type;
|
||||
|
||||
public:
|
||||
typedef IteratorCategory iterator_category;
|
||||
typedef void value_type;
|
||||
typedef typename Iterator1::difference_type difference_type;
|
||||
typedef type* pointer;
|
||||
typedef type& reference;
|
||||
|
||||
Iterator1 first;
|
||||
Iterator2 second;
|
||||
Iterator3 third;
|
||||
|
||||
iterator_triple() { }
|
||||
|
||||
iterator_triple(const Iterator1& _first, const Iterator2& _second,
|
||||
const Iterator3& _third)
|
||||
template<typename Iterator1, typename Iterator2, typename Iterator3,
|
||||
typename IteratorCategory>
|
||||
class iterator_triple
|
||||
{
|
||||
first = _first;
|
||||
second = _second;
|
||||
third = _third;
|
||||
}
|
||||
private:
|
||||
typedef iterator_triple<Iterator1, Iterator2, Iterator3,
|
||||
IteratorCategory> type;
|
||||
|
||||
// Pre-increment operator.
|
||||
type&
|
||||
operator++()
|
||||
{
|
||||
++first;
|
||||
++second;
|
||||
++third;
|
||||
return *this;
|
||||
}
|
||||
public:
|
||||
typedef IteratorCategory iterator_category;
|
||||
typedef void value_type;
|
||||
typedef typename Iterator1::difference_type difference_type;
|
||||
typedef type* pointer;
|
||||
typedef type& reference;
|
||||
|
||||
// Post-increment operator.
|
||||
const type
|
||||
operator++(int)
|
||||
{ return type(first++, second++, third++); }
|
||||
Iterator1 first;
|
||||
Iterator2 second;
|
||||
Iterator3 third;
|
||||
|
||||
// Pre-decrement operator.
|
||||
type&
|
||||
operator--()
|
||||
{
|
||||
--first;
|
||||
--second;
|
||||
--third;
|
||||
return *this;
|
||||
}
|
||||
iterator_triple() { }
|
||||
|
||||
// Post-decrement operator.
|
||||
const type
|
||||
operator--(int)
|
||||
{ return type(first--, second--, third--); }
|
||||
iterator_triple(const Iterator1& _first, const Iterator2& _second,
|
||||
const Iterator3& _third)
|
||||
{
|
||||
first = _first;
|
||||
second = _second;
|
||||
third = _third;
|
||||
}
|
||||
|
||||
// Type conversion.
|
||||
operator Iterator3() const
|
||||
{ return third; }
|
||||
// Pre-increment operator.
|
||||
type&
|
||||
operator++()
|
||||
{
|
||||
++first;
|
||||
++second;
|
||||
++third;
|
||||
return *this;
|
||||
}
|
||||
|
||||
type&
|
||||
operator=(const type& other)
|
||||
{
|
||||
first = other.first;
|
||||
second = other.second;
|
||||
third = other.third;
|
||||
return *this;
|
||||
}
|
||||
// Post-increment operator.
|
||||
const type
|
||||
operator++(int)
|
||||
{ return type(first++, second++, third++); }
|
||||
|
||||
type
|
||||
operator+(difference_type delta) const
|
||||
{ return type(first + delta, second + delta, third + delta); }
|
||||
// Pre-decrement operator.
|
||||
type&
|
||||
operator--()
|
||||
{
|
||||
--first;
|
||||
--second;
|
||||
--third;
|
||||
return *this;
|
||||
}
|
||||
|
||||
difference_type
|
||||
operator-(const type& other) const
|
||||
{ return first - other.first; }
|
||||
// Post-decrement operator.
|
||||
const type
|
||||
operator--(int)
|
||||
{ return type(first--, second--, third--); }
|
||||
|
||||
// Type conversion.
|
||||
operator Iterator3() const
|
||||
{ return third; }
|
||||
|
||||
type&
|
||||
operator=(const type& other)
|
||||
{
|
||||
first = other.first;
|
||||
second = other.second;
|
||||
third = other.third;
|
||||
return *this;
|
||||
}
|
||||
|
||||
type
|
||||
operator+(difference_type delta) const
|
||||
{ return type(first + delta, second + delta, third + delta); }
|
||||
|
||||
difference_type
|
||||
operator-(const type& other) const
|
||||
{ return first - other.first; }
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -52,36 +52,35 @@ namespace __gnu_parallel
|
|||
* grown or not
|
||||
*/
|
||||
template<typename InputIterator>
|
||||
void
|
||||
shrink_and_double(std::vector<InputIterator>& os_starts, size_t& count_to_two, size_t& range_length, const bool make_twice)
|
||||
{
|
||||
++count_to_two;
|
||||
if (not make_twice or count_to_two < 2)
|
||||
{
|
||||
void
|
||||
shrink_and_double(std::vector<InputIterator>& os_starts,
|
||||
size_t& count_to_two, size_t& range_length,
|
||||
const bool make_twice)
|
||||
{
|
||||
++count_to_two;
|
||||
if (not make_twice or count_to_two < 2)
|
||||
shrink(os_starts, count_to_two, range_length);
|
||||
}
|
||||
else
|
||||
{
|
||||
os_starts.resize((os_starts.size() - 1) * 2 + 1);
|
||||
count_to_two = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
os_starts.resize((os_starts.size() - 1) * 2 + 1);
|
||||
count_to_two = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Combines two ranges into one and thus halves the number of ranges.
|
||||
* @param os_starts Start positions worked on (oversampled).
|
||||
* @param count_to_two Counts up to 2.
|
||||
* @param range_length Current length of a chunk. */
|
||||
template<typename InputIterator>
|
||||
void
|
||||
shrink(std::vector<InputIterator>& os_starts, size_t& count_to_two,
|
||||
size_t& range_length)
|
||||
{
|
||||
for (typename std::vector<InputIterator>::size_type i = 0; i <= (os_starts.size() / 2); ++i)
|
||||
{
|
||||
void
|
||||
shrink(std::vector<InputIterator>& os_starts, size_t& count_to_two,
|
||||
size_t& range_length)
|
||||
{
|
||||
for (typename std::vector<InputIterator>::size_type i = 0;
|
||||
i <= (os_starts.size() / 2); ++i)
|
||||
os_starts[i] = os_starts[i * 2];
|
||||
}
|
||||
range_length *= 2;
|
||||
}
|
||||
range_length *= 2;
|
||||
}
|
||||
|
||||
/** @brief Splits a sequence given by input iterators into parts of
|
||||
* almost equal size
|
||||
|
@ -103,79 +102,81 @@ namespace __gnu_parallel
|
|||
* @return Length of the whole sequence.
|
||||
*/
|
||||
template<typename InputIterator, typename FunctorType>
|
||||
size_t
|
||||
list_partition(const InputIterator begin, const InputIterator end,
|
||||
InputIterator* starts, size_t* lengths, const int num_parts,
|
||||
FunctorType& f, int oversampling = 0)
|
||||
{
|
||||
bool make_twice = false;
|
||||
size_t
|
||||
list_partition(const InputIterator begin, const InputIterator end,
|
||||
InputIterator* starts, size_t* lengths, const int num_parts,
|
||||
FunctorType& f, int oversampling = 0)
|
||||
{
|
||||
bool make_twice = false;
|
||||
|
||||
// According to the oversampling factor, the resizing algorithm is chosen.
|
||||
if (oversampling == 0)
|
||||
{
|
||||
make_twice = true;
|
||||
oversampling = 1;
|
||||
}
|
||||
|
||||
std::vector<InputIterator> os_starts(2 * oversampling * num_parts + 1);
|
||||
|
||||
os_starts[0]= begin;
|
||||
InputIterator prev = begin, it = begin;
|
||||
size_t dist_limit = 0, dist = 0;
|
||||
size_t cur = 1, next = 1;
|
||||
size_t range_length = 1;
|
||||
size_t count_to_two = 0;
|
||||
while (it != end){
|
||||
cur = next;
|
||||
for (; cur < os_starts.size() and it != end; ++cur)
|
||||
// According to the oversampling factor, the resizing algorithm is chosen.
|
||||
if (oversampling == 0)
|
||||
{
|
||||
for (dist_limit += range_length; dist < dist_limit and it != end; ++dist)
|
||||
{
|
||||
f(it);
|
||||
++it;
|
||||
}
|
||||
os_starts[cur] = it;
|
||||
make_twice = true;
|
||||
oversampling = 1;
|
||||
}
|
||||
|
||||
// Must compare for end and not cur < os_starts.size() , because
|
||||
// cur could be == os_starts.size() as well
|
||||
if (it == end)
|
||||
break;
|
||||
std::vector<InputIterator> os_starts(2 * oversampling * num_parts + 1);
|
||||
|
||||
shrink_and_double(os_starts, count_to_two, range_length, make_twice);
|
||||
next = os_starts.size()/2 + 1;
|
||||
os_starts[0]= begin;
|
||||
InputIterator prev = begin, it = begin;
|
||||
size_t dist_limit = 0, dist = 0;
|
||||
size_t cur = 1, next = 1;
|
||||
size_t range_length = 1;
|
||||
size_t count_to_two = 0;
|
||||
while (it != end)
|
||||
{
|
||||
cur = next;
|
||||
for (; cur < os_starts.size() and it != end; ++cur)
|
||||
{
|
||||
for (dist_limit += range_length;
|
||||
dist < dist_limit and it != end; ++dist)
|
||||
{
|
||||
f(it);
|
||||
++it;
|
||||
}
|
||||
os_starts[cur] = it;
|
||||
}
|
||||
|
||||
// Must compare for end and not cur < os_starts.size() , because
|
||||
// cur could be == os_starts.size() as well
|
||||
if (it == end)
|
||||
break;
|
||||
|
||||
shrink_and_double(os_starts, count_to_two, range_length, make_twice);
|
||||
next = os_starts.size() / 2 + 1;
|
||||
}
|
||||
|
||||
// Calculation of the parts (one must be extracted from current
|
||||
// because the partition beginning at end, consists only of
|
||||
// itself).
|
||||
size_t size_part = (cur - 1) / num_parts;
|
||||
int size_greater = static_cast<int>((cur - 1) % num_parts);
|
||||
starts[0] = os_starts[0];
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
// Smallest partitions.
|
||||
for (int i = 1; i < (num_parts + 1 - size_greater); ++i)
|
||||
{
|
||||
lengths[i-1] = size_part * range_length;
|
||||
index += size_part;
|
||||
starts[i] = os_starts[index];
|
||||
}
|
||||
|
||||
// Biggest partitions.
|
||||
for (int i = num_parts + 1 - size_greater; i <= num_parts; ++i)
|
||||
{
|
||||
lengths[i-1] = (size_part+1) * range_length;
|
||||
index += (size_part+1);
|
||||
starts[i] = os_starts[index];
|
||||
}
|
||||
|
||||
// Correction of the end size (the end iteration has not finished).
|
||||
lengths[num_parts - 1] -= (dist_limit - dist);
|
||||
|
||||
return dist;
|
||||
}
|
||||
|
||||
// Calculation of the parts (one must be extracted from current
|
||||
// because the partition beginning at end, consists only of
|
||||
// itself).
|
||||
size_t size_part = (cur - 1) / num_parts;
|
||||
int size_greater = static_cast<int>((cur - 1) % num_parts);
|
||||
starts[0] = os_starts[0];
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
// Smallest partitions.
|
||||
for (int i = 1; i < (num_parts + 1 - size_greater); ++i)
|
||||
{
|
||||
lengths[i-1] = size_part * range_length;
|
||||
index += size_part;
|
||||
starts[i] = os_starts[index];
|
||||
}
|
||||
|
||||
// Biggest partitions.
|
||||
for (int i = num_parts + 1 - size_greater; i <= num_parts; ++i)
|
||||
{
|
||||
lengths[i-1] = (size_part+1) * range_length;
|
||||
index += (size_part+1);
|
||||
starts[i] = os_starts[index];
|
||||
}
|
||||
|
||||
// Correction of the end size (the end iteration has not finished).
|
||||
lengths[num_parts - 1] -= (dist_limit - dist);
|
||||
|
||||
return dist;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -76,9 +76,8 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
Comparator comp;
|
||||
|
||||
public:
|
||||
inline
|
||||
LoserTreeExplicit(unsigned int _size, Comparator _comp = std::less<T>())
|
||||
: comp(_comp)
|
||||
: comp(_comp)
|
||||
{
|
||||
size = _size;
|
||||
offset = size;
|
||||
|
@ -92,14 +91,14 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline ~LoserTreeExplicit()
|
||||
~LoserTreeExplicit()
|
||||
{ delete[] losers; }
|
||||
|
||||
inline int
|
||||
int
|
||||
get_min_source()
|
||||
{ return losers[0].source; }
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start(T key, int source, bool sup)
|
||||
{
|
||||
bool inf = false;
|
||||
|
@ -122,10 +121,10 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[0].source = source;
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init() { }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert(T key, bool sup)
|
||||
{
|
||||
bool inf = false;
|
||||
|
@ -151,7 +150,7 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[0].source = source;
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start_stable(T key, int source, bool sup)
|
||||
{
|
||||
bool inf = false;
|
||||
|
@ -176,10 +175,10 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[0].source = source;
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init_stable() { }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert_stable(T key, bool sup)
|
||||
{
|
||||
bool inf = false;
|
||||
|
@ -233,7 +232,7 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
bool first_insert;
|
||||
|
||||
public:
|
||||
inline LoserTree(unsigned int _k, Comparator _comp = std::less<T>())
|
||||
LoserTree(unsigned int _k, Comparator _comp = std::less<T>())
|
||||
: comp(_comp)
|
||||
{
|
||||
ik = _k;
|
||||
|
@ -249,14 +248,14 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
first_insert = true;
|
||||
}
|
||||
|
||||
inline ~LoserTree()
|
||||
~LoserTree()
|
||||
{ ::operator delete(losers); }
|
||||
|
||||
inline int
|
||||
int
|
||||
get_min_source()
|
||||
{ return losers[0].source; }
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start(const T& key, int source, bool sup)
|
||||
{
|
||||
unsigned int pos = k + source;
|
||||
|
@ -303,12 +302,12 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init()
|
||||
{ losers[0] = losers[init_winner(1)]; }
|
||||
|
||||
// Do not pass const reference since key will be used as local variable.
|
||||
inline void
|
||||
void
|
||||
delete_min_insert(T key, bool sup)
|
||||
{
|
||||
int source = losers[0].source;
|
||||
|
@ -329,7 +328,7 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[0].key = key;
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start_stable(const T& key, int source, bool sup)
|
||||
{ return insert_start(key, source, sup); }
|
||||
|
||||
|
@ -361,12 +360,12 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init_stable()
|
||||
{ losers[0] = losers[init_winner_stable(1)]; }
|
||||
|
||||
// Do not pass const reference since key will be used as local variable.
|
||||
inline void
|
||||
void
|
||||
delete_min_insert_stable(T key, bool sup)
|
||||
{
|
||||
int source = losers[0].source;
|
||||
|
@ -432,9 +431,8 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
Comparator comp;
|
||||
|
||||
public:
|
||||
inline
|
||||
LoserTreeReference(unsigned int _k, Comparator _comp = std::less<T>())
|
||||
: comp(_comp)
|
||||
: comp(_comp)
|
||||
{
|
||||
ik = _k;
|
||||
|
||||
|
@ -449,7 +447,7 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[i + k].sup = true;
|
||||
}
|
||||
|
||||
inline ~LoserTreeReference()
|
||||
~LoserTreeReference()
|
||||
{
|
||||
delete[] losers;
|
||||
#ifndef COPY
|
||||
|
@ -457,11 +455,11 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
#endif
|
||||
}
|
||||
|
||||
inline int
|
||||
int
|
||||
get_min_source()
|
||||
{ return losers[0].source; }
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start(T key, int source, bool sup)
|
||||
{
|
||||
unsigned int pos = k + source;
|
||||
|
@ -498,13 +496,13 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init()
|
||||
{
|
||||
losers[0] = losers[init_winner(1)];
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert(T key, bool sup)
|
||||
{
|
||||
int source = losers[0].source;
|
||||
|
@ -529,7 +527,7 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
#endif
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start_stable(T key, int source, bool sup)
|
||||
{ return insert_start(key, source, sup); }
|
||||
|
||||
|
@ -560,11 +558,11 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init_stable()
|
||||
{ losers[0] = losers[init_winner_stable(1)]; }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert_stable(T key, bool sup)
|
||||
{
|
||||
int source = losers[0].source;
|
||||
|
@ -622,7 +620,6 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
Comparator comp;
|
||||
|
||||
public:
|
||||
inline
|
||||
LoserTreePointer(unsigned int _k, Comparator _comp = std::less<T>())
|
||||
: comp(_comp)
|
||||
{
|
||||
|
@ -636,14 +633,14 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[i + k].sup = true;
|
||||
}
|
||||
|
||||
inline ~LoserTreePointer()
|
||||
~LoserTreePointer()
|
||||
{ delete[] losers; }
|
||||
|
||||
inline int
|
||||
int
|
||||
get_min_source()
|
||||
{ return losers[0].source; }
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start(const T& key, int source, bool sup)
|
||||
{
|
||||
unsigned int pos = k + source;
|
||||
|
@ -657,9 +654,7 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
init_winner(unsigned int root)
|
||||
{
|
||||
if (root >= k)
|
||||
{
|
||||
return root;
|
||||
}
|
||||
return root;
|
||||
else
|
||||
{
|
||||
unsigned int left = init_winner (2 * root);
|
||||
|
@ -681,11 +676,11 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init()
|
||||
{ losers[0] = losers[init_winner(1)]; }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert(const T& key, bool sup)
|
||||
{
|
||||
const T* keyp = &key;
|
||||
|
@ -707,7 +702,7 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[0].keyp = keyp;
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start_stable(const T& key, int source, bool sup)
|
||||
{ return insert_start(key, source, sup); }
|
||||
|
||||
|
@ -739,11 +734,11 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init_stable()
|
||||
{ losers[0] = losers[init_winner_stable(1)]; }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert_stable(const T& key, bool sup)
|
||||
{
|
||||
const T* keyp = &key;
|
||||
|
@ -810,9 +805,8 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
|
||||
public:
|
||||
inline
|
||||
LoserTreeUnguarded(unsigned int _k, Comparator _comp = std::less<T>())
|
||||
: comp(_comp)
|
||||
: comp(_comp)
|
||||
{
|
||||
ik = _k;
|
||||
// Next greater or equal power of 2.
|
||||
|
@ -823,17 +817,17 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
map(1, 0, ik);
|
||||
}
|
||||
|
||||
inline ~LoserTreeUnguarded()
|
||||
~LoserTreeUnguarded()
|
||||
{
|
||||
delete[] losers;
|
||||
delete[] mapping;
|
||||
}
|
||||
|
||||
inline int
|
||||
int
|
||||
get_min_source()
|
||||
{ return losers[0].source; }
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start(const T& key, int source, bool)
|
||||
{
|
||||
unsigned int pos = mapping[source];
|
||||
|
@ -868,12 +862,12 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init()
|
||||
{ losers[0] = losers[init_winner(1, 0, ik)]; }
|
||||
|
||||
// Do not pass const reference since key will be used as local variable.
|
||||
inline void
|
||||
void
|
||||
delete_min_insert(const T& key, bool)
|
||||
{
|
||||
losers[0].key = key;
|
||||
|
@ -891,15 +885,15 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start_stable(const T& key, int source, bool)
|
||||
{ return insert_start(key, source, false); }
|
||||
|
||||
inline void
|
||||
void
|
||||
init_stable()
|
||||
{ init(); }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert_stable(const T& key, bool)
|
||||
{
|
||||
losers[0].key = key;
|
||||
|
@ -959,10 +953,9 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
|
||||
public:
|
||||
inline
|
||||
LoserTreePointerUnguarded(unsigned int _k,
|
||||
Comparator _comp = std::less<T>())
|
||||
: comp(_comp)
|
||||
: comp(_comp)
|
||||
{
|
||||
ik = _k;
|
||||
|
||||
|
@ -974,17 +967,17 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
map(1, 0, ik);
|
||||
}
|
||||
|
||||
inline ~LoserTreePointerUnguarded()
|
||||
~LoserTreePointerUnguarded()
|
||||
{
|
||||
delete[] losers;
|
||||
delete[] mapping;
|
||||
}
|
||||
|
||||
inline int
|
||||
int
|
||||
get_min_source()
|
||||
{ return losers[0].source; }
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start(const T& key, int source, bool)
|
||||
{
|
||||
unsigned int pos = mapping[source];
|
||||
|
@ -1019,13 +1012,11 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
init()
|
||||
{
|
||||
losers[0] = losers[init_winner(1, 0, ik)];
|
||||
}
|
||||
{ losers[0] = losers[init_winner(1, 0, ik)]; }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert(const T& key, bool)
|
||||
{
|
||||
const T* keyp = &key;
|
||||
|
@ -1044,15 +1035,15 @@ template<typename T, typename Comparator = std::less<T> >
|
|||
losers[0].keyp = keyp;
|
||||
}
|
||||
|
||||
inline void
|
||||
void
|
||||
insert_start_stable(const T& key, int source, bool)
|
||||
{ return insert_start(key, source, false); }
|
||||
|
||||
inline void
|
||||
void
|
||||
init_stable()
|
||||
{ init(); }
|
||||
|
||||
inline void
|
||||
void
|
||||
delete_min_insert_stable(const T& key, bool)
|
||||
{
|
||||
int& source = losers[0].source;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -56,33 +56,39 @@ namespace __gnu_parallel
|
|||
* @param max_length Maximum number of elements to merge.
|
||||
* @param comp Comparator.
|
||||
* @return Output end iterator. */
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename OutputIterator, typename _DifferenceTp, typename Comparator>
|
||||
OutputIterator
|
||||
merge_advance_usual(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator2& begin2, RandomAccessIterator2 end2, OutputIterator target, _DifferenceTp max_length, Comparator comp)
|
||||
{
|
||||
typedef _DifferenceTp difference_type;
|
||||
while (begin1 != end1 && begin2 != end2 && max_length > 0)
|
||||
{
|
||||
// array1[i1] < array0[i0]
|
||||
if (comp(*begin2, *begin1))
|
||||
*target++ = *begin2++;
|
||||
else
|
||||
*target++ = *begin1++;
|
||||
max_length--;
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename OutputIterator, typename _DifferenceTp,
|
||||
typename Comparator>
|
||||
OutputIterator
|
||||
merge_advance_usual(RandomAccessIterator1& begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2& begin2,
|
||||
RandomAccessIterator2 end2, OutputIterator target,
|
||||
_DifferenceTp max_length, Comparator comp)
|
||||
{
|
||||
typedef _DifferenceTp difference_type;
|
||||
while (begin1 != end1 && begin2 != end2 && max_length > 0)
|
||||
{
|
||||
// array1[i1] < array0[i0]
|
||||
if (comp(*begin2, *begin1))
|
||||
*target++ = *begin2++;
|
||||
else
|
||||
*target++ = *begin1++;
|
||||
max_length--;
|
||||
}
|
||||
|
||||
if (begin1 != end1)
|
||||
{
|
||||
target = std::copy(begin1, begin1 + max_length, target);
|
||||
begin1 += max_length;
|
||||
}
|
||||
else
|
||||
{
|
||||
target = std::copy(begin2, begin2 + max_length, target);
|
||||
begin2 += max_length;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
if (begin1 != end1)
|
||||
{
|
||||
target = std::copy(begin1, begin1 + max_length, target);
|
||||
begin1 += max_length;
|
||||
}
|
||||
else
|
||||
{
|
||||
target = std::copy(begin2, begin2 + max_length, target);
|
||||
begin2 += max_length;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
/** @brief Merge routine being able to merge only the @c max_length
|
||||
* smallest elements.
|
||||
|
@ -99,52 +105,59 @@ namespace __gnu_parallel
|
|||
* @param max_length Maximum number of elements to merge.
|
||||
* @param comp Comparator.
|
||||
* @return Output end iterator. */
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename OutputIterator, typename _DifferenceTp, typename Comparator>
|
||||
OutputIterator
|
||||
merge_advance_movc(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator2& begin2, RandomAccessIterator2 end2, OutputIterator target, _DifferenceTp max_length, Comparator comp)
|
||||
{
|
||||
typedef _DifferenceTp difference_type;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator1>::value_type value_type1;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator2>::value_type value_type2;
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename OutputIterator, typename _DifferenceTp,
|
||||
typename Comparator>
|
||||
OutputIterator
|
||||
merge_advance_movc(RandomAccessIterator1& begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2& begin2,
|
||||
RandomAccessIterator2 end2,
|
||||
OutputIterator target,
|
||||
_DifferenceTp max_length, Comparator comp)
|
||||
{
|
||||
typedef _DifferenceTp difference_type;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator1>::value_type
|
||||
value_type1;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator2>::value_type
|
||||
value_type2;
|
||||
|
||||
#if _GLIBCXX_ASSERTIONS
|
||||
_GLIBCXX_PARALLEL_ASSERT(max_length >= 0);
|
||||
_GLIBCXX_PARALLEL_ASSERT(max_length >= 0);
|
||||
#endif
|
||||
|
||||
while (begin1 != end1 && begin2 != end2 && max_length > 0)
|
||||
{
|
||||
RandomAccessIterator1 next1 = begin1 + 1;
|
||||
RandomAccessIterator2 next2 = begin2 + 1;
|
||||
value_type1 element1 = *begin1;
|
||||
value_type2 element2 = *begin2;
|
||||
while (begin1 != end1 && begin2 != end2 && max_length > 0)
|
||||
{
|
||||
RandomAccessIterator1 next1 = begin1 + 1;
|
||||
RandomAccessIterator2 next2 = begin2 + 1;
|
||||
value_type1 element1 = *begin1;
|
||||
value_type2 element2 = *begin2;
|
||||
|
||||
if (comp(element2, element1))
|
||||
{
|
||||
element1 = element2;
|
||||
begin2 = next2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (comp(element2, element1))
|
||||
{
|
||||
element1 = element2;
|
||||
begin2 = next2;
|
||||
}
|
||||
else
|
||||
begin1 = next1;
|
||||
}
|
||||
|
||||
*target = element1;
|
||||
*target = element1;
|
||||
|
||||
target++;
|
||||
max_length--;
|
||||
}
|
||||
if (begin1 != end1)
|
||||
{
|
||||
target = std::copy(begin1, begin1 + max_length, target);
|
||||
begin1 += max_length;
|
||||
}
|
||||
else
|
||||
{
|
||||
target = std::copy(begin2, begin2 + max_length, target);
|
||||
begin2 += max_length;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
target++;
|
||||
max_length--;
|
||||
}
|
||||
if (begin1 != end1)
|
||||
{
|
||||
target = std::copy(begin1, begin1 + max_length, target);
|
||||
begin1 += max_length;
|
||||
}
|
||||
else
|
||||
{
|
||||
target = std::copy(begin2, begin2 + max_length, target);
|
||||
begin2 += max_length;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
/** @brief Merge routine being able to merge only the @c max_length
|
||||
* smallest elements.
|
||||
|
@ -160,14 +173,20 @@ namespace __gnu_parallel
|
|||
* @param max_length Maximum number of elements to merge.
|
||||
* @param comp Comparator.
|
||||
* @return Output end iterator. */
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename OutputIterator, typename _DifferenceTp, typename Comparator>
|
||||
inline OutputIterator
|
||||
merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator2& begin2, RandomAccessIterator2 end2, OutputIterator target, _DifferenceTp max_length, Comparator comp)
|
||||
{
|
||||
_GLIBCXX_CALL(max_length)
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename OutputIterator, typename _DifferenceTp,
|
||||
typename Comparator>
|
||||
inline OutputIterator
|
||||
merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2& begin2, RandomAccessIterator2 end2,
|
||||
OutputIterator target, _DifferenceTp max_length,
|
||||
Comparator comp)
|
||||
{
|
||||
_GLIBCXX_CALL(max_length)
|
||||
|
||||
return merge_advance_movc(begin1, end1, begin2, end2, target, max_length, comp);
|
||||
}
|
||||
return merge_advance_movc(begin1, end1, begin2, end2, target,
|
||||
max_length, comp);
|
||||
}
|
||||
|
||||
/** @brief Merge routine fallback to sequential in case the
|
||||
iterators of the two input sequences are of different type.
|
||||
|
@ -179,15 +198,20 @@ namespace __gnu_parallel
|
|||
* @param max_length Maximum number of elements to merge.
|
||||
* @param comp Comparator.
|
||||
* @return Output end iterator. */
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename RandomAccessIterator3, typename Comparator>
|
||||
inline RandomAccessIterator3
|
||||
parallel_merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2& begin2, RandomAccessIterator2 end2, //different iterators, parallel implementation not available
|
||||
RandomAccessIterator3 target,
|
||||
typename std::iterator_traits<RandomAccessIterator1>::difference_type max_length, Comparator comp)
|
||||
{
|
||||
return merge_advance(begin1, end1, begin2, end2, target, max_length, comp);
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2,
|
||||
typename RandomAccessIterator3, typename Comparator>
|
||||
inline RandomAccessIterator3
|
||||
parallel_merge_advance(RandomAccessIterator1& begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator2& begin2,
|
||||
// different iterators, parallel implementation
|
||||
// not available
|
||||
RandomAccessIterator2 end2,
|
||||
RandomAccessIterator3 target, typename
|
||||
std::iterator_traits<RandomAccessIterator1>::
|
||||
difference_type max_length, Comparator comp)
|
||||
{ return merge_advance(begin1, end1, begin2, end2, target,
|
||||
max_length, comp); }
|
||||
|
||||
/** @brief Parallel merge routine being able to merge only the @c
|
||||
* max_length smallest elements.
|
||||
|
@ -204,22 +228,33 @@ namespace __gnu_parallel
|
|||
* @param comp Comparator.
|
||||
* @return Output end iterator.
|
||||
*/
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator3, typename Comparator>
|
||||
inline RandomAccessIterator3
|
||||
parallel_merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator1& begin2, RandomAccessIterator1 end2, RandomAccessIterator3 target, typename std::iterator_traits<RandomAccessIterator1>::difference_type max_length, Comparator comp)
|
||||
{
|
||||
typedef typename std::iterator_traits<RandomAccessIterator1>::value_type
|
||||
value_type;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator1>::difference_type
|
||||
difference_type1 /* == difference_type2 */;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator3>::difference_type
|
||||
difference_type3;
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator3,
|
||||
typename Comparator>
|
||||
inline RandomAccessIterator3
|
||||
parallel_merge_advance(RandomAccessIterator1& begin1,
|
||||
RandomAccessIterator1 end1,
|
||||
RandomAccessIterator1& begin2,
|
||||
RandomAccessIterator1 end2,
|
||||
RandomAccessIterator3 target, typename
|
||||
std::iterator_traits<RandomAccessIterator1>::
|
||||
difference_type max_length, Comparator comp)
|
||||
{
|
||||
typedef typename std::iterator_traits<RandomAccessIterator1>::value_type
|
||||
value_type;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator1>::
|
||||
difference_type difference_type1 /* == difference_type2 */;
|
||||
typedef typename std::iterator_traits<RandomAccessIterator3>::
|
||||
difference_type difference_type3;
|
||||
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator1> seqs[2] = { std::make_pair(begin1, end1), std::make_pair(begin2, end2) };
|
||||
RandomAccessIterator3 target_end = parallel_multiway_merge(seqs, seqs + 2, target, comp, max_length, true, false);
|
||||
std::pair<RandomAccessIterator1, RandomAccessIterator1>
|
||||
seqs[2] = { std::make_pair(begin1, end1),
|
||||
std::make_pair(begin2, end2) };
|
||||
RandomAccessIterator3
|
||||
target_end = parallel_multiway_merge(seqs, seqs + 2, target,
|
||||
comp, max_length, true, false);
|
||||
|
||||
return target_end;
|
||||
}
|
||||
return target_end;
|
||||
}
|
||||
} //namespace __gnu_parallel
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
// Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
||||
//
|
||||
// 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
|
||||
|
@ -58,52 +58,55 @@ namespace __gnu_parallel
|
|||
{
|
||||
/** @brief Compare a pair of types lexicographically, ascending. */
|
||||
template<typename T1, typename T2, typename Comparator>
|
||||
class lexicographic : public std::binary_function<std::pair<T1, T2>, std::pair<T1, T2>, bool>
|
||||
{
|
||||
private:
|
||||
Comparator& comp;
|
||||
|
||||
public:
|
||||
lexicographic(Comparator& _comp) : comp(_comp) { }
|
||||
|
||||
// XXX const
|
||||
inline bool
|
||||
operator()(const std::pair<T1, T2>& p1, const std::pair<T1, T2>& p2) const
|
||||
class lexicographic
|
||||
: public std::binary_function<std::pair<T1, T2>, std::pair<T1, T2>, bool>
|
||||
{
|
||||
if (comp(p1.first, p2.first))
|
||||
return true;
|
||||
private:
|
||||
Comparator& comp;
|
||||
|
||||
if (comp(p2.first, p1.first))
|
||||
return false;
|
||||
public:
|
||||
lexicographic(Comparator& _comp) : comp(_comp) { }
|
||||
|
||||
// Firsts are equal.
|
||||
return p1.second < p2.second;
|
||||
}
|
||||
};
|
||||
// XXX const
|
||||
bool
|
||||
operator()(const std::pair<T1, T2>& p1,
|
||||
const std::pair<T1, T2>& p2) const
|
||||
{
|
||||
if (comp(p1.first, p2.first))
|
||||
return true;
|
||||
|
||||
if (comp(p2.first, p1.first))
|
||||
return false;
|
||||
|
||||
// Firsts are equal.
|
||||
return p1.second < p2.second;
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief Compare a pair of types lexicographically, descending. */
|
||||
template<typename T1, typename T2, typename Comparator>
|
||||
class lexicographic_reverse : public std::binary_function<T1, T2, bool>
|
||||
{
|
||||
private:
|
||||
Comparator& comp;
|
||||
|
||||
public:
|
||||
lexicographic_reverse(Comparator& _comp) : comp(_comp) { }
|
||||
|
||||
inline bool
|
||||
operator()(const std::pair<T1, T2>& p1, const std::pair<T1, T2>& p2) const
|
||||
class lexicographic_reverse : public std::binary_function<T1, T2, bool>
|
||||
{
|
||||
if (comp(p2.first, p1.first))
|
||||
return true;
|
||||
private:
|
||||
Comparator& comp;
|
||||
|
||||
if (comp(p1.first, p2.first))
|
||||
return false;
|
||||
public:
|
||||
lexicographic_reverse(Comparator& _comp) : comp(_comp) { }
|
||||
|
||||
// Firsts are equal.
|
||||
return p2.second < p1.second;
|
||||
}
|
||||
};
|
||||
bool
|
||||
operator()(const std::pair<T1, T2>& p1,
|
||||
const std::pair<T1, T2>& p2) const
|
||||
{
|
||||
if (comp(p2.first, p1.first))
|
||||
return true;
|
||||
|
||||
if (comp(p1.first, p2.first))
|
||||
return false;
|
||||
|
||||
// Firsts are equal.
|
||||
return p2.second < p1.second;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Splits several sorted sequences at a certain global rank,
|
||||
|
@ -121,229 +124,243 @@ namespace __gnu_parallel
|
|||
* the respective sequence.
|
||||
* @param comp The ordering functor, defaults to std::less<T>.
|
||||
*/
|
||||
template<typename RanSeqs, typename RankType, typename RankIterator, typename Comparator>
|
||||
void
|
||||
multiseq_partition(RanSeqs begin_seqs, RanSeqs end_seqs, RankType rank,
|
||||
RankIterator begin_offsets,
|
||||
Comparator comp = std::less<
|
||||
typename std::iterator_traits<typename std::iterator_traits<RanSeqs>::value_type::first_type>::value_type>()) // std::less<T>
|
||||
{
|
||||
_GLIBCXX_CALL(end_seqs - begin_seqs)
|
||||
template<typename RanSeqs, typename RankType, typename RankIterator,
|
||||
typename Comparator>
|
||||
void
|
||||
multiseq_partition(RanSeqs begin_seqs, RanSeqs end_seqs,
|
||||
RankType rank,
|
||||
RankIterator begin_offsets,
|
||||
Comparator comp = std::less<
|
||||
typename std::iterator_traits<typename
|
||||
std::iterator_traits<RanSeqs>::value_type::
|
||||
first_type>::value_type>()) // std::less<T>
|
||||
{
|
||||
_GLIBCXX_CALL(end_seqs - begin_seqs)
|
||||
|
||||
typedef typename std::iterator_traits<RanSeqs>::value_type::first_type It;
|
||||
typedef typename std::iterator_traits<It>::difference_type difference_type;
|
||||
typedef typename std::iterator_traits<It>::value_type value_type;
|
||||
typedef typename std::iterator_traits<RanSeqs>::value_type::first_type
|
||||
It;
|
||||
typedef typename std::iterator_traits<It>::difference_type
|
||||
difference_type;
|
||||
typedef typename std::iterator_traits<It>::value_type value_type;
|
||||
|
||||
lexicographic<value_type, int, Comparator> lcomp(comp);
|
||||
lexicographic_reverse<value_type, int, Comparator> lrcomp(comp);
|
||||
lexicographic<value_type, int, Comparator> lcomp(comp);
|
||||
lexicographic_reverse<value_type, int, Comparator> lrcomp(comp);
|
||||
|
||||
// Number of sequences, number of elements in total (possibly
|
||||
// including padding).
|
||||
difference_type m = std::distance(begin_seqs, end_seqs), N = 0, nmax, n, r;
|
||||
// Number of sequences, number of elements in total (possibly
|
||||
// including padding).
|
||||
difference_type m = std::distance(begin_seqs, end_seqs), N = 0,
|
||||
nmax, n, r;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
N += std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
for (int i = 0; i < m; i++)
|
||||
N += std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
|
||||
if (rank == N)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
begin_offsets[i] = begin_seqs[i].second; // Very end.
|
||||
// Return m - 1;
|
||||
}
|
||||
if (rank == N)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
begin_offsets[i] = begin_seqs[i].second; // Very end.
|
||||
// Return m - 1;
|
||||
}
|
||||
|
||||
_GLIBCXX_PARALLEL_ASSERT(m != 0 && N != 0 && rank >= 0 && rank < N);
|
||||
_GLIBCXX_PARALLEL_ASSERT(m != 0 && N != 0 && rank >= 0 && rank < N);
|
||||
|
||||
difference_type* ns = new difference_type[m];
|
||||
difference_type* a = new difference_type[m];
|
||||
difference_type* b = new difference_type[m];
|
||||
difference_type l;
|
||||
difference_type* ns = new difference_type[m];
|
||||
difference_type* a = new difference_type[m];
|
||||
difference_type* b = new difference_type[m];
|
||||
difference_type l;
|
||||
|
||||
ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second);
|
||||
nmax = ns[0];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
nmax = std::max(nmax, ns[i]);
|
||||
}
|
||||
ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second);
|
||||
nmax = ns[0];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
nmax = std::max(nmax, ns[i]);
|
||||
}
|
||||
|
||||
r = log2(nmax) + 1;
|
||||
r = log2(nmax) + 1;
|
||||
|
||||
// Pad all lists to this length, at least as long as any ns[i],
|
||||
// equality iff nmax = 2^k - 1.
|
||||
l = (1ULL << r) - 1;
|
||||
// Pad all lists to this length, at least as long as any ns[i],
|
||||
// equality iff nmax = 2^k - 1.
|
||||
l = (1ULL << r) - 1;
|
||||
|
||||
// From now on, including padding.
|
||||
N = l * m;
|
||||
// From now on, including padding.
|
||||
N = l * m;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
a[i] = 0;
|
||||
b[i] = l;
|
||||
}
|
||||
n = l / 2;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
a[i] = 0;
|
||||
b[i] = l;
|
||||
}
|
||||
n = l / 2;
|
||||
|
||||
// Invariants:
|
||||
// 0 <= a[i] <= ns[i], 0 <= b[i] <= l
|
||||
// Invariants:
|
||||
// 0 <= a[i] <= ns[i], 0 <= b[i] <= l
|
||||
|
||||
#define S(i) (begin_seqs[i].first)
|
||||
|
||||
// Initial partition.
|
||||
std::vector<std::pair<value_type, int> > sample;
|
||||
// Initial partition.
|
||||
std::vector<std::pair<value_type, int> > sample;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
if (n < ns[i]) //sequence long enough
|
||||
sample.push_back(std::make_pair(S(i)[n], i));
|
||||
__gnu_sequential::sort(sample.begin(), sample.end(), lcomp);
|
||||
for (int i = 0; i < m; i++)
|
||||
if (n < ns[i]) //sequence long enough
|
||||
sample.push_back(std::make_pair(S(i)[n], i));
|
||||
__gnu_sequential::sort(sample.begin(), sample.end(), lcomp);
|
||||
|
||||
for (int i = 0; i < m; i++) //conceptual infinity
|
||||
if (n >= ns[i]) //sequence too short, conceptual infinity
|
||||
sample.push_back(std::make_pair(S(i)[0] /*dummy element*/, i));
|
||||
for (int i = 0; i < m; i++) //conceptual infinity
|
||||
if (n >= ns[i]) //sequence too short, conceptual infinity
|
||||
sample.push_back(std::make_pair(S(i)[0] /*dummy element*/, i));
|
||||
|
||||
difference_type localrank = rank * m / N ;
|
||||
difference_type localrank = rank * m / N ;
|
||||
|
||||
int j;
|
||||
for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++)
|
||||
a[sample[j].second] += n + 1;
|
||||
for (; j < m; j++)
|
||||
b[sample[j].second] -= n + 1;
|
||||
int j;
|
||||
for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++)
|
||||
a[sample[j].second] += n + 1;
|
||||
for (; j < m; j++)
|
||||
b[sample[j].second] -= n + 1;
|
||||
|
||||
// Further refinement.
|
||||
while (n > 0)
|
||||
{
|
||||
n /= 2;
|
||||
|
||||
// Further refinement.
|
||||
while (n > 0)
|
||||
{
|
||||
n /= 2;
|
||||
|
||||
int lmax_seq = -1; // to avoid warning
|
||||
const value_type* lmax = NULL; // impossible to avoid the warning?
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
{
|
||||
if (!lmax)
|
||||
{
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
lmax_seq = i;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Max, favor rear sequences.
|
||||
if (!comp(S(i)[a[i] - 1], *lmax))
|
||||
{
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
lmax_seq = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
difference_type middle = (b[i] + a[i]) / 2;
|
||||
if (lmax && middle < ns[i] &&
|
||||
lcomp(std::make_pair(S(i)[middle], i), std::make_pair(*lmax, lmax_seq)))
|
||||
a[i] = std::min(a[i] + n + 1, ns[i]);
|
||||
else
|
||||
b[i] -= n + 1;
|
||||
}
|
||||
|
||||
difference_type leftsize = 0, total = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
leftsize += a[i] / (n + 1);
|
||||
total += l / (n + 1);
|
||||
}
|
||||
|
||||
difference_type skew = static_cast<difference_type>(static_cast<uint64>(total) * rank / N - leftsize);
|
||||
|
||||
if (skew > 0)
|
||||
{
|
||||
// Move to the left, find smallest.
|
||||
std::priority_queue<std::pair<value_type, int>, std::vector<std::pair<value_type, int> >, lexicographic_reverse<value_type, int, Comparator> > pq(lrcomp);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
if (b[i] < ns[i])
|
||||
pq.push(std::make_pair(S(i)[b[i]], i));
|
||||
|
||||
for (; skew != 0 && !pq.empty(); skew--)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
|
||||
a[source] = std::min(a[source] + n + 1, ns[source]);
|
||||
b[source] += n + 1;
|
||||
|
||||
if (b[source] < ns[source])
|
||||
pq.push(std::make_pair(S(source)[b[source]], source));
|
||||
}
|
||||
}
|
||||
else if (skew < 0)
|
||||
{
|
||||
// Move to the right, find greatest.
|
||||
std::priority_queue<std::pair<value_type, int>, std::vector<std::pair<value_type, int> >, lexicographic<value_type, int, Comparator> > pq(lcomp);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
int lmax_seq = -1; // to avoid warning
|
||||
const value_type* lmax = NULL; // impossible to avoid the warning?
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
pq.push(std::make_pair(S(i)[a[i] - 1], i));
|
||||
{
|
||||
if (!lmax)
|
||||
{
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
lmax_seq = i;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Max, favor rear sequences.
|
||||
if (!comp(S(i)[a[i] - 1], *lmax))
|
||||
{
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
lmax_seq = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (; skew != 0; skew++)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
int i;
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
difference_type middle = (b[i] + a[i]) / 2;
|
||||
if (lmax && middle < ns[i] &&
|
||||
lcomp(std::make_pair(S(i)[middle], i),
|
||||
std::make_pair(*lmax, lmax_seq)))
|
||||
a[i] = std::min(a[i] + n + 1, ns[i]);
|
||||
else
|
||||
b[i] -= n + 1;
|
||||
}
|
||||
|
||||
a[source] -= n + 1;
|
||||
b[source] -= n + 1;
|
||||
difference_type leftsize = 0, total = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
leftsize += a[i] / (n + 1);
|
||||
total += l / (n + 1);
|
||||
}
|
||||
|
||||
difference_type skew = static_cast<difference_type>
|
||||
(static_cast<uint64>(total) * rank / N - leftsize);
|
||||
|
||||
if (a[source] > 0)
|
||||
pq.push(std::make_pair(S(source)[a[source] - 1], source));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (skew > 0)
|
||||
{
|
||||
// Move to the left, find smallest.
|
||||
std::priority_queue<std::pair<value_type, int>,
|
||||
std::vector<std::pair<value_type, int> >,
|
||||
lexicographic_reverse<value_type, int, Comparator> >
|
||||
pq(lrcomp);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
if (b[i] < ns[i])
|
||||
pq.push(std::make_pair(S(i)[b[i]], i));
|
||||
|
||||
// Postconditions:
|
||||
// a[i] == b[i] in most cases, except when a[i] has been clamped
|
||||
// because of having reached the boundary
|
||||
for (; skew != 0 && !pq.empty(); skew--)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
|
||||
// Now return the result, calculate the offset.
|
||||
a[source] = std::min(a[source] + n + 1, ns[source]);
|
||||
b[source] += n + 1;
|
||||
|
||||
// Compare the keys on both edges of the border.
|
||||
if (b[source] < ns[source])
|
||||
pq.push(std::make_pair(S(source)[b[source]], source));
|
||||
}
|
||||
}
|
||||
else if (skew < 0)
|
||||
{
|
||||
// Move to the right, find greatest.
|
||||
std::priority_queue<std::pair<value_type, int>,
|
||||
std::vector<std::pair<value_type, int> >,
|
||||
lexicographic<value_type, int, Comparator> > pq(lcomp);
|
||||
|
||||
// Maximum of left edge, minimum of right edge.
|
||||
value_type* maxleft = NULL;
|
||||
value_type* minright = NULL;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
{
|
||||
if (!maxleft)
|
||||
maxleft = &(S(i)[a[i] - 1]);
|
||||
else
|
||||
{
|
||||
// Max, favor rear sequences.
|
||||
if (!comp(S(i)[a[i] - 1], *maxleft))
|
||||
maxleft = &(S(i)[a[i] - 1]);
|
||||
}
|
||||
}
|
||||
if (b[i] < ns[i])
|
||||
{
|
||||
if (!minright)
|
||||
minright = &(S(i)[b[i]]);
|
||||
else
|
||||
{
|
||||
// Min, favor fore sequences.
|
||||
if (comp(S(i)[b[i]], *minright))
|
||||
minright = &(S(i)[b[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m; i++)
|
||||
if (a[i] > 0)
|
||||
pq.push(std::make_pair(S(i)[a[i] - 1], i));
|
||||
|
||||
int seq = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
begin_offsets[i] = S(i) + a[i];
|
||||
for (; skew != 0; skew++)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
|
||||
delete[] ns;
|
||||
delete[] a;
|
||||
delete[] b;
|
||||
}
|
||||
a[source] -= n + 1;
|
||||
b[source] -= n + 1;
|
||||
|
||||
if (a[source] > 0)
|
||||
pq.push(std::make_pair(S(source)[a[source] - 1], source));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Postconditions:
|
||||
// a[i] == b[i] in most cases, except when a[i] has been clamped
|
||||
// because of having reached the boundary
|
||||
|
||||
// Now return the result, calculate the offset.
|
||||
|
||||
// Compare the keys on both edges of the border.
|
||||
|
||||
// Maximum of left edge, minimum of right edge.
|
||||
value_type* maxleft = NULL;
|
||||
value_type* minright = NULL;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
{
|
||||
if (!maxleft)
|
||||
maxleft = &(S(i)[a[i] - 1]);
|
||||
else
|
||||
{
|
||||
// Max, favor rear sequences.
|
||||
if (!comp(S(i)[a[i] - 1], *maxleft))
|
||||
maxleft = &(S(i)[a[i] - 1]);
|
||||
}
|
||||
}
|
||||
if (b[i] < ns[i])
|
||||
{
|
||||
if (!minright)
|
||||
minright = &(S(i)[b[i]]);
|
||||
else
|
||||
{
|
||||
// Min, favor fore sequences.
|
||||
if (comp(S(i)[b[i]], *minright))
|
||||
minright = &(S(i)[b[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int seq = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
begin_offsets[i] = S(i) + a[i];
|
||||
|
||||
delete[] ns;
|
||||
delete[] a;
|
||||
delete[] b;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -360,246 +377,254 @@ namespace __gnu_parallel
|
|||
* selected element is unique, this number is 0.
|
||||
* @param comp The ordering functor, defaults to std::less.
|
||||
*/
|
||||
template<typename T, typename RanSeqs, typename RankType, typename Comparator>
|
||||
T
|
||||
multiseq_selection(RanSeqs begin_seqs, RanSeqs end_seqs, RankType rank,
|
||||
RankType& offset, Comparator comp = std::less<T>())
|
||||
{
|
||||
_GLIBCXX_CALL(end_seqs - begin_seqs)
|
||||
template<typename T, typename RanSeqs, typename RankType,
|
||||
typename Comparator>
|
||||
T
|
||||
multiseq_selection(RanSeqs begin_seqs, RanSeqs end_seqs, RankType rank,
|
||||
RankType& offset, Comparator comp = std::less<T>())
|
||||
{
|
||||
_GLIBCXX_CALL(end_seqs - begin_seqs)
|
||||
|
||||
typedef typename std::iterator_traits<RanSeqs>::value_type::first_type It;
|
||||
typedef typename std::iterator_traits<It>::difference_type difference_type;
|
||||
typedef typename std::iterator_traits<RanSeqs>::value_type::first_type
|
||||
It;
|
||||
typedef typename std::iterator_traits<It>::difference_type
|
||||
difference_type;
|
||||
|
||||
lexicographic<T, int, Comparator> lcomp(comp);
|
||||
lexicographic_reverse<T, int, Comparator> lrcomp(comp);
|
||||
lexicographic<T, int, Comparator> lcomp(comp);
|
||||
lexicographic_reverse<T, int, Comparator> lrcomp(comp);
|
||||
|
||||
// Number of sequences, number of elements in total (possibly
|
||||
// including padding).
|
||||
difference_type m = std::distance(begin_seqs, end_seqs);
|
||||
difference_type N = 0;
|
||||
difference_type nmax, n, r;
|
||||
// Number of sequences, number of elements in total (possibly
|
||||
// including padding).
|
||||
difference_type m = std::distance(begin_seqs, end_seqs);
|
||||
difference_type N = 0;
|
||||
difference_type nmax, n, r;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
N += std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
for (int i = 0; i < m; i++)
|
||||
N += std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
|
||||
if (m == 0 || N == 0 || rank < 0 || rank >= N)
|
||||
{
|
||||
// Result undefined when there is no data or rank is outside bounds.
|
||||
throw std::exception();
|
||||
}
|
||||
if (m == 0 || N == 0 || rank < 0 || rank >= N)
|
||||
{
|
||||
// Result undefined when there is no data or rank is outside bounds.
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
|
||||
difference_type* ns = new difference_type[m];
|
||||
difference_type* a = new difference_type[m];
|
||||
difference_type* b = new difference_type[m];
|
||||
difference_type l;
|
||||
difference_type* ns = new difference_type[m];
|
||||
difference_type* a = new difference_type[m];
|
||||
difference_type* b = new difference_type[m];
|
||||
difference_type l;
|
||||
|
||||
ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second);
|
||||
nmax = ns[0];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
nmax = std::max(nmax, ns[i]);
|
||||
}
|
||||
ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second);
|
||||
nmax = ns[0];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second);
|
||||
nmax = std::max(nmax, ns[i]);
|
||||
}
|
||||
|
||||
r = log2(nmax) + 1;
|
||||
r = log2(nmax) + 1;
|
||||
|
||||
// Pad all lists to this length, at least as long as any ns[i],
|
||||
// equality iff nmax = 2^k - 1
|
||||
l = pow2(r) - 1;
|
||||
// Pad all lists to this length, at least as long as any ns[i],
|
||||
// equality iff nmax = 2^k - 1
|
||||
l = pow2(r) - 1;
|
||||
|
||||
// From now on, including padding.
|
||||
N = l * m;
|
||||
// From now on, including padding.
|
||||
N = l * m;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
a[i] = 0;
|
||||
b[i] = l;
|
||||
}
|
||||
n = l / 2;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
a[i] = 0;
|
||||
b[i] = l;
|
||||
}
|
||||
n = l / 2;
|
||||
|
||||
// Invariants:
|
||||
// 0 <= a[i] <= ns[i], 0 <= b[i] <= l
|
||||
// Invariants:
|
||||
// 0 <= a[i] <= ns[i], 0 <= b[i] <= l
|
||||
|
||||
#define S(i) (begin_seqs[i].first)
|
||||
|
||||
// Initial partition.
|
||||
std::vector<std::pair<T, int> > sample;
|
||||
// Initial partition.
|
||||
std::vector<std::pair<T, int> > sample;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
if (n < ns[i])
|
||||
sample.push_back(std::make_pair(S(i)[n], i));
|
||||
__gnu_sequential::sort(sample.begin(), sample.end(), lcomp, sequential_tag());
|
||||
for (int i = 0; i < m; i++)
|
||||
if (n < ns[i])
|
||||
sample.push_back(std::make_pair(S(i)[n], i));
|
||||
__gnu_sequential::sort(sample.begin(), sample.end(),
|
||||
lcomp, sequential_tag());
|
||||
|
||||
// Conceptual infinity.
|
||||
for (int i = 0; i < m; i++)
|
||||
if (n >= ns[i])
|
||||
sample.push_back(std::make_pair(S(i)[0] /*dummy element*/, i));
|
||||
// Conceptual infinity.
|
||||
for (int i = 0; i < m; i++)
|
||||
if (n >= ns[i])
|
||||
sample.push_back(std::make_pair(S(i)[0] /*dummy element*/, i));
|
||||
|
||||
difference_type localrank = rank * m / N ;
|
||||
difference_type localrank = rank * m / N ;
|
||||
|
||||
int j;
|
||||
for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++)
|
||||
a[sample[j].second] += n + 1;
|
||||
for (; j < m; j++)
|
||||
b[sample[j].second] -= n + 1;
|
||||
int j;
|
||||
for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++)
|
||||
a[sample[j].second] += n + 1;
|
||||
for (; j < m; j++)
|
||||
b[sample[j].second] -= n + 1;
|
||||
|
||||
// Further refinement.
|
||||
while (n > 0)
|
||||
{
|
||||
n /= 2;
|
||||
// Further refinement.
|
||||
while (n > 0)
|
||||
{
|
||||
n /= 2;
|
||||
|
||||
const T* lmax = NULL;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
{
|
||||
if (!lmax)
|
||||
{
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (comp(*lmax, S(i)[a[i] - 1])) //max
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
difference_type middle = (b[i] + a[i]) / 2;
|
||||
if (lmax && middle < ns[i] && comp(S(i)[middle], *lmax))
|
||||
a[i] = std::min(a[i] + n + 1, ns[i]);
|
||||
else
|
||||
b[i] -= n + 1;
|
||||
}
|
||||
|
||||
difference_type leftsize = 0, total = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
leftsize += a[i] / (n + 1);
|
||||
total += l / (n + 1);
|
||||
}
|
||||
|
||||
difference_type skew = (unsigned long long)total * rank / N - leftsize;
|
||||
|
||||
if (skew > 0)
|
||||
{
|
||||
// Move to the left, find smallest.
|
||||
std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int> >, lexicographic_reverse<T, int, Comparator> > pq(lrcomp);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
if (b[i] < ns[i])
|
||||
pq.push(std::make_pair(S(i)[b[i]], i));
|
||||
|
||||
for (; skew != 0 && !pq.empty(); skew--)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
|
||||
a[source] = std::min(a[source] + n + 1, ns[source]);
|
||||
b[source] += n + 1;
|
||||
|
||||
if (b[source] < ns[source])
|
||||
pq.push(std::make_pair(S(source)[b[source]], source));
|
||||
}
|
||||
}
|
||||
else if (skew < 0)
|
||||
{
|
||||
// Move to the right, find greatest.
|
||||
std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int> >, lexicographic<T, int, Comparator> > pq(lcomp);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
const T* lmax = NULL;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
pq.push(std::make_pair(S(i)[a[i] - 1], i));
|
||||
{
|
||||
if (!lmax)
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
else
|
||||
{
|
||||
if (comp(*lmax, S(i)[a[i] - 1])) //max
|
||||
lmax = &(S(i)[a[i] - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (; skew != 0; skew++)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
int i;
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
difference_type middle = (b[i] + a[i]) / 2;
|
||||
if (lmax && middle < ns[i] && comp(S(i)[middle], *lmax))
|
||||
a[i] = std::min(a[i] + n + 1, ns[i]);
|
||||
else
|
||||
b[i] -= n + 1;
|
||||
}
|
||||
|
||||
a[source] -= n + 1;
|
||||
b[source] -= n + 1;
|
||||
difference_type leftsize = 0, total = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
leftsize += a[i] / (n + 1);
|
||||
total += l / (n + 1);
|
||||
}
|
||||
|
||||
if (a[source] > 0)
|
||||
pq.push(std::make_pair(S(source)[a[source] - 1], source));
|
||||
}
|
||||
}
|
||||
}
|
||||
difference_type skew = ((unsigned long long)total * rank / N
|
||||
- leftsize);
|
||||
|
||||
// Postconditions:
|
||||
// a[i] == b[i] in most cases, except when a[i] has been clamped
|
||||
// because of having reached the boundary
|
||||
if (skew > 0)
|
||||
{
|
||||
// Move to the left, find smallest.
|
||||
std::priority_queue<std::pair<T, int>,
|
||||
std::vector<std::pair<T, int> >,
|
||||
lexicographic_reverse<T, int, Comparator> > pq(lrcomp);
|
||||
|
||||
// Now return the result, calculate the offset.
|
||||
for (int i = 0; i < m; i++)
|
||||
if (b[i] < ns[i])
|
||||
pq.push(std::make_pair(S(i)[b[i]], i));
|
||||
|
||||
// Compare the keys on both edges of the border.
|
||||
for (; skew != 0 && !pq.empty(); --skew)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
|
||||
a[source] = std::min(a[source] + n + 1, ns[source]);
|
||||
b[source] += n + 1;
|
||||
|
||||
if (b[source] < ns[source])
|
||||
pq.push(std::make_pair(S(source)[b[source]], source));
|
||||
}
|
||||
}
|
||||
else if (skew < 0)
|
||||
{
|
||||
// Move to the right, find greatest.
|
||||
std::priority_queue<std::pair<T, int>,
|
||||
std::vector<std::pair<T, int> >,
|
||||
lexicographic<T, int, Comparator> > pq(lcomp);
|
||||
|
||||
// Maximum of left edge, minimum of right edge.
|
||||
bool maxleftset = false, minrightset = false;
|
||||
for (int i = 0; i < m; i++)
|
||||
if (a[i] > 0)
|
||||
pq.push(std::make_pair(S(i)[a[i] - 1], i));
|
||||
|
||||
// Impossible to avoid the warning?
|
||||
T maxleft, minright;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
{
|
||||
if (!maxleftset)
|
||||
{
|
||||
maxleft = S(i)[a[i] - 1];
|
||||
maxleftset = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Max.
|
||||
if (comp(maxleft, S(i)[a[i] - 1]))
|
||||
for (; skew != 0; ++skew)
|
||||
{
|
||||
int source = pq.top().second;
|
||||
pq.pop();
|
||||
|
||||
a[source] -= n + 1;
|
||||
b[source] -= n + 1;
|
||||
|
||||
if (a[source] > 0)
|
||||
pq.push(std::make_pair(S(source)[a[source] - 1], source));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Postconditions:
|
||||
// a[i] == b[i] in most cases, except when a[i] has been clamped
|
||||
// because of having reached the boundary
|
||||
|
||||
// Now return the result, calculate the offset.
|
||||
|
||||
// Compare the keys on both edges of the border.
|
||||
|
||||
// Maximum of left edge, minimum of right edge.
|
||||
bool maxleftset = false, minrightset = false;
|
||||
|
||||
// Impossible to avoid the warning?
|
||||
T maxleft, minright;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (a[i] > 0)
|
||||
{
|
||||
if (!maxleftset)
|
||||
{
|
||||
maxleft = S(i)[a[i] - 1];
|
||||
}
|
||||
}
|
||||
if (b[i] < ns[i])
|
||||
{
|
||||
if (!minrightset)
|
||||
{
|
||||
minright = S(i)[b[i]];
|
||||
minrightset = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Min.
|
||||
if (comp(S(i)[b[i]], minright))
|
||||
maxleftset = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Max.
|
||||
if (comp(maxleft, S(i)[a[i] - 1]))
|
||||
maxleft = S(i)[a[i] - 1];
|
||||
}
|
||||
}
|
||||
if (b[i] < ns[i])
|
||||
{
|
||||
if (!minrightset)
|
||||
{
|
||||
minright = S(i)[b[i]];
|
||||
}
|
||||
}
|
||||
minrightset = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Min.
|
||||
if (comp(S(i)[b[i]], minright))
|
||||
minright = S(i)[b[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Minright is the splitter, in any case.
|
||||
// Minright is the splitter, in any case.
|
||||
|
||||
if (!maxleftset || comp(minright, maxleft))
|
||||
{
|
||||
// Good luck, everything is split unambigiously.
|
||||
offset = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We have to calculate an offset.
|
||||
offset = 0;
|
||||
if (!maxleftset || comp(minright, maxleft))
|
||||
{
|
||||
// Good luck, everything is split unambigiously.
|
||||
offset = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We have to calculate an offset.
|
||||
offset = 0;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
difference_type lb = std::lower_bound(S(i), S(i) + ns[i], minright,
|
||||
comp) - S(i);
|
||||
offset += a[i] - lb;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
difference_type lb = std::lower_bound(S(i), S(i) + ns[i],
|
||||
minright,
|
||||
comp) - S(i);
|
||||
offset += a[i] - lb;
|
||||
}
|
||||
}
|
||||
|
||||
delete[] ns;
|
||||
delete[] a;
|
||||
delete[] b;
|
||||
delete[] ns;
|
||||
delete[] a;
|
||||
delete[] b;
|
||||
|
||||
return minright;
|
||||
}
|
||||
return minright;
|
||||
}
|
||||
}
|
||||
|
||||
#undef S
|
||||
|
|
Loading…
Reference in New Issue