ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MeldableHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File MeldableHeap.h --------------------------*/
3/*--------------------------------------------------------------------------*/
56/*--------------------------------------------------------------------------*/
57/*----------------------------- DEFINITIONS --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#ifndef __ADS_MeldableHeap
61 #define __ADS_MeldableHeap
62/* self-identification: #endif constant */
63
64/*--------------------------------------------------------------------------*/
65/*------------------------------ INCLUDES ----------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#include "ADS/Contract.h"
69#include "ADS/Portable.h"
71
72#include <algorithm>
73#include <cstddef>
74#include <cstdint>
75#include <functional>
76#include <memory>
77#include <utility>
78#include <vector>
79
80/*--------------------------------------------------------------------------*/
81/*--------------------------- NAMESPACE ------------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84namespace ADS
85{
86
87/*--------------------------------------------------------------------------*/
88/*-------------------------- CLASS MeldableHeap ----------------------------*/
89/*--------------------------------------------------------------------------*/
90
92template< class T , class Less = std::less< T > ,
93 class A = std::allocator< T > >
95{
96
97 struct Node;
98
99/*--------------------------------------------------------------------------*/
100 public:
101 using Item = T;
102 using Handle = Node *;
103
105 explicit MeldableHeap( Less less = Less() , std::uint64_t seed = 1 ,
106 const A & alloc = A() );
107
109 MeldableHeap( MeldableHeap && o ) noexcept;
110
113
114 MeldableHeap( const MeldableHeap & ) = delete;
115 MeldableHeap & operator=( const MeldableHeap & ) = delete;
116
119
120/*--------------------------------------------------------------------------*/
121
124 Handle push( T x );
125
128 [[nodiscard]] const T & top( void ) const;
129
132 [[nodiscard]] const T * secondTop( void ) const;
133
137 T pop( void );
138
162 T erase( Handle h );
163
165 [[nodiscard]] static const T & get( Handle h );
166
171 void meld( MeldableHeap & o );
172
191 [[nodiscard]] std::vector< T > smallest( std::size_t k ) const;
192
194 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
195
197 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
198
200 void clear( void );
201
202/*--------------------------------------------------------------------------*/
203 private:
205 struct Node
206 {
207 T x;
208 Node * left = nullptr;
209 Node * right = nullptr;
210 Node * parent = nullptr;
211 };
212
213 using NodeAlloc =
214 typename std::allocator_traits< A >::template rebind_alloc< Node >;
215 using Traits = std::allocator_traits< NodeAlloc >;
216
218 bool coin( void );
219
244 Node * merge( Node * a , Node * b );
245
247 Node * make( T && x );
248
250 void drop( Node * u );
251
254 [[nodiscard]] bool subtree( const Node * u , std::size_t & n ) const;
255
258 [[nodiscard]] bool consistent( void ) const;
259
260 Node * f_root = nullptr;
261 std::size_t f_n = 0;
262 ADS_NO_UNIQUE_ADDRESS Less f_less;
263 SplitMix64 f_g;
264 std::uint64_t f_bits = 0;
265 int f_left = 0;
266 ADS_NO_UNIQUE_ADDRESS NodeAlloc f_alloc;
267
268 }; // end( class MeldableHeap )
269
270/*--------------------------------------------------------------------------*/
271
272} // end( namespace ADS )
273
274/*--------------------------------------------------------------------------*/
275/*-------------------------- IMPLEMENTATION --------------------------------*/
276/*--------------------------------------------------------------------------*/
277
278#include "ADS/priority/impl/MeldableHeap.tpp"
279
280/*--------------------------------------------------------------------------*/
281
282#endif /* MeldableHeap.h included */
283
284/*--------------------------------------------------------------------------*/
285/*------------------------ End File MeldableHeap.h -------------------------*/
286/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
Hashing with random tables, beyond the book:
bool empty(void) const
true if there are no items
Definition MeldableHeap.h:197
T pop(void)
removes the smallest item and returns it, merging the two subtrees of the root, in expected time
static const T & get(Handle h)
the item of the handle
MeldableHeap(const MeldableHeap &)=delete
~MeldableHeap()
frees the nodes
const T & top(void) const
the smallest item
MeldableHeap(MeldableHeap &&o) noexcept
the heap of o, which is left empty
const T * secondTop(void) const
the second smallest item, the smaller child of the root, or nil if there are fewer than two items (ex...
T erase(Handle h)
removes the item of the handle and returns it, merging the two subtrees of its node in its place,...
void clear(void)
removes every item
MeldableHeap & operator=(const MeldableHeap &)=delete
Node * Handle
what push() gives, for erase()
Definition MeldableHeap.h:102
Handle push(T x)
adds , merging the heap with a heap of one node, in expected time; the handle of its node
std::vector< T > smallest(std::size_t k) const
the smallest items in increasing order, or all of them if fewer, in time (exercise 10....
MeldableHeap & operator=(MeldableHeap &&o) noexcept
the heap of o, which gets the items of this one
std::size_t size(void) const
the number of items
Definition MeldableHeap.h:194
void meld(MeldableHeap &o)
moves the items of o into this heap, in expected time; o is left empty, and its handles become handl...
MeldableHeap(Less less=Less(), std::uint64_t seed=1, const A &alloc=A())
the empty heap of the order less, its coins drawn from the seed
T Item
the type of the items
Definition MeldableHeap.h:101
the generator SplitMix64, a UniformRandomBitGenerator
Definition RandomHashing.h:122
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
SortedListSet< T > merge(const SortedListSet< T > &A, const SortedListSet< T > &B, bool a, bool b, bool both)
the merge of the sorted lists of and : an item of only, of only or of both goes into the result if...
Definition ADT.h:96