ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MinBoard.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File MinBoard.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
43/*--------------------------------------------------------------------------*/
44/*----------------------------- DEFINITIONS --------------------------------*/
45/*--------------------------------------------------------------------------*/
46
47#ifndef __ADS_MinBoard
48 #define __ADS_MinBoard
49/* self-identification: #endif constant */
50
51/*--------------------------------------------------------------------------*/
52/*------------------------------ INCLUDES ----------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55#include "ADS/Contract.h"
56
57#include <atomic>
58#include <concepts>
59#include <cstddef>
60#include <limits>
61#include <memory>
62#include <type_traits>
63
64/*--------------------------------------------------------------------------*/
65/*--------------------------- NAMESPACE ------------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68namespace ADS
69{
70
71/*--------------------------------------------------------------------------*/
72/*---------------------------- CLASS MinBoard ------------------------------*/
73/*--------------------------------------------------------------------------*/
74
77template< class K >
78 requires( std::totally_ordered< K > && std::is_trivially_copyable_v< K > )
80{
81
82 static_assert( std::atomic< K >::is_always_lock_free ,
83 "the keys are atomic words without locks" );
84
85/*--------------------------------------------------------------------------*/
86 public:
87 using Key = K;
88
90 static constexpr std::size_t kLine = 64;
91
95 explicit MinBoard( std::size_t p , K k = std::numeric_limits< K >::max() )
96 : f_p( p ) , f_s( new Slot[ p ] ) {
97 ADS_PRE( p >= 1 );
98 for( std::size_t i = 0 ; i < p ; ++i )
99 f_s[ i ].k.store( k , std::memory_order_relaxed );
100 }
101
103 [[nodiscard]] std::size_t size( void ) const { return( f_p ); }
104
117 void publish( std::size_t i , K k ) {
118 ADS_PRE( i < f_p );
119 std::atomic< K > & s = f_s[ i ].k;
120 if( s.load( std::memory_order_relaxed ) != k )
121 s.store( k , std::memory_order_relaxed );
122 }
123
126 [[nodiscard]] K at( std::size_t i ) const {
127 ADS_PRE( i < f_p );
128 return( f_s[ i ].k.load( std::memory_order_relaxed ) );
129 }
130
145 [[nodiscard]] K min( void ) const {
146 K m = f_s[ 0 ].k.load( std::memory_order_relaxed );
147 for( std::size_t i = 1 ; i < f_p ; ++i ) {
148 const K x = f_s[ i ].k.load( std::memory_order_relaxed );
149 if( x < m )
150 m = x;
151 }
152 return( m );
153 }
154
157 void reset( K k = std::numeric_limits< K >::max() ) {
158 for( std::size_t i = 0 ; i < f_p ; ++i )
159 f_s[ i ].k.store( k , std::memory_order_relaxed );
160 }
161
162/*--------------------------------------------------------------------------*/
163 private:
165 struct alignas( kLine ) Slot
166 {
167 std::atomic< K > k;
168 };
169
170 static_assert( sizeof( Slot ) == kLine );
171
172 std::size_t f_p;
173 std::unique_ptr< Slot[] > f_s;
174
175 }; // end( class MinBoard )
176
177/*--------------------------------------------------------------------------*/
178
179} // end( namespace ADS )
180
181/*--------------------------------------------------------------------------*/
182
183#endif /* MinBoard.h included */
184
185/*--------------------------------------------------------------------------*/
186/*-------------------------- End File MinBoard.h ---------------------------*/
187/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
void publish(std::size_t i, K k)
in the slot , by a relaxed store made only if the slot holds another key, read by a relaxed load
Definition MinBoard.h:117
std::size_t size(void) const
the number of the slots
Definition MinBoard.h:103
K min(void) const
the least of the keys read in the slots, one relaxed load each: a value of each slot not older than t...
Definition MinBoard.h:145
MinBoard(std::size_t p, K k=std::numeric_limits< K >::max())
the board of slots, each holding (by default the largest key, which no published key is above)
Definition MinBoard.h:95
K Key
the type of the keys
Definition MinBoard.h:87
static constexpr std::size_t kLine
the bytes of a slot, a line of cache
Definition MinBoard.h:90
K at(std::size_t i) const
the key of the slot , by a relaxed load
Definition MinBoard.h:126
void reset(K k=std::numeric_limits< K >::max())
every slot set to
Definition MinBoard.h:157
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96