ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PackedArray.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File PackedArray.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
56/*--------------------------------------------------------------------------*/
57/*----------------------------- DEFINITIONS --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#ifndef __ADS_PackedArray
61 #define __ADS_PackedArray
62/* self-identification: #endif constant */
63
64/*--------------------------------------------------------------------------*/
65/*------------------------------ INCLUDES ----------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#include "ADS/Contract.h"
71
72#include <algorithm>
73#include <array>
74#include <atomic>
75#include <cstddef>
76#include <cstdint>
77#include <span>
78#include <string_view>
79#include <vector>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS
86{
87
88/*--------------------------------------------------------------------------*/
89/*-------------------------- CLASS PackedArray -----------------------------*/
90/*--------------------------------------------------------------------------*/
91
94template< unsigned B , bool Saturate = false >
96{
97
98 static_assert( ( B == 1 ) || ( B == 2 ) || ( B == 4 ) || ( B == 8 ) ||
99 ( B == 16 ) || ( B == 32 ) ,
100 "a field of 1, 2, 4, 8, 16 or 32 bits" );
101
102/*--------------------------------------------------------------------------*/
103 public:
104 using Word = std::uint64_t;
105
107 static constexpr std::size_t PER = 64 / B;
108
110 static constexpr Word MAX = ( Word( 1 ) << B ) - 1;
111
113 PackedArray( void ) = default;
114
117 explicit PackedArray( std::size_t n , Word v = 0 );
118
141 template< Executor E , class F >
142 PackedArray( E & ex , std::size_t n , F && f );
143
149 PackedArray( std::span< const Word > w , std::size_t n );
150
152 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
153
155 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
156
160 [[nodiscard]] Word get( std::size_t i ) const;
161
164 [[nodiscard]] Word operator[]( std::size_t i ) const { return( get( i ) ); }
165
181 bool set( std::size_t i , Word v );
182
185 void push_back( Word v );
186
189 [[nodiscard]] std::size_t clamped( void ) const { return( f_clamped ); }
190
193 [[nodiscard]] const std::vector< Word > & words( void ) const {
194 return( f_w );
195 }
196
198 [[nodiscard]] std::size_t bytes( void ) const {
199 return( f_w.size() * sizeof( Word ) );
200 }
201
203 friend bool operator==( const PackedArray & a , const PackedArray & b ) {
204 return( ( a.f_n == b.f_n ) && ( a.f_w == b.f_w ) );
205 }
206
207/*----------------------------- THE PERSISTENCE ----------------------------*/
208
210 static constexpr std::string_view persistName = "PackedArray";
211
213 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
214 return( detail::persistMix( { B , Saturate } ) );
215 }
216
219 template< class A >
220 void persist( A & a );
221
222/*--------------------------------------------------------------------------*/
223 private:
225 [[nodiscard]] bool clean( void ) const;
226
227 std::vector< Word > f_w;
228 std::size_t f_n = 0;
229 std::size_t f_clamped = 0;
230
231 }; // end( class PackedArray )
232
233/*--------------------------------------------------------------------------*/
234/*--------------------------- CLASS TritArray ------------------------------*/
235/*--------------------------------------------------------------------------*/
236
239{
240
241/*--------------------------------------------------------------------------*/
242 public:
244 TritArray( void ) = default;
245
248 explicit TritArray( std::size_t n , unsigned v = 0 );
249
268 template< Executor E , class F >
269 TritArray( E & ex , std::size_t n , F && f );
270
272 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
273
275 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
276
281 [[nodiscard]] unsigned get( std::size_t i ) const;
282
285 [[nodiscard]] unsigned operator[]( std::size_t i ) const {
286 return( get( i ) );
287 }
288
302 void set( std::size_t i , unsigned v );
303
306 void push_back( unsigned v );
307
309 [[nodiscard]] const std::vector< std::uint8_t > & data( void ) const {
310 return( f_y );
311 }
312
314 [[nodiscard]] std::size_t bytes( void ) const { return( f_y.size() ); }
315
317 friend bool operator==( const TritArray & a , const TritArray & b ) {
318 return( ( a.f_n == b.f_n ) && ( a.f_y == b.f_y ) );
319 }
320
321/*----------------------------- THE PERSISTENCE ----------------------------*/
322
324 static constexpr std::string_view persistName = "TritArray";
325
328 template< class A >
329 void persist( A & a );
330
331/*--------------------------------------------------------------------------*/
332 private:
334 static constexpr std::array< std::array< std::uint8_t , 5 > , 243 > DIGITS =
335 [] {
336 std::array< std::array< std::uint8_t , 5 > , 243 > d{};
337 for( int b = 0 ; b < 243 ; ++b )
338 for( int j = 0 , x = b ; j < 5 ; ++j , x /= 3 )
339 d[ std::size_t( b ) ][ std::size_t( j ) ] = std::uint8_t( x % 3 );
340 return( d );
341 }();
342
344 static constexpr std::uint8_t POW3[ 5 ] = { 1 , 3 , 9 , 27 , 81 };
345
348 [[nodiscard]] bool clean( void ) const;
349
350 std::vector< std::uint8_t > f_y;
351 std::size_t f_n = 0;
352
353 }; // end( class TritArray )
354
355/*--------------------------------------------------------------------------*/
356
357} // end( namespace ADS )
358
359/*--------------------------------------------------------------------------*/
360/*-------------------------- IMPLEMENTATION --------------------------------*/
361/*--------------------------------------------------------------------------*/
362
363#include "ADS/succinct/impl/PackedArray.tpp"
364
365/*--------------------------------------------------------------------------*/
366
367#endif /* PackedArray.h included */
368
369/*--------------------------------------------------------------------------*/
370/*------------------------- End File PackedArray.h -------------------------*/
371/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
Word operator[](std::size_t i) const
as get( i )
Definition PackedArray.h:164
friend bool operator==(const PackedArray &a, const PackedArray &b)
whether the two arrays have the same fields
Definition PackedArray.h:203
static constexpr Word MAX
the largest value of a field,
Definition PackedArray.h:110
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
std::size_t clamped(void) const
the values clamped by the constructors, by set() and by push_back(), 0 unless Saturate
Definition PackedArray.h:189
std::uint64_t Word
the type of the words
Definition PackedArray.h:104
PackedArray(std::span< const Word > w, std::size_t n)
the fields of the words , the field in the bits from of the word , as in words() (and in the int_...
bool empty(void) const
whether there is no field
Definition PackedArray.h:155
PackedArray(std::size_t n, Word v=0)
fields of the value
std::size_t bytes(void) const
the memory of the fields, in bytes
Definition PackedArray.h:198
const std::vector< Word > & words(void) const
the words, the field in the word , the bits past the last field 0
Definition PackedArray.h:193
std::size_t size(void) const
the number of fields,
Definition PackedArray.h:152
PackedArray(void)=default
the empty array
PackedArray(E &ex, std::size_t n, F &&f)
the fields f( i ) for , filled by blocks of whole words on the threads of ex, time on threads
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition PackedArray.h:210
static constexpr std::size_t PER
the fields in a word
Definition PackedArray.h:107
static constexpr std::uint64_t persistTag(void)
the width of the fields, in a file
Definition PackedArray.h:213
bool set(std::size_t i, Word v)
the field set to , or to if is larger and Saturate; true if was clamped
Word get(std::size_t i) const
the value of the field , the bits on of the word
void push_back(Word v)
appends a field of the value , clamped if Saturate
const std::vector< std::uint8_t > & data(void) const
the bytes, five values each, those past the last value 0
Definition PackedArray.h:309
bool empty(void) const
whether there is no value
Definition PackedArray.h:275
friend bool operator==(const TritArray &a, const TritArray &b)
whether the two arrays have the same values
Definition PackedArray.h:317
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition PackedArray.h:324
TritArray(std::size_t n, unsigned v=0)
values
TritArray(E &ex, std::size_t n, F &&f)
the values f( i ) for , filled by blocks of whole bytes on the threads of ex
TritArray(void)=default
the empty array
void push_back(unsigned v)
appends the value
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
unsigned operator[](std::size_t i) const
as get( i )
Definition PackedArray.h:285
std::size_t size(void) const
the number of values,
Definition PackedArray.h:272
void set(std::size_t i, unsigned v)
the value set to
std::size_t bytes(void) const
the memory of the values, in bytes
Definition PackedArray.h:314
unsigned get(std::size_t i) const
the value , the digit of the byte , from the table of the digits of the 243 bytes
Definition ADT.h:96