103#ifndef ARROW_C_DATA_INTERFACE
104 #define ARROW_C_DATA_INTERFACE
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 >;
162template< ArrowNumber T >
165 if constexpr( std::is_same_v< T , float > )
167 else if constexpr( std::is_same_v< T , double > )
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"
175 return(
sizeof( T ) == 1 ?
"C"
176 :
sizeof( T ) == 2 ?
"S"
177 :
sizeof( T ) == 4 ?
"I"
186struct IsPackedArray : std::false_type
189template<
unsigned B ,
bool S >
190struct IsPackedArray< PackedArray< B , S > > : std::true_type
195concept ArrowSequence = std::ranges::sized_range< const X > &&
196 ArrowNumber< std::ranges::range_value_t< const X > >;
204 std::is_same_v< X , BitVector > || detail::IsPackedArray< X >::value ||
205 detail::ArrowSequence< X >;
218 std::shared_ptr< const void > owner;
219 std::vector< std::uint64_t >
copy;
220 const void * buffers[ 2 ] = { nullptr ,
nullptr };
224inline constexpr std::uint64_t kArrowEmpty = 0;
227inline void arrowReleaseArray(
ArrowArray * a )
noexcept
236inline void arrowReleaseSchema(
ArrowSchema * s )
noexcept
245template<
class U ,
class F >
246inline void arrowCopy( ArrowKeep & k , std::size_t n , F && f )
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 ) );
258inline void arrowLittleWords( ArrowKeep & k , std::span< const Word > w )
260 k.copy.assign( w.begin() , w.end() );
261 if constexpr( std::endian::native != std::endian::little )
262 for(
auto & x : k.copy )
267[[nodiscard]]
inline bool arrowHasNulls(
const ArrowArray & a )
270 ( a.
buffers[ 0 ] ==
nullptr ) )
274 const auto * v =
static_cast< const unsigned char *
>( a.
buffers[ 0 ] );
276 if( ! ( ( v[ i / 8 ] >> ( i % 8 ) ) & 1 ) )
285 const char * f ,
const char * who )
287 const auto fail = [ & ](
const char * what ) {
288 throw( std::invalid_argument( std::string(
"ADS::" ) + who +
": " + what ) );
291 fail(
"a column of Arrow already released" );
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" );
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" );
309inline void arrowIntegers(
const ArrowArray & a ,
char f , F && use )
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 ] );
319 each(
static_cast< const std::int8_t *
>( p ) );
322 each(
static_cast< const std::uint8_t *
>( p ) );
325 each(
static_cast< const std::int16_t *
>( p ) );
328 each(
static_cast< const std::uint16_t *
>( p ) );
331 each(
static_cast< const std::int32_t *
>( p ) );
334 each(
static_cast< const std::uint32_t *
>( p ) );
337 each(
static_cast< const std::int64_t *
>( p ) );
340 each(
static_cast< const std::uint64_t *
>( p ) );
360 std::string_view name =
"" )
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 > ) {
372 data = x->words().data();
374 detail::arrowLittleWords( *k , x->words() );
375 data = k->copy.data();
378 else if constexpr( detail::IsPackedArray< Y >::value ) {
379 constexpr unsigned B = 64 / Y::PER;
380 if constexpr( B == 1 )
382 else if constexpr( B <= 8 )
384 else if constexpr( B == 16 )
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();
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();
404 using T = std::ranges::range_value_t< const Y >;
406 if constexpr( std::ranges::contiguous_range< const Y > )
407 data = std::ranges::data( *x );
409 auto it = std::ranges::begin( *x );
410 detail::arrowCopy< T >( *k , n ,
411 [ & ]( std::size_t ) {
return( *it++ ); } );
412 data = k->copy.data();
415 if( data ==
nullptr )
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 ,
425 std::int64_t( n ) , 0 , 0 , 2 , 0 , k->buffers , nullptr ,
427 &detail::arrowReleaseArray , k.get()
437 requires ArrowExportable< X >
439 std::string_view name =
"" )
441 exportArrow( std::make_shared< const X >( std::move( x ) ) , a , s , name );
451template< ArrowNumber T >
475 o.f_a.release =
nullptr;
483 o.f_a.release =
nullptr;
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 ) ) );
511 [[nodiscard]]
const T *
begin(
void )
const {
return(
values().data() ); }
514 [[nodiscard]]
const T *
end(
void )
const {
521 void reset(
void )
noexcept {
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 );
547 std::memcpy( w.data() , p + o / 8 , ( n + 7 ) / 8 );
548 if constexpr( std::endian::native != std::endian::little )
550 x = detail::byteSwap( x );
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 );
566template<
unsigned B ,
bool S = false >
571 if constexpr( B == 1 )
575 return( P( q , b.
size() , [ & ]( std::size_t i ) {
576 return( typename P::Word( b[ i ] ) );
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 ) > )
584 throw( std::invalid_argument(
"ADS::toPackedArray: a negative value" ) );
585 v[ i ] = typename P::Word( x );
587 if( v[ i ] > P::MAX )
588 throw( std::invalid_argument(
"ADS::toPackedArray: a value of more "
589 "bits than a field" ) );
592 return( P( q , v.size() , [ & ]( std::size_t i ) { return( v[ i ] ); } ) );
599template< ArrowNumber T >
605 return( std::vector< T >() );
607 static_cast< const T *
>( a.
buffers[ 1 ] ) + std::size_t( a.
offset );
608 return( std::vector< T >( p , p + a.
length ) );
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
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