32 #ifndef _GLIBCXX_PARALLEL_LOSERTREE_H
33 #define _GLIBCXX_PARALLEL_LOSERTREE_H 1
40 namespace __gnu_parallel
54 template<
typename _Tp,
typename _Compare>
69 unsigned int _M_ik, _M_k, _M_offset;
109 for (
unsigned int __i = _M_ik - 1; __i < _M_k; ++__i)
120 for (
unsigned int __i = 0; __i < (2 * _M_k); ++__i)
136 unsigned int __pos = _M_k + __source;
141 for (
unsigned int __i = 0; __i < (2 * _M_k); ++__i)
142 ::
new(&(
_M_losers[__i]._M_key)) _Tp(__key);
167 template<
bool __stable,
typename _Tp,
183 __init_winner(
unsigned int __root)
189 unsigned int __left = __init_winner(2 * __root);
190 unsigned int __right = __init_winner(2 * __root + 1);
191 if (_M_losers[__right]._M_sup
192 || (!_M_losers[__left]._M_sup
193 && !_M_comp(_M_losers[__right]._M_key,
194 _M_losers[__left]._M_key)))
197 _M_losers[__root] = _M_losers[__right];
203 _M_losers[__root] = _M_losers[__left];
210 { _M_losers[0] = _M_losers[__init_winner(1)]; }
224 #if _GLIBCXX_ASSERTIONS
226 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
229 int __source = _M_losers[0]._M_source;
230 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
234 if ((__sup && (!_M_losers[__pos]._M_sup
235 || _M_losers[__pos]._M_source < __source))
236 || (!__sup && !_M_losers[__pos]._M_sup
237 && ((_M_comp(_M_losers[__pos]._M_key, __key))
238 || (!_M_comp(__key, _M_losers[__pos]._M_key)
239 && _M_losers[__pos]._M_source < __source))))
242 std::swap(_M_losers[__pos]._M_sup, __sup);
243 std::swap(_M_losers[__pos]._M_source, __source);
244 swap(_M_losers[__pos]._M_key, __key);
248 _M_losers[0]._M_sup = __sup;
249 _M_losers[0]._M_source = __source;
250 _M_losers[0]._M_key = __key;
259 template<
typename _Tp,
typename _Compare>
264 using _Base::_M_log_k;
266 using _Base::_M_losers;
267 using _Base::_M_first_insert;
271 : _Base::_LoserTreeBase(__k, __comp)
288 unsigned int __left = __init_winner(2 * __root);
289 unsigned int __right = __init_winner(2 * __root + 1);
290 if (_M_losers[__right]._M_sup
291 || (!_M_losers[__left]._M_sup
292 && !_M_comp(_M_losers[__right]._M_key,
293 _M_losers[__left]._M_key)))
296 _M_losers[__root] = _M_losers[__right];
302 _M_losers[__root] = _M_losers[__left];
310 { _M_losers[0] = _M_losers[__init_winner(1)]; }
325 #if _GLIBCXX_ASSERTIONS
327 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
330 int __source = _M_losers[0]._M_source;
331 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
335 if (__sup || (!_M_losers[__pos]._M_sup
336 && _M_comp(_M_losers[__pos]._M_key, __key)))
339 std::swap(_M_losers[__pos]._M_sup, __sup);
340 std::swap(_M_losers[__pos]._M_source, __source);
341 swap(_M_losers[__pos]._M_key, __key);
345 _M_losers[0]._M_sup = __sup;
346 _M_losers[0]._M_source = __source;
347 _M_losers[0]._M_key = __key;
354 template<
typename _Tp,
typename _Compare>
366 unsigned int _M_ik, _M_k, _M_offset;
380 _M_losers =
new _Loser[_M_k * 2];
381 for (
unsigned int __i = _M_ik - 1; __i < _M_k; __i++)
382 _M_losers[__i + _M_k]._M_sup =
true;
386 {
delete[] _M_losers; }
388 int __get_min_source()
389 {
return _M_losers[0]._M_source; }
391 void __insert_start(
const _Tp& __key,
int __source,
bool __sup)
393 unsigned int __pos = _M_k + __source;
395 _M_losers[__pos]._M_sup = __sup;
396 _M_losers[__pos]._M_source = __source;
397 _M_losers[__pos]._M_keyp = &__key;
406 template<
bool __stable,
typename _Tp,
typename _Compare>
412 using _Base::_M_losers;
416 : _Base::_LoserTreePointerBase(__k, __comp)
420 __init_winner(
unsigned int __root)
426 unsigned int __left = __init_winner(2 * __root);
427 unsigned int __right = __init_winner(2 * __root + 1);
428 if (_M_losers[__right]._M_sup
429 || (!_M_losers[__left]._M_sup
430 && !_M_comp(*_M_losers[__right]._M_keyp,
431 *_M_losers[__left]._M_keyp)))
434 _M_losers[__root] = _M_losers[__right];
440 _M_losers[__root] = _M_losers[__left];
447 { _M_losers[0] = _M_losers[__init_winner(1)]; }
449 void __delete_min_insert(
const _Tp& __key,
bool __sup)
451 #if _GLIBCXX_ASSERTIONS
453 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
456 const _Tp* __keyp = &__key;
457 int __source = _M_losers[0]._M_source;
458 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
462 if ((__sup && (!_M_losers[__pos]._M_sup
463 || _M_losers[__pos]._M_source < __source))
464 || (!__sup && !_M_losers[__pos]._M_sup &&
465 ((_M_comp(*_M_losers[__pos]._M_keyp, *__keyp))
466 || (!_M_comp(*__keyp, *_M_losers[__pos]._M_keyp)
467 && _M_losers[__pos]._M_source < __source))))
470 std::swap(_M_losers[__pos]._M_sup, __sup);
471 std::swap(_M_losers[__pos]._M_source, __source);
472 std::swap(_M_losers[__pos]._M_keyp, __keyp);
476 _M_losers[0]._M_sup = __sup;
477 _M_losers[0]._M_source = __source;
478 _M_losers[0]._M_keyp = __keyp;
487 template<
typename _Tp,
typename _Compare>
493 using _Base::_M_losers;
497 : _Base::_LoserTreePointerBase(__k, __comp)
501 __init_winner(
unsigned int __root)
507 unsigned int __left = __init_winner(2 * __root);
508 unsigned int __right = __init_winner(2 * __root + 1);
509 if (_M_losers[__right]._M_sup
510 || (!_M_losers[__left]._M_sup
511 && !_M_comp(*_M_losers[__right]._M_keyp,
512 *_M_losers[__left]._M_keyp)))
515 _M_losers[__root] = _M_losers[__right];
521 _M_losers[__root] = _M_losers[__left];
528 { _M_losers[0] = _M_losers[__init_winner(1)]; }
530 void __delete_min_insert(
const _Tp& __key,
bool __sup)
532 #if _GLIBCXX_ASSERTIONS
534 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
537 const _Tp* __keyp = &__key;
538 int __source = _M_losers[0]._M_source;
539 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
543 if (__sup || (!_M_losers[__pos]._M_sup
544 && _M_comp(*_M_losers[__pos]._M_keyp, *__keyp)))
547 std::swap(_M_losers[__pos]._M_sup, __sup);
548 std::swap(_M_losers[__pos]._M_source, __source);
549 std::swap(_M_losers[__pos]._M_keyp, __keyp);
553 _M_losers[0]._M_sup = __sup;
554 _M_losers[0]._M_source = __source;
555 _M_losers[0]._M_keyp = __keyp;
569 template<
typename _Tp,
typename _Compare>
579 unsigned int _M_ik, _M_k, _M_offset;
594 _M_losers =
static_cast<_Loser*
>(::operator
new(2 * _M_k
597 for (
unsigned int __i = 0; __i < _M_k; ++__i)
599 ::new(&(_M_losers[__i]._M_key)) _Tp(__sentinel);
600 _M_losers[__i]._M_source = -1;
602 for (
unsigned int __i = _M_k + _M_ik - 1; __i < (2 * _M_k); ++__i)
604 ::new(&(_M_losers[__i]._M_key)) _Tp(__sentinel);
605 _M_losers[__i]._M_source = -1;
611 for (
unsigned int __i = 0; __i < (2 * _M_k); ++__i)
612 _M_losers[__i].~_Loser();
613 ::operator
delete(_M_losers);
619 #if _GLIBCXX_ASSERTIONS
621 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
623 return _M_losers[0]._M_source;
627 __insert_start(
const _Tp& __key,
int __source,
bool)
629 unsigned int __pos = _M_k + __source;
631 ::new(&(_M_losers[__pos]._M_key)) _Tp(__key);
632 _M_losers[__pos]._M_source = __source;
641 template<
bool __stable,
typename _Tp,
typename _Compare>
647 using _Base::_M_losers;
652 : _Base::_LoserTreeUnguardedBase(__k, __sentinel, __comp)
656 __init_winner(
unsigned int __root)
662 unsigned int __left = __init_winner(2 * __root);
663 unsigned int __right = __init_winner(2 * __root + 1);
664 if (!_M_comp(_M_losers[__right]._M_key,
665 _M_losers[__left]._M_key))
668 _M_losers[__root] = _M_losers[__right];
674 _M_losers[__root] = _M_losers[__left];
683 _M_losers[0] = _M_losers[__init_winner(1)];
685 #if _GLIBCXX_ASSERTIONS
688 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
695 __delete_min_insert(_Tp __key,
bool)
698 #if _GLIBCXX_ASSERTIONS
700 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
703 int __source = _M_losers[0]._M_source;
704 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
708 if (_M_comp(_M_losers[__pos]._M_key, __key)
709 || (!_M_comp(__key, _M_losers[__pos]._M_key)
710 && _M_losers[__pos]._M_source < __source))
713 std::swap(_M_losers[__pos]._M_source, __source);
714 swap(_M_losers[__pos]._M_key, __key);
718 _M_losers[0]._M_source = __source;
719 _M_losers[0]._M_key = __key;
728 template<
typename _Tp,
typename _Compare>
734 using _Base::_M_losers;
739 : _Base::_LoserTreeUnguardedBase(__k, __sentinel, __comp)
743 __init_winner(
unsigned int __root)
749 unsigned int __left = __init_winner(2 * __root);
750 unsigned int __right = __init_winner(2 * __root + 1);
752 #if _GLIBCXX_ASSERTIONS
754 if (_M_losers[__left]._M_source == -1)
755 _GLIBCXX_PARALLEL_ASSERT(_M_losers[__right]._M_source == -1);
758 if (!_M_comp(_M_losers[__right]._M_key,
759 _M_losers[__left]._M_key))
762 _M_losers[__root] = _M_losers[__right];
768 _M_losers[__root] = _M_losers[__left];
777 _M_losers[0] = _M_losers[__init_winner(1)];
779 #if _GLIBCXX_ASSERTIONS
782 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
789 __delete_min_insert(_Tp __key,
bool)
792 #if _GLIBCXX_ASSERTIONS
794 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
797 int __source = _M_losers[0]._M_source;
798 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
802 if (_M_comp(_M_losers[__pos]._M_key, __key))
805 std::swap(_M_losers[__pos]._M_source, __source);
806 swap(_M_losers[__pos]._M_key, __key);
810 _M_losers[0]._M_source = __source;
811 _M_losers[0]._M_key = __key;
821 template<
typename _Tp,
typename _Compare>
831 unsigned int _M_ik, _M_k, _M_offset;
847 _M_losers =
new _Loser[2 * _M_k];
849 for (
unsigned int __i = _M_k + _M_ik - 1; __i < (2 * _M_k); ++__i)
851 _M_losers[__i]._M_keyp = &__sentinel;
852 _M_losers[__i]._M_source = -1;
857 {
delete[] _M_losers; }
862 #if _GLIBCXX_ASSERTIONS
864 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
866 return _M_losers[0]._M_source;
870 __insert_start(
const _Tp& __key,
int __source,
bool)
872 unsigned int __pos = _M_k + __source;
874 _M_losers[__pos]._M_keyp = &__key;
875 _M_losers[__pos]._M_source = __source;
884 template<
bool __stable,
typename _Tp,
typename _Compare>
890 using _Base::_M_losers;
895 : _Base::_LoserTreePointerUnguardedBase(__k, __sentinel, __comp)
899 __init_winner(
unsigned int __root)
905 unsigned int __left = __init_winner(2 * __root);
906 unsigned int __right = __init_winner(2 * __root + 1);
907 if (!_M_comp(*_M_losers[__right]._M_keyp,
908 *_M_losers[__left]._M_keyp))
911 _M_losers[__root] = _M_losers[__right];
917 _M_losers[__root] = _M_losers[__left];
926 _M_losers[0] = _M_losers[__init_winner(1)];
928 #if _GLIBCXX_ASSERTIONS
931 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
936 __delete_min_insert(
const _Tp& __key,
bool __sup)
938 #if _GLIBCXX_ASSERTIONS
940 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
943 const _Tp* __keyp = &__key;
944 int __source = _M_losers[0]._M_source;
945 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
949 if (_M_comp(*_M_losers[__pos]._M_keyp, *__keyp)
950 || (!_M_comp(*__keyp, *_M_losers[__pos]._M_keyp)
951 && _M_losers[__pos]._M_source < __source))
954 std::swap(_M_losers[__pos]._M_source, __source);
955 std::swap(_M_losers[__pos]._M_keyp, __keyp);
959 _M_losers[0]._M_source = __source;
960 _M_losers[0]._M_keyp = __keyp;
969 template<
typename _Tp,
typename _Compare>
975 using _Base::_M_losers;
980 : _Base::_LoserTreePointerUnguardedBase(__k, __sentinel, __comp)
984 __init_winner(
unsigned int __root)
990 unsigned int __left = __init_winner(2 * __root);
991 unsigned int __right = __init_winner(2 * __root + 1);
993 #if _GLIBCXX_ASSERTIONS
995 if (_M_losers[__left]._M_source == -1)
996 _GLIBCXX_PARALLEL_ASSERT(_M_losers[__right]._M_source == -1);
999 if (!_M_comp(*_M_losers[__right]._M_keyp,
1000 *_M_losers[__left]._M_keyp))
1003 _M_losers[__root] = _M_losers[__right];
1009 _M_losers[__root] = _M_losers[__left];
1018 _M_losers[0] = _M_losers[__init_winner(1)];
1020 #if _GLIBCXX_ASSERTIONS
1023 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
1028 __delete_min_insert(
const _Tp& __key,
bool __sup)
1030 #if _GLIBCXX_ASSERTIONS
1032 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
1035 const _Tp* __keyp = &__key;
1036 int __source = _M_losers[0]._M_source;
1037 for (
unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
1041 if (_M_comp(*(_M_losers[__pos]._M_keyp), *__keyp))
1044 std::swap(_M_losers[__pos]._M_source, __source);
1045 std::swap(_M_losers[__pos]._M_keyp, __keyp);
1049 _M_losers[0]._M_source = __source;
1050 _M_losers[0]._M_keyp = __keyp;
Internal representation of _LoserTree __elements.
bool _M_first_insert
State flag that determines whether the _LoserTree is empty.
Stable implementation of unguarded _LoserTree.
Unguarded loser tree, keeping only pointers to the elements in the tree structure.
_Compare _M_comp
_Compare to use.
Internal representation of a _LoserTree element.
void __delete_min_insert(_Tp __key, bool __sup)
Delete the smallest element and insert a new element from the previously smallest element's sequence...
_Tp _M_key
_M_key of the element in the _LoserTree.
Defines on whether to include algorithm variants.
void __insert_start(const _Tp &__key, int __source, bool __sup)
Initializes the sequence "_M_source" with the element "__key".
unsigned int __init_winner(unsigned int __root)
Base class of _Loser Tree implementation using pointers.
_Size __rd_log2(_Size __n)
Calculates the rounded-down logarithm of __n for base 2.
~_LoserTreeBase()
The destructor.
bool _M_sup
flag, true iff this is a "maximum" __sentinel.
Base class for unguarded _LoserTree implementation.
Guarded loser/tournament tree.
Stable _LoserTree variant.
_LoserTreeBase(unsigned int __k, _Compare __comp)
The constructor.
Stable unguarded _LoserTree variant storing pointers.
int _M_source
__index of the __source __sequence.
void __delete_min_insert(_Tp __key, bool __sup)
One of the comparison functors.
_Loser * _M_losers
_LoserTree __elements.
Sequential helper functions. This file is a GNU parallel extension to the Standard C++ Library...
Stable _LoserTree implementation.