ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LoserTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File LoserTree.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
54/*--------------------------------------------------------------------------*/
55/*----------------------------- DEFINITIONS --------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58#ifndef __ADS_LoserTree
59 #define __ADS_LoserTree
60/* self-identification: #endif constant */
61
62/*--------------------------------------------------------------------------*/
63/*------------------------------ INCLUDES ----------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#include "ADS/Contract.h"
67#include "ADS/sort/Sorted.h"
68
69#include <algorithm>
70#include <cstddef>
71#include <functional>
72#include <optional>
73#include <utility>
74#include <vector>
75
76/*--------------------------------------------------------------------------*/
77/*--------------------------- NAMESPACE ------------------------------------*/
78/*--------------------------------------------------------------------------*/
79
80namespace ADS
81{
82
83/*--------------------------------------------------------------------------*/
84/*---------------------------- CLASS LoserTree -----------------------------*/
85/*--------------------------------------------------------------------------*/
86
89template< class T , class C = std::less<> >
91{
92
93/*--------------------------------------------------------------------------*/
94 public:
124 explicit LoserTree( std::vector< std::optional< T > > keys , C less = C() );
125
127 [[nodiscard]] std::size_t players( void ) const;
128
130 [[nodiscard]] bool empty( void ) const;
131
134 [[nodiscard]] std::size_t winner( void ) const;
135
138 [[nodiscard]] const T & top( void ) const;
139
159 void replace( std::optional< T > key );
160
161/*--------------------------------------------------------------------------*/
162 private:
164 [[nodiscard]] bool beats( std::size_t i , std::size_t j ) const;
165
167 [[nodiscard]] bool winnerIsBest( void ) const;
168
169 std::vector< std::optional< T > > f_key;
170 std::vector< std::size_t > f_node;
172 C f_less;
173
174 }; // end( class LoserTree )
175
197template< class T , class C = std::less<> >
198std::vector< T > kWayMerge( const std::vector< std::vector< T > > & R ,
199 C less = C() );
200
201/*--------------------------------------------------------------------------*/
202
203} // end( namespace ADS )
204
205/*--------------------------------------------------------------------------*/
206/*-------------------------- IMPLEMENTATION --------------------------------*/
207/*--------------------------------------------------------------------------*/
208
209#include "ADS/priority/impl/LoserTree.tpp"
210
211/*--------------------------------------------------------------------------*/
212
213#endif /* LoserTree.h included */
214
215/*--------------------------------------------------------------------------*/
216/*-------------------------- End File LoserTree.h --------------------------*/
217/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
std::size_t winner(void) const
the player with the smallest key
void replace(std::optional< T > key)
the winner takes the key, or is exhausted if it is empty, and the matches on its path are replayed,...
bool empty(void) const
whether all the players are exhausted
const T & top(void) const
the key of the winner
LoserTree(std::vector< std::optional< T > > keys, C less=C())
the tree of the players with their first keys, an empty key for an exhausted one, its matches played...
std::size_t players(void) const
the number of players,
std::vector< T > kWayMerge(const std::vector< std::vector< T > > &R, C less=C())
the items of the sorted vectors of , merged through a tree of losers
Definition ADT.h:96