ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BitVector.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File BitVector.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
72/*--------------------------------------------------------------------------*/
73/*----------------------------- DEFINITIONS --------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76#ifndef __ADS_BitVector
77 #define __ADS_BitVector
78/* self-identification: #endif constant */
79
80/*--------------------------------------------------------------------------*/
81/*------------------------------ INCLUDES ----------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84#include "ADS/Contract.h"
85#include "ADS/SimdInline.h"
89
90#include <algorithm>
91#include <array>
92#include <atomic>
93#include <bit>
94#include <compare>
95#include <concepts>
96#include <cstddef>
97#include <cstdint>
98#include <iterator>
99#include <span>
100#include <string>
101#include <string_view>
102#include <type_traits>
103#include <utility>
104#include <vector>
105
106/*--------------------------------------------------------------------------*/
107/*--------------------------- NAMESPACE ------------------------------------*/
108/*--------------------------------------------------------------------------*/
109
110namespace ADS
111{
112
113namespace detail
114{
115
121template< class T >
122class LineAllocator
123{
124 public:
126 using value_type = T;
128 using adopts_mapping = void;
130 using propagate_on_container_move_assignment = std::true_type;
132 using propagate_on_container_swap = std::true_type;
134 using is_always_equal = std::false_type;
135 static constexpr std::size_t LINE = 64;
136
137 LineAllocator( void ) = default;
139 explicit LineAllocator( MappedFile m ) noexcept : f_map( std::move( m ) ) { }
140 template< class U >
141 LineAllocator( const LineAllocator< U > & o ) noexcept : f_map( o.f_map ) { }
142
144 [[nodiscard]] LineAllocator
145 select_on_container_copy_construction( void ) const {
146 return( LineAllocator() );
147 }
148
149 [[nodiscard]] T * allocate( std::size_t n ) {
150 if( f_map )
151 if( void * p = f_map.take( n * sizeof( T ) ) )
152 return( static_cast< T * >( p ) );
153 if( n * sizeof( T ) >= HugePageAllocator< T >::PAGE )
154 return( HugePageAllocator< T >().allocate( n ) );
155 return( static_cast< T * >(
156 ::operator new( n * sizeof( T ) , std::align_val_t( LINE ) ) ) );
157 }
158
161 template< class U >
162 void construct( U * p ) noexcept {
163 ::new( static_cast< void * >( p ) ) U;
164 }
165 template< class U , class... A >
166 void construct( U * p , A &&... a ) {
167 ::new( static_cast< void * >( p ) ) U( std::forward< A >( a )... );
168 }
169
170 void deallocate( T * p , std::size_t n ) noexcept {
171 if( f_map && f_map.holds( p ) )
172 return;
173 if( n * sizeof( T ) >= HugePageAllocator< T >::PAGE )
174 HugePageAllocator< T >().deallocate( p , n );
175 else
176 ::operator delete( p , std::align_val_t( LINE ) );
177 }
178
180 template< class U >
181 bool operator==( const LineAllocator< U > & o ) const noexcept {
182 return( f_map == o.f_map );
183 }
184
185 private:
186 template< class U >
187 friend class LineAllocator;
188
189 MappedFile f_map;
190 };
191
198class BitRuns
199{
200 public:
202 BitRuns( Word * W , std::size_t R ) : f_W( W ) , f_run( R ) { }
203
205 void open( std::size_t r , std::size_t s , std::size_t e ) {
206 f_run[ r ] = Run{ s , 0 , s , e };
207 }
208
210 void push( std::size_t r , bool b ) {
211 Run & R = f_run[ r ];
212 R.acc |= Word( b ) << ( R.p & 63 );
213 if( ( ++R.p & 63 ) == 0 )
214 flush( R , ( R.p >> 6 ) - 1 );
215 }
216
218 void close( void ) {
219 for( Run & R : f_run )
220 if( R.p & 63 )
221 flush( R , R.p >> 6 );
222 }
223
224 private:
226 struct Run
227 {
228 std::size_t p;
229 Word acc;
230 std::size_t s ,
231 e;
232 };
233
236 void flush( Run & R , std::size_t w ) {
237 if( const Word a = R.acc ) {
238 if( ( 64 * w >= R.s ) && ( 64 * w + 64 <= R.e ) )
239 f_W[ w ] = a;
240 else
241 std::atomic_ref< Word >( f_W[ w ] )
242 .fetch_or( a , std::memory_order_relaxed );
243 }
244 R.acc = 0;
245 }
246
247 Word * f_W;
248 std::vector< Run > f_run;
249 };
250
254inline void copyBits( Word * D , std::size_t d , const Word * S ,
255 std::size_t s ,
256 std::size_t len )
257{
258 while( len > 0 ) {
259 const std::size_t o = d & 63 ,
260 take = std::min< std::size_t >( len , 64 - o );
261 const std::size_t q = s >> 6 , r = s & 63;
262 Word x = S[ q ] >> r;
263 if( r + take > 64 )
264 x |= S[ q + 1 ] << ( 64 - r );
265 if( take < 64 )
266 x &= ( Word( 1 ) << take ) - 1;
267 D[ d >> 6 ] |= x << o;
268 d += take;
269 s += take;
270 len -= take;
271 }
272 }
273
274} // end( namespace detail )
275
277using Words = std::vector< Word , detail::LineAllocator< Word > >;
278
305[[nodiscard]] constexpr int selectInWord( Word x , int k );
306
307namespace detail
308{
309
312template< class R >
313concept RankSelectBits = requires( const R & r , std::size_t i )
314{
315 { r.size() } -> std::convertible_to< std::size_t >;
316 { r.ones() } -> std::convertible_to< std::size_t >;
317 { r[ i ] } -> std::convertible_to< bool >;
318 { r.rank1( i ) } -> std::convertible_to< std::size_t >;
319 };
320
333template< bool Scalar = false >
334[[nodiscard]] std::size_t lineRank( const Word * w , unsigned p );
335
340std::uint64_t copyCount( Word * P , const Word * w , std::size_t n ,
341 std::size_t w0 , std::size_t w1 );
342
348[[nodiscard]] std::size_t chunkUnits( std::size_t u , std::size_t chunk ,
349 std::size_t chunks , std::size_t top );
350
355template< RankSelectBits R >
356std::size_t selected( const R & r , std::size_t k , bool b , std::size_t p );
357
359template< RankSelectBits R >
360[[nodiscard]] bool rankConsistent( const R & r );
361
362} // end( namespace detail )
363
364/*--------------------------------------------------------------------------*/
365/*------------------------ CLASS SubscriptIterator -------------------------*/
366/*--------------------------------------------------------------------------*/
367
370template< class O , class T >
372{
373 public:
375 using value_type = T;
377 using reference = T;
379 using difference_type = std::ptrdiff_t;
381 using iterator_concept = std::random_access_iterator_tag;
384 using iterator_category = std::input_iterator_tag;
385
387 SubscriptIterator( void ) = default;
389 SubscriptIterator( const O * o , std::size_t i )
390 : f_o( o ) , f_i( difference_type( i ) ) { }
391
393 reference operator*( void ) const { return( ( *this )[ 0 ] ); }
396 return( ( *f_o )[ std::size_t( f_i + k ) ] );
397 }
398
401 ++f_i;
402 return( *this );
403 }
404
406 --f_i;
407 return( *this );
408 }
409
411 auto t = *this;
412 ++f_i;
413 return( t );
414 }
415
417 auto t = *this;
418 --f_i;
419 return( t );
420 }
421
423 f_i += k;
424 return( *this );
425 }
426
428 f_i -= k;
429 return( *this );
430 }
431
434 difference_type k ) {
435 return( a += k );
436 }
437
440 return( a += k );
441 }
442
444 difference_type k ) {
445 return( a -= k );
446 }
447
449 const SubscriptIterator & b ) {
450 return( a.f_i - b.f_i );
451 }
452
454 bool operator==( const SubscriptIterator & o ) const {
455 return( f_i == o.f_i );
456 }
457
458 std::strong_ordering operator<=>( const SubscriptIterator & o ) const {
459 return( f_i <=> o.f_i );
460 }
461
463 [[nodiscard]] std::size_t index( void ) const {
464 return( std::size_t( f_i ) );
465 }
466
467 private:
468 const O * f_o = nullptr;
469 difference_type f_i = 0;
470
471 }; // end( class SubscriptIterator )
472
473/*--------------------------------------------------------------------------*/
474/*---------------------------- CLASS BitVector -----------------------------*/
475/*--------------------------------------------------------------------------*/
476
479{
480
481/*--------------------------------------------------------------------------*/
482 public:
484 BitVector( void ) = default;
485
487 explicit BitVector( std::size_t n , bool b = false );
488
491 explicit BitVector( std::string_view s );
492
497 BitVector( std::span< const Word > w , std::size_t n );
498
500 [[nodiscard]] std::size_t size( void ) const;
501
503 [[nodiscard]] bool empty( void ) const;
504
507 [[nodiscard]] bool operator[]( std::size_t i ) const;
508
512 void set( std::size_t i , bool b = true );
513
516 void push_back( bool b );
517
535 void append( Word v , int len );
536
553 [[nodiscard]] Word get( std::size_t i , int len ) const;
554
560 void put( std::size_t i , Word v , int len );
561
563 [[nodiscard]] std::size_t count( void ) const;
564
568 [[nodiscard]] const Words & words( void ) const;
569
571 [[nodiscard]] std::string toString( void ) const;
572
574 void reserve( std::size_t n );
575
577 void clear( void );
578
580 friend bool operator==( const BitVector & a , const BitVector & b ) = default;
581
582/*----------------------------- THE PERSISTENCE ----------------------------*/
583
585 static constexpr std::string_view persistName = "BitVector";
586
590 template< class A >
591 void persist( A & a );
592
593/*------------------------- THE STANDARD INTERFACE -------------------------*/
594
596 using value_type = bool;
598 using size_type = std::size_t;
600 using difference_type = std::ptrdiff_t;
602 using reference = bool;
604 using const_reference = bool;
609
611 [[nodiscard]] const_iterator begin( void ) const {
612 return( const_iterator( this , 0 ) );
613 }
614
615 [[nodiscard]] const_iterator end( void ) const {
616 return( const_iterator( this , f_n ) );
617 }
618
619 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
621 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
622
623/*--------------------------------------------------------------------------*/
624 private:
625 template< bool >
626 friend class BasicRankSelect; // which copy the words
627 friend class CompactRankSelect; // as they count them
628 friend class WaveletMatrix; // which write the words
629 template< class >
630 friend class HuffmanWaveletTree; // by their threads
631
633 [[nodiscard]] static std::size_t wordsFor( std::size_t n ) {
634 return( ( n / 64 + 9 ) & ~std::size_t( 7 ) );
635 }
636
638 [[nodiscard]] bool clean( void ) const;
639
640 Words f_w = Words( 8 , 0 );
641 std::size_t f_n = 0;
642
643 }; // end( class BitVector )
644
645/*--------------------------------------------------------------------------*/
646
647} // end( namespace ADS )
648
649/*--------------------------------------------------------------------------*/
650/*-------------------------- IMPLEMENTATION --------------------------------*/
651/*--------------------------------------------------------------------------*/
652
653#include "ADS/succinct/impl/BitVector.tpp"
654
655/*--------------------------------------------------------------------------*/
656
657#endif /* BitVector.h included */
658
659/*--------------------------------------------------------------------------*/
660/*-------------------------- End File BitVector.h --------------------------*/
661/*--------------------------------------------------------------------------*/
The broadword tricks of Knuth (section 7.1.3 of The Art of Computer Programming), beyond the book: op...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
The vector kernels of ADS that are inlined in the middle of a search, on Google Highway,...
BitVector(std::size_t n, bool b=false)
bits equal to
bool const_reference
an item, returned by value
Definition BitVector.h:604
bool value_type
the type of an item
Definition BitVector.h:596
void set(std::size_t i, bool b=true)
writes in the bit
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition BitVector.h:585
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]: the size and the words,...
void clear(void)
no bit
BitVector(std::span< const Word > w, std::size_t n)
the bits of the words , the bit in the bit of the word , as in words()
std::size_t size_type
the type of a size
Definition BitVector.h:598
friend class BasicRankSelect
Definition BitVector.h:626
friend bool operator==(const BitVector &a, const BitVector &b)=default
whether the two vectors have the same bits
bool reference
an item, returned by value
Definition BitVector.h:602
BitVector(std::string_view s)
the bits written in , the bit 0 first
SubscriptIterator< BitVector, bool > const_iterator
the constant iterator, random access
Definition BitVector.h:606
const_iterator cbegin(void) const
the same as begin()
Definition BitVector.h:619
friend class CompactRankSelect
Definition BitVector.h:627
friend class WaveletMatrix
Definition BitVector.h:628
void push_back(bool b)
appends the bit
void reserve(std::size_t n)
room for bits
bool empty(void) const
whether there is no bit
const_iterator cend(void) const
the same as end()
Definition BitVector.h:621
BitVector(void)=default
the empty vector
Word get(std::size_t i, int len) const
the bits from the place , the bit in the bit 0 of the result
const Words & words(void) const
the words, the bits past the size being 0; they are at least , a multiple of 8, and start on a line o...
void put(std::size_t i, Word v, int len)
writes the bits of from the place , its bit 0 in the bit
std::size_t count(void) const
the number of 1 bits, in time
const_iterator begin(void) const
the iterator on the first item
Definition BitVector.h:611
void append(Word v, int len)
appends the bits of , its bit 0 first
friend class HuffmanWaveletTree
Definition BitVector.h:630
const_iterator end(void) const
the iterator past the last item
Definition BitVector.h:615
const_iterator iterator
the same as const_iterator
Definition BitVector.h:608
std::ptrdiff_t difference_type
the type of the distance of two places
Definition BitVector.h:600
std::string toString(void) const
the bits as a string of '0' and '1', the bit 0 first
bool operator[](std::size_t i) const
the bit
std::size_t size(void) const
the number of bits
static constexpr std::size_t PAGE
the size of a huge page, in bytes
Definition HugePageAllocator.h:177
the random-access constant iterator on the items T of the owner O, read by ( *o )[ i ]
Definition BitVector.h:372
reference operator*(void) const
the item
Definition BitVector.h:393
friend SubscriptIterator operator+(SubscriptIterator a, difference_type k)
the iterator places after
Definition BitVector.h:433
bool operator==(const SubscriptIterator &o) const
whether the two iterators are on the same place
Definition BitVector.h:454
std::uint64_t reference
Definition BitVector.h:377
friend SubscriptIterator operator-(SubscriptIterator a, difference_type k)
the iterator places before
Definition BitVector.h:443
SubscriptIterator operator--(int)
the previous item, returning the iterator before it
Definition BitVector.h:416
SubscriptIterator & operator+=(difference_type k)
places ahead
Definition BitVector.h:422
SubscriptIterator & operator--(void)
the previous item
Definition BitVector.h:405
std::random_access_iterator_tag iterator_concept
Definition BitVector.h:381
SubscriptIterator operator++(int)
the next item, returning the iterator before it
Definition BitVector.h:410
SubscriptIterator & operator++(void)
the next item
Definition BitVector.h:400
std::strong_ordering operator<=>(const SubscriptIterator &o) const
the order of the places of the two iterators
Definition BitVector.h:458
friend SubscriptIterator operator+(difference_type k, SubscriptIterator a)
the iterator places after
Definition BitVector.h:438
std::input_iterator_tag iterator_category
Definition BitVector.h:384
std::ptrdiff_t difference_type
Definition BitVector.h:379
SubscriptIterator(void)=default
an iterator on no owner
reference operator[](difference_type k) const
the item places ahead
Definition BitVector.h:395
std::uint64_t value_type
Definition BitVector.h:375
std::size_t index(void) const
the place of the item
Definition BitVector.h:463
SubscriptIterator & operator-=(difference_type k)
places back
Definition BitVector.h:427
friend difference_type operator-(const SubscriptIterator &a, const SubscriptIterator &b)
the places from to
Definition BitVector.h:448
SubscriptIterator(const O *o, std::size_t i)
the iterator on the item of *o
Definition BitVector.h:389
std::vector< Word, detail::LineAllocator< Word > > Words
the words of a BitVector, on lines of the cache
Definition BitVector.h:277
constexpr int selectInWord(Word x, int k)
the place of the -th 1 of the word , counting from 0 and the places from the least significant bit
Definition ADT.h:96
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73