76#ifndef __ADS_BitVector
77 #define __ADS_BitVector
101#include <string_view>
102#include <type_traits>
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;
137 LineAllocator(
void ) =
default;
139 explicit LineAllocator( MappedFile m ) noexcept : f_map( std::move( m ) ) { }
141 LineAllocator(
const LineAllocator< U > & o ) noexcept : f_map( o.f_map ) { }
144 [[nodiscard]] LineAllocator
145 select_on_container_copy_construction(
void )
const {
146 return( LineAllocator() );
149 [[nodiscard]] T * allocate( std::size_t n ) {
151 if(
void * p = f_map.take( n *
sizeof( T ) ) )
152 return(
static_cast< T *
>( p ) );
154 return( HugePageAllocator< T >().allocate( n ) );
155 return(
static_cast< T *
>(
156 ::operator
new( n *
sizeof( T ) , std::align_val_t( LINE ) ) ) );
162 void construct( U * p )
noexcept {
163 ::new(
static_cast< void *
>( p ) ) U;
165 template<
class U ,
class... A >
166 void construct( U * p , A &&... a ) {
167 ::new(
static_cast< void *
>( p ) ) U( std::forward< A >( a )... );
170 void deallocate( T * p , std::size_t n )
noexcept {
171 if( f_map && f_map.holds( p ) )
174 HugePageAllocator< T >().deallocate( p , n );
176 ::operator
delete( p , std::align_val_t( LINE ) );
181 bool operator==(
const LineAllocator< U > & o )
const noexcept {
182 return( f_map == o.f_map );
187 friend class LineAllocator;
202 BitRuns(
Word * W , std::size_t R ) : f_W( W ) , f_run( R ) { }
205 void open( std::size_t r , std::size_t s , std::size_t e ) {
206 f_run[ r ] = Run{ s , 0 , s , e };
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 );
219 for( Run & R : f_run )
221 flush( R , R.p >> 6 );
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 ) )
241 std::atomic_ref< Word >( f_W[ w ] )
242 .fetch_or( a , std::memory_order_relaxed );
248 std::vector< Run > f_run;
254inline void copyBits(
Word * D , std::size_t d ,
const Word * S ,
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;
264 x |= S[ q + 1 ] << ( 64 - r );
266 x &= (
Word( 1 ) << take ) - 1;
267 D[ d >> 6 ] |= x << o;
277using Words = std::vector< Word , detail::LineAllocator< Word > >;
313concept RankSelectBits =
requires(
const R & r , std::size_t i )
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 >;
333template<
bool Scalar = false >
334[[nodiscard]] std::size_t lineRank(
const Word * w ,
unsigned p );
340std::uint64_t copyCount(
Word * P ,
const Word * w , std::size_t n ,
341 std::size_t w0 , std::size_t w1 );
348[[nodiscard]] std::size_t chunkUnits( std::size_t u , std::size_t chunk ,
349 std::size_t chunks , std::size_t top );
355template< RankSelectBits R >
356std::size_t selected(
const R & r , std::size_t k ,
bool b , std::size_t p );
359template< RankSelectBits R >
360[[nodiscard]]
bool rankConsistent(
const R & r );
370template<
class O ,
class T >
396 return( ( *f_o )[ std::size_t( f_i + k ) ] );
450 return( a.f_i - b.f_i );
455 return( f_i == o.f_i );
459 return( f_i <=> o.f_i );
463 [[nodiscard]] std::size_t
index(
void )
const {
464 return( std::size_t( f_i ) );
468 const O * f_o =
nullptr;
500 [[nodiscard]] std::size_t
size(
void )
const;
503 [[nodiscard]]
bool empty(
void )
const;
512 void set( std::size_t i ,
bool b =
true );
553 [[nodiscard]]
Word get( std::size_t i ,
int len )
const;
563 [[nodiscard]] std::size_t
count(
void )
const;
633 [[nodiscard]]
static std::size_t wordsFor( std::size_t n ) {
634 return( ( n / 64 + 9 ) & ~std::size_t( 7 ) );
638 [[nodiscard]]
bool clean(
void )
const;
653#include "ADS/succinct/impl/BitVector.tpp"
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,...
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
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73