15 #ifndef STXXL_ALGO_LOSERTREE_HEADER
16 #define STXXL_ALGO_LOSERTREE_HEADER
25 template <
typename RunCursorType,
26 typename RunCursorCmpType>
43 int_type left = init_winner(2 * root);
44 int_type right = init_winner(2 * root + 1);
45 if (cmp(current[left], current[right]))
72 STXXL_VERBOSE2(
"loser_tree: logK=" << logK <<
" nruns=" << nruns <<
" K=" << kReg);
74 #ifdef STXXL_SORT_SINGLE_PREFETCHER
75 current =
new RunCursorType[kReg];
76 RunCursorType::set_prefetcher(p);
78 current =
new RunCursorType[kReg];
79 for (i = 0; i < kReg; ++i)
80 current[i].prefetcher() = p;
84 for (i = 0; i < nruns; ++i)
86 current[i].buffer = p->pull_block();
91 for (i = nruns; i < kReg; ++i)
93 current[i].make_inf();
97 entry[0] = init_winner(1);
107 std::swap(logK, obj.
logK);
109 std::swap(entry, obj.
entry);
110 std::swap(current, obj.
current);
111 std::swap(cmp, obj.
cmp);
118 RunCursorType* currentE, * winnerE;
122 while (
LIKELY(out_first != out_last))
124 winnerE = current + winnerIndex;
125 *(out_first) = winnerE->current();
130 #define TreeStep(L) \
133 currentE = current + \
134 regEntry[(winnerIndex + (1 << LogK)) >> (((int(LogK - L) + 1) >= 0) ? ((LogK - L) + 1) : 0)]; \
135 if (cmp(*currentE, *winnerE)) \
137 std::swap(regEntry[(winnerIndex + (1 << LogK)) \
138 >> (((int(LogK - L) + 1) >= 0) ? ((LogK - L) + 1) : 0)], winnerIndex); \
139 winnerE = currentE; \
157 regEntry[0] = winnerIndex;
162 while (
LIKELY(out_first != out_last))
164 *out_first = current->current();
172 RunCursorType* currentE, * winnerE;
176 while (
LIKELY(out_first != out_last))
178 winnerE = current + winnerIndex;
179 *(out_first) = winnerE->current();
184 for (
int_type i = (winnerIndex + kReg) >> 1; i > 0; i >>= 1)
186 currentE = current + entry[i];
188 if (cmp(*currentE, *winnerE))
190 std::swap(entry[i], winnerIndex);
196 entry[0] = winnerIndex;
205 multi_merge_unrolled_0(out_first, out_last);
208 multi_merge_unrolled<1>(out_first, out_last);
211 multi_merge_unrolled<2>(out_first, out_last);
214 multi_merge_unrolled<3>(out_first, out_last);
217 multi_merge_unrolled<4>(out_first, out_last);
220 multi_merge_unrolled<5>(out_first, out_last);
223 multi_merge_unrolled<6>(out_first, out_last);
226 multi_merge_unrolled<7>(out_first, out_last);
229 multi_merge_unrolled<8>(out_first, out_last);
232 multi_merge_unrolled<9>(out_first, out_last);
235 multi_merge_unrolled<10>(out_first, out_last);
238 multi_merge_k(out_first, out_last);
248 template <
typename RunCursorType,
249 typename RunCursorCmpType>
258 #endif // !STXXL_ALGO_LOSERTREE_HEADER
loser_tree(prefetcher_type *p, int_type nruns, RunCursorCmpType c)
void multi_merge_unrolled(value_type *out_first, value_type *out_last)
RunCursorType::value_type value_type
#define STXXL_VERBOSE2(x)
void multi_merge_k(value_type *out_first, value_type *out_last)
choose_int_types< my_pointer_size >::int_type int_type
void multi_merge_unrolled_0(value_type *out_first, value_type *out_last)
#define STXXL_BEGIN_NAMESPACE
int_type init_winner(int_type root)
RunCursorType::prefetcher_type prefetcher_type
unsigned int ilog2_ceil(const IntegerType &i)
calculate the log2 ceiling of an integer type (by repeated bit shifts)
void swap(loser_tree &obj)
void multi_merge(value_type *out_first, value_type *out_last)
#define STXXL_END_NAMESPACE