ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SequenceHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File SequenceHeap.h --------------------------*/
3/*--------------------------------------------------------------------------*/
101/*--------------------------------------------------------------------------*/
102/*----------------------------- DEFINITIONS --------------------------------*/
103/*--------------------------------------------------------------------------*/
104
105#ifndef __ADS_SequenceHeap
106 #define __ADS_SequenceHeap
107/* self-identification: #endif constant */
108
109/*--------------------------------------------------------------------------*/
110/*------------------------------ INCLUDES ----------------------------------*/
111/*--------------------------------------------------------------------------*/
112
113#include "ADS/Contract.h"
116#include "ADS/sort/Sort.h"
117
118#include <algorithm>
119#include <cstddef>
120#include <functional>
121#include <iterator>
122#include <utility>
123#include <vector>
124
125/*--------------------------------------------------------------------------*/
126/*--------------------------- NAMESPACE ------------------------------------*/
127/*--------------------------------------------------------------------------*/
128
129namespace ADS
130{
131
132/*--------------------------------------------------------------------------*/
133/*-------------------------- CLASS SequenceHeap ----------------------------*/
134/*--------------------------------------------------------------------------*/
135
137template< class T , class Better = std::less< T > >
139{
140
141/*--------------------------------------------------------------------------*/
142 public:
143 using Item = T;
144
146 static constexpr std::size_t M = 4096;
147
149 static constexpr std::size_t K = 16;
150
152 explicit SequenceHeap( Better better = Better() )
153 : f_better( std::move( better ) ) { }
154
193 void push( const T & x );
194
197 [[nodiscard]] const T & top( void );
198
240 T pop( void );
241
243 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
244
246 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
247
249 void clear( void );
250
251/*--------------------------------------------------------------------------*/
252 private:
254 struct Run
255 {
256 std::vector< T > a;
257 std::size_t at = 0;
258
260 [[nodiscard]] bool done( void ) const { return( at == a.size() ); }
261
263 [[nodiscard]] std::size_t left( void ) const { return( a.size() - at ); }
264 };
265
267 struct Group
268 {
269 std::vector< Run > seqs;
270 Run buf;
271 };
272
275 void flush( void );
276
279 void join( std::size_t i , Run && s );
280
282 void refill( Group & g );
283
286 void refillDeletion( void );
287
289 static constexpr std::size_t D = 4;
290
293 void up( std::size_t i );
294
296 void down( std::size_t i , T x );
297
300 void ready( void );
301
304 void close( void );
305
307 void refillBulk( void );
308
310 void sortRange( T * a , T * b );
311
314 T * partition( T * a , T * b , const T & pv );
315
318 void merge( std::vector< Run * > & R , std::vector< T > & out ,
319 std::size_t n );
320
323 void merge2( const T * a , std::size_t na , const T * b , std::size_t nb ,
324 std::vector< T > & out ) const;
325
328 [[nodiscard]] bool bt( const T & a , const T & b ) const {
329 if constexpr( detail::kKeyed< T , Better > )
330 return( detail::orderKeyBy< Better >( a ) <
331 detail::orderKeyBy< Better >( b ) );
332 else
333 return( f_better( a , b ) );
334 }
335
337 [[nodiscard]] bool worse( const T & a , const T & b ) const {
338 return( bt( b , a ) );
339 }
340
342 [[nodiscard]] bool ordered( void ) const;
343
344 std::vector< T > f_ins;
345 Run f_del;
346 std::vector< Group > f_groups;
347 std::vector< T > f_scratch;
348 bool f_heaped = true;
349 bool f_open = false;
350 int f_scans = 0;
351 bool f_minLast = false;
353 std::vector< T , HugePageAllocator< T > > f_bulk;
354 std::size_t f_lo = 0;
357 std::vector< std::pair< std::size_t , std::size_t > > f_piv;
358 Run f_bbuf;
359 std::vector< T > f_key;
360 std::vector< unsigned char > f_dead;
361 std::vector< std::size_t > f_lose;
362 std::vector< std::size_t > f_lose2;
363 std::vector< T * > f_cur , f_end;
364 std::size_t f_n = 0;
365 Better f_better;
366
367 }; // end( class SequenceHeap )
368
369/*--------------------------------------------------------------------------*/
370
371} // end( namespace ADS )
372
373/*--------------------------------------------------------------------------*/
374/*-------------------------- IMPLEMENTATION --------------------------------*/
375/*--------------------------------------------------------------------------*/
376
377#include "ADS/priority/impl/SequenceHeap.tpp"
378
379/*--------------------------------------------------------------------------*/
380
381#endif /* SequenceHeap.h included */
382
383/*--------------------------------------------------------------------------*/
384/*------------------------- End File SequenceHeap.h ------------------------*/
385/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
The unsigned key of an item whose order is that of an unsigned word, beyond the book,...
The sort that chooses its algorithm, beyond the book: sort() and stableSort() look at what they sort ...
bool empty(void) const
whether the heap has no item
Definition SequenceHeap.h:243
T pop(void)
removes the best item and returns it, in amortized time
const T & top(void)
the best item
std::size_t size(void) const
the number of items,
Definition SequenceHeap.h:246
T Item
the type of the items
Definition SequenceHeap.h:143
void clear(void)
removes all the items
void push(const T &x)
pushes , in amortized time
SequenceHeap(Better better=Better())
the empty heap of the order better
Definition SequenceHeap.h:152
static constexpr std::size_t K
the sequences of a group,
Definition SequenceHeap.h:149
static constexpr std::size_t M
the items of the insertion heap and of a buffer,
Definition SequenceHeap.h:146
Definition ADT.h:96