17 #ifndef STXXL_PARALLEL_LOSERTREE_HEADER
18 #define STXXL_PARALLEL_LOSERTREE_HEADER
43 template <
typename ValueType,
typename Comparator = std::less<ValueType> >
77 Comparator _comp = std::less<ValueType>())
84 losers =
static_cast<Loser*
>(
operator new (2 * k *
sizeof(
Loser)));
88 losers[i + k].
sup =
true;
104 os << i <<
" " << losers[i].key <<
" from " << losers[i].source <<
", " << losers[i].sup <<
"\n";
110 return losers[0].source;
125 losers[pos].sup = sup;
126 losers[pos].source = source;
132 new (&(losers[i].key))ValueType(key);
133 first_insert =
false;
136 losers[pos].key = key;
154 size_type right = init_winner(2 * root + 1);
155 if (losers[right].sup ||
156 (!losers[left].sup && !comp(losers[right].key, losers[left].key)))
158 losers[root] = losers[right];
163 losers[root] = losers[left];
171 losers[0] = losers[init_winner(1)];
188 template <
bool Stable ,
189 typename ValueType,
typename Comparator = std::less<ValueType> >
198 using base_type::losers;
199 using base_type::comp;
206 void delete_min_insert(ValueType key,
bool sup)
210 source_type source = losers[0].source;
211 for (size_type pos = (k + source) / 2; pos > 0; pos /= 2)
215 (!losers[pos].sup && comp(losers[pos].key, key)))
218 swap(losers[pos].sup, sup);
219 swap(losers[pos].source, source);
220 swap(losers[pos].key, key);
225 losers[0].source = source;
243 template <
typename ValueType,
typename Comparator>
253 using base_type::losers;
254 using base_type::comp;
261 void delete_min_insert(ValueType key,
bool sup)
265 source_type source = losers[0].source;
266 for (size_type pos = (k + source) / 2; pos > 0; pos /= 2)
268 if ((sup && (!losers[pos].sup || losers[pos].source < source)) ||
269 (!sup && !losers[pos].sup &&
270 ((comp(losers[pos].key, key)) ||
271 (!comp(key, losers[pos].key) && losers[pos].source < source))))
274 swap(losers[pos].sup, sup);
275 swap(losers[pos].source, source);
276 swap(losers[pos].key, key);
281 losers[0].source = source;
291 template <
typename T,
typename Comparator = std::less<T> >
296 #define KEY(i) losers[i].key
297 #define KEY_SOURCE(i) key
299 #define KEY(i) keys[losers[i].source]
300 #define KEY_SOURCE(i) keys[i]
325 losers =
new Loser[k * 2];
329 for (
unsigned int i = ik - 1; i < k; i++)
330 losers[i + k].sup =
true;
343 for (
unsigned int i = 0; i < (k * 2); i++)
344 os << i <<
" " <<
KEY(i) <<
" from " << losers[i].source <<
", " << losers[i].sup <<
"\n";
349 return losers[0].source;
354 unsigned int pos = k + source;
356 losers[pos].sup = sup;
357 losers[pos].source = source;
369 unsigned int left = init_winner(2 * root);
370 unsigned int right = init_winner(2 * root + 1);
371 if (losers[right].sup ||
372 (!losers[left].sup && !comp(
KEY(right),
KEY(left))))
374 losers[root] = losers[right];
379 losers[root] = losers[left];
387 losers[0] = losers[init_winner(1)];
394 int source = losers[0].source;
395 for (
unsigned int pos = (k + source) / 2; pos > 0; pos /= 2)
401 swap(losers[pos].sup, sup);
402 swap(losers[pos].source, source);
410 losers[0].source = source;
418 return insert_start(key, source, sup);
429 unsigned int left = init_winner(2 * root);
430 unsigned int right = init_winner(2 * root + 1);
431 if (losers[right].sup ||
432 (!losers[left].sup && !comp(
KEY(right),
KEY(left))))
434 losers[root] = losers[right];
439 losers[root] = losers[left];
447 losers[0] = losers[init_winner_stable(1)];
454 int source = losers[0].source;
455 for (
unsigned int pos = (k + source) / 2; pos > 0; pos /= 2)
458 if ((sup && (!losers[pos].sup || losers[pos].source < source)) ||
459 (!sup && !losers[pos].sup &&
461 (!comp(
KEY_SOURCE(source),
KEY(pos)) && losers[pos].source < source))))
463 swap(losers[pos].sup, sup);
464 swap(losers[pos].source, source);
472 losers[0].source = source;
491 template <
typename ValueType,
typename Comparator = std::less<ValueType> >
525 Comparator _comp = std::less<ValueType>())
528 losers(new
Loser[k * 2]),
533 losers[i + k].sup =
true;
546 os << i <<
" " << losers[i].keyp <<
" from " << losers[i].source <<
", " << losers[i].sup <<
"\n";
552 return losers[0].source;
567 losers[pos].sup = sup;
568 losers[pos].source = source;
569 losers[pos].keyp = &key;
587 size_type right = init_winner(2 * root + 1);
588 if (losers[right].sup ||
589 (!losers[left].sup && !comp(*losers[right].keyp, *losers[left].keyp)))
591 losers[root] = losers[right];
596 losers[root] = losers[left];
604 losers[0] = losers[init_winner(1)];
618 template <
bool Stable ,
619 typename ValueType,
typename Comparator = std::less<ValueType> >
628 using base_type::losers;
629 using base_type::comp;
635 void delete_min_insert(
const ValueType& key,
bool sup)
639 const ValueType* keyp = &key;
640 source_type source = losers[0].source;
641 for (size_type pos = (k + source) / 2; pos > 0; pos /= 2)
645 (!losers[pos].sup && comp(*losers[pos].keyp, *keyp)))
647 swap(losers[pos].sup, sup);
648 swap(losers[pos].source, source);
649 swap(losers[pos].keyp, keyp);
654 losers[0].source = source;
655 losers[0].keyp = keyp;
669 template <
typename ValueType,
typename Comparator>
679 using base_type::losers;
680 using base_type::comp;
686 void delete_min_insert(
const ValueType& key,
bool sup)
690 const ValueType* keyp = &key;
691 source_type source = losers[0].source;
692 for (size_type pos = (k + source) / 2; pos > 0; pos /= 2)
695 if ((sup && (!losers[pos].sup || losers[pos].source < source)) ||
696 (!sup && !losers[pos].sup &&
697 ((comp(*losers[pos].keyp, *keyp)) ||
698 (!comp(*keyp, *losers[pos].keyp) && losers[pos].source < source))))
700 swap(losers[pos].sup, sup);
701 swap(losers[pos].source, source);
702 swap(losers[pos].keyp, keyp);
707 losers[0].source = source;
708 losers[0].keyp = keyp;
721 template <
typename ValueType,
typename Comparator = std::less<ValueType> >
745 Comparator _comp = std::less<ValueType>())
748 losers(new
Loser[k * 2]),
751 for (
unsigned int i = 0; i < 2 * k; i++)
753 losers[i].source = -1;
754 losers[i].key = _sentinel;
765 for (
unsigned int i = 0; i < k + ik; i++)
766 os << i <<
" " << losers[i].key <<
" from " << losers[i].source <<
"\n";
772 assert(losers[0].source != -1 &&
"Data underrun in unguarded merging.");
773 return losers[0].source;
778 unsigned int pos = k + source;
780 losers[pos].source = source;
781 losers[pos].key = key;
792 unsigned int left = init_winner(2 * root);
793 unsigned int right = init_winner(2 * root + 1);
794 if (!comp(losers[right].key, losers[left].key))
796 losers[root] = losers[right];
801 losers[root] = losers[left];
809 losers[0] = losers[init_winner(1)];
813 template <
bool Stable ,
814 typename ValueType,
typename Comparator = std::less<ValueType> >
821 using base_type::losers;
822 using base_type::comp;
826 Comparator _comp = std::less<ValueType>())
835 int source = losers[0].source;
836 for (
unsigned int pos = (k + source) / 2; pos > 0; pos /= 2)
839 if (comp(losers[pos].key, key))
842 swap(losers[pos].source, source);
843 swap(losers[pos].key, key);
847 losers[0].source = source;
852 template <
typename ValueType,
typename Comparator>
860 using base_type::losers;
861 using base_type::comp;
865 Comparator _comp = std::less<ValueType>())
874 int source = losers[0].source;
875 for (
unsigned int pos = (k + source) / 2; pos > 0; pos /= 2)
877 if (!comp(key, losers[pos].key) && losers[pos].source < source)
880 swap(losers[pos].source, source);
881 swap(losers[pos].key, key);
885 losers[0].source = source;
900 template <
typename ValueType,
typename Comparator = std::less<ValueType> >
924 Comparator _comp = std::less<ValueType>())
927 losers(new
Loser[k * 2]),
930 for (
unsigned int i = ik - 1; i < k; i++)
932 losers[i + k].source = -1;
933 losers[i + k].keyp = &_sentinel;
944 for (
unsigned int i = 0; i < k + ik; i++)
945 os << i <<
" " << *losers[i].keyp <<
" from " << losers[i].source <<
"\n";
950 return losers[0].source;
955 unsigned int pos = k + source;
957 losers[pos].source = source;
958 losers[pos].keyp = &key;
969 unsigned int left = init_winner(2 * root);
970 unsigned int right = init_winner(2 * root + 1);
971 if (!comp(*losers[right].keyp, *losers[left].keyp))
973 losers[root] = losers[right];
978 losers[root] = losers[left];
986 losers[0] = losers[init_winner(1)];
990 template <
bool Stable ,
991 typename ValueType,
typename Comparator = std::less<ValueType> >
999 using base_type::losers;
1000 using base_type::comp;
1004 Comparator _comp = std::less<ValueType>())
1012 const ValueType* keyp = &key;
1013 int source = losers[0].source;
1014 for (
unsigned int pos = (k + source) / 2; pos > 0; pos /= 2)
1017 if (comp(*losers[pos].keyp, *keyp))
1020 swap(losers[pos].source, source);
1021 swap(losers[pos].keyp, keyp);
1025 losers[0].source = source;
1026 losers[0].keyp = keyp;
1030 template <
typename ValueType,
typename Comparator>
1038 using base_type::losers;
1039 using base_type::comp;
1043 Comparator _comp = std::less<ValueType>())
1051 const ValueType* keyp = &key;
1052 int source = losers[0].source;
1053 for (
unsigned int pos = (k + source) / 2; pos > 0; pos /= 2)
1056 if (comp(*losers[pos].keyp, *keyp) ||
1057 (!comp(*keyp, *losers[pos].keyp) && losers[pos].source < source))
1060 swap(losers[pos].source, source);
1061 swap(losers[pos].keyp, keyp);
1065 losers[0].source = source;
1066 losers[0].keyp = keyp;
1074 #endif // !STXXL_PARALLEL_LOSERTREE_HEADER
void insert_start(T key, int source, bool sup)
unsigned int init_winner(unsigned int root)
LoserTreePointerUnguardedBase< ValueType, Comparator > base_type
int source
index of source
Loser * losers
array containing loser tree nodes
source_type get_min_source()
return the index of the player with the smallest element.
base_type::source_type source_type
const size_type k
log_2(ik) next greater power of 2
Internal representation of a loser tree player/node.
const size_type k
log_2(ik) next greater power of 2
source_type source
index of source
Internal representation of a loser tree player/node.
base_type::size_type size_type
Loser * losers
array containing loser tree nodes
base_type::size_type size_type
Comparator comp
the comparator object
bool sup
flag, true iff is a virtual maximum sentinel
void print(std::ostream &os)
void insert_start(const ValueType &key, int source)
unsigned int ik
number of nodes
base_type::size_type size_type
unsigned int init_winner(unsigned int root)
source_type get_min_source()
return the index of the player with the smallest element.
LoserTreeCopyUnguarded(unsigned int _k, const ValueType &_sentinel, Comparator _comp=std::less< ValueType >())
LoserTreePointerUnguarded(unsigned int _k, const ValueType &_sentinel, Comparator _comp=std::less< ValueType >())
LoserTreeCopyUnguardedBase(unsigned int _k, const ValueType &_sentinel, Comparator _comp=std::less< ValueType >())
base_type::source_type source_type
LoserTreePointerBase< ValueType, Comparator > base_type
int source
index of source
LoserTreePointerUnguarded(unsigned int _k, const ValueType &_sentinel, Comparator _comp=std::less< ValueType >())
base_type::size_type size_type
void print(std::ostream &os)
void print(std::ostream &os)
LoserTreePointerBase(size_type _k, Comparator _comp=std::less< ValueType >())
LoserTreeCopyBase< ValueType, Comparator >::size_type size_type
size of counters and array indexes
void delete_min_insert(const ValueType &key)
ValueType key
copy of key value of the element in this node
bool first_insert
still have to construct keys
void delete_min_insert(T, bool sup)
unsigned int k
log_2(ik) next greater power of 2
LoserTreeCopyBase< ValueType, Comparator > base_type
ValueType key
copy of key value of the element in this node
LoserTreeCopyUnguarded(unsigned int _k, const ValueType &_sentinel, Comparator _comp=std::less< ValueType >())
base_type::source_type source_type
LoserTreeCopyUnguardedBase< ValueType, Comparator > base_type
LoserTreePointerBase< ValueType, Comparator > base_type
LoserTreeReference(unsigned int _k, Comparator _comp=std::less< T >())
LoserTreeCopyBase< ValueType, Comparator > base_type
#define STXXL_BEGIN_NAMESPACE
void insert_start(const ValueType &key, source_type source, bool sup)
unsigned int size_type
size of counters and array indexes
LoserTreeCopyBase(size_type _k, Comparator _comp=std::less< ValueType >())
~LoserTreeCopyUnguardedBase()
int source_type
type of the source field
LoserTreePointerUnguardedBase(unsigned int _k, const ValueType &_sentinel, Comparator _comp=std::less< ValueType >())
Loser * losers
array containing loser tree nodes
void delete_min_insert(const ValueType &key)
const size_type ik
number of nodes
unsigned int ik
number of nodes
void insert_start_stable(T key, int source, bool sup)
unsigned int init_winner(unsigned int root)
void print(std::ostream &os)
const ValueType * keyp
copy of key value of the element in this node
Comparator comp
the comparator object
LoserTreePointerUnguardedBase< ValueType, Comparator > base_type
Comparator comp
the comparator object
size_type init_winner(size_type root)
LoserTreeCopyBase< ValueType, Comparator >::source_type source_type
type of the source field
Loser * losers
array containing loser tree nodes
void insert_start(const ValueType &key, source_type source, bool sup)
LoserTreeCopyUnguardedBase< ValueType, Comparator > base_type
unsigned int k
log_2(ik) next greater power of 2
const size_type ik
number of nodes
void print(std::ostream &os)
~LoserTreePointerUnguardedBase()
const ValueType * keyp
pointer to key value of the element in this node
size_type init_winner(size_type root)
void delete_min_insert(ValueType key)
bool sup
flag, true iff is a virtual maximum sentinel
source_type source
index of source
Internal representation of a loser tree player/node.
Comparator comp
the comparator object
void delete_min_insert(ValueType key)
void delete_min_insert_stable(T, bool sup)
static Integral round_up_to_power_of_two(Integral n)
Internal representation of a loser tree player/node.
unsigned int init_winner_stable(unsigned int root)
#define STXXL_END_NAMESPACE
base_type::source_type source_type
int get_min_source()
return the index of the player with the smallest element.
void insert_start(const ValueType &key, int source)