15 #ifndef STXXL_ALGO_LOSERTREE_HEADER
16 #define STXXL_ALGO_LOSERTREE_HEADER
26 template <
typename run_cursor_type,
27 typename run_cursor_cmp_type>
34 run_cursor_cmp_type
cmp;
44 int_type left = init_winner(2 * root);
45 int_type right = init_winner(2 * root + 1);
46 if (cmp(current[left], current[right]))
66 run_cursor_cmp_type c) : cmp(c)
72 STXXL_VERBOSE2(
"loser_tree: logK=" << logK <<
" nruns=" << nruns <<
" K=" << kReg);
74 #ifdef STXXL_SORT_SINGLE_PREFETCHER
75 current =
new run_cursor_type[kReg];
76 run_cursor_type::set_prefetcher(p);
78 current =
new run_cursor_type[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 run_cursor_type* 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 run_cursor_type* 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 run_cursor_type,
249 typename run_cursor_cmp_type>
258 #endif // !STXXL_ALGO_LOSERTREE_HEADER
void multi_merge_unrolled(value_type *out_first, value_type *out_last)
void multi_merge_k(value_type *out_first, value_type *out_last)
#define STXXL_VERBOSE2(x)
int_type init_winner(int_type root)
choose_int_types< my_pointer_size >::int_type int_type
void multi_merge_unrolled_0(value_type *out_first, value_type *out_last)
run_cursor_type::prefetcher_type prefetcher_type
#define STXXL_BEGIN_NAMESPACE
void multi_merge(value_type *out_first, value_type *out_last)
run_cursor_type::value_type value_type
unsigned int ilog2_ceil(const IntegerType &i)
calculate the log2 ceiling of an integer type (by repeated bit shifts)
loser_tree(prefetcher_type *p, int_type nruns, run_cursor_cmp_type c)
run_cursor_type * current
void swap(loser_tree &obj)
#define STXXL_END_NAMESPACE