ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DaryHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File DaryHeap.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
88/*--------------------------------------------------------------------------*/
89/*----------------------------- DEFINITIONS --------------------------------*/
90/*--------------------------------------------------------------------------*/
91
92#ifndef __ADS_DaryHeap
93 #define __ADS_DaryHeap
94/* self-identification: #endif constant */
95
96/*--------------------------------------------------------------------------*/
97/*------------------------------ INCLUDES ----------------------------------*/
98/*--------------------------------------------------------------------------*/
99
100#include "ADS/Contract.h"
103
104#include <algorithm>
105#include <cstddef>
106#include <functional>
107#include <iterator>
108#include <utility>
109#include <vector>
110
111/*--------------------------------------------------------------------------*/
112/*--------------------------- NAMESPACE ------------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115namespace ADS
116{
117
118/*--------------------------------------------------------------------------*/
119/*---------------------------- CLASS DaryHeap ------------------------------*/
120/*--------------------------------------------------------------------------*/
121
125template< class T , class Better = std::less< T > , unsigned D = 4 ,
126 bool BottomUp = false , class A = HugePageAllocator< T > >
128{
129
130 static_assert( D >= 2 , "a heap needs at least two children per node" );
131
132/*--------------------------------------------------------------------------*/
133 public:
134 using Item = T;
135
137 explicit DaryHeap( Better better = Better() , const A & alloc = A() );
138
155 void push( const T & x );
156
159 [[nodiscard]] const T & top( void ) const;
160
187 T pop( void );
188
211 T erase( std::size_t i );
212
216 [[nodiscard]] const T * secondTop( void ) const;
217
236 [[nodiscard]] std::vector< T > smallest( std::size_t k ) const;
237
241 [[nodiscard]] static constexpr std::size_t parent( std::size_t i ) {
242 return( ( i - 1 ) / D );
243 }
244
247 [[nodiscard]] static constexpr std::size_t child( std::size_t i ,
248 std::size_t j ) {
249 return( D * i + j + 1 );
250 }
251
253 [[nodiscard]] bool empty( void ) const;
254
256 [[nodiscard]] std::size_t size( void ) const;
257
260 void reserve( std::size_t n );
261
263 void clear( void );
264
282 template< std::input_iterator I , std::sentinel_for< I > S >
283 void assign( I first , S last );
284
285/*------------------------- THE STANDARD INTERFACE -------------------------*/
286
287 // a constant view of the items in the order of the heap (the root first,
288 // the rest in no useful order), contiguous; a change through it would
289 // break the heap
290
292 using value_type = T;
294 using size_type = std::size_t;
296 using difference_type = std::ptrdiff_t;
298 using reference = const T &;
300 using const_reference = const T &;
302 using iterator = const T *;
304 using const_iterator = const T *;
306 using allocator_type = A;
308 [[nodiscard]] allocator_type get_allocator( void ) const {
309 return( allocator_type( f_A.get_allocator() ) );
310 }
311
313 [[nodiscard]] const_iterator begin( void ) const { return( f_A.data() ); }
315 [[nodiscard]] const_iterator end( void ) const {
316 return( f_A.data() + f_A.size() );
317 }
318
319 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
321 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
322
323/*--------------------------------------------------------------------------*/
324 private:
325 // the hole at i moves up (down) until x fits in it; x comes as a value,
326 // never stored and read back, which would stall the forwarding of the
327 // store to the load
328
329 void up( std::size_t i , T x );
330
331 void down( std::size_t i , T x );
332
335 void bottomUp( T x );
336
339 [[nodiscard]] bool ordered( void ) const;
340
342 detail::AllocVector< A , T > f_A;
343 Better f_better;
344
345 }; // end( class DaryHeap )
346
347/*--------------------------------------------------------------------------*/
348
349} // end( namespace ADS )
350
351/*--------------------------------------------------------------------------*/
352/*-------------------------- IMPLEMENTATION --------------------------------*/
353/*--------------------------------------------------------------------------*/
354
355#include "ADS/priority/impl/DaryHeap.tpp"
356
357/*--------------------------------------------------------------------------*/
358
359#endif /* DaryHeap.h included */
360
361/*--------------------------------------------------------------------------*/
362/*-------------------------- End File DaryHeap.h ---------------------------*/
363/*--------------------------------------------------------------------------*/
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
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...
void assign(I first, S last)
the items of in place of those of the heap, arranged by the construction of Floyd in time for item...
void clear(void)
removes all the items
std::ptrdiff_t difference_type
Definition DaryHeap.h:296
DaryHeap(Better better=Better(), const A &alloc=A())
the empty heap of the order better, its vector from alloc
const_iterator cbegin(void) const
the same as begin()
Definition DaryHeap.h:319
const_iterator cend(void) const
the same as end()
Definition DaryHeap.h:321
void reserve(std::size_t n)
room for items, so that the pushes up to do not reallocate
std::size_t size_type
Definition DaryHeap.h:294
const_iterator end(void) const
the end of the view
Definition DaryHeap.h:315
static constexpr std::size_t parent(std::size_t i)
the parent of the place , (the layout of Eytzinger with children, exercise 10.4 of Morin's book)
Definition DaryHeap.h:241
allocator_type get_allocator(void) const
the allocator of the vector
Definition DaryHeap.h:308
E Item
Definition DaryHeap.h:134
T pop(void)
removes the best item and returns it, the last item moved down from the root, in time
HugePageAllocator< E > allocator_type
Definition DaryHeap.h:306
E value_type
Definition DaryHeap.h:292
std::size_t size(void) const
the number of items,
static constexpr std::size_t child(std::size_t i, std::size_t j)
the child of the place , (exercise 10.4 of Morin's book)
Definition DaryHeap.h:247
const_iterator begin(void) const
the first item of the view
Definition DaryHeap.h:313
bool empty(void) const
whether the heap has no item
const E & reference
Definition DaryHeap.h:298
const E * const_iterator
Definition DaryHeap.h:304
const E & const_reference
Definition DaryHeap.h:300
const E * iterator
Definition DaryHeap.h:302
void push(const T &x)
pushes , moving it up while better than its parent, in time
std::vector< T > smallest(std::size_t k) const
the best items in order, or all of them if fewer, in time (exercise 10.10 of Morin's book)
const T * secondTop(void) const
the second best item, the best of the children of the root, or nil if there are fewer than two items,...
const T & top(void) const
the best item
T erase(std::size_t i)
removes the item in the place of the view and returns it, in time: the last item takes its place an...
Definition ADT.h:96