3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file parallel/losertree.h
26 * @brief Many generic loser tree variants.
27 * This file is a GNU parallel extension to the Standard C++ Library.
30 // Written by Johannes Singler.
32 #ifndef _GLIBCXX_PARALLEL_LOSERTREE_H
33 #define _GLIBCXX_PARALLEL_LOSERTREE_H 1
35 #include <bits/stl_algobase.h>
36 #include <bits/stl_function.h>
37 #include <parallel/features.h>
38 #include <parallel/base.h>
40 namespace __gnu_parallel
43 * @brief Guarded loser/tournament tree.
45 * The smallest element is at the top.
47 * Guarding is done explicitly through one flag _M_sup per element,
48 * inf is not needed due to a better initialization routine. This
49 * is a well-performing variant.
51 * @param _Tp the element type
52 * @param _Compare the comparator to use, defaults to std::less<_Tp>
54 template<typename _Tp, typename _Compare>
58 /** @brief Internal representation of a _LoserTree element. */
61 /** @brief flag, true iff this is a "maximum" __sentinel. */
63 /** @brief __index of the __source __sequence. */
65 /** @brief _M_key of the element in the _LoserTree. */
69 unsigned int _M_ik, _M_k, _M_offset;
72 unsigned int _M_log_k;
74 /** @brief _LoserTree __elements. */
77 /** @brief _Compare to use. */
81 * @brief State flag that determines whether the _LoserTree is empty.
83 * Only used for building the _LoserTree.
89 * @brief The constructor.
91 * @param __k The number of sequences to merge.
92 * @param __comp The comparator to use.
94 _LoserTreeBase(unsigned int __k, _Compare __comp)
99 // Compute log_2{_M_k} for the _Loser Tree
100 _M_log_k = __rd_log2(_M_ik - 1) + 1;
102 // Next greater power of 2.
103 _M_k = 1 << _M_log_k;
106 // Avoid default-constructing _M_losers[]._M_key
107 _M_losers = static_cast<_Loser*>(::operator new(2 * _M_k
109 for (unsigned int __i = _M_ik - 1; __i < _M_k; ++__i)
110 _M_losers[__i + _M_k]._M_sup = true;
112 _M_first_insert = true;
116 * @brief The destructor.
119 { ::operator delete(_M_losers); }
122 * @brief Initializes the sequence "_M_source" with the element "__key".
124 * @param __key the element to insert
125 * @param __source __index of the __source __sequence
126 * @param __sup flag that determines whether the value to insert is an
127 * explicit __supremum.
130 __insert_start(const _Tp& __key, int __source, bool __sup)
132 unsigned int __pos = _M_k + __source;
136 // Construct all keys, so we can easily deconstruct them.
137 for (unsigned int __i = 0; __i < (2 * _M_k); ++__i)
138 new(&(_M_losers[__i]._M_key)) _Tp(__key);
139 _M_first_insert = false;
142 new(&(_M_losers[__pos]._M_key)) _Tp(__key);
144 _M_losers[__pos]._M_sup = __sup;
145 _M_losers[__pos]._M_source = __source;
149 * @return the index of the sequence with the smallest element.
151 int __get_min_source()
152 { return _M_losers[0]._M_source; }
156 * @brief Stable _LoserTree variant.
158 * Provides the stable implementations of insert_start, __init_winner,
159 * __init and __delete_min_insert.
161 * Unstable variant is done using partial specialisation below.
163 template<bool __stable/* default == true */, typename _Tp,
166 : public _LoserTreeBase<_Tp, _Compare>
168 typedef _LoserTreeBase<_Tp, _Compare> _Base;
170 using _Base::_M_losers;
171 using _Base::_M_first_insert;
174 _LoserTree(unsigned int __k, _Compare __comp)
175 : _Base::_LoserTreeBase(__k, __comp)
179 __init_winner(unsigned int __root)
185 unsigned int __left = __init_winner(2 * __root);
186 unsigned int __right = __init_winner(2 * __root + 1);
187 if (_M_losers[__right]._M_sup
188 || (!_M_losers[__left]._M_sup
189 && !_M_comp(_M_losers[__right]._M_key,
190 _M_losers[__left]._M_key)))
192 // Left one is less or equal.
193 _M_losers[__root] = _M_losers[__right];
198 // Right one is less.
199 _M_losers[__root] = _M_losers[__left];
206 { _M_losers[0] = _M_losers[__init_winner(1)]; }
209 * @brief Delete the smallest element and insert a new element from
210 * the previously smallest element's sequence.
212 * This implementation is stable.
214 // Do not pass a const reference since __key will be used as
217 __delete_min_insert(_Tp __key, bool __sup)
220 #if _GLIBCXX_ASSERTIONS
221 // no dummy sequence can ever be at the top!
222 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
225 int __source = _M_losers[0]._M_source;
226 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
229 // The smaller one gets promoted, ties are broken by _M_source.
230 if ((__sup && (!_M_losers[__pos]._M_sup
231 || _M_losers[__pos]._M_source < __source))
232 || (!__sup && !_M_losers[__pos]._M_sup
233 && ((_M_comp(_M_losers[__pos]._M_key, __key))
234 || (!_M_comp(__key, _M_losers[__pos]._M_key)
235 && _M_losers[__pos]._M_source < __source))))
237 // The other one is smaller.
238 std::swap(_M_losers[__pos]._M_sup, __sup);
239 std::swap(_M_losers[__pos]._M_source, __source);
240 swap(_M_losers[__pos]._M_key, __key);
244 _M_losers[0]._M_sup = __sup;
245 _M_losers[0]._M_source = __source;
246 _M_losers[0]._M_key = __key;
251 * @brief Unstable _LoserTree variant.
253 * Stability (non-stable here) is selected with partial specialization.
255 template<typename _Tp, typename _Compare>
256 class _LoserTree</* __stable == */false, _Tp, _Compare>
257 : public _LoserTreeBase<_Tp, _Compare>
259 typedef _LoserTreeBase<_Tp, _Compare> _Base;
260 using _Base::_M_log_k;
262 using _Base::_M_losers;
263 using _Base::_M_first_insert;
266 _LoserTree(unsigned int __k, _Compare __comp)
267 : _Base::_LoserTreeBase(__k, __comp)
271 * Computes the winner of the competition at position "__root".
273 * Called recursively (starting at 0) to build the initial tree.
275 * @param __root __index of the "game" to start.
278 __init_winner(unsigned int __root)
284 unsigned int __left = __init_winner(2 * __root);
285 unsigned int __right = __init_winner(2 * __root + 1);
286 if (_M_losers[__right]._M_sup
287 || (!_M_losers[__left]._M_sup
288 && !_M_comp(_M_losers[__right]._M_key,
289 _M_losers[__left]._M_key)))
291 // Left one is less or equal.
292 _M_losers[__root] = _M_losers[__right];
297 // Right one is less.
298 _M_losers[__root] = _M_losers[__left];
306 { _M_losers[0] = _M_losers[__init_winner(1)]; }
309 * Delete the _M_key smallest element and insert the element __key
312 * @param __key the _M_key to insert
313 * @param __sup true iff __key is an explicitly marked supremum
315 // Do not pass a const reference since __key will be used as local
318 __delete_min_insert(_Tp __key, bool __sup)
321 #if _GLIBCXX_ASSERTIONS
322 // no dummy sequence can ever be at the top!
323 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
326 int __source = _M_losers[0]._M_source;
327 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
330 // The smaller one gets promoted.
331 if (__sup || (!_M_losers[__pos]._M_sup
332 && _M_comp(_M_losers[__pos]._M_key, __key)))
334 // The other one is smaller.
335 std::swap(_M_losers[__pos]._M_sup, __sup);
336 std::swap(_M_losers[__pos]._M_source, __source);
337 swap(_M_losers[__pos]._M_key, __key);
341 _M_losers[0]._M_sup = __sup;
342 _M_losers[0]._M_source = __source;
343 _M_losers[0]._M_key = __key;
348 * @brief Base class of _Loser Tree implementation using pointers.
350 template<typename _Tp, typename _Compare>
351 class _LoserTreePointerBase
354 /** @brief Internal representation of _LoserTree __elements. */
362 unsigned int _M_ik, _M_k, _M_offset;
367 _LoserTreePointerBase(unsigned int __k,
368 _Compare __comp = std::less<_Tp>())
373 // Next greater power of 2.
374 _M_k = 1 << (__rd_log2(_M_ik - 1) + 1);
376 _M_losers = new _Loser[_M_k * 2];
377 for (unsigned int __i = _M_ik - 1; __i < _M_k; __i++)
378 _M_losers[__i + _M_k]._M_sup = true;
381 ~_LoserTreePointerBase()
382 { ::operator delete[](_M_losers); }
384 int __get_min_source()
385 { return _M_losers[0]._M_source; }
387 void __insert_start(const _Tp& __key, int __source, bool __sup)
389 unsigned int __pos = _M_k + __source;
391 _M_losers[__pos]._M_sup = __sup;
392 _M_losers[__pos]._M_source = __source;
393 _M_losers[__pos]._M_keyp = &__key;
398 * @brief Stable _LoserTree implementation.
400 * The unstable variant is implemented using partial instantiation below.
402 template<bool __stable/* default == true */, typename _Tp, typename _Compare>
403 class _LoserTreePointer
404 : public _LoserTreePointerBase<_Tp, _Compare>
406 typedef _LoserTreePointerBase<_Tp, _Compare> _Base;
408 using _Base::_M_losers;
411 _LoserTreePointer(unsigned int __k, _Compare __comp = std::less<_Tp>())
412 : _Base::_LoserTreePointerBase(__k, __comp)
416 __init_winner(unsigned int __root)
422 unsigned int __left = __init_winner(2 * __root);
423 unsigned int __right = __init_winner(2 * __root + 1);
424 if (_M_losers[__right]._M_sup
425 || (!_M_losers[__left]._M_sup
426 && !_M_comp(*_M_losers[__right]._M_keyp,
427 *_M_losers[__left]._M_keyp)))
429 // Left one is less or equal.
430 _M_losers[__root] = _M_losers[__right];
435 // Right one is less.
436 _M_losers[__root] = _M_losers[__left];
443 { _M_losers[0] = _M_losers[__init_winner(1)]; }
445 void __delete_min_insert(const _Tp& __key, bool __sup)
447 #if _GLIBCXX_ASSERTIONS
448 // no dummy sequence can ever be at the top!
449 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
452 const _Tp* __keyp = &__key;
453 int __source = _M_losers[0]._M_source;
454 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
457 // The smaller one gets promoted, ties are broken by __source.
458 if ((__sup && (!_M_losers[__pos]._M_sup
459 || _M_losers[__pos]._M_source < __source))
460 || (!__sup && !_M_losers[__pos]._M_sup &&
461 ((_M_comp(*_M_losers[__pos]._M_keyp, *__keyp))
462 || (!_M_comp(*__keyp, *_M_losers[__pos]._M_keyp)
463 && _M_losers[__pos]._M_source < __source))))
465 // The other one is smaller.
466 std::swap(_M_losers[__pos]._M_sup, __sup);
467 std::swap(_M_losers[__pos]._M_source, __source);
468 std::swap(_M_losers[__pos]._M_keyp, __keyp);
472 _M_losers[0]._M_sup = __sup;
473 _M_losers[0]._M_source = __source;
474 _M_losers[0]._M_keyp = __keyp;
479 * @brief Unstable _LoserTree implementation.
481 * The stable variant is above.
483 template<typename _Tp, typename _Compare>
484 class _LoserTreePointer</* __stable == */false, _Tp, _Compare>
485 : public _LoserTreePointerBase<_Tp, _Compare>
487 typedef _LoserTreePointerBase<_Tp, _Compare> _Base;
489 using _Base::_M_losers;
492 _LoserTreePointer(unsigned int __k, _Compare __comp = std::less<_Tp>())
493 : _Base::_LoserTreePointerBase(__k, __comp)
497 __init_winner(unsigned int __root)
503 unsigned int __left = __init_winner(2 * __root);
504 unsigned int __right = __init_winner(2 * __root + 1);
505 if (_M_losers[__right]._M_sup
506 || (!_M_losers[__left]._M_sup
507 && !_M_comp(*_M_losers[__right]._M_keyp,
508 *_M_losers[__left]._M_keyp)))
510 // Left one is less or equal.
511 _M_losers[__root] = _M_losers[__right];
516 // Right one is less.
517 _M_losers[__root] = _M_losers[__left];
524 { _M_losers[0] = _M_losers[__init_winner(1)]; }
526 void __delete_min_insert(const _Tp& __key, bool __sup)
528 #if _GLIBCXX_ASSERTIONS
529 // no dummy sequence can ever be at the top!
530 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
533 const _Tp* __keyp = &__key;
534 int __source = _M_losers[0]._M_source;
535 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
538 // The smaller one gets promoted.
539 if (__sup || (!_M_losers[__pos]._M_sup
540 && _M_comp(*_M_losers[__pos]._M_keyp, *__keyp)))
542 // The other one is smaller.
543 std::swap(_M_losers[__pos]._M_sup, __sup);
544 std::swap(_M_losers[__pos]._M_source, __source);
545 std::swap(_M_losers[__pos]._M_keyp, __keyp);
549 _M_losers[0]._M_sup = __sup;
550 _M_losers[0]._M_source = __source;
551 _M_losers[0]._M_keyp = __keyp;
555 /** @brief Base class for unguarded _LoserTree implementation.
557 * The whole element is copied into the tree structure.
559 * No guarding is done, therefore not a single input sequence must
560 * run empty. Unused __sequence heads are marked with a sentinel which
561 * is > all elements that are to be merged.
563 * This is a very fast variant.
565 template<typename _Tp, typename _Compare>
566 class _LoserTreeUnguardedBase
575 unsigned int _M_ik, _M_k, _M_offset;
580 _LoserTreeUnguardedBase(unsigned int __k, const _Tp __sentinel,
581 _Compare __comp = std::less<_Tp>())
586 // Next greater power of 2.
587 _M_k = 1 << (__rd_log2(_M_ik - 1) + 1);
589 // Avoid default-constructing _M_losers[]._M_key
590 _M_losers = static_cast<_Loser*>(::operator new(2 * _M_k
593 for (unsigned int __i = _M_k + _M_ik - 1; __i < (2 * _M_k); ++__i)
595 _M_losers[__i]._M_key = __sentinel;
596 _M_losers[__i]._M_source = -1;
600 ~_LoserTreeUnguardedBase()
601 { ::operator delete(_M_losers); }
606 #if _GLIBCXX_ASSERTIONS
607 // no dummy sequence can ever be at the top!
608 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
610 return _M_losers[0]._M_source;
614 __insert_start(const _Tp& __key, int __source, bool)
616 unsigned int __pos = _M_k + __source;
618 new(&(_M_losers[__pos]._M_key)) _Tp(__key);
619 _M_losers[__pos]._M_source = __source;
624 * @brief Stable implementation of unguarded _LoserTree.
626 * Unstable variant is selected below with partial specialization.
628 template<bool __stable/* default == true */, typename _Tp, typename _Compare>
629 class _LoserTreeUnguarded
630 : public _LoserTreeUnguardedBase<_Tp, _Compare>
632 typedef _LoserTreeUnguardedBase<_Tp, _Compare> _Base;
634 using _Base::_M_losers;
637 _LoserTreeUnguarded(unsigned int __k, const _Tp __sentinel,
638 _Compare __comp = std::less<_Tp>())
639 : _Base::_LoserTreeUnguardedBase(__k, __sentinel, __comp)
643 __init_winner(unsigned int __root)
649 unsigned int __left = __init_winner(2 * __root);
650 unsigned int __right = __init_winner(2 * __root + 1);
651 if (!_M_comp(_M_losers[__right]._M_key,
652 _M_losers[__left]._M_key))
654 // Left one is less or equal.
655 _M_losers[__root] = _M_losers[__right];
660 // Right one is less.
661 _M_losers[__root] = _M_losers[__left];
670 _M_losers[0] = _M_losers[__init_winner(1)];
672 #if _GLIBCXX_ASSERTIONS
673 // no dummy sequence can ever be at the top at the beginning
675 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
679 // Do not pass a const reference since __key will be used as
682 __delete_min_insert(_Tp __key, bool)
685 #if _GLIBCXX_ASSERTIONS
686 // no dummy sequence can ever be at the top!
687 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
690 int __source = _M_losers[0]._M_source;
691 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
694 // The smaller one gets promoted, ties are broken by _M_source.
695 if (_M_comp(_M_losers[__pos]._M_key, __key)
696 || (!_M_comp(__key, _M_losers[__pos]._M_key)
697 && _M_losers[__pos]._M_source < __source))
699 // The other one is smaller.
700 std::swap(_M_losers[__pos]._M_source, __source);
701 swap(_M_losers[__pos]._M_key, __key);
705 _M_losers[0]._M_source = __source;
706 _M_losers[0]._M_key = __key;
711 * @brief Non-Stable implementation of unguarded _LoserTree.
713 * Stable implementation is above.
715 template<typename _Tp, typename _Compare>
716 class _LoserTreeUnguarded</* __stable == */false, _Tp, _Compare>
717 : public _LoserTreeUnguardedBase<_Tp, _Compare>
719 typedef _LoserTreeUnguardedBase<_Tp, _Compare> _Base;
721 using _Base::_M_losers;
724 _LoserTreeUnguarded(unsigned int __k, const _Tp __sentinel,
725 _Compare __comp = std::less<_Tp>())
726 : _Base::_LoserTreeUnguardedBase(__k, __sentinel, __comp)
730 __init_winner(unsigned int __root)
736 unsigned int __left = __init_winner(2 * __root);
737 unsigned int __right = __init_winner(2 * __root + 1);
739 #if _GLIBCXX_ASSERTIONS
740 // If __left one is sentinel then __right one must be, too.
741 if (_M_losers[__left]._M_source == -1)
742 _GLIBCXX_PARALLEL_ASSERT(_M_losers[__right]._M_source == -1);
745 if (!_M_comp(_M_losers[__right]._M_key,
746 _M_losers[__left]._M_key))
748 // Left one is less or equal.
749 _M_losers[__root] = _M_losers[__right];
754 // Right one is less.
755 _M_losers[__root] = _M_losers[__left];
764 _M_losers[0] = _M_losers[__init_winner(1)];
766 #if _GLIBCXX_ASSERTIONS
767 // no dummy sequence can ever be at the top at the beginning
769 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
773 // Do not pass a const reference since __key will be used as
776 __delete_min_insert(_Tp __key, bool)
778 #if _GLIBCXX_ASSERTIONS
779 // no dummy sequence can ever be at the top!
780 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
783 int __source = _M_losers[0]._M_source;
784 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
787 // The smaller one gets promoted.
788 if (_M_comp(_M_losers[__pos]._M_key, __key))
790 // The other one is smaller.
791 std::swap(_M_losers[__pos]._M_source, __source);
792 swap(_M_losers[__pos]._M_key, __key);
796 _M_losers[0]._M_source = __source;
797 _M_losers[0]._M_key = __key;
801 /** @brief Unguarded loser tree, keeping only pointers to the
802 * elements in the tree structure.
804 * No guarding is done, therefore not a single input sequence must
805 * run empty. This is a very fast variant.
807 template<typename _Tp, typename _Compare>
808 class _LoserTreePointerUnguardedBase
817 unsigned int _M_ik, _M_k, _M_offset;
823 _LoserTreePointerUnguardedBase(unsigned int __k, const _Tp& __sentinel,
824 _Compare __comp = std::less<_Tp>())
829 // Next greater power of 2.
830 _M_k = 1 << (__rd_log2(_M_ik - 1) + 1);
832 // Avoid default-constructing _M_losers[]._M_key
833 _M_losers = new _Loser[2 * _M_k];
835 for (unsigned int __i = _M_k + _M_ik - 1; __i < (2 * _M_k); ++__i)
837 _M_losers[__i]._M_keyp = &__sentinel;
838 _M_losers[__i]._M_source = -1;
842 ~_LoserTreePointerUnguardedBase()
843 { delete[] _M_losers; }
848 #if _GLIBCXX_ASSERTIONS
849 // no dummy sequence can ever be at the top!
850 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
852 return _M_losers[0]._M_source;
856 __insert_start(const _Tp& __key, int __source, bool)
858 unsigned int __pos = _M_k + __source;
860 _M_losers[__pos]._M_keyp = &__key;
861 _M_losers[__pos]._M_source = __source;
866 * @brief Stable unguarded _LoserTree variant storing pointers.
868 * Unstable variant is implemented below using partial specialization.
870 template<bool __stable/* default == true */, typename _Tp, typename _Compare>
871 class _LoserTreePointerUnguarded
872 : public _LoserTreePointerUnguardedBase<_Tp, _Compare>
874 typedef _LoserTreePointerUnguardedBase<_Tp, _Compare> _Base;
876 using _Base::_M_losers;
879 _LoserTreePointerUnguarded(unsigned int __k, const _Tp& __sentinel,
880 _Compare __comp = std::less<_Tp>())
881 : _Base::_LoserTreePointerUnguardedBase(__k, __sentinel, __comp)
885 __init_winner(unsigned int __root)
891 unsigned int __left = __init_winner(2 * __root);
892 unsigned int __right = __init_winner(2 * __root + 1);
893 if (!_M_comp(*_M_losers[__right]._M_keyp,
894 *_M_losers[__left]._M_keyp))
896 // Left one is less or equal.
897 _M_losers[__root] = _M_losers[__right];
902 // Right one is less.
903 _M_losers[__root] = _M_losers[__left];
912 _M_losers[0] = _M_losers[__init_winner(1)];
914 #if _GLIBCXX_ASSERTIONS
915 // no dummy sequence can ever be at the top at the beginning
917 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
922 __delete_min_insert(const _Tp& __key, bool __sup)
924 #if _GLIBCXX_ASSERTIONS
925 // no dummy sequence can ever be at the top!
926 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
929 const _Tp* __keyp = &__key;
930 int __source = _M_losers[0]._M_source;
931 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
934 // The smaller one gets promoted, ties are broken by _M_source.
935 if (_M_comp(*_M_losers[__pos]._M_keyp, *__keyp)
936 || (!_M_comp(*__keyp, *_M_losers[__pos]._M_keyp)
937 && _M_losers[__pos]._M_source < __source))
939 // The other one is smaller.
940 std::swap(_M_losers[__pos]._M_source, __source);
941 std::swap(_M_losers[__pos]._M_keyp, __keyp);
945 _M_losers[0]._M_source = __source;
946 _M_losers[0]._M_keyp = __keyp;
951 * @brief Unstable unguarded _LoserTree variant storing pointers.
953 * Stable variant is above.
955 template<typename _Tp, typename _Compare>
956 class _LoserTreePointerUnguarded</* __stable == */false, _Tp, _Compare>
957 : public _LoserTreePointerUnguardedBase<_Tp, _Compare>
959 typedef _LoserTreePointerUnguardedBase<_Tp, _Compare> _Base;
961 using _Base::_M_losers;
964 _LoserTreePointerUnguarded(unsigned int __k, const _Tp& __sentinel,
965 _Compare __comp = std::less<_Tp>())
966 : _Base::_LoserTreePointerUnguardedBase(__k, __sentinel, __comp)
970 __init_winner(unsigned int __root)
976 unsigned int __left = __init_winner(2 * __root);
977 unsigned int __right = __init_winner(2 * __root + 1);
979 #if _GLIBCXX_ASSERTIONS
980 // If __left one is sentinel then __right one must be, too.
981 if (_M_losers[__left]._M_source == -1)
982 _GLIBCXX_PARALLEL_ASSERT(_M_losers[__right]._M_source == -1);
985 if (!_M_comp(*_M_losers[__right]._M_keyp,
986 *_M_losers[__left]._M_keyp))
988 // Left one is less or equal.
989 _M_losers[__root] = _M_losers[__right];
994 // Right one is less.
995 _M_losers[__root] = _M_losers[__left];
1004 _M_losers[0] = _M_losers[__init_winner(1)];
1006 #if _GLIBCXX_ASSERTIONS
1007 // no dummy sequence can ever be at the top at the beginning
1009 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
1014 __delete_min_insert(const _Tp& __key, bool __sup)
1016 #if _GLIBCXX_ASSERTIONS
1017 // no dummy sequence can ever be at the top!
1018 _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
1021 const _Tp* __keyp = &__key;
1022 int __source = _M_losers[0]._M_source;
1023 for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
1026 // The smaller one gets promoted.
1027 if (_M_comp(*(_M_losers[__pos]._M_keyp), *__keyp))
1029 // The other one is smaller.
1030 std::swap(_M_losers[__pos]._M_source, __source);
1031 std::swap(_M_losers[__pos]._M_keyp, __keyp);
1035 _M_losers[0]._M_source = __source;
1036 _M_losers[0]._M_keyp = __keyp;
1039 } // namespace __gnu_parallel
1041 #endif /* _GLIBCXX_PARALLEL_LOSERTREE_H */