ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RoaringBitmap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File RoaringBitmap.h --------------------------*/
3/*--------------------------------------------------------------------------*/
119/*--------------------------------------------------------------------------*/
120/*----------------------------- DEFINITIONS --------------------------------*/
121/*--------------------------------------------------------------------------*/
122
123#ifndef __ADS_RoaringBitmap
124 #define __ADS_RoaringBitmap
125/* self-identification: #endif constant */
126
127/*--------------------------------------------------------------------------*/
128/*------------------------------ INCLUDES ----------------------------------*/
129/*--------------------------------------------------------------------------*/
130
131#include "ADS/Contract.h"
132#include "ADS/Portable.h"
133#include "ADS/Simd.h"
135#include "ADS/set/BoolSet.h"
136
137#include <cstddef>
138#include <bit>
139#include <cstdint>
140#include <iterator>
141#include <memory>
142#include <span>
143#include <vector>
144
145/*--------------------------------------------------------------------------*/
146/*--------------------------- NAMESPACE ------------------------------------*/
147/*--------------------------------------------------------------------------*/
148
149namespace ADS
150{
151
152/*--------------------------------------------------------------------------*/
153/*-------------------------- CLASS RoaringBitmap ---------------------------*/
154/*--------------------------------------------------------------------------*/
155
158{
159
160/*--------------------------------------------------------------------------*/
161 public:
162 using Item = std::uint32_t;
163
165 static constexpr std::uint32_t ARRAY = 4096;
166
168 RoaringBitmap( void ) = default;
169
172
174 RoaringBitmap( RoaringBitmap && o ) noexcept = default;
175
178
182 RoaringBitmap( const std::uint32_t * a , std::size_t n );
183
201 [[nodiscard]] bool contains( std::uint32_t x ) const;
202
222 bool insert( std::uint32_t x );
223
242 bool remove( std::uint32_t x );
243
245 [[nodiscard]] std::size_t size( void ) const;
246
248 [[nodiscard]] bool empty( void ) const;
249
251 void clear( void );
252
254 void swap( RoaringBitmap & o ) noexcept;
255
257 [[nodiscard]] std::size_t containers( void ) const;
258
260 [[nodiscard]] std::size_t bitmaps( void ) const;
261
263 [[nodiscard]] std::size_t bytes( void ) const;
264
266 template< class F >
267 void forEach( F && f ) const;
268
270 [[nodiscard]] std::vector< std::uint32_t > items( void ) const;
271
289 [[nodiscard]] std::size_t rank( std::uint32_t x ) const;
290
291/*-------------------------- THE PORTABLE FORMAT ---------------------------*/
292
295 [[nodiscard]] std::size_t portableSizeInBytes( void ) const;
296
300 std::size_t writePortable( std::span< std::byte > out ) const;
301
303 [[nodiscard]] std::vector< std::byte > writePortable( void ) const;
304
311 [[nodiscard]] static RoaringBitmap
312 readPortable( std::span< const std::byte > in ,
313 std::size_t * used = nullptr );
314
316 const RoaringBitmap & B );
318 const RoaringBitmap & B );
320 const RoaringBitmap & B );
322 const RoaringBitmap & B );
323
324/*--------------------------------------------------------------------------*/
325 private:
326 static constexpr std::size_t WORDS = 1024;
327
329 struct Block
330 {
331 std::uint16_t key = 0;
332 std::uint32_t card = 0;
333 std::vector< std::uint16_t , detail::UninitAllocator< std::uint16_t > > a;
335 std::unique_ptr< std::uint64_t[] > w;
336
338 [[nodiscard]] bool bits( void ) const { return( w != nullptr ); }
339 };
340
342 enum class Op {
343 AND ,
344 OR ,
345 ANDNOT ,
346 XOR
347 };
348
350 template< Op O >
351 static RoaringBitmap combine( const RoaringBitmap & A ,
352 const RoaringBitmap & B );
353
355 template< Op O >
356 static Block combine( const Block & a , const Block & b );
357
359 static Block copy( const Block & b );
360
362 static void toBitmap( Block & b );
363
365 static void toArray( Block & b );
366
369 static Block fromWords( std::uint16_t key ,
370 std::unique_ptr< std::uint64_t[] > w ,
371 std::uint32_t card );
372
392 [[nodiscard]] std::size_t find( std::uint16_t key ) const;
393
395 [[nodiscard]] bool valid( void ) const;
396
398 void rekey( void );
399
400 std::vector< Block > f_B;
401 std::vector< std::uint16_t > f_K;
402 std::size_t f_size = 0;
403
404/*------------------------- THE STANDARD INTERFACE -------------------------*/
405 public:
406 // a constant view of the items in increasing order, forward
407
410 {
411 public:
412 using value_type = std::uint32_t;
413 using reference = std::uint32_t;
414 using difference_type = std::ptrdiff_t;
416 using iterator_concept = std::forward_iterator_tag;
418 using iterator_category = std::input_iterator_tag;
419
421 const_iterator( void ) = default;
422
424 reference operator*( void ) const { return( f_x ); }
425
429 if( f_word ) { // the next bit of the word
430 f_word &= f_word - 1;
431 if( f_word ) {
432 f_x = f_base + std::uint32_t( std::countr_zero( f_word ) );
433 return( *this );
434 }
435 }
436 ++f_i;
437 settle();
438 return( *this );
439 }
440
442 auto t = *this;
443 ++*this;
444 return( t );
445 }
446
448 bool operator==( const const_iterator & o ) const {
449 return( ( f_b == o.f_b ) && ( f_i == o.f_i ) && ( f_x == o.f_x ) );
450 }
451
452 private:
453 friend class RoaringBitmap;
454
457 const_iterator( const RoaringBitmap * s , std::size_t b );
458
461 void settle( void );
462
463 const RoaringBitmap * f_s = nullptr;
464 std::size_t f_b = 0;
465 std::size_t f_i = 0;
466 std::uint64_t f_word = 0;
467 std::uint32_t f_base = 0;
468 std::uint32_t f_x = 0;
469
470 }; // end( class const_iterator )
471
472 using value_type = std::uint32_t;
473 using size_type = std::size_t;
474 using difference_type = std::ptrdiff_t;
475 using reference = std::uint32_t;
476 using const_reference = std::uint32_t;
478
480 [[nodiscard]] const_iterator begin( void ) const {
481 return( const_iterator( this , 0 ) );
482 }
483
484 [[nodiscard]] const_iterator end( void ) const {
485 return( const_iterator( this , f_B.size() ) );
486 }
487
488 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
490 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
491
508 [[nodiscard]] const_iterator lowerBound( std::uint32_t x ) const;
509
532 [[nodiscard]] const_iterator select( std::size_t i ) const;
533
534 }; // end( class RoaringBitmap )
535
555
574 const RoaringBitmap & B );
575
595 const RoaringBitmap & B );
596
616 const RoaringBitmap & B );
617
618/*--------------------------------------------------------------------------*/
619
620} // end( namespace ADS )
621
622/*--------------------------------------------------------------------------*/
623/*-------------------------- IMPLEMENTATION --------------------------------*/
624/*--------------------------------------------------------------------------*/
625
626#include "ADS/set/impl/RoaringBitmap.tpp"
627
628/*--------------------------------------------------------------------------*/
629
630#endif /* RoaringBitmap.h included */
631
632/*--------------------------------------------------------------------------*/
633/*------------------------ End File RoaringBitmap.h ------------------------*/
634/*--------------------------------------------------------------------------*/
The SET of the integers in as a boolean vector of section 8.1: contains(), insert() and remove() in ...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The vector kernels of ADS on Google Highway, with ADS_WITH_HIGHWAY: the loops over arrays that a stru...
the iterator on the items in increasing order, forward
Definition RoaringBitmap.h:410
bool operator==(const const_iterator &o) const
equal on the same item of the same container
Definition RoaringBitmap.h:448
std::forward_iterator_tag iterator_concept
forward for the concepts of C++20
Definition RoaringBitmap.h:416
const_iterator(void)=default
an iterator on no set
const_iterator & operator++(void)
to the next item: the next bit of the word of a bitmap, or the next place, word or container
Definition RoaringBitmap.h:428
const_iterator operator++(int)
to the next item, returning the iterator before the step
Definition RoaringBitmap.h:441
std::ptrdiff_t difference_type
the distances
Definition RoaringBitmap.h:414
std::uint32_t reference
an item, by value
Definition RoaringBitmap.h:413
friend class RoaringBitmap
Definition RoaringBitmap.h:453
reference operator*(void) const
the item
Definition RoaringBitmap.h:424
std::uint32_t value_type
the items
Definition RoaringBitmap.h:412
std::input_iterator_tag iterator_category
input for the older algorithms, since *it is a value
Definition RoaringBitmap.h:418
a set of integers of 32 bits as a Roaring bitmap
Definition RoaringBitmap.h:158
std::vector< std::uint32_t > items(void) const
the items in increasing order
RoaringBitmap(RoaringBitmap &&o) noexcept=default
the containers of o, which is left empty
void swap(RoaringBitmap &o) noexcept
the items of this set and of o exchanged,
const_iterator lowerBound(std::uint32_t x) const
the first item not smaller than , end() if none
RoaringBitmap(void)=default
the empty set
static constexpr std::uint32_t ARRAY
the items of an array container at most
Definition RoaringBitmap.h:165
std::size_t bitmaps(void) const
the containers that are bitmaps
const_iterator iterator
the same, constant
Definition RoaringBitmap.h:477
std::size_t containers(void) const
the containers
bool contains(std::uint32_t x) const
whether is in the set
std::size_t writePortable(std::span< std::byte > out) const
the set written at the start of in the portable format of Roaring, with no container of runs; the by...
std::size_t portableSizeInBytes(void) const
the bytes of the set in the portable format of Roaring: 8, then 8 per container, 2 per item of an arr...
friend RoaringBitmap setUnion(const RoaringBitmap &A, const RoaringBitmap &B)
the union, by the merge of the containers
const_iterator select(std::size_t i) const
the item of rank (from 0), end() if size()
std::vector< std::byte > writePortable(void) const
the set in the portable format of Roaring, in an array of its own
std::ptrdiff_t difference_type
the distances
Definition RoaringBitmap.h:474
void forEach(F &&f) const
f( x ) on the items in increasing order
bool insert(std::uint32_t x)
into the set; true if it was not there
RoaringBitmap & operator=(RoaringBitmap o) noexcept
the containers of o, a copy or a move (the copy and swap idiom)
std::size_t size_type
the sizes
Definition RoaringBitmap.h:473
static RoaringBitmap readPortable(std::span< const std::byte > in, std::size_t *used=nullptr)
the set written in the portable format of Roaring at the start of , its containers of runs read as ar...
friend RoaringBitmap setDifference(const RoaringBitmap &A, const RoaringBitmap &B)
the difference, by the merge of the containers
std::uint32_t reference
an item, by value
Definition RoaringBitmap.h:475
std::size_t size(void) const
the number of items
std::size_t bytes(void) const
the bytes of the containers
RoaringBitmap(const std::uint32_t *a, std::size_t n)
the set of the strictly increasing items of , a container per chunk in one scan
std::uint32_t Item
the type of the items
Definition RoaringBitmap.h:162
bool empty(void) const
whether the set has no item
const_iterator cend(void) const
as end()
Definition RoaringBitmap.h:490
std::uint32_t const_reference
an item, by value
Definition RoaringBitmap.h:476
std::uint32_t value_type
the items
Definition RoaringBitmap.h:472
const_iterator cbegin(void) const
as begin()
Definition RoaringBitmap.h:488
bool remove(std::uint32_t x)
out of the set; true if it was there
friend RoaringBitmap setSymmetricDifference(const RoaringBitmap &A, const RoaringBitmap &B)
the symmetric difference, by the merge of the containers
std::size_t rank(std::uint32_t x) const
the items smaller than : the counts of the containers before that of , , and the place of in it
void clear(void)
all the items removed
RoaringBitmap(const RoaringBitmap &o)
a copy, with containers of its own
const_iterator begin(void) const
the iterator on the smallest item
Definition RoaringBitmap.h:480
const_iterator end(void) const
the iterator past the largest item
Definition RoaringBitmap.h:484
friend RoaringBitmap setIntersection(const RoaringBitmap &A, const RoaringBitmap &B)
the intersection, by the merge of the containers
BoolSet setIntersection(const BoolSet &A, const BoolSet &B)
the intersection, a set of dimension
BoolSet setUnion(const BoolSet &A, const BoolSet &B)
the union, a set of dimension
BoolSet setDifference(const BoolSet &A, const BoolSet &B)
the difference, a set of dimension A.dim()
BoolSet setSymmetricDifference(const BoolSet &A, const BoolSet &B)
the symmetric difference, the items in exactly one of and , a set of dimension
Definition ADT.h:96