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:
Paolo Carlini 2008-01-09 18:27:27 +00:00 committed by Paolo Carlini
parent c3e203cf80
commit 531898c3c2
15 changed files with 3558 additions and 2971 deletions

View file

@ -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> 2008-01-09 Francesco Biscani <bluescarni@gmail.com>
* include/tr1_impl/hashtable (_Hashtable): Fix friend declaration * include/tr1_impl/hashtable (_Hashtable): Fix friend declaration

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // software; you can redistribute it and/or modify it under the terms
@ -57,215 +57,230 @@ namespace __parallel
// Sequential fallback // Sequential fallback
template<typename InputIterator1, typename InputIterator2> template<typename InputIterator1, typename InputIterator2>
inline pair<InputIterator1, InputIterator2> inline pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
__gnu_parallel::sequential_tag) __gnu_parallel::sequential_tag)
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2); } { return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2); }
// Sequential fallback // Sequential fallback
template<typename InputIterator1, typename InputIterator2, typename Predicate> template<typename InputIterator1, typename InputIterator2,
inline pair<InputIterator1, InputIterator2> typename Predicate>
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, inline pair<InputIterator1, InputIterator2>
Predicate pred, __gnu_parallel::sequential_tag) mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); } Predicate pred, __gnu_parallel::sequential_tag)
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
// Sequential fallback for input iterator case // Sequential fallback for input iterator case
template<typename InputIterator1, typename InputIterator2, typename Predicate, typename IteratorTag1, typename IteratorTag2> template<typename InputIterator1, typename InputIterator2,
inline pair<InputIterator1, InputIterator2> typename Predicate, typename IteratorTag1, typename IteratorTag2>
mismatch_switch(InputIterator1 begin1, InputIterator1 end1, inline pair<InputIterator1, InputIterator2>
InputIterator2 begin2, Predicate pred, IteratorTag1, mismatch_switch(InputIterator1 begin1, InputIterator1 end1,
IteratorTag2) InputIterator2 begin2, Predicate pred, IteratorTag1,
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); } IteratorTag2)
{ return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); }
// Parallel mismatch for random access iterators // Parallel mismatch for random access iterators
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Predicate> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
pair<RandomAccessIterator1, RandomAccessIterator2> typename Predicate>
mismatch_switch(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, pair<RandomAccessIterator1, RandomAccessIterator2>
RandomAccessIterator2 begin2, Predicate pred, mismatch_switch(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
random_access_iterator_tag, random_access_iterator_tag) RandomAccessIterator2 begin2, Predicate pred,
{ random_access_iterator_tag, random_access_iterator_tag)
if (_GLIBCXX_PARALLEL_CONDITION(true)) {
{ 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)); RandomAccessIterator1 res =
} __gnu_parallel::find_template(begin1, end1, begin2, pred,
else __gnu_parallel::
return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred); mismatch_selector()).first;
} return make_pair(res , begin2 + (res - begin1));
}
else
return _GLIBCXX_STD_P::mismatch(begin1, end1, begin2, pred);
}
// Public interface // Public interface
template<typename InputIterator1, typename InputIterator2> template<typename InputIterator1, typename InputIterator2>
inline pair<InputIterator1, InputIterator2> inline pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2) mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
{ {
typedef std::iterator_traits<InputIterator1> iterator1_traits; typedef std::iterator_traits<InputIterator1> iterator1_traits;
typedef std::iterator_traits<InputIterator2> iterator2_traits; typedef std::iterator_traits<InputIterator2> iterator2_traits;
typedef typename iterator1_traits::value_type value1_type; typedef typename iterator1_traits::value_type value1_type;
typedef typename iterator2_traits::value_type value2_type; typedef typename iterator2_traits::value_type value2_type;
typedef typename iterator1_traits::iterator_category iterator1_category; typedef typename iterator1_traits::iterator_category iterator1_category;
typedef typename iterator2_traits::iterator_category iterator2_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(), return mismatch_switch(begin1, end1, begin2, equal_to_type(),
iterator1_category(), iterator2_category()); iterator1_category(), iterator2_category());
} }
// Public interface // Public interface
template<typename InputIterator1, typename InputIterator2, typename Predicate> template<typename InputIterator1, typename InputIterator2,
inline pair<InputIterator1, InputIterator2> typename Predicate>
mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, inline pair<InputIterator1, InputIterator2>
Predicate pred) mismatch(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
{ Predicate pred)
typedef std::iterator_traits<InputIterator1> iterator1_traits; {
typedef std::iterator_traits<InputIterator2> iterator2_traits; typedef std::iterator_traits<InputIterator1> iterator1_traits;
typedef typename iterator1_traits::iterator_category iterator1_category; typedef std::iterator_traits<InputIterator2> iterator2_traits;
typedef typename iterator2_traits::iterator_category iterator2_category; 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(), return mismatch_switch(begin1, end1, begin2, pred, iterator1_category(),
iterator2_category()); iterator2_category());
} }
// Sequential fallback // Sequential fallback
template<typename InputIterator1, typename InputIterator2> template<typename InputIterator1, typename InputIterator2>
inline bool inline bool
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
__gnu_parallel::sequential_tag) __gnu_parallel::sequential_tag)
{ return _GLIBCXX_STD_P::equal(begin1, end1, begin2); } { return _GLIBCXX_STD_P::equal(begin1, end1, begin2); }
// Sequential fallback // Sequential fallback
template<typename InputIterator1, typename InputIterator2, typename Predicate> template<typename InputIterator1, typename InputIterator2,
inline bool typename Predicate>
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, inline bool
Predicate pred, __gnu_parallel::sequential_tag) equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
{ return _GLIBCXX_STD_P::equal(begin1, end1, begin2, pred); } Predicate pred, __gnu_parallel::sequential_tag)
{ return _GLIBCXX_STD_P::equal(begin1, end1, begin2, pred); }
// Public interface // Public interface
template<typename InputIterator1, typename InputIterator2> template<typename InputIterator1, typename InputIterator2>
inline bool inline bool
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2) equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2)
{ return mismatch(begin1, end1, begin2).first == end1; } { return mismatch(begin1, end1, begin2).first == end1; }
// Public interface // Public interface
template<typename InputIterator1, typename InputIterator2, typename Predicate> template<typename InputIterator1, typename InputIterator2,
inline bool typename Predicate>
equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, inline bool
Predicate pred) equal(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2,
{ return mismatch(begin1, end1, begin2, pred).first == end1; } Predicate pred)
{ return mismatch(begin1, end1, begin2, pred).first == end1; }
// Sequential fallback // Sequential fallback
template<typename InputIterator1, typename InputIterator2> template<typename InputIterator1, typename InputIterator2>
inline bool inline bool
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1, lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
InputIterator2 begin2, InputIterator2 end2, InputIterator2 begin2, InputIterator2 end2,
__gnu_parallel::sequential_tag) __gnu_parallel::sequential_tag)
{ { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1, begin2, end2); begin2, end2); }
}
// Sequential fallback // Sequential fallback
template<typename InputIterator1, typename InputIterator2, typename Predicate> template<typename InputIterator1, typename InputIterator2,
inline bool typename Predicate>
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1, inline bool
InputIterator2 begin2, InputIterator2 end2, lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
Predicate pred, __gnu_parallel::sequential_tag) InputIterator2 begin2, InputIterator2 end2,
{ Predicate pred, __gnu_parallel::sequential_tag)
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1, { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
begin2, end2, pred); begin2, end2, pred); }
}
// Sequential fallback for input iterator case // Sequential fallback for input iterator case
template<typename InputIterator1, typename InputIterator2, typename Predicate, typename IteratorTag1, typename IteratorTag2> template<typename InputIterator1, typename InputIterator2,
inline bool typename Predicate, typename IteratorTag1, typename IteratorTag2>
lexicographical_compare_switch(InputIterator1 begin1, InputIterator1 end1, inline bool
InputIterator2 begin2, InputIterator2 end2, lexicographical_compare_switch(InputIterator1 begin1, InputIterator1 end1,
Predicate pred, IteratorTag1, IteratorTag2) InputIterator2 begin2, InputIterator2 end2,
{ Predicate pred, IteratorTag1, IteratorTag2)
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1, { return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
begin2, end2, pred); begin2, end2, pred); }
}
// Parallel lexicographical_compare for random access iterators // Parallel lexicographical_compare for random access iterators
// Limitation: Both valuetypes must be the same // Limitation: Both valuetypes must be the same
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Predicate> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
bool typename Predicate>
lexicographical_compare_switch(RandomAccessIterator1 begin1, bool
RandomAccessIterator1 end1, lexicographical_compare_switch(RandomAccessIterator1 begin1,
RandomAccessIterator2 begin2, RandomAccessIterator1 end1,
RandomAccessIterator2 end2, Predicate pred, RandomAccessIterator2 begin2,
random_access_iterator_tag, RandomAccessIterator2 end2, Predicate pred,
random_access_iterator_tag) random_access_iterator_tag,
{ random_access_iterator_tag)
if (_GLIBCXX_PARALLEL_CONDITION(true)) {
{ if (_GLIBCXX_PARALLEL_CONDITION(true))
typedef iterator_traits<RandomAccessIterator1> traits1_type; {
typedef typename traits1_type::value_type value1_type; typedef iterator_traits<RandomAccessIterator1> traits1_type;
typedef typename traits1_type::value_type value1_type;
typedef iterator_traits<RandomAccessIterator2> traits2_type; typedef iterator_traits<RandomAccessIterator2> traits2_type;
typedef typename traits2_type::value_type value2_type; typedef typename traits2_type::value_type value2_type;
typedef __gnu_parallel::equal_from_less<Predicate, value1_type, value2_type> equal_type; typedef __gnu_parallel::equal_from_less<Predicate, value1_type,
value2_type> equal_type;
// Longer sequence in first place. // Longer sequence in first place.
if ((end1 - begin1) < (end2 - begin2)) if ((end1 - begin1) < (end2 - begin2))
{ {
typedef pair<RandomAccessIterator1, RandomAccessIterator2> pair_type; typedef pair<RandomAccessIterator1, RandomAccessIterator2>
pair_type mm = mismatch_switch(begin1, end1, begin2, pair_type;
equal_type(pred), pair_type mm = mismatch_switch(begin1, end1, begin2,
random_access_iterator_tag(), equal_type(pred),
random_access_iterator_tag()); random_access_iterator_tag(),
random_access_iterator_tag());
return (mm.first == end1) || bool(pred(*mm.first, *mm.second)); return (mm.first == end1) || bool(pred(*mm.first, *mm.second));
} }
else else
{ {
typedef pair<RandomAccessIterator2, RandomAccessIterator1> pair_type; typedef pair<RandomAccessIterator2, RandomAccessIterator1>
pair_type mm = mismatch_switch(begin2, end2, begin1, pair_type;
equal_type(pred), pair_type mm = mismatch_switch(begin2, end2, begin1,
random_access_iterator_tag(), equal_type(pred),
random_access_iterator_tag()); random_access_iterator_tag(),
random_access_iterator_tag());
return (mm.first != end2) && bool(pred(*mm.second, *mm.first)); return (mm.first != end2) && bool(pred(*mm.second, *mm.first));
} }
} }
else else
return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1, begin2, end2, pred); return _GLIBCXX_STD_P::lexicographical_compare(begin1, end1,
} begin2, end2, pred);
}
// Public interface // Public interface
template<typename InputIterator1, typename InputIterator2> template<typename InputIterator1, typename InputIterator2>
inline bool inline bool
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, InputIterator2 end2) 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 iterator_traits<InputIterator1> traits1_type;
typedef typename traits1_type::iterator_category iterator1_category; typedef typename traits1_type::value_type value1_type;
typedef typename traits1_type::iterator_category iterator1_category;
typedef iterator_traits<InputIterator2> traits2_type; typedef iterator_traits<InputIterator2> traits2_type;
typedef typename traits2_type::value_type value2_type; typedef typename traits2_type::value_type value2_type;
typedef typename traits2_type::iterator_category iterator2_category; typedef typename traits2_type::iterator_category iterator2_category;
typedef __gnu_parallel::less<value1_type, value2_type> less_type; typedef __gnu_parallel::less<value1_type, value2_type> less_type;
return lexicographical_compare_switch(begin1, end1, begin2, end2, return lexicographical_compare_switch(begin1, end1, begin2, end2,
less_type(), iterator1_category(), less_type(), iterator1_category(),
iterator2_category()); iterator2_category());
} }
// Public interface // Public interface
template<typename InputIterator1, typename InputIterator2, typename Predicate> template<typename InputIterator1, typename InputIterator2,
inline bool typename Predicate>
lexicographical_compare(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, InputIterator2 end2, Predicate pred) inline bool
{ lexicographical_compare(InputIterator1 begin1, InputIterator1 end1,
typedef iterator_traits<InputIterator1> traits1_type; InputIterator2 begin2, InputIterator2 end2,
typedef typename traits1_type::iterator_category iterator1_category; Predicate pred)
{
typedef iterator_traits<InputIterator1> traits1_type;
typedef typename traits1_type::iterator_category iterator1_category;
typedef iterator_traits<InputIterator2> traits2_type; typedef iterator_traits<InputIterator2> traits2_type;
typedef typename traits2_type::iterator_category iterator2_category; typedef typename traits2_type::iterator_category iterator2_category;
return lexicographical_compare_switch(begin1, end1, begin2, end2, pred, return lexicographical_compare_switch(begin1, end1, begin2, end2, pred,
iterator1_category(), iterator1_category(),
iterator2_category()); iterator2_category());
} }
} // end namespace } // end namespace
} // end namespace } // end namespace

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // software; you can redistribute it and/or modify it under the terms
@ -102,7 +102,7 @@ template<typename RandomAccessIterator>
* this part. * this part.
* @pre @c (end-begin)>=1 */ * @pre @c (end-begin)>=1 */
template<typename RandomAccessIterator, typename Comparator> 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, qsb_divide(RandomAccessIterator begin, RandomAccessIterator end,
Comparator comp, thread_index_t num_threads) Comparator comp, thread_index_t num_threads)
{ {
@ -164,7 +164,7 @@ template<typename RandomAccessIterator, typename Comparator>
* @param num_threads * @param num_threads
* Number of threads that are allowed to work on this part. */ * Number of threads that are allowed to work on this part. */
template<typename RandomAccessIterator, typename Comparator> template<typename RandomAccessIterator, typename Comparator>
inline void void
qsb_conquer(QSBThreadLocal<RandomAccessIterator>** tls, qsb_conquer(QSBThreadLocal<RandomAccessIterator>** tls,
RandomAccessIterator begin, RandomAccessIterator end, RandomAccessIterator begin, RandomAccessIterator end,
Comparator comp, Comparator comp,
@ -240,7 +240,7 @@ template<typename RandomAccessIterator, typename Comparator>
* @param iam Number of the thread processing this function. * @param iam Number of the thread processing this function.
*/ */
template<typename RandomAccessIterator, typename Comparator> template<typename RandomAccessIterator, typename Comparator>
inline void void
qsb_local_sort_with_helping(QSBThreadLocal<RandomAccessIterator>** tls, qsb_local_sort_with_helping(QSBThreadLocal<RandomAccessIterator>** tls,
Comparator& comp, int iam, bool wait) Comparator& comp, int iam, bool wait)
{ {
@ -418,7 +418,7 @@ template<typename RandomAccessIterator, typename Comparator>
* this part. * this part.
*/ */
template<typename RandomAccessIterator, typename Comparator> template<typename RandomAccessIterator, typename Comparator>
inline void void
parallel_sort_qsb(RandomAccessIterator begin, RandomAccessIterator end, parallel_sort_qsb(RandomAccessIterator begin, RandomAccessIterator end,
Comparator comp, Comparator comp,
typename std::iterator_traits<RandomAccessIterator> typename std::iterator_traits<RandomAccessIterator>

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // software; you can redistribute it and/or modify it under the terms
@ -53,28 +53,32 @@ namespace __gnu_parallel
*/ */
// XXX Comparator default template argument // XXX Comparator default template argument
template<typename InputIterator, typename Comparator> template<typename InputIterator, typename Comparator>
bool bool
is_sorted(InputIterator begin, InputIterator end, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>()) is_sorted(InputIterator begin, InputIterator end,
{ Comparator comp
if (begin == end) = 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; 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. * @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 // XXX Comparator default template argument
template<typename InputIterator, typename Comparator> template<typename InputIterator, typename Comparator>
bool bool
is_sorted_failure(InputIterator begin, InputIterator end, InputIterator& first_failure, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>()) is_sorted_failure(InputIterator begin, InputIterator end,
{ InputIterator& first_failure,
if (begin == 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))
{
first_failure = current;
printf("is_sorted: check failed before position %lld.\n",
position);
return false;
}
recent = current;
position++;
}
first_failure = end;
return true; 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. * @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. * @return @c true if sorted, @c false otherwise.
*/ */
template<typename InputIterator, typename Comparator> template<typename InputIterator, typename Comparator>
bool bool
// XXX Comparator default template argument // XXX Comparator default template argument
is_sorted_print_failures(InputIterator begin, InputIterator end, Comparator comp = std::less<typename std::iterator_traits<InputIterator>::value_type>()) is_sorted_print_failures(InputIterator begin, InputIterator end,
{ Comparator comp
if (begin == end) = std::less<typename std::iterator_traits
return true; <InputIterator>::value_type>())
{
if (begin == end)
return true;
InputIterator recent(begin); InputIterator recent(begin);
bool ok = true; bool ok = true;
for (InputIterator pos(begin + 1); pos != end; pos++) for (InputIterator pos(begin + 1); pos != end; pos++)
{ {
if (comp(*pos, *recent)) if (comp(*pos, *recent))
{ {
printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2), printf("%ld: %d %d %d %d\n", pos - begin, *(pos - 2),
*(pos- 1), *pos, *(pos + 1)); *(pos- 1), *pos, *(pos + 1));
ok = false; ok = false;
} }
recent = pos; recent = pos;
} }
return ok; return ok;
} }
} }
#endif #endif

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // 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(),...) * @param selector Functionality (e. g. std::find_if (), std::equal(),...)
* @return Place of finding in both sequences. * @return Place of finding in both sequences.
*/ */
template< template<typename RandomAccessIterator1,
typename RandomAccessIterator1, typename RandomAccessIterator2,
typename RandomAccessIterator2, typename Pred,
typename Pred, typename Selector>
typename Selector>
std::pair<RandomAccessIterator1, RandomAccessIterator2> std::pair<RandomAccessIterator1, RandomAccessIterator2>
find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
RandomAccessIterator2 begin2, Pred pred, Selector selector) RandomAccessIterator2 begin2, Pred pred, Selector selector)
@ -96,11 +95,10 @@ template<
* @param selector Functionality (e. g. std::find_if (), std::equal(),...) * @param selector Functionality (e. g. std::find_if (), std::equal(),...)
* @return Place of finding in both sequences. * @return Place of finding in both sequences.
*/ */
template< template<typename RandomAccessIterator1,
typename RandomAccessIterator1, typename RandomAccessIterator2,
typename RandomAccessIterator2, typename Pred,
typename Pred, typename Selector>
typename Selector>
std::pair<RandomAccessIterator1, RandomAccessIterator2> std::pair<RandomAccessIterator1, RandomAccessIterator2>
find_template(RandomAccessIterator1 begin1, find_template(RandomAccessIterator1 begin1,
RandomAccessIterator1 end1, RandomAccessIterator1 end1,
@ -190,11 +188,10 @@ template<
* for CSB, the blocks are allocated in a predetermined manner, * for CSB, the blocks are allocated in a predetermined manner,
* namely spacial round-robin. * namely spacial round-robin.
*/ */
template< template<typename RandomAccessIterator1,
typename RandomAccessIterator1, typename RandomAccessIterator2,
typename RandomAccessIterator2, typename Pred,
typename Pred, typename Selector>
typename Selector>
std::pair<RandomAccessIterator1, RandomAccessIterator2> std::pair<RandomAccessIterator1, RandomAccessIterator2>
find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
RandomAccessIterator2 begin2, Pred pred, Selector selector, RandomAccessIterator2 begin2, Pred pred, Selector selector,
@ -311,11 +308,10 @@ template<
* blocks are allocated in a predetermined manner, namely spacial * blocks are allocated in a predetermined manner, namely spacial
* round-robin. * round-robin.
*/ */
template< template<typename RandomAccessIterator1,
typename RandomAccessIterator1, typename RandomAccessIterator2,
typename RandomAccessIterator2, typename Pred,
typename Pred, typename Selector>
typename Selector>
std::pair<RandomAccessIterator1, RandomAccessIterator2> std::pair<RandomAccessIterator1, RandomAccessIterator2>
find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
RandomAccessIterator2 begin2, Pred pred, Selector selector, RandomAccessIterator2 begin2, Pred pred, Selector selector,

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // 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 i2 Iterator on second sequence (unused).
* @param pred Find predicate. * @param pred Find predicate.
*/ */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline bool typename Pred>
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred) bool
{ return pred(*i1); } operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
{ return pred(*i1); }
/** @brief Corresponding sequential algorithm on a sequence. /** @brief Corresponding sequential algorithm on a sequence.
* @param begin1 Begin iterator of first sequence. * @param begin1 Begin iterator of first sequence.
@ -71,12 +72,14 @@ namespace __gnu_parallel
* @param begin2 Begin iterator of second sequence. * @param begin2 Begin iterator of second sequence.
* @param pred Find predicate. * @param pred Find predicate.
*/ */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline std::pair<RandomAccessIterator1, RandomAccessIterator2> typename Pred>
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred) std::pair<RandomAccessIterator1, RandomAccessIterator2>
{ sequential_algorithm(RandomAccessIterator1 begin1,
return std::make_pair(find_if(begin1, end1, pred, sequential_tag()), begin2); 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. */ /** @brief Test predicate on two adjacent elements. */
@ -87,13 +90,14 @@ namespace __gnu_parallel
* @param i2 Iterator on second sequence (unused). * @param i2 Iterator on second sequence (unused).
* @param pred Find predicate. * @param pred Find predicate.
*/ */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline bool typename Pred>
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred) bool
{ operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
// Passed end iterator is one short. {
return pred(*i1, *(i1 + 1)); // Passed end iterator is one short.
} return pred(*i1, *(i1 + 1));
}
/** @brief Corresponding sequential algorithm on a sequence. /** @brief Corresponding sequential algorithm on a sequence.
* @param begin1 Begin iterator of first sequence. * @param begin1 Begin iterator of first sequence.
@ -101,16 +105,20 @@ namespace __gnu_parallel
* @param begin2 Begin iterator of second sequence. * @param begin2 Begin iterator of second sequence.
* @param pred Find predicate. * @param pred Find predicate.
*/ */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline std::pair<RandomAccessIterator1, RandomAccessIterator2> typename Pred>
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred) std::pair<RandomAccessIterator1, RandomAccessIterator2>
{ sequential_algorithm(RandomAccessIterator1 begin1,
// Passed end iterator is one short. RandomAccessIterator1 end1,
RandomAccessIterator1 spot = adjacent_find(begin1, end1 + 1, pred, sequential_tag()); RandomAccessIterator2 begin2, Pred pred)
if (spot == (end1 + 1)) {
spot = end1; // Passed end iterator is one short.
return std::make_pair(spot, begin2); 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. */ /** @brief Test inverted predicate on a single element. */
@ -122,10 +130,11 @@ namespace __gnu_parallel
* @param i2 Iterator on second sequence (unused). * @param i2 Iterator on second sequence (unused).
* @param pred Find predicate. * @param pred Find predicate.
*/ */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline bool typename Pred>
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred) bool
{ return !pred(*i1, *i2); } operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred pred)
{ return !pred(*i1, *i2); }
/** /**
* @brief Corresponding sequential algorithm on a sequence. * @brief Corresponding sequential algorithm on a sequence.
@ -134,12 +143,13 @@ namespace __gnu_parallel
* @param begin2 Begin iterator of second sequence. * @param begin2 Begin iterator of second sequence.
* @param pred Find predicate. * @param pred Find predicate.
*/ */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline std::pair<RandomAccessIterator1, RandomAccessIterator2> typename Pred>
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred) std::pair<RandomAccessIterator1, RandomAccessIterator2>
{ sequential_algorithm(RandomAccessIterator1 begin1,
return mismatch(begin1, end1, begin2, pred, sequential_tag()); 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 i1 Iterator on first sequence.
* @param i2 Iterator on second sequence (unused). * @param i2 Iterator on second sequence (unused).
* @param pred Find predicate. */ * @param pred Find predicate. */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline bool typename Pred>
operator()(RandomAccessIterator1 i1, RandomAccessIterator2 i2, Pred 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)) for (ForwardIterator pos_in_candidates = begin;
return true; pos_in_candidates != end; ++pos_in_candidates)
return false; if (pred(*i1, *pos_in_candidates))
} return true;
return false;
}
/** @brief Corresponding sequential algorithm on a sequence. /** @brief Corresponding sequential algorithm on a sequence.
* @param begin1 Begin iterator of first sequence. * @param begin1 Begin iterator of first sequence.
* @param end1 End iterator of first sequence. * @param end1 End iterator of first sequence.
* @param begin2 Begin iterator of second sequence. * @param begin2 Begin iterator of second sequence.
* @param pred Find predicate. */ * @param pred Find predicate. */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Pred> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline std::pair<RandomAccessIterator1, RandomAccessIterator2> typename Pred>
sequential_algorithm(RandomAccessIterator1 begin1, RandomAccessIterator1 end1, RandomAccessIterator2 begin2, Pred pred) std::pair<RandomAccessIterator1, RandomAccessIterator2>
{ sequential_algorithm(RandomAccessIterator1 begin1,
return std::make_pair(find_first_of(begin1, end1, begin, end, pred, sequential_tag()), begin2); RandomAccessIterator1 end1,
} RandomAccessIterator2 begin2, Pred pred)
{ return std::make_pair(find_first_of(begin1, end1, begin, end, pred,
sequential_tag()), begin2); }
}; };
} }

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // software; you can redistribute it and/or modify it under the terms
@ -61,22 +61,42 @@ namespace __gnu_parallel
* @param output Output iterator. * @param output Output iterator.
* @param bound Maximum number of elements processed. * @param bound Maximum number of elements processed.
* @param parallelism_tag Parallelization method */ * @param parallelism_tag Parallelization method */
template<typename InputIterator, typename UserOp, typename Functionality, typename Red, typename Result> template<typename InputIterator, typename UserOp,
UserOp typename Functionality, typename Red, typename Result>
for_each_template_random_access(InputIterator begin, InputIterator end, UserOp
UserOp user_op, Functionality& functionality, for_each_template_random_access(InputIterator begin, InputIterator end,
Red reduction, Result reduction_start, UserOp user_op,
Result& output, Functionality& functionality,
typename std::iterator_traits<InputIterator>::difference_type bound, parallelism parallelism_tag) Red reduction, Result reduction_start,
{ Result& output, typename
if (parallelism_tag == parallel_unbalanced) std::iterator_traits<InputIterator>::
return for_each_template_random_access_ed(begin, end, user_op, functionality, reduction, reduction_start, output, bound); difference_type bound,
else if (parallelism_tag == parallel_omp_loop) parallelism parallelism_tag)
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) if (parallelism_tag == parallel_unbalanced)
return for_each_template_random_access_omp_loop(begin, end, user_op, functionality, reduction, reduction_start, output, bound); return for_each_template_random_access_ed(begin, end, user_op,
else //e. g. parallel_balanced functionality, reduction,
return for_each_template_random_access_workstealing(begin, end, user_op, functionality, reduction, reduction_start, output, bound); 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);
} }
} }

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // software; you can redistribute it and/or modify it under the terms
@ -63,11 +63,12 @@ namespace __gnu_parallel
* @param o Operator. * @param o Operator.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
template<typename Op> template<typename Op>
inline bool operator()(Op& o, It i) bool
{ operator()(Op& o, It i)
o(*i); {
return true; o(*i);
} return true;
}
}; };
/** @brief std::generate() selector. */ /** @brief std::generate() selector. */
@ -78,11 +79,12 @@ namespace __gnu_parallel
* @param o Operator. * @param o Operator.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
template<typename Op> template<typename Op>
inline bool operator()(Op& o, It i) bool
{ operator()(Op& o, It i)
*i = o(); {
return true; *i = o();
} return true;
}
}; };
/** @brief std::fill() selector. */ /** @brief std::fill() selector. */
@ -93,11 +95,12 @@ namespace __gnu_parallel
* @param v Current value. * @param v Current value.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
template<typename Val> template<typename Val>
inline bool operator()(Val& v, It i) bool
{ operator()(Val& v, It i)
*i = v; {
return true; *i = v;
} return true;
}
}; };
/** @brief std::transform() selector, one input sequence variant. */ /** @brief std::transform() selector, one input sequence variant. */
@ -108,11 +111,12 @@ namespace __gnu_parallel
* @param o Operator. * @param o Operator.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
template<typename Op> template<typename Op>
inline bool operator()(Op& o, It i) bool
{ operator()(Op& o, It i)
*i.second = o(*i.first); {
return true; *i.second = o(*i.first);
} return true;
}
}; };
/** @brief std::transform() selector, two input sequences variant. */ /** @brief std::transform() selector, two input sequences variant. */
@ -123,11 +127,12 @@ namespace __gnu_parallel
* @param o Operator. * @param o Operator.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
template<typename Op> template<typename Op>
inline bool operator()(Op& o, It i) bool
{ operator()(Op& o, It i)
*i.third = o(*i.first, *i.second); {
return true; *i.third = o(*i.first, *i.second);
} return true;
}
}; };
/** @brief std::replace() selector. */ /** @brief std::replace() selector. */
@ -144,7 +149,8 @@ namespace __gnu_parallel
/** @brief Functor execution. /** @brief Functor execution.
* @param v Current value. * @param v Current value.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
inline bool operator()(T& v, It i) bool
operator()(T& v, It i)
{ {
if (*i == v) if (*i == v)
*i = new_val; *i = new_val;
@ -166,7 +172,8 @@ namespace __gnu_parallel
/** @brief Functor execution. /** @brief Functor execution.
* @param o Operator. * @param o Operator.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
inline bool operator()(Op& o, It i) bool
operator()(Op& o, It i)
{ {
if (o(*i)) if (o(*i))
*i = new_val; *i = new_val;
@ -183,8 +190,9 @@ namespace __gnu_parallel
* @param i Iterator referencing object. * @param i Iterator referencing object.
* @return 1 if count, 0 if does not count. */ * @return 1 if count, 0 if does not count. */
template<typename Val> template<typename Val>
inline Diff operator()(Val& v, It i) Diff
{ return (v == *i) ? 1 : 0; } operator()(Val& v, It i)
{ return (v == *i) ? 1 : 0; }
}; };
/** @brief std::count_if () selector. */ /** @brief std::count_if () selector. */
@ -196,8 +204,9 @@ namespace __gnu_parallel
* @param i Iterator referencing object. * @param i Iterator referencing object.
* @return 1 if count, 0 if does not count. */ * @return 1 if count, 0 if does not count. */
template<typename Op> template<typename Op>
inline Diff operator()(Op& o, It i) Diff
{ return (o(*i)) ? 1 : 0; } operator()(Op& o, It i)
{ return (o(*i)) ? 1 : 0; }
}; };
/** @brief std::accumulate() selector. */ /** @brief std::accumulate() selector. */
@ -209,8 +218,8 @@ namespace __gnu_parallel
* @param i Iterator referencing object. * @param i Iterator referencing object.
* @return The current value. */ * @return The current value. */
template<typename Op> template<typename Op>
inline typename std::iterator_traits<It>::value_type operator()(Op o, It i) typename std::iterator_traits<It>::value_type operator()(Op o, It i)
{ return *i; } { return *i; }
}; };
/** @brief std::inner_product() selector. */ /** @brief std::inner_product() selector. */
@ -226,18 +235,21 @@ namespace __gnu_parallel
/** @brief Constructor. /** @brief Constructor.
* @param b1 Begin iterator of first sequence. * @param b1 Begin iterator of first sequence.
* @param b2 Begin iterator of second 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. /** @brief Functor execution.
* @param mult Multiplication functor. * @param mult Multiplication functor.
* @param current Iterator referencing object. * @param current Iterator referencing object.
* @return Inner product elemental result. */ * @return Inner product elemental result. */
template<typename Op> template<typename Op>
inline T operator()(Op mult, It current) T
{ operator()(Op mult, It current)
typename std::iterator_traits<It>::difference_type position = current - begin1_iterator; {
return mult(*current, *(begin2_iterator + position)); 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. */ /** @brief Selector that just returns the passed iterator. */
@ -249,8 +261,9 @@ namespace __gnu_parallel
* @param i Iterator referencing object. * @param i Iterator referencing object.
* @return Passed iterator. */ * @return Passed iterator. */
template<typename Op> template<typename Op>
inline It operator()(Op o, It i) It
{ return i; } operator()(Op o, It i)
{ return i; }
}; };
/** @brief Selector that returns the difference between two adjacent /** @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> struct adjacent_difference_selector : public generic_for_each_selector<It>
{ {
template<typename Op> template<typename Op>
inline bool operator()(Op& o, It i) bool
{ operator()(Op& o, It i)
typename It::first_type go_back_one = i.first; {
--go_back_one; typename It::first_type go_back_one = i.first;
*i.second = o(*i.first, *go_back_one); --go_back_one;
return true; *i.second = o(*i.first, *go_back_one);
} return true;
}
}; };
// XXX move into type_traits? // XXX move into type_traits?
@ -280,14 +294,15 @@ namespace __gnu_parallel
/** @brief Functor execution. /** @brief Functor execution.
* @param i Iterator referencing object. */ * @param i Iterator referencing object. */
template<typename It> template<typename It>
inline void operator()(It i) void
{ } operator()(It i) { }
}; };
/** @brief Reduction function doing nothing. */ /** @brief Reduction function doing nothing. */
struct dummy_reduct struct dummy_reduct
{ {
inline bool operator()(bool /*x*/, bool /*y*/) const bool
operator()(bool /*x*/, bool /*y*/) const
{ return true; } { return true; }
}; };
@ -300,7 +315,8 @@ namespace __gnu_parallel
explicit min_element_reduct(Comp &c) : comp(c) 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)) if (comp(*x, *y))
return x; return x;
@ -318,7 +334,8 @@ namespace __gnu_parallel
explicit max_element_reduct(Comp& c) : comp(c) 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)) if (comp(*x, *y))
return y; return y;
@ -336,7 +353,9 @@ namespace __gnu_parallel
explicit accumulate_binop_reduct(BinOp& b) : binop(b) {} explicit accumulate_binop_reduct(BinOp& b) : binop(b) {}
template<typename Result, typename Addend> 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); }
}; };
} }

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // software; you can redistribute it and/or modify it under the terms
@ -47,156 +47,158 @@ namespace __gnu_parallel
* applied to both child iterators. * applied to both child iterators.
*/ */
template<typename Iterator1, typename Iterator2, typename IteratorCategory> template<typename Iterator1, typename Iterator2, typename IteratorCategory>
class iterator_pair : public std::pair<Iterator1, Iterator2> 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++()
{ {
++base_type::first; private:
++base_type::second; typedef iterator_pair<Iterator1, Iterator2, IteratorCategory> type;
return *this; typedef std::pair<Iterator1, Iterator2> base_type;
}
// Post-increment operator. public:
const type typedef IteratorCategory iterator_category;
operator++(int) typedef void value_type;
{ return type(base_type::first++, base_type::second++); }
// Pre-decrement operator. typedef std::iterator_traits<Iterator1> traits_type;
type& typedef typename traits_type::difference_type difference_type;
operator--() typedef type* pointer;
{ typedef type& reference;
--base_type::first;
--base_type::second;
return *this;
}
// Post-decrement operator. iterator_pair() { }
const type
operator--(int)
{ return type(base_type::first--, base_type::second--); }
// Type conversion. iterator_pair(const Iterator1& first, const Iterator2& second)
operator Iterator2() const : base_type(first, second) { }
{ return base_type::second; }
type& // Pre-increment operator.
operator=(const type& other) type&
{ operator++()
base_type::first = other.first; {
base_type::second = other.second; ++base_type::first;
return *this; ++base_type::second;
} return *this;
}
type // Post-increment operator.
operator+(difference_type delta) const const type
{ return type(base_type::first + delta, base_type::second + delta); } operator++(int)
{ return type(base_type::first++, base_type::second++); }
difference_type // Pre-decrement operator.
operator-(const type& other) const type&
{ return base_type::first - other.first; } 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 /** @brief A triple of iterators. The usual iterator operations are
applied to all three child iterators. applied to all three child iterators.
*/ */
template<typename Iterator1, typename Iterator2, typename Iterator3, typename IteratorCategory> template<typename Iterator1, typename Iterator2, typename Iterator3,
class iterator_triple 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)
{ {
first = _first; private:
second = _second; typedef iterator_triple<Iterator1, Iterator2, Iterator3,
third = _third; IteratorCategory> type;
}
// Pre-increment operator. public:
type& typedef IteratorCategory iterator_category;
operator++() typedef void value_type;
{ typedef typename Iterator1::difference_type difference_type;
++first; typedef type* pointer;
++second; typedef type& reference;
++third;
return *this;
}
// Post-increment operator. Iterator1 first;
const type Iterator2 second;
operator++(int) Iterator3 third;
{ return type(first++, second++, third++); }
// Pre-decrement operator. iterator_triple() { }
type&
operator--()
{
--first;
--second;
--third;
return *this;
}
// Post-decrement operator. iterator_triple(const Iterator1& _first, const Iterator2& _second,
const type const Iterator3& _third)
operator--(int) {
{ return type(first--, second--, third--); } first = _first;
second = _second;
third = _third;
}
// Type conversion. // Pre-increment operator.
operator Iterator3() const type&
{ return third; } operator++()
{
++first;
++second;
++third;
return *this;
}
type& // Post-increment operator.
operator=(const type& other) const type
{ operator++(int)
first = other.first; { return type(first++, second++, third++); }
second = other.second;
third = other.third;
return *this;
}
type // Pre-decrement operator.
operator+(difference_type delta) const type&
{ return type(first + delta, second + delta, third + delta); } operator--()
{
--first;
--second;
--third;
return *this;
}
difference_type // Post-decrement operator.
operator-(const type& other) const const type
{ return first - other.first; } 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; }
}; };
} }

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // software; you can redistribute it and/or modify it under the terms
@ -52,36 +52,35 @@ namespace __gnu_parallel
* grown or not * grown or not
*/ */
template<typename InputIterator> template<typename InputIterator>
void void
shrink_and_double(std::vector<InputIterator>& os_starts, size_t& count_to_two, size_t& range_length, const bool make_twice) shrink_and_double(std::vector<InputIterator>& os_starts,
{ size_t& count_to_two, size_t& range_length,
++count_to_two; const bool make_twice)
if (not make_twice or count_to_two < 2) {
{ ++count_to_two;
if (not make_twice or count_to_two < 2)
shrink(os_starts, count_to_two, range_length); shrink(os_starts, count_to_two, range_length);
} else
else {
{ os_starts.resize((os_starts.size() - 1) * 2 + 1);
os_starts.resize((os_starts.size() - 1) * 2 + 1); count_to_two = 0;
count_to_two = 0; }
} }
}
/** @brief Combines two ranges into one and thus halves the number of ranges. /** @brief Combines two ranges into one and thus halves the number of ranges.
* @param os_starts Start positions worked on (oversampled). * @param os_starts Start positions worked on (oversampled).
* @param count_to_two Counts up to 2. * @param count_to_two Counts up to 2.
* @param range_length Current length of a chunk. */ * @param range_length Current length of a chunk. */
template<typename InputIterator> template<typename InputIterator>
void void
shrink(std::vector<InputIterator>& os_starts, size_t& count_to_two, shrink(std::vector<InputIterator>& os_starts, size_t& count_to_two,
size_t& range_length) size_t& range_length)
{ {
for (typename std::vector<InputIterator>::size_type i = 0; i <= (os_starts.size() / 2); ++i) for (typename std::vector<InputIterator>::size_type i = 0;
{ i <= (os_starts.size() / 2); ++i)
os_starts[i] = os_starts[i * 2]; os_starts[i] = os_starts[i * 2];
} range_length *= 2;
range_length *= 2; }
}
/** @brief Splits a sequence given by input iterators into parts of /** @brief Splits a sequence given by input iterators into parts of
* almost equal size * almost equal size
@ -103,79 +102,81 @@ namespace __gnu_parallel
* @return Length of the whole sequence. * @return Length of the whole sequence.
*/ */
template<typename InputIterator, typename FunctorType> template<typename InputIterator, typename FunctorType>
size_t size_t
list_partition(const InputIterator begin, const InputIterator end, list_partition(const InputIterator begin, const InputIterator end,
InputIterator* starts, size_t* lengths, const int num_parts, InputIterator* starts, size_t* lengths, const int num_parts,
FunctorType& f, int oversampling = 0) FunctorType& f, int oversampling = 0)
{ {
bool make_twice = false; bool make_twice = false;
// According to the oversampling factor, the resizing algorithm is chosen. // According to the oversampling factor, the resizing algorithm is chosen.
if (oversampling == 0) 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)
{ {
for (dist_limit += range_length; dist < dist_limit and it != end; ++dist) make_twice = true;
{ oversampling = 1;
f(it);
++it;
}
os_starts[cur] = it;
} }
// Must compare for end and not cur < os_starts.size() , because std::vector<InputIterator> os_starts(2 * oversampling * num_parts + 1);
// cur could be == os_starts.size() as well
if (it == end)
break;
shrink_and_double(os_starts, count_to_two, range_length, make_twice); os_starts[0]= begin;
next = os_starts.size()/2 + 1; 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 #endif

View file

@ -76,9 +76,8 @@ template<typename T, typename Comparator = std::less<T> >
Comparator comp; Comparator comp;
public: public:
inline
LoserTreeExplicit(unsigned int _size, Comparator _comp = std::less<T>()) LoserTreeExplicit(unsigned int _size, Comparator _comp = std::less<T>())
: comp(_comp) : comp(_comp)
{ {
size = _size; size = _size;
offset = size; offset = size;
@ -92,14 +91,14 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline ~LoserTreeExplicit() ~LoserTreeExplicit()
{ delete[] losers; } { delete[] losers; }
inline int int
get_min_source() get_min_source()
{ return losers[0].source; } { return losers[0].source; }
inline void void
insert_start(T key, int source, bool sup) insert_start(T key, int source, bool sup)
{ {
bool inf = false; bool inf = false;
@ -122,10 +121,10 @@ template<typename T, typename Comparator = std::less<T> >
losers[0].source = source; losers[0].source = source;
} }
inline void void
init() { } init() { }
inline void void
delete_min_insert(T key, bool sup) delete_min_insert(T key, bool sup)
{ {
bool inf = false; bool inf = false;
@ -151,7 +150,7 @@ template<typename T, typename Comparator = std::less<T> >
losers[0].source = source; losers[0].source = source;
} }
inline void void
insert_start_stable(T key, int source, bool sup) insert_start_stable(T key, int source, bool sup)
{ {
bool inf = false; bool inf = false;
@ -176,10 +175,10 @@ template<typename T, typename Comparator = std::less<T> >
losers[0].source = source; losers[0].source = source;
} }
inline void void
init_stable() { } init_stable() { }
inline void void
delete_min_insert_stable(T key, bool sup) delete_min_insert_stable(T key, bool sup)
{ {
bool inf = false; bool inf = false;
@ -233,7 +232,7 @@ template<typename T, typename Comparator = std::less<T> >
bool first_insert; bool first_insert;
public: public:
inline LoserTree(unsigned int _k, Comparator _comp = std::less<T>()) LoserTree(unsigned int _k, Comparator _comp = std::less<T>())
: comp(_comp) : comp(_comp)
{ {
ik = _k; ik = _k;
@ -249,14 +248,14 @@ template<typename T, typename Comparator = std::less<T> >
first_insert = true; first_insert = true;
} }
inline ~LoserTree() ~LoserTree()
{ ::operator delete(losers); } { ::operator delete(losers); }
inline int int
get_min_source() get_min_source()
{ return losers[0].source; } { return losers[0].source; }
inline void void
insert_start(const T& key, int source, bool sup) insert_start(const T& key, int source, bool sup)
{ {
unsigned int pos = k + source; unsigned int pos = k + source;
@ -303,12 +302,12 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init() init()
{ losers[0] = losers[init_winner(1)]; } { losers[0] = losers[init_winner(1)]; }
// Do not pass const reference since key will be used as local variable. // Do not pass const reference since key will be used as local variable.
inline void void
delete_min_insert(T key, bool sup) delete_min_insert(T key, bool sup)
{ {
int source = losers[0].source; int source = losers[0].source;
@ -329,7 +328,7 @@ template<typename T, typename Comparator = std::less<T> >
losers[0].key = key; losers[0].key = key;
} }
inline void void
insert_start_stable(const T& key, int source, bool sup) insert_start_stable(const T& key, int source, bool sup)
{ return insert_start(key, source, sup); } { return insert_start(key, source, sup); }
@ -361,12 +360,12 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init_stable() init_stable()
{ losers[0] = losers[init_winner_stable(1)]; } { losers[0] = losers[init_winner_stable(1)]; }
// Do not pass const reference since key will be used as local variable. // Do not pass const reference since key will be used as local variable.
inline void void
delete_min_insert_stable(T key, bool sup) delete_min_insert_stable(T key, bool sup)
{ {
int source = losers[0].source; int source = losers[0].source;
@ -432,9 +431,8 @@ template<typename T, typename Comparator = std::less<T> >
Comparator comp; Comparator comp;
public: public:
inline
LoserTreeReference(unsigned int _k, Comparator _comp = std::less<T>()) LoserTreeReference(unsigned int _k, Comparator _comp = std::less<T>())
: comp(_comp) : comp(_comp)
{ {
ik = _k; ik = _k;
@ -449,7 +447,7 @@ template<typename T, typename Comparator = std::less<T> >
losers[i + k].sup = true; losers[i + k].sup = true;
} }
inline ~LoserTreeReference() ~LoserTreeReference()
{ {
delete[] losers; delete[] losers;
#ifndef COPY #ifndef COPY
@ -457,11 +455,11 @@ template<typename T, typename Comparator = std::less<T> >
#endif #endif
} }
inline int int
get_min_source() get_min_source()
{ return losers[0].source; } { return losers[0].source; }
inline void void
insert_start(T key, int source, bool sup) insert_start(T key, int source, bool sup)
{ {
unsigned int pos = k + source; unsigned int pos = k + source;
@ -498,13 +496,13 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init() init()
{ {
losers[0] = losers[init_winner(1)]; losers[0] = losers[init_winner(1)];
} }
inline void void
delete_min_insert(T key, bool sup) delete_min_insert(T key, bool sup)
{ {
int source = losers[0].source; int source = losers[0].source;
@ -529,7 +527,7 @@ template<typename T, typename Comparator = std::less<T> >
#endif #endif
} }
inline void void
insert_start_stable(T key, int source, bool sup) insert_start_stable(T key, int source, bool sup)
{ return insert_start(key, source, sup); } { return insert_start(key, source, sup); }
@ -560,11 +558,11 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init_stable() init_stable()
{ losers[0] = losers[init_winner_stable(1)]; } { losers[0] = losers[init_winner_stable(1)]; }
inline void void
delete_min_insert_stable(T key, bool sup) delete_min_insert_stable(T key, bool sup)
{ {
int source = losers[0].source; int source = losers[0].source;
@ -622,7 +620,6 @@ template<typename T, typename Comparator = std::less<T> >
Comparator comp; Comparator comp;
public: public:
inline
LoserTreePointer(unsigned int _k, Comparator _comp = std::less<T>()) LoserTreePointer(unsigned int _k, Comparator _comp = std::less<T>())
: comp(_comp) : comp(_comp)
{ {
@ -636,14 +633,14 @@ template<typename T, typename Comparator = std::less<T> >
losers[i + k].sup = true; losers[i + k].sup = true;
} }
inline ~LoserTreePointer() ~LoserTreePointer()
{ delete[] losers; } { delete[] losers; }
inline int int
get_min_source() get_min_source()
{ return losers[0].source; } { return losers[0].source; }
inline void void
insert_start(const T& key, int source, bool sup) insert_start(const T& key, int source, bool sup)
{ {
unsigned int pos = k + source; unsigned int pos = k + source;
@ -657,9 +654,7 @@ template<typename T, typename Comparator = std::less<T> >
init_winner(unsigned int root) init_winner(unsigned int root)
{ {
if (root >= k) if (root >= k)
{ return root;
return root;
}
else else
{ {
unsigned int left = init_winner (2 * root); unsigned int left = init_winner (2 * root);
@ -681,11 +676,11 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init() init()
{ losers[0] = losers[init_winner(1)]; } { losers[0] = losers[init_winner(1)]; }
inline void void
delete_min_insert(const T& key, bool sup) delete_min_insert(const T& key, bool sup)
{ {
const T* keyp = &key; const T* keyp = &key;
@ -707,7 +702,7 @@ template<typename T, typename Comparator = std::less<T> >
losers[0].keyp = keyp; losers[0].keyp = keyp;
} }
inline void void
insert_start_stable(const T& key, int source, bool sup) insert_start_stable(const T& key, int source, bool sup)
{ return insert_start(key, source, sup); } { return insert_start(key, source, sup); }
@ -739,11 +734,11 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init_stable() init_stable()
{ losers[0] = losers[init_winner_stable(1)]; } { losers[0] = losers[init_winner_stable(1)]; }
inline void void
delete_min_insert_stable(const T& key, bool sup) delete_min_insert_stable(const T& key, bool sup)
{ {
const T* keyp = &key; const T* keyp = &key;
@ -810,9 +805,8 @@ template<typename T, typename Comparator = std::less<T> >
} }
public: public:
inline
LoserTreeUnguarded(unsigned int _k, Comparator _comp = std::less<T>()) LoserTreeUnguarded(unsigned int _k, Comparator _comp = std::less<T>())
: comp(_comp) : comp(_comp)
{ {
ik = _k; ik = _k;
// Next greater or equal power of 2. // Next greater or equal power of 2.
@ -823,17 +817,17 @@ template<typename T, typename Comparator = std::less<T> >
map(1, 0, ik); map(1, 0, ik);
} }
inline ~LoserTreeUnguarded() ~LoserTreeUnguarded()
{ {
delete[] losers; delete[] losers;
delete[] mapping; delete[] mapping;
} }
inline int int
get_min_source() get_min_source()
{ return losers[0].source; } { return losers[0].source; }
inline void void
insert_start(const T& key, int source, bool) insert_start(const T& key, int source, bool)
{ {
unsigned int pos = mapping[source]; unsigned int pos = mapping[source];
@ -868,12 +862,12 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init() init()
{ losers[0] = losers[init_winner(1, 0, ik)]; } { losers[0] = losers[init_winner(1, 0, ik)]; }
// Do not pass const reference since key will be used as local variable. // Do not pass const reference since key will be used as local variable.
inline void void
delete_min_insert(const T& key, bool) delete_min_insert(const T& key, bool)
{ {
losers[0].key = key; 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) insert_start_stable(const T& key, int source, bool)
{ return insert_start(key, source, false); } { return insert_start(key, source, false); }
inline void void
init_stable() init_stable()
{ init(); } { init(); }
inline void void
delete_min_insert_stable(const T& key, bool) delete_min_insert_stable(const T& key, bool)
{ {
losers[0].key = key; losers[0].key = key;
@ -959,10 +953,9 @@ template<typename T, typename Comparator = std::less<T> >
} }
public: public:
inline
LoserTreePointerUnguarded(unsigned int _k, LoserTreePointerUnguarded(unsigned int _k,
Comparator _comp = std::less<T>()) Comparator _comp = std::less<T>())
: comp(_comp) : comp(_comp)
{ {
ik = _k; ik = _k;
@ -974,17 +967,17 @@ template<typename T, typename Comparator = std::less<T> >
map(1, 0, ik); map(1, 0, ik);
} }
inline ~LoserTreePointerUnguarded() ~LoserTreePointerUnguarded()
{ {
delete[] losers; delete[] losers;
delete[] mapping; delete[] mapping;
} }
inline int int
get_min_source() get_min_source()
{ return losers[0].source; } { return losers[0].source; }
inline void void
insert_start(const T& key, int source, bool) insert_start(const T& key, int source, bool)
{ {
unsigned int pos = mapping[source]; unsigned int pos = mapping[source];
@ -1019,13 +1012,11 @@ template<typename T, typename Comparator = std::less<T> >
} }
} }
inline void void
init() 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) delete_min_insert(const T& key, bool)
{ {
const T* keyp = &key; const T* keyp = &key;
@ -1044,15 +1035,15 @@ template<typename T, typename Comparator = std::less<T> >
losers[0].keyp = keyp; losers[0].keyp = keyp;
} }
inline void void
insert_start_stable(const T& key, int source, bool) insert_start_stable(const T& key, int source, bool)
{ return insert_start(key, source, false); } { return insert_start(key, source, false); }
inline void void
init_stable() init_stable()
{ init(); } { init(); }
inline void void
delete_min_insert_stable(const T& key, bool) delete_min_insert_stable(const T& key, bool)
{ {
int& source = losers[0].source; int& source = losers[0].source;

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // 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 max_length Maximum number of elements to merge.
* @param comp Comparator. * @param comp Comparator.
* @return Output end iterator. */ * @return Output end iterator. */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename OutputIterator, typename _DifferenceTp, typename Comparator> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
OutputIterator typename OutputIterator, typename _DifferenceTp,
merge_advance_usual(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator2& begin2, RandomAccessIterator2 end2, OutputIterator target, _DifferenceTp max_length, Comparator comp) typename Comparator>
{ OutputIterator
typedef _DifferenceTp difference_type; merge_advance_usual(RandomAccessIterator1& begin1,
while (begin1 != end1 && begin2 != end2 && max_length > 0) RandomAccessIterator1 end1,
{ RandomAccessIterator2& begin2,
// array1[i1] < array0[i0] RandomAccessIterator2 end2, OutputIterator target,
if (comp(*begin2, *begin1)) _DifferenceTp max_length, Comparator comp)
*target++ = *begin2++; {
else typedef _DifferenceTp difference_type;
*target++ = *begin1++; while (begin1 != end1 && begin2 != end2 && max_length > 0)
max_length--; {
} // array1[i1] < array0[i0]
if (comp(*begin2, *begin1))
*target++ = *begin2++;
else
*target++ = *begin1++;
max_length--;
}
if (begin1 != end1) if (begin1 != end1)
{ {
target = std::copy(begin1, begin1 + max_length, target); target = std::copy(begin1, begin1 + max_length, target);
begin1 += max_length; begin1 += max_length;
} }
else else
{ {
target = std::copy(begin2, begin2 + max_length, target); target = std::copy(begin2, begin2 + max_length, target);
begin2 += max_length; begin2 += max_length;
} }
return target; return target;
} }
/** @brief Merge routine being able to merge only the @c max_length /** @brief Merge routine being able to merge only the @c max_length
* smallest elements. * smallest elements.
@ -99,52 +105,59 @@ namespace __gnu_parallel
* @param max_length Maximum number of elements to merge. * @param max_length Maximum number of elements to merge.
* @param comp Comparator. * @param comp Comparator.
* @return Output end iterator. */ * @return Output end iterator. */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename OutputIterator, typename _DifferenceTp, typename Comparator> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
OutputIterator typename OutputIterator, typename _DifferenceTp,
merge_advance_movc(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator2& begin2, RandomAccessIterator2 end2, OutputIterator target, _DifferenceTp max_length, Comparator comp) typename Comparator>
{ OutputIterator
typedef _DifferenceTp difference_type; merge_advance_movc(RandomAccessIterator1& begin1,
typedef typename std::iterator_traits<RandomAccessIterator1>::value_type value_type1; RandomAccessIterator1 end1,
typedef typename std::iterator_traits<RandomAccessIterator2>::value_type value_type2; 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 #if _GLIBCXX_ASSERTIONS
_GLIBCXX_PARALLEL_ASSERT(max_length >= 0); _GLIBCXX_PARALLEL_ASSERT(max_length >= 0);
#endif #endif
while (begin1 != end1 && begin2 != end2 && max_length > 0) while (begin1 != end1 && begin2 != end2 && max_length > 0)
{ {
RandomAccessIterator1 next1 = begin1 + 1; RandomAccessIterator1 next1 = begin1 + 1;
RandomAccessIterator2 next2 = begin2 + 1; RandomAccessIterator2 next2 = begin2 + 1;
value_type1 element1 = *begin1; value_type1 element1 = *begin1;
value_type2 element2 = *begin2; value_type2 element2 = *begin2;
if (comp(element2, element1)) if (comp(element2, element1))
{ {
element1 = element2; element1 = element2;
begin2 = next2; begin2 = next2;
} }
else else
{
begin1 = next1; begin1 = next1;
}
*target = element1; *target = element1;
target++; target++;
max_length--; max_length--;
} }
if (begin1 != end1) if (begin1 != end1)
{ {
target = std::copy(begin1, begin1 + max_length, target); target = std::copy(begin1, begin1 + max_length, target);
begin1 += max_length; begin1 += max_length;
} }
else else
{ {
target = std::copy(begin2, begin2 + max_length, target); target = std::copy(begin2, begin2 + max_length, target);
begin2 += max_length; begin2 += max_length;
} }
return target; return target;
} }
/** @brief Merge routine being able to merge only the @c max_length /** @brief Merge routine being able to merge only the @c max_length
* smallest elements. * smallest elements.
@ -160,14 +173,20 @@ namespace __gnu_parallel
* @param max_length Maximum number of elements to merge. * @param max_length Maximum number of elements to merge.
* @param comp Comparator. * @param comp Comparator.
* @return Output end iterator. */ * @return Output end iterator. */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename OutputIterator, typename _DifferenceTp, typename Comparator> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline OutputIterator typename OutputIterator, typename _DifferenceTp,
merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator2& begin2, RandomAccessIterator2 end2, OutputIterator target, _DifferenceTp max_length, Comparator comp) typename Comparator>
{ inline OutputIterator
_GLIBCXX_CALL(max_length) 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 /** @brief Merge routine fallback to sequential in case the
iterators of the two input sequences are of different type. 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 max_length Maximum number of elements to merge.
* @param comp Comparator. * @param comp Comparator.
* @return Output end iterator. */ * @return Output end iterator. */
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename RandomAccessIterator3, typename Comparator> template<typename RandomAccessIterator1, typename RandomAccessIterator2,
inline RandomAccessIterator3 typename RandomAccessIterator3, typename Comparator>
parallel_merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, inline RandomAccessIterator3
RandomAccessIterator2& begin2, RandomAccessIterator2 end2, //different iterators, parallel implementation not available parallel_merge_advance(RandomAccessIterator1& begin1,
RandomAccessIterator3 target, RandomAccessIterator1 end1,
typename std::iterator_traits<RandomAccessIterator1>::difference_type max_length, Comparator comp) RandomAccessIterator2& begin2,
{ // different iterators, parallel implementation
return merge_advance(begin1, end1, begin2, end2, target, max_length, comp); // 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 /** @brief Parallel merge routine being able to merge only the @c
* max_length smallest elements. * max_length smallest elements.
@ -204,22 +228,33 @@ namespace __gnu_parallel
* @param comp Comparator. * @param comp Comparator.
* @return Output end iterator. * @return Output end iterator.
*/ */
template<typename RandomAccessIterator1, typename RandomAccessIterator3, typename Comparator> template<typename RandomAccessIterator1, typename RandomAccessIterator3,
inline RandomAccessIterator3 typename Comparator>
parallel_merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1, RandomAccessIterator1& begin2, RandomAccessIterator1 end2, RandomAccessIterator3 target, typename std::iterator_traits<RandomAccessIterator1>::difference_type max_length, Comparator comp) inline RandomAccessIterator3
{ parallel_merge_advance(RandomAccessIterator1& begin1,
typedef typename std::iterator_traits<RandomAccessIterator1>::value_type RandomAccessIterator1 end1,
value_type; RandomAccessIterator1& begin2,
typedef typename std::iterator_traits<RandomAccessIterator1>::difference_type RandomAccessIterator1 end2,
difference_type1 /* == difference_type2 */; RandomAccessIterator3 target, typename
typedef typename std::iterator_traits<RandomAccessIterator3>::difference_type std::iterator_traits<RandomAccessIterator1>::
difference_type3; 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) }; std::pair<RandomAccessIterator1, RandomAccessIterator1>
RandomAccessIterator3 target_end = parallel_multiway_merge(seqs, seqs + 2, target, comp, max_length, true, false); 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 } //namespace __gnu_parallel
#endif #endif

View file

@ -1,6 +1,6 @@
// -*- C++ -*- // -*- 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 // 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 // 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. */ /** @brief Compare a pair of types lexicographically, ascending. */
template<typename T1, typename T2, typename Comparator> template<typename T1, typename T2, typename Comparator>
class lexicographic : public std::binary_function<std::pair<T1, T2>, std::pair<T1, T2>, bool> 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
{ {
if (comp(p1.first, p2.first)) private:
return true; Comparator& comp;
if (comp(p2.first, p1.first)) public:
return false; lexicographic(Comparator& _comp) : comp(_comp) { }
// Firsts are equal. // XXX const
return p1.second < p2.second; 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. */ /** @brief Compare a pair of types lexicographically, descending. */
template<typename T1, typename T2, typename Comparator> template<typename T1, typename T2, typename Comparator>
class lexicographic_reverse : public std::binary_function<T1, T2, bool> 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
{ {
if (comp(p2.first, p1.first)) private:
return true; Comparator& comp;
if (comp(p1.first, p2.first)) public:
return false; lexicographic_reverse(Comparator& _comp) : comp(_comp) { }
// Firsts are equal. bool
return p2.second < p1.second; 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, * @brief Splits several sorted sequences at a certain global rank,
@ -121,229 +124,243 @@ namespace __gnu_parallel
* the respective sequence. * the respective sequence.
* @param comp The ordering functor, defaults to std::less<T>. * @param comp The ordering functor, defaults to std::less<T>.
*/ */
template<typename RanSeqs, typename RankType, typename RankIterator, typename Comparator> template<typename RanSeqs, typename RankType, typename RankIterator,
void typename Comparator>
multiseq_partition(RanSeqs begin_seqs, RanSeqs end_seqs, RankType rank, void
RankIterator begin_offsets, multiseq_partition(RanSeqs begin_seqs, RanSeqs end_seqs,
Comparator comp = std::less< RankType rank,
typename std::iterator_traits<typename std::iterator_traits<RanSeqs>::value_type::first_type>::value_type>()) // std::less<T> RankIterator begin_offsets,
{ Comparator comp = std::less<
_GLIBCXX_CALL(end_seqs - begin_seqs) 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<RanSeqs>::value_type::first_type
typedef typename std::iterator_traits<It>::difference_type difference_type; It;
typedef typename std::iterator_traits<It>::value_type value_type; 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<value_type, int, Comparator> lcomp(comp);
lexicographic_reverse<value_type, int, Comparator> lrcomp(comp); lexicographic_reverse<value_type, int, Comparator> lrcomp(comp);
// Number of sequences, number of elements in total (possibly // Number of sequences, number of elements in total (possibly
// including padding). // including padding).
difference_type m = std::distance(begin_seqs, end_seqs), N = 0, nmax, n, r; difference_type m = std::distance(begin_seqs, end_seqs), N = 0,
nmax, n, r;
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
N += std::distance(begin_seqs[i].first, begin_seqs[i].second); N += std::distance(begin_seqs[i].first, begin_seqs[i].second);
if (rank == N) if (rank == N)
{ {
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
begin_offsets[i] = begin_seqs[i].second; // Very end. begin_offsets[i] = begin_seqs[i].second; // Very end.
// Return m - 1; // 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* ns = new difference_type[m];
difference_type* a = new difference_type[m]; difference_type* a = new difference_type[m];
difference_type* b = new difference_type[m]; difference_type* b = new difference_type[m];
difference_type l; difference_type l;
ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second); ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second);
nmax = ns[0]; nmax = ns[0];
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
{ {
ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second); ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second);
nmax = std::max(nmax, ns[i]); 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], // Pad all lists to this length, at least as long as any ns[i],
// equality iff nmax = 2^k - 1. // equality iff nmax = 2^k - 1.
l = (1ULL << r) - 1; l = (1ULL << r) - 1;
// From now on, including padding. // From now on, including padding.
N = l * m; N = l * m;
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
{ {
a[i] = 0; a[i] = 0;
b[i] = l; b[i] = l;
} }
n = l / 2; n = l / 2;
// Invariants: // Invariants:
// 0 <= a[i] <= ns[i], 0 <= b[i] <= l // 0 <= a[i] <= ns[i], 0 <= b[i] <= l
#define S(i) (begin_seqs[i].first) #define S(i) (begin_seqs[i].first)
// Initial partition. // Initial partition.
std::vector<std::pair<value_type, int> > sample; std::vector<std::pair<value_type, int> > sample;
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
if (n < ns[i]) //sequence long enough if (n < ns[i]) //sequence long enough
sample.push_back(std::make_pair(S(i)[n], i)); sample.push_back(std::make_pair(S(i)[n], i));
__gnu_sequential::sort(sample.begin(), sample.end(), lcomp); __gnu_sequential::sort(sample.begin(), sample.end(), lcomp);
for (int i = 0; i < m; i++) //conceptual infinity for (int i = 0; i < m; i++) //conceptual infinity
if (n >= ns[i]) //sequence too short, conceptual infinity if (n >= ns[i]) //sequence too short, conceptual infinity
sample.push_back(std::make_pair(S(i)[0] /*dummy element*/, 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; int j;
for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++) for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++)
a[sample[j].second] += n + 1; a[sample[j].second] += n + 1;
for (; j < m; j++) for (; j < m; j++)
b[sample[j].second] -= n + 1; b[sample[j].second] -= n + 1;
// Further refinement. // Further refinement.
while (n > 0) while (n > 0)
{ {
n /= 2; n /= 2;
int lmax_seq = -1; // to avoid warning int lmax_seq = -1; // to avoid warning
const value_type* lmax = NULL; // impossible to avoid the warning? const value_type* lmax = NULL; // impossible to avoid the warning?
for (int i = 0; i < m; i++) 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++)
if (a[i] > 0) 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 i;
{ for (i = 0; i < m; i++)
int source = pq.top().second; {
pq.pop(); 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; difference_type leftsize = 0, total = 0;
b[source] -= n + 1; for (int i = 0; i < m; i++)
{
leftsize += a[i] / (n + 1);
total += l / (n + 1);
}
if (a[source] > 0) difference_type skew = static_cast<difference_type>
pq.push(std::make_pair(S(source)[a[source] - 1], source)); (static_cast<uint64>(total) * rank / N - leftsize);
}
}
}
// Postconditions: if (skew > 0)
// a[i] == b[i] in most cases, except when a[i] has been clamped {
// because of having reached the boundary // 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);
// 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();
// Maximum of left edge, minimum of right edge. a[source] = std::min(a[source] + n + 1, ns[source]);
value_type* maxleft = NULL; b[source] += n + 1;
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; if (b[source] < ns[source])
for (int i = 0; i < m; i++) pq.push(std::make_pair(S(source)[b[source]], source));
begin_offsets[i] = S(i) + a[i]; }
}
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);
delete[] ns; for (int i = 0; i < m; i++)
delete[] a; if (a[i] > 0)
delete[] b; pq.push(std::make_pair(S(i)[a[i] - 1], i));
}
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.
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. * selected element is unique, this number is 0.
* @param comp The ordering functor, defaults to std::less. * @param comp The ordering functor, defaults to std::less.
*/ */
template<typename T, typename RanSeqs, typename RankType, typename Comparator> template<typename T, typename RanSeqs, typename RankType,
T typename Comparator>
multiseq_selection(RanSeqs begin_seqs, RanSeqs end_seqs, RankType rank, T
RankType& offset, Comparator comp = std::less<T>()) multiseq_selection(RanSeqs begin_seqs, RanSeqs end_seqs, RankType rank,
{ RankType& offset, Comparator comp = std::less<T>())
_GLIBCXX_CALL(end_seqs - begin_seqs) {
_GLIBCXX_CALL(end_seqs - begin_seqs)
typedef typename std::iterator_traits<RanSeqs>::value_type::first_type It; typedef typename std::iterator_traits<RanSeqs>::value_type::first_type
typedef typename std::iterator_traits<It>::difference_type difference_type; It;
typedef typename std::iterator_traits<It>::difference_type
difference_type;
lexicographic<T, int, Comparator> lcomp(comp); lexicographic<T, int, Comparator> lcomp(comp);
lexicographic_reverse<T, int, Comparator> lrcomp(comp); lexicographic_reverse<T, int, Comparator> lrcomp(comp);
// Number of sequences, number of elements in total (possibly // Number of sequences, number of elements in total (possibly
// including padding). // including padding).
difference_type m = std::distance(begin_seqs, end_seqs); difference_type m = std::distance(begin_seqs, end_seqs);
difference_type N = 0; difference_type N = 0;
difference_type nmax, n, r; difference_type nmax, n, r;
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
N += std::distance(begin_seqs[i].first, begin_seqs[i].second); N += std::distance(begin_seqs[i].first, begin_seqs[i].second);
if (m == 0 || N == 0 || rank < 0 || rank >= N) if (m == 0 || N == 0 || rank < 0 || rank >= N)
{ {
// Result undefined when there is no data or rank is outside bounds. // Result undefined when there is no data or rank is outside bounds.
throw std::exception(); throw std::exception();
} }
difference_type* ns = new difference_type[m]; difference_type* ns = new difference_type[m];
difference_type* a = new difference_type[m]; difference_type* a = new difference_type[m];
difference_type* b = new difference_type[m]; difference_type* b = new difference_type[m];
difference_type l; difference_type l;
ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second); ns[0] = std::distance(begin_seqs[0].first, begin_seqs[0].second);
nmax = ns[0]; nmax = ns[0];
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
{ {
ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second); ns[i] = std::distance(begin_seqs[i].first, begin_seqs[i].second);
nmax = std::max(nmax, ns[i]); 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], // Pad all lists to this length, at least as long as any ns[i],
// equality iff nmax = 2^k - 1 // equality iff nmax = 2^k - 1
l = pow2(r) - 1; l = pow2(r) - 1;
// From now on, including padding. // From now on, including padding.
N = l * m; N = l * m;
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
{ {
a[i] = 0; a[i] = 0;
b[i] = l; b[i] = l;
} }
n = l / 2; n = l / 2;
// Invariants: // Invariants:
// 0 <= a[i] <= ns[i], 0 <= b[i] <= l // 0 <= a[i] <= ns[i], 0 <= b[i] <= l
#define S(i) (begin_seqs[i].first) #define S(i) (begin_seqs[i].first)
// Initial partition. // Initial partition.
std::vector<std::pair<T, int> > sample; std::vector<std::pair<T, int> > sample;
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
if (n < ns[i]) if (n < ns[i])
sample.push_back(std::make_pair(S(i)[n], i)); sample.push_back(std::make_pair(S(i)[n], i));
__gnu_sequential::sort(sample.begin(), sample.end(), lcomp, sequential_tag()); __gnu_sequential::sort(sample.begin(), sample.end(),
lcomp, sequential_tag());
// Conceptual infinity. // Conceptual infinity.
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
if (n >= ns[i]) if (n >= ns[i])
sample.push_back(std::make_pair(S(i)[0] /*dummy element*/, 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; int j;
for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++) for (j = 0; j < localrank && ((n + 1) <= ns[sample[j].second]); j++)
a[sample[j].second] += n + 1; a[sample[j].second] += n + 1;
for (; j < m; j++) for (; j < m; j++)
b[sample[j].second] -= n + 1; b[sample[j].second] -= n + 1;
// Further refinement. // Further refinement.
while (n > 0) while (n > 0)
{ {
n /= 2; n /= 2;
const T* lmax = NULL; const T* lmax = NULL;
for (int i = 0; i < m; i++) 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++)
if (a[i] > 0) 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 i;
{ for (i = 0; i < m; i++)
int source = pq.top().second; {
pq.pop(); 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; difference_type leftsize = 0, total = 0;
b[source] -= n + 1; for (int i = 0; i < m; i++)
{
leftsize += a[i] / (n + 1);
total += l / (n + 1);
}
if (a[source] > 0) difference_type skew = ((unsigned long long)total * rank / N
pq.push(std::make_pair(S(source)[a[source] - 1], source)); - leftsize);
}
}
}
// Postconditions: if (skew > 0)
// a[i] == b[i] in most cases, except when a[i] has been clamped {
// because of having reached the boundary // 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();
// Maximum of left edge, minimum of right edge. a[source] = std::min(a[source] + n + 1, ns[source]);
bool maxleftset = false, minrightset = false; b[source] += n + 1;
// Impossible to avoid the warning? if (b[source] < ns[source])
T maxleft, minright; pq.push(std::make_pair(S(source)[b[source]], source));
for (int i = 0; i < m; i++) }
{ }
if (a[i] > 0) else if (skew < 0)
{ {
if (!maxleftset) // Move to the right, find greatest.
{ std::priority_queue<std::pair<T, int>,
maxleft = S(i)[a[i] - 1]; std::vector<std::pair<T, int> >,
maxleftset = true; lexicographic<T, int, Comparator> > pq(lcomp);
}
else for (int i = 0; i < m; i++)
{ if (a[i] > 0)
// Max. pq.push(std::make_pair(S(i)[a[i] - 1], i));
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]; maxleft = S(i)[a[i] - 1];
} maxleftset = true;
} }
if (b[i] < ns[i]) else
{ {
if (!minrightset) // Max.
{ if (comp(maxleft, S(i)[a[i] - 1]))
minright = S(i)[b[i]]; maxleft = S(i)[a[i] - 1];
minrightset = true; }
} }
else if (b[i] < ns[i])
{ {
// Min. if (!minrightset)
if (comp(S(i)[b[i]], minright)) {
minright = S(i)[b[i]]; 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)) if (!maxleftset || comp(minright, maxleft))
{ {
// Good luck, everything is split unambigiously. // Good luck, everything is split unambigiously.
offset = 0; offset = 0;
} }
else else
{ {
// We have to calculate an offset. // We have to calculate an offset.
offset = 0; offset = 0;
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++)
{ {
difference_type lb = std::lower_bound(S(i), S(i) + ns[i], minright, difference_type lb = std::lower_bound(S(i), S(i) + ns[i],
comp) - S(i); minright,
offset += a[i] - lb; comp) - S(i);
} offset += a[i] - lb;
} }
}
delete[] ns; delete[] ns;
delete[] a; delete[] a;
delete[] b; delete[] b;
return minright; return minright;
} }
} }
#undef S #undef S