ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Arrow.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Arrow.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
62/*--------------------------------------------------------------------------*/
63/*----------------------------- DEFINITIONS --------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#ifndef __ADS_Arrow
67 #define __ADS_Arrow
68/* self-identification: #endif constant */
69
70/*--------------------------------------------------------------------------*/
71/*------------------------------ INCLUDES ----------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#include "ADS/Contract.h"
75#include "ADS/Portable.h"
79
80#include <bit>
81#include <cstddef>
82#include <cstdint>
83#include <cstring>
84#include <iterator>
85#include <memory>
86#include <ranges>
87#include <span>
88#include <stdexcept>
89#include <string>
90#include <string_view>
91#include <type_traits>
92#include <utility>
93#include <vector>
94
95/*--------------------------------------------------------------------------*/
96/*------------------------ THE ARROW C DATA INTERFACE ----------------------*/
97/*--------------------------------------------------------------------------*/
98
99// the two structures as the specification gives them, under its guard;
100// the macros ARROW_FLAG_* are left to Arrow and nanoarrow, the latter
101// taking the first of them as the guard of its own definitions
102
103#ifndef ARROW_C_DATA_INTERFACE
104 #define ARROW_C_DATA_INTERFACE
105
106extern "C" {
107
110{
111 const char * format;
112 const char * name;
113 const char * metadata;
114 std::int64_t flags;
115 std::int64_t n_children;
118 void ( *release )( struct ArrowSchema * );
120 };
121
124{
125 std::int64_t length;
126 std::int64_t null_count;
127 std::int64_t offset;
128 std::int64_t n_buffers;
129 std::int64_t n_children;
130 const void ** buffers;
133 void ( *release )( struct ArrowArray * );
135 };
136
137 } // end( extern "C" )
138
139#endif /* ARROW_C_DATA_INTERFACE */
140
141/*--------------------------------------------------------------------------*/
142/*--------------------------- NAMESPACE ------------------------------------*/
143/*--------------------------------------------------------------------------*/
144
145namespace ADS
146{
147
148/*--------------------------------------------------------------------------*/
149/*------------------------------- THE TYPES --------------------------------*/
150/*--------------------------------------------------------------------------*/
151
154template< class T >
155concept ArrowNumber =
156 ( std::is_integral_v< T > && ! std::is_same_v< T , bool > &&
157 ( sizeof( T ) == 1 || sizeof( T ) == 2 || sizeof( T ) == 4 ||
158 sizeof( T ) == 8 ) ) ||
159 std::is_same_v< T , float > || std::is_same_v< T , double >;
160
162template< ArrowNumber T >
163[[nodiscard]] constexpr const char * arrowFormat( void )
164{
165 if constexpr( std::is_same_v< T , float > )
166 return( "f" );
167 else if constexpr( std::is_same_v< T , double > )
168 return( "g" );
169 else if constexpr( std::is_signed_v< T > )
170 return( sizeof( T ) == 1 ? "c"
171 : sizeof( T ) == 2 ? "s"
172 : sizeof( T ) == 4 ? "i"
173 : "l" );
174 else
175 return( sizeof( T ) == 1 ? "C"
176 : sizeof( T ) == 2 ? "S"
177 : sizeof( T ) == 4 ? "I"
178 : "L" );
179 }
180
181namespace detail
182{
183
185template< class X >
186struct IsPackedArray : std::false_type
187{ };
188
189template< unsigned B , bool S >
190struct IsPackedArray< PackedArray< B , S > > : std::true_type
191{ };
192
194template< class X >
195concept ArrowSequence = std::ranges::sized_range< const X > &&
196 ArrowNumber< std::ranges::range_value_t< const X > >;
197
198} // end( namespace detail )
199
202template< class X >
204 std::is_same_v< X , BitVector > || detail::IsPackedArray< X >::value ||
205 detail::ArrowSequence< X >;
206
207namespace detail
208{
209
210/*--------------------------------------------------------------------------*/
211/*--------------------------- CLASS ArrowKeep ------------------------------*/
212/*--------------------------------------------------------------------------*/
213
216struct ArrowKeep
217{
218 std::shared_ptr< const void > owner;
219 std::vector< std::uint64_t > copy;
220 const void * buffers[ 2 ] = { nullptr , nullptr };
221 };
222
224inline constexpr std::uint64_t kArrowEmpty = 0;
225
227inline void arrowReleaseArray( ArrowArray * a ) noexcept
228{
229 delete static_cast< ArrowKeep * >( a->private_data );
230 a->private_data = nullptr;
231 a->release = nullptr;
232 }
233
236inline void arrowReleaseSchema( ArrowSchema * s ) noexcept
237{
238 delete static_cast< std::string * >( s->private_data );
239 s->private_data = nullptr;
240 s->release = nullptr;
241 }
242
245template< class U , class F >
246inline void arrowCopy( ArrowKeep & k , std::size_t n , F && f )
247{
248 k.copy.assign( ( n * sizeof( U ) + 7 ) / 8 , 0 );
249 auto * p = reinterpret_cast< unsigned char * >( k.copy.data() );
250 for( std::size_t i = 0 ; i < n ; ++i ) {
251 const U v = U( f( i ) );
252 std::memcpy( p + i * sizeof( U ) , &v , sizeof( U ) );
253 }
254 }
255
258inline void arrowLittleWords( ArrowKeep & k , std::span< const Word > w )
259{
260 k.copy.assign( w.begin() , w.end() );
261 if constexpr( std::endian::native != std::endian::little )
262 for( auto & x : k.copy )
263 x = byteSwap( x );
264 }
265
267[[nodiscard]] inline bool arrowHasNulls( const ArrowArray & a )
268{
269 if( ( a.null_count == 0 ) || ( a.n_buffers < 1 ) ||
270 ( a.buffers[ 0 ] == nullptr ) )
271 return( false );
272 if( a.null_count > 0 )
273 return( true );
274 const auto * v = static_cast< const unsigned char * >( a.buffers[ 0 ] );
275 for( std::int64_t i = a.offset ; i < a.offset + a.length ; ++i )
276 if( ! ( ( v[ i / 8 ] >> ( i % 8 ) ) & 1 ) ) // not known: counted
277 return( true );
278 return( false );
279 }
280
284inline void arrowCheck( const ArrowArray & a , const ArrowSchema & s ,
285 const char * f , const char * who )
286{
287 const auto fail = [ & ]( const char * what ) {
288 throw( std::invalid_argument( std::string( "ADS::" ) + who + ": " + what ) );
289 };
290 if( ( a.release == nullptr ) || ( s.release == nullptr ) )
291 fail( "a column of Arrow already released" );
292 const std::string_view g = s.format ? s.format : "";
293 if( f ? ( g != f )
294 : ( g.size() != 1 || std::string_view( "cCsSiIlL" ).find( g[ 0 ] ) ==
295 std::string_view::npos ) )
296 fail( "a column of Arrow of another type" );
297 if( ( a.n_buffers != 2 ) || ( a.n_children != 0 ) || ( s.n_children != 0 ) ||
298 ( a.length < 0 ) || ( a.offset < 0 ) ||
299 ( a.length > 0 && a.buffers[ 1 ] == nullptr ) )
300 fail( "a column of Arrow with no buffer of values, or with children" );
301 if( arrowHasNulls( a ) )
302 fail( "a column of Arrow with nulls" );
303 }
304
308template< class F >
309inline void arrowIntegers( const ArrowArray & a , char f , F && use )
310{
311 const void * p = a.buffers[ 1 ];
312 const std::size_t o = std::size_t( a.offset ) , n = std::size_t( a.length );
313 auto each = [ & ]< class U >( const U * v ) {
314 for( std::size_t i = 0 ; i < n ; ++i )
315 use( i , v[ o + i ] );
316 };
317 switch( f ) {
318 case 'c':
319 each( static_cast< const std::int8_t * >( p ) );
320 break;
321 case 'C':
322 each( static_cast< const std::uint8_t * >( p ) );
323 break;
324 case 's':
325 each( static_cast< const std::int16_t * >( p ) );
326 break;
327 case 'S':
328 each( static_cast< const std::uint16_t * >( p ) );
329 break;
330 case 'i':
331 each( static_cast< const std::int32_t * >( p ) );
332 break;
333 case 'I':
334 each( static_cast< const std::uint32_t * >( p ) );
335 break;
336 case 'l':
337 each( static_cast< const std::int64_t * >( p ) );
338 break;
339 default:
340 each( static_cast< const std::uint64_t * >( p ) );
341 break;
342 }
343 }
344
345} // end( namespace detail )
346
347/*--------------------------------------------------------------------------*/
348/*------------------------------- THE EXPORT -------------------------------*/
349/*--------------------------------------------------------------------------*/
350
357template< class X >
359void exportArrow( std::shared_ptr< X > x , ArrowArray * a , ArrowSchema * s ,
360 std::string_view name = "" )
361{
362 using Y = std::remove_const_t< X >;
363 ADS_PRE( ( x != nullptr ) && ( a != nullptr ) && ( s != nullptr ) );
364 constexpr bool little = std::endian::native == std::endian::little;
365 auto k = std::make_unique< detail::ArrowKeep >();
366 const void * data = nullptr;
367 const char * format = nullptr;
368 const std::size_t n = std::ranges::size( *x );
369 if constexpr( std::is_same_v< Y , BitVector > ) {
370 format = "b";
371 if constexpr( little )
372 data = x->words().data();
373 else {
374 detail::arrowLittleWords( *k , x->words() );
375 data = k->copy.data();
376 }
377 }
378 else if constexpr( detail::IsPackedArray< Y >::value ) {
379 constexpr unsigned B = 64 / Y::PER;
380 if constexpr( B == 1 )
381 format = "b";
382 else if constexpr( B <= 8 )
383 format = "C";
384 else if constexpr( B == 16 )
385 format = "S";
386 else
387 format = "I";
388 if constexpr( ( B == 1 || B >= 8 ) && little )
389 data = x->words().data();
390 else if constexpr( B == 1 ) {
391 detail::arrowLittleWords( *k , x->words() );
392 data = k->copy.data();
393 }
394 else {
395 using U = std::conditional_t<
396 ( B <= 8 ) , std::uint8_t ,
397 std::conditional_t< ( B == 16 ) , std::uint16_t , std::uint32_t > >;
398 detail::arrowCopy< U >(
399 *k , n , [ & ]( std::size_t i ) { return( x->get( i ) ); } );
400 data = k->copy.data();
401 }
402 }
403 else {
404 using T = std::ranges::range_value_t< const Y >;
405 format = arrowFormat< T >();
406 if constexpr( std::ranges::contiguous_range< const Y > )
407 data = std::ranges::data( *x );
408 else {
409 auto it = std::ranges::begin( *x );
410 detail::arrowCopy< T >( *k , n ,
411 [ & ]( std::size_t ) { return( *it++ ); } );
412 data = k->copy.data();
413 }
414 }
415 if( data == nullptr ) // an empty column, whose buffer is still there
416 data = &detail::kArrowEmpty;
417 auto nm = std::make_unique< std::string >( name );
418 k->owner = std::shared_ptr< const void >( std::move( x ) );
419 k->buffers[ 1 ] = data;
420 *s = ArrowSchema{ format , nm->c_str() , nullptr , 0 ,
421 0 , nullptr , nullptr ,
422 &detail::arrowReleaseSchema ,
423 nm.get() };
424 *a = ArrowArray{
425 std::int64_t( n ) , 0 , 0 , 2 , 0 , k->buffers , nullptr ,
426 nullptr ,
427 &detail::arrowReleaseArray , k.get()
428 };
429 nm.release();
430 k.release();
431 }
432
436template< class X >
437 requires ArrowExportable< X >
438void exportArrow( X x , ArrowArray * a , ArrowSchema * s ,
439 std::string_view name = "" )
440{
441 exportArrow( std::make_shared< const X >( std::move( x ) ) , a , s , name );
442 }
443
444/*--------------------------------------------------------------------------*/
445/*---------------------------- CLASS ArrowColumn ---------------------------*/
446/*--------------------------------------------------------------------------*/
447
451template< ArrowNumber T >
453{
454
455/*--------------------------------------------------------------------------*/
456 public:
461 ArrowColumn( ArrowArray * a , const ArrowSchema & s ) {
462 detail::arrowCheck( *a , s , arrowFormat< T >() , "ArrowColumn" );
463 f_a = *a;
464 a->release = nullptr; // moved, as the specification allows
465 }
466
468 ArrowColumn( const ArrowColumn & ) = delete;
469
471 ArrowColumn & operator=( const ArrowColumn & ) = delete;
472
474 ArrowColumn( ArrowColumn && o ) noexcept : f_a( o.f_a ) {
475 o.f_a.release = nullptr;
476 }
477
479 ArrowColumn & operator=( ArrowColumn && o ) noexcept {
480 if( this != &o ) {
481 reset();
482 f_a = o.f_a;
483 o.f_a.release = nullptr;
484 }
485 return( *this );
486 }
487
489 ~ArrowColumn( void ) { reset(); }
490
492 [[nodiscard]] std::span< const T > values( void ) const {
493 if( f_a.release == nullptr || f_a.length == 0 )
494 return( std::span< const T >() );
495 return( std::span< const T >( static_cast< const T * >( f_a.buffers[ 1 ] ) +
496 std::size_t( f_a.offset ) ,
497 std::size_t( f_a.length ) ) );
498 }
499
501 [[nodiscard]] std::size_t size( void ) const { return( values().size() ); }
502
505 [[nodiscard]] T operator[]( std::size_t i ) const {
506 ADS_PRE( i < size() );
507 return( values()[ i ] );
508 }
509
511 [[nodiscard]] const T * begin( void ) const { return( values().data() ); }
512
514 [[nodiscard]] const T * end( void ) const {
515 return( values().data() + size() );
516 }
517
518/*--------------------------------------------------------------------------*/
519 private:
521 void reset( void ) noexcept {
522 if( f_a.release )
523 f_a.release( &f_a );
524 f_a.release = nullptr;
525 }
526
527 ArrowArray f_a{};
528
529 }; // end( class ArrowColumn )
530
531/*--------------------------------------------------------------------------*/
532/*------------------------------- THE IMPORT -------------------------------*/
533/*--------------------------------------------------------------------------*/
534
539inline BitVector toBitVector( const ArrowArray & a , const ArrowSchema & s )
540{
541 detail::arrowCheck( a , s , "b" , "toBitVector" );
542 const std::size_t n = std::size_t( a.length ) , o = std::size_t( a.offset );
543 const auto * p = static_cast< const unsigned char * >( a.buffers[ 1 ] );
544 std::vector< Word > w( ( n + 63 ) / 64 , 0 );
545 if( o % 8 == 0 ) { // whole bytes, in the order of a little-endian word
546 if( n > 0 )
547 std::memcpy( w.data() , p + o / 8 , ( n + 7 ) / 8 );
548 if constexpr( std::endian::native != std::endian::little )
549 for( auto & x : w )
550 x = detail::byteSwap( x );
551 }
552 else
553 for( std::size_t i = 0 ; i < n ; ++i )
554 if( ( p[ ( o + i ) / 8 ] >> ( ( o + i ) % 8 ) ) & 1 )
555 w[ i / 64 ] |= Word( 1 ) << ( i % 64 );
556 return( BitVector( w , n ) ); // the bits past n cleared
557 }
558
566template< unsigned B , bool S = false >
568 const ArrowSchema & s )
569{
570 using P = PackedArray< B , S >;
571 if constexpr( B == 1 )
572 if( s.format && std::string_view( s.format ) == "b" ) {
573 const BitVector b = toBitVector( a , s );
574 Sequential q;
575 return( P( q , b.size() , [ & ]( std::size_t i ) {
576 return( typename P::Word( b[ i ] ) );
577 } ) );
578 }
579 detail::arrowCheck( a , s , nullptr , "toPackedArray" );
580 std::vector< typename P::Word > v( std::size_t( a.length ) );
581 detail::arrowIntegers( a , s.format[ 0 ] , [ & ]( std::size_t i , auto x ) {
582 if constexpr( std::is_signed_v< decltype( x ) > )
583 if( x < 0 )
584 throw( std::invalid_argument( "ADS::toPackedArray: a negative value" ) );
585 v[ i ] = typename P::Word( x );
586 if constexpr( ! S )
587 if( v[ i ] > P::MAX )
588 throw( std::invalid_argument( "ADS::toPackedArray: a value of more "
589 "bits than a field" ) );
590 } );
591 Sequential q;
592 return( P( q , v.size() , [ & ]( std::size_t i ) { return( v[ i ] ); } ) );
593 }
594
599template< ArrowNumber T >
600[[nodiscard]] std::vector< T > toVector( const ArrowArray & a ,
601 const ArrowSchema & s )
602{
603 detail::arrowCheck( a , s , arrowFormat< T >() , "toVector" );
604 if( a.length == 0 )
605 return( std::vector< T >() );
606 const T * p =
607 static_cast< const T * >( a.buffers[ 1 ] ) + std::size_t( a.offset );
608 return( std::vector< T >( p , p + a.length ) );
609 }
610
611/*--------------------------------------------------------------------------*/
612
613} // end( namespace ADS )
614
615/*--------------------------------------------------------------------------*/
616
617#endif /* Arrow.h included */
618
619/*--------------------------------------------------------------------------*/
620/*---------------------------- End File Arrow.h ----------------------------*/
621/*--------------------------------------------------------------------------*/
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
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...
Arrays of small unsigned integers packed in words, beyond the book:
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
T operator[](std::size_t i) const
the value
Definition Arrow.h:505
~ArrowColumn(void)
the column released by its producer
Definition Arrow.h:489
std::span< const T > values(void) const
the values, in the memory of the producer
Definition Arrow.h:492
ArrowColumn & operator=(ArrowColumn &&o) noexcept
the column of , which is left with none, this one released
Definition Arrow.h:479
ArrowColumn(ArrowArray *a, const ArrowSchema &s)
the column of type , moved here: is left released, and is only read
Definition Arrow.h:461
ArrowColumn & operator=(const ArrowColumn &)=delete
no copy, since the column has one owner
ArrowColumn(const ArrowColumn &)=delete
no copy, since the column has one owner
const T * end(void) const
past the last value
Definition Arrow.h:514
std::size_t size(void) const
the number of the values
Definition Arrow.h:501
const T * begin(void) const
the first value
Definition Arrow.h:511
ArrowColumn(ArrowColumn &&o) noexcept
the column of , which is left with none
Definition Arrow.h:474
a vector of bits packed in words of 64 bits
Definition BitVector.h:479
std::size_t size(void) const
the number of bits
unsigned fields of bits, to a word, the values above clamped to it if Saturate
Definition PackedArray.h:96
the executor of the calling thread alone, which makes the calls in their order
Definition Executor.h:265
what exportArrow() exports: a BitVector, a PackedArray or a sized sequence of numbers
Definition Arrow.h:203
the numbers that are the values of a column of Arrow: the integers of 8, 16, 32 and 64 bits,...
Definition Arrow.h:155
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
@ little
the least significant byte first
Definition Persistence.h:120
constexpr const char * arrowFormat(void)
the format of Arrow of the numbers of type
Definition Arrow.h:163
PackedArray< B, S > toPackedArray(const ArrowArray &a, const ArrowSchema &s)
the column of type copied into a PackedArray< B , S >: a column of booleans if ,...
Definition Arrow.h:567
void exportArrow(std::shared_ptr< X > x, ArrowArray *a, ArrowSchema *s, std::string_view name="")
the structure as a column of Arrow of no nulls, named , in and , which its consumer releases; the v...
Definition Arrow.h:359
BitVector toBitVector(const ArrowArray &a, const ArrowSchema &s)
the column of booleans of type copied into a BitVector; and are only read
Definition Arrow.h:539
std::vector< T > toVector(const ArrowArray &a, const ArrowSchema &s)
the column of numbers of type , of type , copied into a std::vector; and are only read
Definition Arrow.h:600
BinaryTree< T > * copy(const BinaryTree< T > *t)
a deep copy of the subtree , nil if is nil; the caller owns it
Definition ADT.h:96
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73
the buffers of a column of Arrow
Definition Arrow.h:124
void(* release)(struct ArrowArray *)
null once released
Definition Arrow.h:133
void * private_data
of the producer
Definition Arrow.h:134
std::int64_t n_children
the children
Definition Arrow.h:129
std::int64_t offset
the first item in the buffers
Definition Arrow.h:127
std::int64_t n_buffers
the buffers
Definition Arrow.h:128
std::int64_t length
the items
Definition Arrow.h:125
struct ArrowArray ** children
the children
Definition Arrow.h:131
std::int64_t null_count
the nulls, -1 if not known
Definition Arrow.h:126
struct ArrowArray * dictionary
the dictionary, or null
Definition Arrow.h:132
const void ** buffers
the buffers, the validity first
Definition Arrow.h:130
the type of a column of Arrow
Definition Arrow.h:110
std::int64_t flags
the flags of the field
Definition Arrow.h:114
std::int64_t n_children
the types of the children
Definition Arrow.h:115
const char * metadata
the metadata, or null
Definition Arrow.h:113
const char * name
the name of the field, or null
Definition Arrow.h:112
void * private_data
of the producer
Definition Arrow.h:119
const char * format
the type, as a string
Definition Arrow.h:111
struct ArrowSchema * dictionary
the type of the dictionary, or null
Definition Arrow.h:117
void(* release)(struct ArrowSchema *)
null once released
Definition Arrow.h:118
struct ArrowSchema ** children
the types of the children
Definition Arrow.h:116