Program Listing for File LoserTree.h

↰ Return to documentation for file (ADS/priority/LoserTree.h)

/*--------------------------------------------------------------------------*/
/*--------------------------- File LoserTree.h -----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef __ADS_LoserTree
 #define __ADS_LoserTree
                      /* self-identification: #endif constant */

/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"

#include <algorithm>
#include <cstddef>
#include <functional>
#include <optional>
#include <utility>
#include <vector>

/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/

namespace ADS {

/*--------------------------------------------------------------------------*/
/*---------------------------- CLASS LoserTree -----------------------------*/
/*--------------------------------------------------------------------------*/

template< class T , class C = std::less<> >
class LoserTree {

/*--------------------------------------------------------------------------*/
 public:

 explicit LoserTree( std::vector< std::optional< T > > keys ,
                     C less = C() );

 [[nodiscard]] std::size_t players( void ) const;

 [[nodiscard]] bool empty( void ) const;

 [[nodiscard]] std::size_t winner( void ) const;

 [[nodiscard]] const T & top( void ) const;

 void replace( std::optional< T > key );

/*--------------------------------------------------------------------------*/
 private:

 [[nodiscard]] bool beats( std::size_t i , std::size_t j ) const;

 [[nodiscard]] bool winnerIsBest( void ) const;

 std::vector< std::optional< T > > f_key;
 std::vector< std::size_t > f_node;
 C f_less;

 };  // end( class LoserTree )

template< class T , class C = std::less<> >
std::vector< T > kWayMerge( const std::vector< std::vector< T > > & R ,
                            C less = C() );

/*--------------------------------------------------------------------------*/

}  // end( namespace ADS )

/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/priority/impl/LoserTree.tpp"

/*--------------------------------------------------------------------------*/

#endif  /* LoserTree.h included */

/*--------------------------------------------------------------------------*/
/*-------------------------- End File LoserTree.h --------------------------*/
/*--------------------------------------------------------------------------*/