Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1064 lines
28KB

  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/losertree.h
  21. * @brief Many generic loser tree variants.
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_LOSERTREE_H
  26. #define _GLIBCXX_PARALLEL_LOSERTREE_H 1
  27. #include <bits/stl_algobase.h>
  28. #include <bits/stl_function.h>
  29. #include <parallel/features.h>
  30. #include <parallel/base.h>
  31. namespace __gnu_parallel
  32. {
  33. /**
  34. * @brief Guarded loser/tournament tree.
  35. *
  36. * The smallest element is at the top.
  37. *
  38. * Guarding is done explicitly through one flag _M_sup per element,
  39. * inf is not needed due to a better initialization routine. This
  40. * is a well-performing variant.
  41. *
  42. * @param _Tp the element type
  43. * @param _Compare the comparator to use, defaults to std::less<_Tp>
  44. */
  45. template<typename _Tp, typename _Compare>
  46. class _LoserTreeBase
  47. {
  48. protected:
  49. /** @brief Internal representation of a _LoserTree element. */
  50. struct _Loser
  51. {
  52. /** @brief flag, true iff this is a "maximum" __sentinel. */
  53. bool _M_sup;
  54. /** @brief __index of the __source __sequence. */
  55. int _M_source;
  56. /** @brief _M_key of the element in the _LoserTree. */
  57. _Tp _M_key;
  58. };
  59. unsigned int _M_ik, _M_k, _M_offset;
  60. /** log_2{_M_k} */
  61. unsigned int _M_log_k;
  62. /** @brief _LoserTree __elements. */
  63. _Loser* _M_losers;
  64. /** @brief _Compare to use. */
  65. _Compare _M_comp;
  66. /**
  67. * @brief State flag that determines whether the _LoserTree is empty.
  68. *
  69. * Only used for building the _LoserTree.
  70. */
  71. bool _M_first_insert;
  72. public:
  73. /**
  74. * @brief The constructor.
  75. *
  76. * @param __k The number of sequences to merge.
  77. * @param __comp The comparator to use.
  78. */
  79. _LoserTreeBase(unsigned int __k, _Compare __comp)
  80. : _M_comp(__comp)
  81. {
  82. _M_ik = __k;
  83. // Compute log_2{_M_k} for the _Loser Tree
  84. _M_log_k = __rd_log2(_M_ik - 1) + 1;
  85. // Next greater power of 2.
  86. _M_k = 1 << _M_log_k;
  87. _M_offset = _M_k;
  88. // Avoid default-constructing _M_losers[]._M_key
  89. _M_losers = static_cast<_Loser*>(::operator new(2 * _M_k
  90. * sizeof(_Loser)));
  91. for (unsigned int __i = _M_ik - 1; __i < _M_k; ++__i)
  92. _M_losers[__i + _M_k]._M_sup = true;
  93. _M_first_insert = true;
  94. }
  95. /**
  96. * @brief The destructor.
  97. */
  98. ~_LoserTreeBase()
  99. {
  100. for (unsigned int __i = 0; __i < (2 * _M_k); ++__i)
  101. _M_losers[__i].~_Loser();
  102. ::operator delete(_M_losers);
  103. }
  104. /**
  105. * @brief Initializes the sequence "_M_source" with the element "__key".
  106. *
  107. * @param __key the element to insert
  108. * @param __source __index of the __source __sequence
  109. * @param __sup flag that determines whether the value to insert is an
  110. * explicit __supremum.
  111. */
  112. void
  113. __insert_start(const _Tp& __key, int __source, bool __sup)
  114. {
  115. unsigned int __pos = _M_k + __source;
  116. if (_M_first_insert)
  117. {
  118. // Construct all keys, so we can easily destruct them.
  119. for (unsigned int __i = 0; __i < (2 * _M_k); ++__i)
  120. ::new(&(_M_losers[__i]._M_key)) _Tp(__key);
  121. _M_first_insert = false;
  122. }
  123. else
  124. _M_losers[__pos]._M_key = __key;
  125. _M_losers[__pos]._M_sup = __sup;
  126. _M_losers[__pos]._M_source = __source;
  127. }
  128. /**
  129. * @return the index of the sequence with the smallest element.
  130. */
  131. int __get_min_source()
  132. { return _M_losers[0]._M_source; }
  133. };
  134. /**
  135. * @brief Stable _LoserTree variant.
  136. *
  137. * Provides the stable implementations of insert_start, __init_winner,
  138. * __init and __delete_min_insert.
  139. *
  140. * Unstable variant is done using partial specialisation below.
  141. */
  142. template<bool __stable/* default == true */, typename _Tp,
  143. typename _Compare>
  144. class _LoserTree
  145. : public _LoserTreeBase<_Tp, _Compare>
  146. {
  147. typedef _LoserTreeBase<_Tp, _Compare> _Base;
  148. using _Base::_M_k;
  149. using _Base::_M_comp;
  150. using _Base::_M_losers;
  151. using _Base::_M_first_insert;
  152. public:
  153. _LoserTree(unsigned int __k, _Compare __comp)
  154. : _Base::_LoserTreeBase(__k, __comp)
  155. { }
  156. unsigned int
  157. __init_winner(unsigned int __root)
  158. {
  159. if (__root >= _M_k)
  160. return __root;
  161. else
  162. {
  163. unsigned int __left = __init_winner(2 * __root);
  164. unsigned int __right = __init_winner(2 * __root + 1);
  165. if (_M_losers[__right]._M_sup
  166. || (!_M_losers[__left]._M_sup
  167. && !_M_comp(_M_losers[__right]._M_key,
  168. _M_losers[__left]._M_key)))
  169. {
  170. // Left one is less or equal.
  171. _M_losers[__root] = _M_losers[__right];
  172. return __left;
  173. }
  174. else
  175. {
  176. // Right one is less.
  177. _M_losers[__root] = _M_losers[__left];
  178. return __right;
  179. }
  180. }
  181. }
  182. void __init()
  183. { _M_losers[0] = _M_losers[__init_winner(1)]; }
  184. /**
  185. * @brief Delete the smallest element and insert a new element from
  186. * the previously smallest element's sequence.
  187. *
  188. * This implementation is stable.
  189. */
  190. // Do not pass a const reference since __key will be used as
  191. // local variable.
  192. void
  193. __delete_min_insert(_Tp __key, bool __sup)
  194. {
  195. using std::swap;
  196. #if _GLIBCXX_PARALLEL_ASSERTIONS
  197. // no dummy sequence can ever be at the top!
  198. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  199. #endif
  200. int __source = _M_losers[0]._M_source;
  201. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  202. __pos /= 2)
  203. {
  204. // The smaller one gets promoted, ties are broken by _M_source.
  205. if ((__sup && (!_M_losers[__pos]._M_sup
  206. || _M_losers[__pos]._M_source < __source))
  207. || (!__sup && !_M_losers[__pos]._M_sup
  208. && ((_M_comp(_M_losers[__pos]._M_key, __key))
  209. || (!_M_comp(__key, _M_losers[__pos]._M_key)
  210. && _M_losers[__pos]._M_source < __source))))
  211. {
  212. // The other one is smaller.
  213. std::swap(_M_losers[__pos]._M_sup, __sup);
  214. std::swap(_M_losers[__pos]._M_source, __source);
  215. swap(_M_losers[__pos]._M_key, __key);
  216. }
  217. }
  218. _M_losers[0]._M_sup = __sup;
  219. _M_losers[0]._M_source = __source;
  220. _M_losers[0]._M_key = __key;
  221. }
  222. };
  223. /**
  224. * @brief Unstable _LoserTree variant.
  225. *
  226. * Stability (non-stable here) is selected with partial specialization.
  227. */
  228. template<typename _Tp, typename _Compare>
  229. class _LoserTree</* __stable == */false, _Tp, _Compare>
  230. : public _LoserTreeBase<_Tp, _Compare>
  231. {
  232. typedef _LoserTreeBase<_Tp, _Compare> _Base;
  233. using _Base::_M_log_k;
  234. using _Base::_M_k;
  235. using _Base::_M_comp;
  236. using _Base::_M_losers;
  237. using _Base::_M_first_insert;
  238. public:
  239. _LoserTree(unsigned int __k, _Compare __comp)
  240. : _Base::_LoserTreeBase(__k, __comp)
  241. { }
  242. /**
  243. * Computes the winner of the competition at position "__root".
  244. *
  245. * Called recursively (starting at 0) to build the initial tree.
  246. *
  247. * @param __root __index of the "game" to start.
  248. */
  249. unsigned int
  250. __init_winner(unsigned int __root)
  251. {
  252. if (__root >= _M_k)
  253. return __root;
  254. else
  255. {
  256. unsigned int __left = __init_winner(2 * __root);
  257. unsigned int __right = __init_winner(2 * __root + 1);
  258. if (_M_losers[__right]._M_sup
  259. || (!_M_losers[__left]._M_sup
  260. && !_M_comp(_M_losers[__right]._M_key,
  261. _M_losers[__left]._M_key)))
  262. {
  263. // Left one is less or equal.
  264. _M_losers[__root] = _M_losers[__right];
  265. return __left;
  266. }
  267. else
  268. {
  269. // Right one is less.
  270. _M_losers[__root] = _M_losers[__left];
  271. return __right;
  272. }
  273. }
  274. }
  275. void
  276. __init()
  277. { _M_losers[0] = _M_losers[__init_winner(1)]; }
  278. /**
  279. * Delete the _M_key smallest element and insert the element __key
  280. * instead.
  281. *
  282. * @param __key the _M_key to insert
  283. * @param __sup true iff __key is an explicitly marked supremum
  284. */
  285. // Do not pass a const reference since __key will be used as local
  286. // variable.
  287. void
  288. __delete_min_insert(_Tp __key, bool __sup)
  289. {
  290. using std::swap;
  291. #if _GLIBCXX_PARALLEL_ASSERTIONS
  292. // no dummy sequence can ever be at the top!
  293. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  294. #endif
  295. int __source = _M_losers[0]._M_source;
  296. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  297. __pos /= 2)
  298. {
  299. // The smaller one gets promoted.
  300. if (__sup || (!_M_losers[__pos]._M_sup
  301. && _M_comp(_M_losers[__pos]._M_key, __key)))
  302. {
  303. // The other one is smaller.
  304. std::swap(_M_losers[__pos]._M_sup, __sup);
  305. std::swap(_M_losers[__pos]._M_source, __source);
  306. swap(_M_losers[__pos]._M_key, __key);
  307. }
  308. }
  309. _M_losers[0]._M_sup = __sup;
  310. _M_losers[0]._M_source = __source;
  311. _M_losers[0]._M_key = __key;
  312. }
  313. };
  314. /**
  315. * @brief Base class of _Loser Tree implementation using pointers.
  316. */
  317. template<typename _Tp, typename _Compare>
  318. class _LoserTreePointerBase
  319. {
  320. protected:
  321. /** @brief Internal representation of _LoserTree __elements. */
  322. struct _Loser
  323. {
  324. bool _M_sup;
  325. int _M_source;
  326. const _Tp* _M_keyp;
  327. };
  328. unsigned int _M_ik, _M_k, _M_offset;
  329. _Loser* _M_losers;
  330. _Compare _M_comp;
  331. public:
  332. _LoserTreePointerBase(unsigned int __k,
  333. _Compare __comp = std::less<_Tp>())
  334. : _M_comp(__comp)
  335. {
  336. _M_ik = __k;
  337. // Next greater power of 2.
  338. _M_k = 1 << (__rd_log2(_M_ik - 1) + 1);
  339. _M_offset = _M_k;
  340. _M_losers = new _Loser[_M_k * 2];
  341. for (unsigned int __i = _M_ik - 1; __i < _M_k; __i++)
  342. _M_losers[__i + _M_k]._M_sup = true;
  343. }
  344. ~_LoserTreePointerBase()
  345. { delete[] _M_losers; }
  346. int __get_min_source()
  347. { return _M_losers[0]._M_source; }
  348. void __insert_start(const _Tp& __key, int __source, bool __sup)
  349. {
  350. unsigned int __pos = _M_k + __source;
  351. _M_losers[__pos]._M_sup = __sup;
  352. _M_losers[__pos]._M_source = __source;
  353. _M_losers[__pos]._M_keyp = &__key;
  354. }
  355. };
  356. /**
  357. * @brief Stable _LoserTree implementation.
  358. *
  359. * The unstable variant is implemented using partial instantiation below.
  360. */
  361. template<bool __stable/* default == true */, typename _Tp, typename _Compare>
  362. class _LoserTreePointer
  363. : public _LoserTreePointerBase<_Tp, _Compare>
  364. {
  365. typedef _LoserTreePointerBase<_Tp, _Compare> _Base;
  366. using _Base::_M_k;
  367. using _Base::_M_comp;
  368. using _Base::_M_losers;
  369. public:
  370. _LoserTreePointer(unsigned int __k, _Compare __comp = std::less<_Tp>())
  371. : _Base::_LoserTreePointerBase(__k, __comp)
  372. { }
  373. unsigned int
  374. __init_winner(unsigned int __root)
  375. {
  376. if (__root >= _M_k)
  377. return __root;
  378. else
  379. {
  380. unsigned int __left = __init_winner(2 * __root);
  381. unsigned int __right = __init_winner(2 * __root + 1);
  382. if (_M_losers[__right]._M_sup
  383. || (!_M_losers[__left]._M_sup
  384. && !_M_comp(*_M_losers[__right]._M_keyp,
  385. *_M_losers[__left]._M_keyp)))
  386. {
  387. // Left one is less or equal.
  388. _M_losers[__root] = _M_losers[__right];
  389. return __left;
  390. }
  391. else
  392. {
  393. // Right one is less.
  394. _M_losers[__root] = _M_losers[__left];
  395. return __right;
  396. }
  397. }
  398. }
  399. void __init()
  400. { _M_losers[0] = _M_losers[__init_winner(1)]; }
  401. void __delete_min_insert(const _Tp& __key, bool __sup)
  402. {
  403. #if _GLIBCXX_PARALLEL_ASSERTIONS
  404. // no dummy sequence can ever be at the top!
  405. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  406. #endif
  407. const _Tp* __keyp = &__key;
  408. int __source = _M_losers[0]._M_source;
  409. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  410. __pos /= 2)
  411. {
  412. // The smaller one gets promoted, ties are broken by __source.
  413. if ((__sup && (!_M_losers[__pos]._M_sup
  414. || _M_losers[__pos]._M_source < __source))
  415. || (!__sup && !_M_losers[__pos]._M_sup &&
  416. ((_M_comp(*_M_losers[__pos]._M_keyp, *__keyp))
  417. || (!_M_comp(*__keyp, *_M_losers[__pos]._M_keyp)
  418. && _M_losers[__pos]._M_source < __source))))
  419. {
  420. // The other one is smaller.
  421. std::swap(_M_losers[__pos]._M_sup, __sup);
  422. std::swap(_M_losers[__pos]._M_source, __source);
  423. std::swap(_M_losers[__pos]._M_keyp, __keyp);
  424. }
  425. }
  426. _M_losers[0]._M_sup = __sup;
  427. _M_losers[0]._M_source = __source;
  428. _M_losers[0]._M_keyp = __keyp;
  429. }
  430. };
  431. /**
  432. * @brief Unstable _LoserTree implementation.
  433. *
  434. * The stable variant is above.
  435. */
  436. template<typename _Tp, typename _Compare>
  437. class _LoserTreePointer</* __stable == */false, _Tp, _Compare>
  438. : public _LoserTreePointerBase<_Tp, _Compare>
  439. {
  440. typedef _LoserTreePointerBase<_Tp, _Compare> _Base;
  441. using _Base::_M_k;
  442. using _Base::_M_comp;
  443. using _Base::_M_losers;
  444. public:
  445. _LoserTreePointer(unsigned int __k, _Compare __comp = std::less<_Tp>())
  446. : _Base::_LoserTreePointerBase(__k, __comp)
  447. { }
  448. unsigned int
  449. __init_winner(unsigned int __root)
  450. {
  451. if (__root >= _M_k)
  452. return __root;
  453. else
  454. {
  455. unsigned int __left = __init_winner(2 * __root);
  456. unsigned int __right = __init_winner(2 * __root + 1);
  457. if (_M_losers[__right]._M_sup
  458. || (!_M_losers[__left]._M_sup
  459. && !_M_comp(*_M_losers[__right]._M_keyp,
  460. *_M_losers[__left]._M_keyp)))
  461. {
  462. // Left one is less or equal.
  463. _M_losers[__root] = _M_losers[__right];
  464. return __left;
  465. }
  466. else
  467. {
  468. // Right one is less.
  469. _M_losers[__root] = _M_losers[__left];
  470. return __right;
  471. }
  472. }
  473. }
  474. void __init()
  475. { _M_losers[0] = _M_losers[__init_winner(1)]; }
  476. void __delete_min_insert(const _Tp& __key, bool __sup)
  477. {
  478. #if _GLIBCXX_PARALLEL_ASSERTIONS
  479. // no dummy sequence can ever be at the top!
  480. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  481. #endif
  482. const _Tp* __keyp = &__key;
  483. int __source = _M_losers[0]._M_source;
  484. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  485. __pos /= 2)
  486. {
  487. // The smaller one gets promoted.
  488. if (__sup || (!_M_losers[__pos]._M_sup
  489. && _M_comp(*_M_losers[__pos]._M_keyp, *__keyp)))
  490. {
  491. // The other one is smaller.
  492. std::swap(_M_losers[__pos]._M_sup, __sup);
  493. std::swap(_M_losers[__pos]._M_source, __source);
  494. std::swap(_M_losers[__pos]._M_keyp, __keyp);
  495. }
  496. }
  497. _M_losers[0]._M_sup = __sup;
  498. _M_losers[0]._M_source = __source;
  499. _M_losers[0]._M_keyp = __keyp;
  500. }
  501. };
  502. /** @brief Base class for unguarded _LoserTree implementation.
  503. *
  504. * The whole element is copied into the tree structure.
  505. *
  506. * No guarding is done, therefore not a single input sequence must
  507. * run empty. Unused __sequence heads are marked with a sentinel which
  508. * is &gt; all elements that are to be merged.
  509. *
  510. * This is a very fast variant.
  511. */
  512. template<typename _Tp, typename _Compare>
  513. class _LoserTreeUnguardedBase
  514. {
  515. protected:
  516. struct _Loser
  517. {
  518. int _M_source;
  519. _Tp _M_key;
  520. };
  521. unsigned int _M_ik, _M_k, _M_offset;
  522. _Loser* _M_losers;
  523. _Compare _M_comp;
  524. public:
  525. _LoserTreeUnguardedBase(unsigned int __k, const _Tp& __sentinel,
  526. _Compare __comp = std::less<_Tp>())
  527. : _M_comp(__comp)
  528. {
  529. _M_ik = __k;
  530. // Next greater power of 2.
  531. _M_k = 1 << (__rd_log2(_M_ik - 1) + 1);
  532. _M_offset = _M_k;
  533. // Avoid default-constructing _M_losers[]._M_key
  534. _M_losers = static_cast<_Loser*>(::operator new(2 * _M_k
  535. * sizeof(_Loser)));
  536. for (unsigned int __i = 0; __i < _M_k; ++__i)
  537. {
  538. ::new(&(_M_losers[__i]._M_key)) _Tp(__sentinel);
  539. _M_losers[__i]._M_source = -1;
  540. }
  541. for (unsigned int __i = _M_k + _M_ik - 1; __i < (2 * _M_k); ++__i)
  542. {
  543. ::new(&(_M_losers[__i]._M_key)) _Tp(__sentinel);
  544. _M_losers[__i]._M_source = -1;
  545. }
  546. }
  547. ~_LoserTreeUnguardedBase()
  548. {
  549. for (unsigned int __i = 0; __i < (2 * _M_k); ++__i)
  550. _M_losers[__i].~_Loser();
  551. ::operator delete(_M_losers);
  552. }
  553. int
  554. __get_min_source()
  555. {
  556. #if _GLIBCXX_PARALLEL_ASSERTIONS
  557. // no dummy sequence can ever be at the top!
  558. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  559. #endif
  560. return _M_losers[0]._M_source;
  561. }
  562. void
  563. __insert_start(const _Tp& __key, int __source, bool)
  564. {
  565. unsigned int __pos = _M_k + __source;
  566. ::new(&(_M_losers[__pos]._M_key)) _Tp(__key);
  567. _M_losers[__pos]._M_source = __source;
  568. }
  569. };
  570. /**
  571. * @brief Stable implementation of unguarded _LoserTree.
  572. *
  573. * Unstable variant is selected below with partial specialization.
  574. */
  575. template<bool __stable/* default == true */, typename _Tp, typename _Compare>
  576. class _LoserTreeUnguarded
  577. : public _LoserTreeUnguardedBase<_Tp, _Compare>
  578. {
  579. typedef _LoserTreeUnguardedBase<_Tp, _Compare> _Base;
  580. using _Base::_M_k;
  581. using _Base::_M_comp;
  582. using _Base::_M_losers;
  583. public:
  584. _LoserTreeUnguarded(unsigned int __k, const _Tp& __sentinel,
  585. _Compare __comp = std::less<_Tp>())
  586. : _Base::_LoserTreeUnguardedBase(__k, __sentinel, __comp)
  587. { }
  588. unsigned int
  589. __init_winner(unsigned int __root)
  590. {
  591. if (__root >= _M_k)
  592. return __root;
  593. else
  594. {
  595. unsigned int __left = __init_winner(2 * __root);
  596. unsigned int __right = __init_winner(2 * __root + 1);
  597. if (!_M_comp(_M_losers[__right]._M_key,
  598. _M_losers[__left]._M_key))
  599. {
  600. // Left one is less or equal.
  601. _M_losers[__root] = _M_losers[__right];
  602. return __left;
  603. }
  604. else
  605. {
  606. // Right one is less.
  607. _M_losers[__root] = _M_losers[__left];
  608. return __right;
  609. }
  610. }
  611. }
  612. void
  613. __init()
  614. {
  615. _M_losers[0] = _M_losers[__init_winner(1)];
  616. #if _GLIBCXX_PARALLEL_ASSERTIONS
  617. // no dummy sequence can ever be at the top at the beginning
  618. // (0 sequences!)
  619. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  620. #endif
  621. }
  622. // Do not pass a const reference since __key will be used as
  623. // local variable.
  624. void
  625. __delete_min_insert(_Tp __key, bool)
  626. {
  627. using std::swap;
  628. #if _GLIBCXX_PARALLEL_ASSERTIONS
  629. // no dummy sequence can ever be at the top!
  630. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  631. #endif
  632. int __source = _M_losers[0]._M_source;
  633. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  634. __pos /= 2)
  635. {
  636. // The smaller one gets promoted, ties are broken by _M_source.
  637. if (_M_comp(_M_losers[__pos]._M_key, __key)
  638. || (!_M_comp(__key, _M_losers[__pos]._M_key)
  639. && _M_losers[__pos]._M_source < __source))
  640. {
  641. // The other one is smaller.
  642. std::swap(_M_losers[__pos]._M_source, __source);
  643. swap(_M_losers[__pos]._M_key, __key);
  644. }
  645. }
  646. _M_losers[0]._M_source = __source;
  647. _M_losers[0]._M_key = __key;
  648. }
  649. };
  650. /**
  651. * @brief Non-Stable implementation of unguarded _LoserTree.
  652. *
  653. * Stable implementation is above.
  654. */
  655. template<typename _Tp, typename _Compare>
  656. class _LoserTreeUnguarded</* __stable == */false, _Tp, _Compare>
  657. : public _LoserTreeUnguardedBase<_Tp, _Compare>
  658. {
  659. typedef _LoserTreeUnguardedBase<_Tp, _Compare> _Base;
  660. using _Base::_M_k;
  661. using _Base::_M_comp;
  662. using _Base::_M_losers;
  663. public:
  664. _LoserTreeUnguarded(unsigned int __k, const _Tp& __sentinel,
  665. _Compare __comp = std::less<_Tp>())
  666. : _Base::_LoserTreeUnguardedBase(__k, __sentinel, __comp)
  667. { }
  668. unsigned int
  669. __init_winner(unsigned int __root)
  670. {
  671. if (__root >= _M_k)
  672. return __root;
  673. else
  674. {
  675. unsigned int __left = __init_winner(2 * __root);
  676. unsigned int __right = __init_winner(2 * __root + 1);
  677. #if _GLIBCXX_PARALLEL_ASSERTIONS
  678. // If __left one is sentinel then __right one must be, too.
  679. if (_M_losers[__left]._M_source == -1)
  680. _GLIBCXX_PARALLEL_ASSERT(_M_losers[__right]._M_source == -1);
  681. #endif
  682. if (!_M_comp(_M_losers[__right]._M_key,
  683. _M_losers[__left]._M_key))
  684. {
  685. // Left one is less or equal.
  686. _M_losers[__root] = _M_losers[__right];
  687. return __left;
  688. }
  689. else
  690. {
  691. // Right one is less.
  692. _M_losers[__root] = _M_losers[__left];
  693. return __right;
  694. }
  695. }
  696. }
  697. void
  698. __init()
  699. {
  700. _M_losers[0] = _M_losers[__init_winner(1)];
  701. #if _GLIBCXX_PARALLEL_ASSERTIONS
  702. // no dummy sequence can ever be at the top at the beginning
  703. // (0 sequences!)
  704. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  705. #endif
  706. }
  707. // Do not pass a const reference since __key will be used as
  708. // local variable.
  709. void
  710. __delete_min_insert(_Tp __key, bool)
  711. {
  712. using std::swap;
  713. #if _GLIBCXX_PARALLEL_ASSERTIONS
  714. // no dummy sequence can ever be at the top!
  715. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  716. #endif
  717. int __source = _M_losers[0]._M_source;
  718. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  719. __pos /= 2)
  720. {
  721. // The smaller one gets promoted.
  722. if (_M_comp(_M_losers[__pos]._M_key, __key))
  723. {
  724. // The other one is smaller.
  725. std::swap(_M_losers[__pos]._M_source, __source);
  726. swap(_M_losers[__pos]._M_key, __key);
  727. }
  728. }
  729. _M_losers[0]._M_source = __source;
  730. _M_losers[0]._M_key = __key;
  731. }
  732. };
  733. /** @brief Unguarded loser tree, keeping only pointers to the
  734. * elements in the tree structure.
  735. *
  736. * No guarding is done, therefore not a single input sequence must
  737. * run empty. This is a very fast variant.
  738. */
  739. template<typename _Tp, typename _Compare>
  740. class _LoserTreePointerUnguardedBase
  741. {
  742. protected:
  743. struct _Loser
  744. {
  745. int _M_source;
  746. const _Tp* _M_keyp;
  747. };
  748. unsigned int _M_ik, _M_k, _M_offset;
  749. _Loser* _M_losers;
  750. _Compare _M_comp;
  751. public:
  752. _LoserTreePointerUnguardedBase(unsigned int __k, const _Tp& __sentinel,
  753. _Compare __comp = std::less<_Tp>())
  754. : _M_comp(__comp)
  755. {
  756. _M_ik = __k;
  757. // Next greater power of 2.
  758. _M_k = 1 << (__rd_log2(_M_ik - 1) + 1);
  759. _M_offset = _M_k;
  760. // Avoid default-constructing _M_losers[]._M_key
  761. _M_losers = new _Loser[2 * _M_k];
  762. for (unsigned int __i = _M_k + _M_ik - 1; __i < (2 * _M_k); ++__i)
  763. {
  764. _M_losers[__i]._M_keyp = &__sentinel;
  765. _M_losers[__i]._M_source = -1;
  766. }
  767. }
  768. ~_LoserTreePointerUnguardedBase()
  769. { delete[] _M_losers; }
  770. int
  771. __get_min_source()
  772. {
  773. #if _GLIBCXX_PARALLEL_ASSERTIONS
  774. // no dummy sequence can ever be at the top!
  775. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  776. #endif
  777. return _M_losers[0]._M_source;
  778. }
  779. void
  780. __insert_start(const _Tp& __key, int __source, bool)
  781. {
  782. unsigned int __pos = _M_k + __source;
  783. _M_losers[__pos]._M_keyp = &__key;
  784. _M_losers[__pos]._M_source = __source;
  785. }
  786. };
  787. /**
  788. * @brief Stable unguarded _LoserTree variant storing pointers.
  789. *
  790. * Unstable variant is implemented below using partial specialization.
  791. */
  792. template<bool __stable/* default == true */, typename _Tp, typename _Compare>
  793. class _LoserTreePointerUnguarded
  794. : public _LoserTreePointerUnguardedBase<_Tp, _Compare>
  795. {
  796. typedef _LoserTreePointerUnguardedBase<_Tp, _Compare> _Base;
  797. using _Base::_M_k;
  798. using _Base::_M_comp;
  799. using _Base::_M_losers;
  800. public:
  801. _LoserTreePointerUnguarded(unsigned int __k, const _Tp& __sentinel,
  802. _Compare __comp = std::less<_Tp>())
  803. : _Base::_LoserTreePointerUnguardedBase(__k, __sentinel, __comp)
  804. { }
  805. unsigned int
  806. __init_winner(unsigned int __root)
  807. {
  808. if (__root >= _M_k)
  809. return __root;
  810. else
  811. {
  812. unsigned int __left = __init_winner(2 * __root);
  813. unsigned int __right = __init_winner(2 * __root + 1);
  814. if (!_M_comp(*_M_losers[__right]._M_keyp,
  815. *_M_losers[__left]._M_keyp))
  816. {
  817. // Left one is less or equal.
  818. _M_losers[__root] = _M_losers[__right];
  819. return __left;
  820. }
  821. else
  822. {
  823. // Right one is less.
  824. _M_losers[__root] = _M_losers[__left];
  825. return __right;
  826. }
  827. }
  828. }
  829. void
  830. __init()
  831. {
  832. _M_losers[0] = _M_losers[__init_winner(1)];
  833. #if _GLIBCXX_PARALLEL_ASSERTIONS
  834. // no dummy sequence can ever be at the top at the beginning
  835. // (0 sequences!)
  836. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  837. #endif
  838. }
  839. void
  840. __delete_min_insert(const _Tp& __key, bool __sup)
  841. {
  842. #if _GLIBCXX_PARALLEL_ASSERTIONS
  843. // no dummy sequence can ever be at the top!
  844. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  845. #endif
  846. const _Tp* __keyp = &__key;
  847. int __source = _M_losers[0]._M_source;
  848. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  849. __pos /= 2)
  850. {
  851. // The smaller one gets promoted, ties are broken by _M_source.
  852. if (_M_comp(*_M_losers[__pos]._M_keyp, *__keyp)
  853. || (!_M_comp(*__keyp, *_M_losers[__pos]._M_keyp)
  854. && _M_losers[__pos]._M_source < __source))
  855. {
  856. // The other one is smaller.
  857. std::swap(_M_losers[__pos]._M_source, __source);
  858. std::swap(_M_losers[__pos]._M_keyp, __keyp);
  859. }
  860. }
  861. _M_losers[0]._M_source = __source;
  862. _M_losers[0]._M_keyp = __keyp;
  863. }
  864. };
  865. /**
  866. * @brief Unstable unguarded _LoserTree variant storing pointers.
  867. *
  868. * Stable variant is above.
  869. */
  870. template<typename _Tp, typename _Compare>
  871. class _LoserTreePointerUnguarded</* __stable == */false, _Tp, _Compare>
  872. : public _LoserTreePointerUnguardedBase<_Tp, _Compare>
  873. {
  874. typedef _LoserTreePointerUnguardedBase<_Tp, _Compare> _Base;
  875. using _Base::_M_k;
  876. using _Base::_M_comp;
  877. using _Base::_M_losers;
  878. public:
  879. _LoserTreePointerUnguarded(unsigned int __k, const _Tp& __sentinel,
  880. _Compare __comp = std::less<_Tp>())
  881. : _Base::_LoserTreePointerUnguardedBase(__k, __sentinel, __comp)
  882. { }
  883. unsigned int
  884. __init_winner(unsigned int __root)
  885. {
  886. if (__root >= _M_k)
  887. return __root;
  888. else
  889. {
  890. unsigned int __left = __init_winner(2 * __root);
  891. unsigned int __right = __init_winner(2 * __root + 1);
  892. #if _GLIBCXX_PARALLEL_ASSERTIONS
  893. // If __left one is sentinel then __right one must be, too.
  894. if (_M_losers[__left]._M_source == -1)
  895. _GLIBCXX_PARALLEL_ASSERT(_M_losers[__right]._M_source == -1);
  896. #endif
  897. if (!_M_comp(*_M_losers[__right]._M_keyp,
  898. *_M_losers[__left]._M_keyp))
  899. {
  900. // Left one is less or equal.
  901. _M_losers[__root] = _M_losers[__right];
  902. return __left;
  903. }
  904. else
  905. {
  906. // Right one is less.
  907. _M_losers[__root] = _M_losers[__left];
  908. return __right;
  909. }
  910. }
  911. }
  912. void
  913. __init()
  914. {
  915. _M_losers[0] = _M_losers[__init_winner(1)];
  916. #if _GLIBCXX_PARALLEL_ASSERTIONS
  917. // no dummy sequence can ever be at the top at the beginning
  918. // (0 sequences!)
  919. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  920. #endif
  921. }
  922. void
  923. __delete_min_insert(const _Tp& __key, bool __sup)
  924. {
  925. #if _GLIBCXX_PARALLEL_ASSERTIONS
  926. // no dummy sequence can ever be at the top!
  927. _GLIBCXX_PARALLEL_ASSERT(_M_losers[0]._M_source != -1);
  928. #endif
  929. const _Tp* __keyp = &__key;
  930. int __source = _M_losers[0]._M_source;
  931. for (unsigned int __pos = (_M_k + __source) / 2; __pos > 0;
  932. __pos /= 2)
  933. {
  934. // The smaller one gets promoted.
  935. if (_M_comp(*(_M_losers[__pos]._M_keyp), *__keyp))
  936. {
  937. // The other one is smaller.
  938. std::swap(_M_losers[__pos]._M_source, __source);
  939. std::swap(_M_losers[__pos]._M_keyp, __keyp);
  940. }
  941. }
  942. _M_losers[0]._M_source = __source;
  943. _M_losers[0]._M_keyp = __keyp;
  944. }
  945. };
  946. } // namespace __gnu_parallel
  947. #endif /* _GLIBCXX_PARALLEL_LOSERTREE_H */