14 #ifndef STXXL_COMMON_WINNER_TREE_HEADER
15 #define STXXL_COMMON_WINNER_TREE_HEADER
41 template <
typename Comparator>
66 return os <<
"replay_time=" << o.
replay_time << std::endl
91 STXXL_ASSERT(num_players > 0 && num_players <= invalid_key);
95 m_tree.resize(treesize, invalid_key);
102 while (index >= m_num_slots)
104 replay_on_change(index);
111 while (index >= m_num_slots)
113 m_tree[((m_tree.size() / 2) + index)] = index;
121 m_tree[((m_tree.size() / 2) + index)] = invalid_key;
129 replay_on_change(index,
true);
143 m_stats.remove_player_time.start();
147 while (p > 0 && m_tree[p] == index) {
148 m_tree[p] = invalid_key;
153 if (m_tree[0] == index)
154 m_tree[0] = invalid_key;
156 m_stats.remove_player_time.stop();
164 replay_on_change(index,
true);
171 replay_on_change(index);
184 return (m_tree[0] == invalid_key);
197 replay_on_change(m_tree[0]);
213 m_stats.replay_time.start();
227 if (m_tree[p] == invalid_key)
229 else if (m_tree[p + 1] == invalid_key)
231 else if (m_less(m_tree[p], m_tree[p + 1]))
240 m_stats.replay_time.stop();
247 m_stats.double_num_slots_time.start();
249 m_num_slots = m_num_slots << 1;
250 size_t old_tree_size = m_tree.size();
251 size_t tree_size = (m_num_slots << 1) - 1;
252 m_tree.resize(tree_size, invalid_key);
255 size_t old_index = i;
257 size_t new_index = old_index + (1 << old_level);
258 m_tree[new_index] = m_tree[old_index];
261 size_t step_size = (1 <<
ilog2_floor(old_tree_size));
262 size_t index = tree_size - 1;
264 while (step_size > 0) {
265 for (
size_t i = 0; i < step_size; ++i) {
266 m_tree[index] = invalid_key;
273 m_stats.double_num_slots_time.stop();
279 std::fill(m_tree.begin(), m_tree.end(), invalid_key);
286 m_tree.resize(treesize, invalid_key);
300 resize_and_clear(num_players);
302 activate_without_replay(i);
322 if (m_tree[lc] == invalid_key)
324 else if (m_tree[rc] == invalid_key)
326 else if (m_less(m_tree[lc], m_tree[rc]))
338 std::ostringstream ss;
342 if (i >= j + levelsize) {
347 if (m_tree[i] != invalid_key)
348 ss << m_tree[i] <<
" ";
366 #endif // !STXXL_COMMON_WINNER_TREE_HEADER
void double_num_slots()
Doubles the number of slots and adapts the tree so it's a valid winner tree again.
#define STXXL_ASSERT(condition)
friend std::ostream & operator<<(std::ostream &os, const uint_pair &a)
make a uint_pair outputtable via iostreams, using unsigned long long.
std::string to_string() const
Returns a readable representation of the winner tree as string.
void replay_on_pop()
Replay after the value of the winning player has changed.
stats_type m_stats
Collection of stats from the winner_tree.
void activate_player(unsigned_type index)
activate one of the static players or add a new one and replay
void deactivate_player(unsigned_type index)
deactivate a player and replay.
Comparator & m_less
the comparator object of this tree
The class winner_tree is a binary tournament tree.
void notify_change(unsigned_type index)
Notify that the value of the player at index has changed.
void replay_on_change(unsigned_type index, bool done=false)
bool empty() const
Returns if all players are deactivated.
Class fake_timer is a drop-in replacement for timer, which does nothing.
void clear()
Deactivate all players.
void deactivate_player_step(unsigned_type index)
size_t num_slots() const
Return the current number of slots.
unsigned_type top() const
Returns the winner. Remember running replay_on_pop() if the value of the winner changes.
unsigned_type m_num_slots
number of slots for the players (2^k)
stats_timer remove_player_time
void activate_without_replay(unsigned_type index)
activate a player and resize if necessary
winner_tree(unsigned_type num_players, Comparator &less)
void resize_and_rebuild(unsigned_type num_players)
#define STXXL_BEGIN_NAMESPACE
void resize(unsigned_type num_players)
void rebuild()
Build from winner tree from scratch.
static uint_pair max()
return an uint_pair instance containing the largest value possible
std::vector< unsigned_type > m_tree
the binary tree of size 2^(k+1)-1
unsigned int ilog2_floor(IntegerType i)
calculate the log2 floor of an integer type (by repeated bit shifts)
stats_timer double_num_slots_time
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
unsigned int ilog2_ceil(const IntegerType &i)
calculate the log2 ceiling of an integer type (by repeated bit shifts)
void resize_and_clear(unsigned_type num_players)
Deactivate all players and resize the tree.
void replay_on_deactivations(unsigned_type index)
Replay after the player at index has been deactivated.
fake_timer stats_timer
Defines if statistics are gathered: fake_timer or timer.
Collection of stats from the winner_tree.
void print_stats() const
Print statistical data.
void deactivate_without_replay(unsigned_type index)
deactivate a player
#define STXXL_END_NAMESPACE