ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MultiwayMerge.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File MultiwayMerge.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
86/*--------------------------------------------------------------------------*/
87/*----------------------------- DEFINITIONS --------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90#ifndef __ADS_MultiwayMerge
91 #define __ADS_MultiwayMerge
92/* self-identification: #endif constant */
93
94/*--------------------------------------------------------------------------*/
95/*------------------------------ INCLUDES ----------------------------------*/
96/*--------------------------------------------------------------------------*/
97
98#include "ADS/Contract.h"
99#include "ADS/sort/Sorted.h"
100
101#include <algorithm>
102#include <cstddef>
103#include <cstdint>
104#include <cstring>
105#include <functional>
106#include <iterator>
107#include <limits>
108#include <type_traits>
109#include <utility>
110#include <vector>
111
112/*--------------------------------------------------------------------------*/
113/*--------------------------- NAMESPACE ------------------------------------*/
114/*--------------------------------------------------------------------------*/
115
116namespace ADS
117{
118
119namespace detail
120{
121
123template< class T , class C >
124inline constexpr bool kNaturalOrder =
125 std::is_arithmetic_v< T > &&
126 ( std::is_same_v< C , std::less<> > || std::is_same_v< C ,
127 std::less< T > > ||
128 std::is_same_v< C , std::greater<> > ||
129 std::is_same_v< C , std::greater< T > > );
130
131} // end( namespace detail )
132
133/*--------------------------------------------------------------------------*/
134/*-------------------------- CLASS FlatLoserTree ---------------------------*/
135/*--------------------------------------------------------------------------*/
136
139template< class T , class C = std::less<> >
141{
142
143/*--------------------------------------------------------------------------*/
144 public:
147 explicit FlatLoserTree( std::size_t k , C less = C() );
148
150 [[nodiscard]] std::size_t sources( void ) const;
151
155 void start( std::size_t s , const T & x );
156
184 void build( void );
185
187 [[nodiscard]] bool empty( void ) const;
188
191 [[nodiscard]] std::size_t winner( void ) const;
192
195 [[nodiscard]] const T & top( void ) const;
196
215 void replaceTop( const T & x );
216
220 void exhaustTop( void );
221
222/*--------------------------------------------------------------------------*/
223 private:
225 static constexpr bool kNatural = detail::kNaturalOrder< T , C >;
226
228 static constexpr bool kUp =
229 std::is_same_v< C , std::less<> > || std::is_same_v< C , std::less< T > >;
230
232 static constexpr bool kByValue = std::is_trivially_copyable_v< T > &&
233 ( sizeof( T ) <= 16 ) &&
234 std::is_default_constructible_v< T >;
235
236 using Key = std::conditional_t< kByValue , T , const T * >;
237
239 static constexpr std::uint32_t kSup = std::uint32_t( 1 ) << 31;
240
242 struct Node
243 {
244 Key key;
245 std::uint32_t src;
246 };
247
249 static const T & keyOf( const Node & a );
250
252 static T supKey( void );
253
255 [[nodiscard]] bool beats( const Node & a , const Node & b ) const;
256
258 void replay( Node cur );
259
261 [[nodiscard]] bool winnerIsBest( void ) const;
262
263 std::size_t f_k;
264 std::size_t f_K;
265 std::vector< Node > f_node;
266 std::vector< Node > f_leaf;
267 C f_less;
268
269 }; // end( class FlatLoserTree )
270
271/*--------------------------------------------------------------------------*/
272
316template< class I , class O , class C = std::less<> >
317O multiwayMerge( const std::vector< std::pair< I , I > > & R , O out ,
318 C less = C() );
319
320/*--------------------------------------------------------------------------*/
321
322} // end( namespace ADS )
323
324/*--------------------------------------------------------------------------*/
325/*-------------------------- IMPLEMENTATION --------------------------------*/
326/*--------------------------------------------------------------------------*/
327
328#include "ADS/priority/impl/MultiwayMerge.tpp"
329
330/*--------------------------------------------------------------------------*/
331
332#endif /* MultiwayMerge.h included */
333
334/*--------------------------------------------------------------------------*/
335/*------------------------ End File MultiwayMerge.h ------------------------*/
336/*--------------------------------------------------------------------------*/
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...
void start(std::size_t s, const T &x)
before build(): the source starts with the key , whose address is kept if T is large
bool empty(void) const
whether all the sources are exhausted
std::size_t winner(void) const
the source with the smallest key
void build(void)
plays the matches of the keys given by start(), in time
FlatLoserTree(std::size_t k, C less=C())
the tree of sources, all exhausted
std::size_t sources(void) const
the number of sources,
const T & top(void) const
the key of the winner
void exhaustTop(void)
the winner is exhausted, and the matches on its path are replayed as by replaceTop(),...
void replaceTop(const T &x)
the winner takes the key , and the matches on its path are replayed, matches
O multiwayMerge(E &ex, const std::vector< std::pair< I, I > > &R, O out, C less=C())
the items of the sorted ranges merged into out on the threads of ex, the range of lower index first ...
Definition ADT.h:96