ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
OrderMaintenanceList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------- File OrderMaintenanceList.h ----------------------*/
3/*--------------------------------------------------------------------------*/
66/*--------------------------------------------------------------------------*/
67/*----------------------------- DEFINITIONS --------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#ifndef __ADS_OrderMaintenanceList
71 #define __ADS_OrderMaintenanceList
72/* self-identification: #endif constant */
73
74/*--------------------------------------------------------------------------*/
75/*------------------------------ INCLUDES ----------------------------------*/
76/*--------------------------------------------------------------------------*/
77
78#include "ADS/Contract.h"
79
80#include <array>
81#include <cmath>
82#include <cstddef>
83#include <cstdint>
84#include <utility>
85
86/*--------------------------------------------------------------------------*/
87/*--------------------------- NAMESPACE ------------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90namespace ADS
91{
92
93/*--------------------------------------------------------------------------*/
94/*----------------------- CLASS OrderMaintenanceList -----------------------*/
95/*--------------------------------------------------------------------------*/
96
99template< class T >
101{
102
103 struct Node;
104 struct Bucket;
105
106/*--------------------------------------------------------------------------*/
107 public:
108 using Item = T;
109
111 using Element = const Node *;
112
114 static constexpr std::size_t B = 64;
115
117 OrderMaintenanceList( void ) = default;
118
121
124
127
129 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
130
160 Element addAfter( Element e , const T & v );
161
165 void remove( Element e );
166
170 [[nodiscard]] bool testBefore( Element a , Element b ) const {
171 ADS_PRE( a && b && a->bucket && b->bucket );
172 return( a->bucket == b->bucket ? a->label < b->label
173 : a->bucket->label < b->bucket->label );
174 }
175
177 [[nodiscard]] static const T & value( Element e ) { return( e->item ); }
178
180 [[nodiscard]] Element first( void ) const { return( f_first ); }
181
184 [[nodiscard]] Element next( Element e ) const {
185 ADS_PRE( e && e->bucket );
186 return( e->next );
187 }
188
191 [[nodiscard]] std::pair< std::size_t , std::size_t >
192 relabelWork( void ) const {
193 return( std::pair< std::size_t , std::size_t >( f_relabels , f_work ) );
194 }
195
196/*--------------------------------------------------------------------------*/
197 private:
199 struct Node
200 {
201 T item;
202 Node * prev = nullptr;
203 Node * next = nullptr;
204 Bucket * bucket = nullptr;
205 std::uint64_t label = 0;
206 };
207
209 struct Bucket
210 {
211 Bucket * prev = nullptr;
212 Bucket * next = nullptr;
213 Node * head = nullptr;
214 std::size_t size = 0;
215 std::uint64_t label = 0;
216 };
217
219 static constexpr std::uint64_t kU = std::uint64_t( 1 ) << 63;
220
222 void relabelBucket( Bucket * b );
223
227 void split( Bucket * b );
228
255 void addBucketAfter( Bucket * b , Bucket * c );
256
259 [[nodiscard]] bool valid( const Bucket * b ) const;
260
262 [[nodiscard]] static const std::array< double , 64 > & caps( void );
263
264 Bucket f_head;
265 Node * f_first = nullptr;
266 std::size_t f_n = 0;
267 std::size_t f_relabels = 0;
268 std::size_t f_work = 0;
269
270 }; // end( class OrderMaintenanceList )
271
272/*--------------------------------------------------------------------------*/
273
274} // end( namespace ADS )
275
276/*--------------------------------------------------------------------------*/
277/*-------------------------- IMPLEMENTATION --------------------------------*/
278/*--------------------------------------------------------------------------*/
279
280#include "ADS/sequence/impl/OrderMaintenanceList.tpp"
281
282/*--------------------------------------------------------------------------*/
283
284#endif /* OrderMaintenanceList.h included */
285
286/*--------------------------------------------------------------------------*/
287/*--------------------- End File OrderMaintenanceList.h --------------------*/
288/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
static const T & value(Element e)
the item of
Definition OrderMaintenanceList.h:177
static constexpr std::size_t B
the most elements of a bucket
Definition OrderMaintenanceList.h:114
OrderMaintenanceList(const OrderMaintenanceList &)=delete
no copy, since the elements are pointers
void remove(Element e)
out of the list, and its bucket if it empties, in time, no label changed
const Node * Element
an element of the list, valid until it is removed
Definition OrderMaintenanceList.h:111
T Item
the type of the items
Definition OrderMaintenanceList.h:108
bool testBefore(Element a, Element b) const
whether comes before , by the labels of their buckets and by their local labels, in time
Definition OrderMaintenanceList.h:170
Element first(void) const
the first element, nil if the list is empty
Definition OrderMaintenanceList.h:180
std::pair< std::size_t, std::size_t > relabelWork(void) const
the number of the relabelings of a range of buckets so far, and the labels written by them and by the...
Definition OrderMaintenanceList.h:192
~OrderMaintenanceList()
frees the elements
Element next(Element e) const
the element after , nil after the last
Definition OrderMaintenanceList.h:184
OrderMaintenanceList(void)=default
the empty list
Element addAfter(Element e, const T &v)
a new element of right after , at the front if is nil, in amortized time
OrderMaintenanceList & operator=(const OrderMaintenanceList &)=delete
no copy, since the elements are pointers
std::size_t size(void) const
the number of elements
Definition OrderMaintenanceList.h:129
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96