3 // Copyright (C) 2007-2018 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.
120 for (unsigned int __i
= 0; __i
< (2 * _M_k
); ++__i
)
121 _M_losers
[__i
].~_Loser();
122 ::operator delete(_M_losers
);
126 * @brief Initializes the sequence "_M_source" with the element "__key".
128 * @param __key the element to insert
129 * @param __source __index of the __source __sequence
130 * @param __sup flag that determines whether the value to insert is an
131 * explicit __supremum.
134 __insert_start(const _Tp
& __key
, int __source
, bool __sup
)
136 unsigned int __pos
= _M_k
+ __source
;
140 // Construct all keys, so we can easily destruct them.
141 for (unsigned int __i
= 0; __i
< (2 * _M_k
); ++__i
)
142 ::new(&(_M_losers
[__i
]._M_key
)) _Tp(__key
);
143 _M_first_insert
= false;
146 _M_losers
[__pos
]._M_key
= __key
;
148 _M_losers
[__pos
]._M_sup
= __sup
;
149 _M_losers
[__pos
]._M_source
= __source
;
153 * @return the index of the sequence with the smallest element.
155 int __get_min_source()
156 { return _M_losers
[0]._M_source
; }
160 * @brief Stable _LoserTree variant.
162 * Provides the stable implementations of insert_start, __init_winner,
163 * __init and __delete_min_insert.
165 * Unstable variant is done using partial specialisation below.
167 template<bool __stable
/* default == true */, typename _Tp
,
170 : public _LoserTreeBase
<_Tp
, _Compare
>
172 typedef _LoserTreeBase
<_Tp
, _Compare
> _Base
;
174 using _Base::_M_comp
;
175 using _Base::_M_losers
;
176 using _Base::_M_first_insert
;
179 _LoserTree(unsigned int __k
, _Compare __comp
)
180 : _Base::_LoserTreeBase(__k
, __comp
)
184 __init_winner(unsigned int __root
)
190 unsigned int __left
= __init_winner(2 * __root
);
191 unsigned int __right
= __init_winner(2 * __root
+ 1);
192 if (_M_losers
[__right
]._M_sup
193 || (!_M_losers
[__left
]._M_sup
194 && !_M_comp(_M_losers
[__right
]._M_key
,
195 _M_losers
[__left
]._M_key
)))
197 // Left one is less or equal.
198 _M_losers
[__root
] = _M_losers
[__right
];
203 // Right one is less.
204 _M_losers
[__root
] = _M_losers
[__left
];
211 { _M_losers
[0] = _M_losers
[__init_winner(1)]; }
214 * @brief Delete the smallest element and insert a new element from
215 * the previously smallest element's sequence.
217 * This implementation is stable.
219 // Do not pass a const reference since __key will be used as
222 __delete_min_insert(_Tp __key
, bool __sup
)
225 #if _GLIBCXX_PARALLEL_ASSERTIONS
226 // no dummy sequence can ever be at the top!
227 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
230 int __source
= _M_losers
[0]._M_source
;
231 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
234 // The smaller one gets promoted, ties are broken by _M_source.
235 if ((__sup
&& (!_M_losers
[__pos
]._M_sup
236 || _M_losers
[__pos
]._M_source
< __source
))
237 || (!__sup
&& !_M_losers
[__pos
]._M_sup
238 && ((_M_comp(_M_losers
[__pos
]._M_key
, __key
))
239 || (!_M_comp(__key
, _M_losers
[__pos
]._M_key
)
240 && _M_losers
[__pos
]._M_source
< __source
))))
242 // The other one is smaller.
243 std::swap(_M_losers
[__pos
]._M_sup
, __sup
);
244 std::swap(_M_losers
[__pos
]._M_source
, __source
);
245 swap(_M_losers
[__pos
]._M_key
, __key
);
249 _M_losers
[0]._M_sup
= __sup
;
250 _M_losers
[0]._M_source
= __source
;
251 _M_losers
[0]._M_key
= __key
;
256 * @brief Unstable _LoserTree variant.
258 * Stability (non-stable here) is selected with partial specialization.
260 template<typename _Tp
, typename _Compare
>
261 class _LoserTree
</* __stable == */false, _Tp
, _Compare
>
262 : public _LoserTreeBase
<_Tp
, _Compare
>
264 typedef _LoserTreeBase
<_Tp
, _Compare
> _Base
;
265 using _Base::_M_log_k
;
267 using _Base::_M_comp
;
268 using _Base::_M_losers
;
269 using _Base::_M_first_insert
;
272 _LoserTree(unsigned int __k
, _Compare __comp
)
273 : _Base::_LoserTreeBase(__k
, __comp
)
277 * Computes the winner of the competition at position "__root".
279 * Called recursively (starting at 0) to build the initial tree.
281 * @param __root __index of the "game" to start.
284 __init_winner(unsigned int __root
)
290 unsigned int __left
= __init_winner(2 * __root
);
291 unsigned int __right
= __init_winner(2 * __root
+ 1);
292 if (_M_losers
[__right
]._M_sup
293 || (!_M_losers
[__left
]._M_sup
294 && !_M_comp(_M_losers
[__right
]._M_key
,
295 _M_losers
[__left
]._M_key
)))
297 // Left one is less or equal.
298 _M_losers
[__root
] = _M_losers
[__right
];
303 // Right one is less.
304 _M_losers
[__root
] = _M_losers
[__left
];
312 { _M_losers
[0] = _M_losers
[__init_winner(1)]; }
315 * Delete the _M_key smallest element and insert the element __key
318 * @param __key the _M_key to insert
319 * @param __sup true iff __key is an explicitly marked supremum
321 // Do not pass a const reference since __key will be used as local
324 __delete_min_insert(_Tp __key
, bool __sup
)
327 #if _GLIBCXX_PARALLEL_ASSERTIONS
328 // no dummy sequence can ever be at the top!
329 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
332 int __source
= _M_losers
[0]._M_source
;
333 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
336 // The smaller one gets promoted.
337 if (__sup
|| (!_M_losers
[__pos
]._M_sup
338 && _M_comp(_M_losers
[__pos
]._M_key
, __key
)))
340 // The other one is smaller.
341 std::swap(_M_losers
[__pos
]._M_sup
, __sup
);
342 std::swap(_M_losers
[__pos
]._M_source
, __source
);
343 swap(_M_losers
[__pos
]._M_key
, __key
);
347 _M_losers
[0]._M_sup
= __sup
;
348 _M_losers
[0]._M_source
= __source
;
349 _M_losers
[0]._M_key
= __key
;
354 * @brief Base class of _Loser Tree implementation using pointers.
356 template<typename _Tp
, typename _Compare
>
357 class _LoserTreePointerBase
360 /** @brief Internal representation of _LoserTree __elements. */
368 unsigned int _M_ik
, _M_k
, _M_offset
;
373 _LoserTreePointerBase(unsigned int __k
,
374 _Compare __comp
= std::less
<_Tp
>())
379 // Next greater power of 2.
380 _M_k
= 1 << (__rd_log2(_M_ik
- 1) + 1);
382 _M_losers
= new _Loser
[_M_k
* 2];
383 for (unsigned int __i
= _M_ik
- 1; __i
< _M_k
; __i
++)
384 _M_losers
[__i
+ _M_k
]._M_sup
= true;
387 ~_LoserTreePointerBase()
388 { delete[] _M_losers
; }
390 int __get_min_source()
391 { return _M_losers
[0]._M_source
; }
393 void __insert_start(const _Tp
& __key
, int __source
, bool __sup
)
395 unsigned int __pos
= _M_k
+ __source
;
397 _M_losers
[__pos
]._M_sup
= __sup
;
398 _M_losers
[__pos
]._M_source
= __source
;
399 _M_losers
[__pos
]._M_keyp
= &__key
;
404 * @brief Stable _LoserTree implementation.
406 * The unstable variant is implemented using partial instantiation below.
408 template<bool __stable
/* default == true */, typename _Tp
, typename _Compare
>
409 class _LoserTreePointer
410 : public _LoserTreePointerBase
<_Tp
, _Compare
>
412 typedef _LoserTreePointerBase
<_Tp
, _Compare
> _Base
;
414 using _Base::_M_comp
;
415 using _Base::_M_losers
;
418 _LoserTreePointer(unsigned int __k
, _Compare __comp
= std::less
<_Tp
>())
419 : _Base::_LoserTreePointerBase(__k
, __comp
)
423 __init_winner(unsigned int __root
)
429 unsigned int __left
= __init_winner(2 * __root
);
430 unsigned int __right
= __init_winner(2 * __root
+ 1);
431 if (_M_losers
[__right
]._M_sup
432 || (!_M_losers
[__left
]._M_sup
433 && !_M_comp(*_M_losers
[__right
]._M_keyp
,
434 *_M_losers
[__left
]._M_keyp
)))
436 // Left one is less or equal.
437 _M_losers
[__root
] = _M_losers
[__right
];
442 // Right one is less.
443 _M_losers
[__root
] = _M_losers
[__left
];
450 { _M_losers
[0] = _M_losers
[__init_winner(1)]; }
452 void __delete_min_insert(const _Tp
& __key
, bool __sup
)
454 #if _GLIBCXX_PARALLEL_ASSERTIONS
455 // no dummy sequence can ever be at the top!
456 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
459 const _Tp
* __keyp
= &__key
;
460 int __source
= _M_losers
[0]._M_source
;
461 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
464 // The smaller one gets promoted, ties are broken by __source.
465 if ((__sup
&& (!_M_losers
[__pos
]._M_sup
466 || _M_losers
[__pos
]._M_source
< __source
))
467 || (!__sup
&& !_M_losers
[__pos
]._M_sup
&&
468 ((_M_comp(*_M_losers
[__pos
]._M_keyp
, *__keyp
))
469 || (!_M_comp(*__keyp
, *_M_losers
[__pos
]._M_keyp
)
470 && _M_losers
[__pos
]._M_source
< __source
))))
472 // The other one is smaller.
473 std::swap(_M_losers
[__pos
]._M_sup
, __sup
);
474 std::swap(_M_losers
[__pos
]._M_source
, __source
);
475 std::swap(_M_losers
[__pos
]._M_keyp
, __keyp
);
479 _M_losers
[0]._M_sup
= __sup
;
480 _M_losers
[0]._M_source
= __source
;
481 _M_losers
[0]._M_keyp
= __keyp
;
486 * @brief Unstable _LoserTree implementation.
488 * The stable variant is above.
490 template<typename _Tp
, typename _Compare
>
491 class _LoserTreePointer
</* __stable == */false, _Tp
, _Compare
>
492 : public _LoserTreePointerBase
<_Tp
, _Compare
>
494 typedef _LoserTreePointerBase
<_Tp
, _Compare
> _Base
;
496 using _Base::_M_comp
;
497 using _Base::_M_losers
;
500 _LoserTreePointer(unsigned int __k
, _Compare __comp
= std::less
<_Tp
>())
501 : _Base::_LoserTreePointerBase(__k
, __comp
)
505 __init_winner(unsigned int __root
)
511 unsigned int __left
= __init_winner(2 * __root
);
512 unsigned int __right
= __init_winner(2 * __root
+ 1);
513 if (_M_losers
[__right
]._M_sup
514 || (!_M_losers
[__left
]._M_sup
515 && !_M_comp(*_M_losers
[__right
]._M_keyp
,
516 *_M_losers
[__left
]._M_keyp
)))
518 // Left one is less or equal.
519 _M_losers
[__root
] = _M_losers
[__right
];
524 // Right one is less.
525 _M_losers
[__root
] = _M_losers
[__left
];
532 { _M_losers
[0] = _M_losers
[__init_winner(1)]; }
534 void __delete_min_insert(const _Tp
& __key
, bool __sup
)
536 #if _GLIBCXX_PARALLEL_ASSERTIONS
537 // no dummy sequence can ever be at the top!
538 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
541 const _Tp
* __keyp
= &__key
;
542 int __source
= _M_losers
[0]._M_source
;
543 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
546 // The smaller one gets promoted.
547 if (__sup
|| (!_M_losers
[__pos
]._M_sup
548 && _M_comp(*_M_losers
[__pos
]._M_keyp
, *__keyp
)))
550 // The other one is smaller.
551 std::swap(_M_losers
[__pos
]._M_sup
, __sup
);
552 std::swap(_M_losers
[__pos
]._M_source
, __source
);
553 std::swap(_M_losers
[__pos
]._M_keyp
, __keyp
);
557 _M_losers
[0]._M_sup
= __sup
;
558 _M_losers
[0]._M_source
= __source
;
559 _M_losers
[0]._M_keyp
= __keyp
;
563 /** @brief Base class for unguarded _LoserTree implementation.
565 * The whole element is copied into the tree structure.
567 * No guarding is done, therefore not a single input sequence must
568 * run empty. Unused __sequence heads are marked with a sentinel which
569 * is > all elements that are to be merged.
571 * This is a very fast variant.
573 template<typename _Tp
, typename _Compare
>
574 class _LoserTreeUnguardedBase
583 unsigned int _M_ik
, _M_k
, _M_offset
;
588 _LoserTreeUnguardedBase(unsigned int __k
, const _Tp
& __sentinel
,
589 _Compare __comp
= std::less
<_Tp
>())
594 // Next greater power of 2.
595 _M_k
= 1 << (__rd_log2(_M_ik
- 1) + 1);
597 // Avoid default-constructing _M_losers[]._M_key
598 _M_losers
= static_cast<_Loser
*>(::operator new(2 * _M_k
601 for (unsigned int __i
= 0; __i
< _M_k
; ++__i
)
603 ::new(&(_M_losers
[__i
]._M_key
)) _Tp(__sentinel
);
604 _M_losers
[__i
]._M_source
= -1;
606 for (unsigned int __i
= _M_k
+ _M_ik
- 1; __i
< (2 * _M_k
); ++__i
)
608 ::new(&(_M_losers
[__i
]._M_key
)) _Tp(__sentinel
);
609 _M_losers
[__i
]._M_source
= -1;
613 ~_LoserTreeUnguardedBase()
615 for (unsigned int __i
= 0; __i
< (2 * _M_k
); ++__i
)
616 _M_losers
[__i
].~_Loser();
617 ::operator delete(_M_losers
);
623 #if _GLIBCXX_PARALLEL_ASSERTIONS
624 // no dummy sequence can ever be at the top!
625 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
627 return _M_losers
[0]._M_source
;
631 __insert_start(const _Tp
& __key
, int __source
, bool)
633 unsigned int __pos
= _M_k
+ __source
;
635 ::new(&(_M_losers
[__pos
]._M_key
)) _Tp(__key
);
636 _M_losers
[__pos
]._M_source
= __source
;
641 * @brief Stable implementation of unguarded _LoserTree.
643 * Unstable variant is selected below with partial specialization.
645 template<bool __stable
/* default == true */, typename _Tp
, typename _Compare
>
646 class _LoserTreeUnguarded
647 : public _LoserTreeUnguardedBase
<_Tp
, _Compare
>
649 typedef _LoserTreeUnguardedBase
<_Tp
, _Compare
> _Base
;
651 using _Base::_M_comp
;
652 using _Base::_M_losers
;
655 _LoserTreeUnguarded(unsigned int __k
, const _Tp
& __sentinel
,
656 _Compare __comp
= std::less
<_Tp
>())
657 : _Base::_LoserTreeUnguardedBase(__k
, __sentinel
, __comp
)
661 __init_winner(unsigned int __root
)
667 unsigned int __left
= __init_winner(2 * __root
);
668 unsigned int __right
= __init_winner(2 * __root
+ 1);
669 if (!_M_comp(_M_losers
[__right
]._M_key
,
670 _M_losers
[__left
]._M_key
))
672 // Left one is less or equal.
673 _M_losers
[__root
] = _M_losers
[__right
];
678 // Right one is less.
679 _M_losers
[__root
] = _M_losers
[__left
];
688 _M_losers
[0] = _M_losers
[__init_winner(1)];
690 #if _GLIBCXX_PARALLEL_ASSERTIONS
691 // no dummy sequence can ever be at the top at the beginning
693 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
697 // Do not pass a const reference since __key will be used as
700 __delete_min_insert(_Tp __key
, bool)
703 #if _GLIBCXX_PARALLEL_ASSERTIONS
704 // no dummy sequence can ever be at the top!
705 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
708 int __source
= _M_losers
[0]._M_source
;
709 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
712 // The smaller one gets promoted, ties are broken by _M_source.
713 if (_M_comp(_M_losers
[__pos
]._M_key
, __key
)
714 || (!_M_comp(__key
, _M_losers
[__pos
]._M_key
)
715 && _M_losers
[__pos
]._M_source
< __source
))
717 // The other one is smaller.
718 std::swap(_M_losers
[__pos
]._M_source
, __source
);
719 swap(_M_losers
[__pos
]._M_key
, __key
);
723 _M_losers
[0]._M_source
= __source
;
724 _M_losers
[0]._M_key
= __key
;
729 * @brief Non-Stable implementation of unguarded _LoserTree.
731 * Stable implementation is above.
733 template<typename _Tp
, typename _Compare
>
734 class _LoserTreeUnguarded
</* __stable == */false, _Tp
, _Compare
>
735 : public _LoserTreeUnguardedBase
<_Tp
, _Compare
>
737 typedef _LoserTreeUnguardedBase
<_Tp
, _Compare
> _Base
;
739 using _Base::_M_comp
;
740 using _Base::_M_losers
;
743 _LoserTreeUnguarded(unsigned int __k
, const _Tp
& __sentinel
,
744 _Compare __comp
= std::less
<_Tp
>())
745 : _Base::_LoserTreeUnguardedBase(__k
, __sentinel
, __comp
)
749 __init_winner(unsigned int __root
)
755 unsigned int __left
= __init_winner(2 * __root
);
756 unsigned int __right
= __init_winner(2 * __root
+ 1);
758 #if _GLIBCXX_PARALLEL_ASSERTIONS
759 // If __left one is sentinel then __right one must be, too.
760 if (_M_losers
[__left
]._M_source
== -1)
761 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[__right
]._M_source
== -1);
764 if (!_M_comp(_M_losers
[__right
]._M_key
,
765 _M_losers
[__left
]._M_key
))
767 // Left one is less or equal.
768 _M_losers
[__root
] = _M_losers
[__right
];
773 // Right one is less.
774 _M_losers
[__root
] = _M_losers
[__left
];
783 _M_losers
[0] = _M_losers
[__init_winner(1)];
785 #if _GLIBCXX_PARALLEL_ASSERTIONS
786 // no dummy sequence can ever be at the top at the beginning
788 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
792 // Do not pass a const reference since __key will be used as
795 __delete_min_insert(_Tp __key
, bool)
798 #if _GLIBCXX_PARALLEL_ASSERTIONS
799 // no dummy sequence can ever be at the top!
800 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
803 int __source
= _M_losers
[0]._M_source
;
804 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
807 // The smaller one gets promoted.
808 if (_M_comp(_M_losers
[__pos
]._M_key
, __key
))
810 // The other one is smaller.
811 std::swap(_M_losers
[__pos
]._M_source
, __source
);
812 swap(_M_losers
[__pos
]._M_key
, __key
);
816 _M_losers
[0]._M_source
= __source
;
817 _M_losers
[0]._M_key
= __key
;
821 /** @brief Unguarded loser tree, keeping only pointers to the
822 * elements in the tree structure.
824 * No guarding is done, therefore not a single input sequence must
825 * run empty. This is a very fast variant.
827 template<typename _Tp
, typename _Compare
>
828 class _LoserTreePointerUnguardedBase
837 unsigned int _M_ik
, _M_k
, _M_offset
;
843 _LoserTreePointerUnguardedBase(unsigned int __k
, const _Tp
& __sentinel
,
844 _Compare __comp
= std::less
<_Tp
>())
849 // Next greater power of 2.
850 _M_k
= 1 << (__rd_log2(_M_ik
- 1) + 1);
852 // Avoid default-constructing _M_losers[]._M_key
853 _M_losers
= new _Loser
[2 * _M_k
];
855 for (unsigned int __i
= _M_k
+ _M_ik
- 1; __i
< (2 * _M_k
); ++__i
)
857 _M_losers
[__i
]._M_keyp
= &__sentinel
;
858 _M_losers
[__i
]._M_source
= -1;
862 ~_LoserTreePointerUnguardedBase()
863 { delete[] _M_losers
; }
868 #if _GLIBCXX_PARALLEL_ASSERTIONS
869 // no dummy sequence can ever be at the top!
870 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
872 return _M_losers
[0]._M_source
;
876 __insert_start(const _Tp
& __key
, int __source
, bool)
878 unsigned int __pos
= _M_k
+ __source
;
880 _M_losers
[__pos
]._M_keyp
= &__key
;
881 _M_losers
[__pos
]._M_source
= __source
;
886 * @brief Stable unguarded _LoserTree variant storing pointers.
888 * Unstable variant is implemented below using partial specialization.
890 template<bool __stable
/* default == true */, typename _Tp
, typename _Compare
>
891 class _LoserTreePointerUnguarded
892 : public _LoserTreePointerUnguardedBase
<_Tp
, _Compare
>
894 typedef _LoserTreePointerUnguardedBase
<_Tp
, _Compare
> _Base
;
896 using _Base::_M_comp
;
897 using _Base::_M_losers
;
900 _LoserTreePointerUnguarded(unsigned int __k
, const _Tp
& __sentinel
,
901 _Compare __comp
= std::less
<_Tp
>())
902 : _Base::_LoserTreePointerUnguardedBase(__k
, __sentinel
, __comp
)
906 __init_winner(unsigned int __root
)
912 unsigned int __left
= __init_winner(2 * __root
);
913 unsigned int __right
= __init_winner(2 * __root
+ 1);
914 if (!_M_comp(*_M_losers
[__right
]._M_keyp
,
915 *_M_losers
[__left
]._M_keyp
))
917 // Left one is less or equal.
918 _M_losers
[__root
] = _M_losers
[__right
];
923 // Right one is less.
924 _M_losers
[__root
] = _M_losers
[__left
];
933 _M_losers
[0] = _M_losers
[__init_winner(1)];
935 #if _GLIBCXX_PARALLEL_ASSERTIONS
936 // no dummy sequence can ever be at the top at the beginning
938 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
943 __delete_min_insert(const _Tp
& __key
, bool __sup
)
945 #if _GLIBCXX_PARALLEL_ASSERTIONS
946 // no dummy sequence can ever be at the top!
947 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
950 const _Tp
* __keyp
= &__key
;
951 int __source
= _M_losers
[0]._M_source
;
952 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
955 // The smaller one gets promoted, ties are broken by _M_source.
956 if (_M_comp(*_M_losers
[__pos
]._M_keyp
, *__keyp
)
957 || (!_M_comp(*__keyp
, *_M_losers
[__pos
]._M_keyp
)
958 && _M_losers
[__pos
]._M_source
< __source
))
960 // The other one is smaller.
961 std::swap(_M_losers
[__pos
]._M_source
, __source
);
962 std::swap(_M_losers
[__pos
]._M_keyp
, __keyp
);
966 _M_losers
[0]._M_source
= __source
;
967 _M_losers
[0]._M_keyp
= __keyp
;
972 * @brief Unstable unguarded _LoserTree variant storing pointers.
974 * Stable variant is above.
976 template<typename _Tp
, typename _Compare
>
977 class _LoserTreePointerUnguarded
</* __stable == */false, _Tp
, _Compare
>
978 : public _LoserTreePointerUnguardedBase
<_Tp
, _Compare
>
980 typedef _LoserTreePointerUnguardedBase
<_Tp
, _Compare
> _Base
;
982 using _Base::_M_comp
;
983 using _Base::_M_losers
;
986 _LoserTreePointerUnguarded(unsigned int __k
, const _Tp
& __sentinel
,
987 _Compare __comp
= std::less
<_Tp
>())
988 : _Base::_LoserTreePointerUnguardedBase(__k
, __sentinel
, __comp
)
992 __init_winner(unsigned int __root
)
998 unsigned int __left
= __init_winner(2 * __root
);
999 unsigned int __right
= __init_winner(2 * __root
+ 1);
1001 #if _GLIBCXX_PARALLEL_ASSERTIONS
1002 // If __left one is sentinel then __right one must be, too.
1003 if (_M_losers
[__left
]._M_source
== -1)
1004 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[__right
]._M_source
== -1);
1007 if (!_M_comp(*_M_losers
[__right
]._M_keyp
,
1008 *_M_losers
[__left
]._M_keyp
))
1010 // Left one is less or equal.
1011 _M_losers
[__root
] = _M_losers
[__right
];
1016 // Right one is less.
1017 _M_losers
[__root
] = _M_losers
[__left
];
1026 _M_losers
[0] = _M_losers
[__init_winner(1)];
1028 #if _GLIBCXX_PARALLEL_ASSERTIONS
1029 // no dummy sequence can ever be at the top at the beginning
1031 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
1036 __delete_min_insert(const _Tp
& __key
, bool __sup
)
1038 #if _GLIBCXX_PARALLEL_ASSERTIONS
1039 // no dummy sequence can ever be at the top!
1040 _GLIBCXX_PARALLEL_ASSERT(_M_losers
[0]._M_source
!= -1);
1043 const _Tp
* __keyp
= &__key
;
1044 int __source
= _M_losers
[0]._M_source
;
1045 for (unsigned int __pos
= (_M_k
+ __source
) / 2; __pos
> 0;
1048 // The smaller one gets promoted.
1049 if (_M_comp(*(_M_losers
[__pos
]._M_keyp
), *__keyp
))
1051 // The other one is smaller.
1052 std::swap(_M_losers
[__pos
]._M_source
, __source
);
1053 std::swap(_M_losers
[__pos
]._M_keyp
, __keyp
);
1057 _M_losers
[0]._M_source
= __source
;
1058 _M_losers
[0]._M_keyp
= __keyp
;
1061 } // namespace __gnu_parallel
1063 #endif /* _GLIBCXX_PARALLEL_LOSERTREE_H */