ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BoolSet.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File BoolSet.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
41/*--------------------------------------------------------------------------*/
42/*----------------------------- DEFINITIONS --------------------------------*/
43/*--------------------------------------------------------------------------*/
44
45#ifndef __ADS_BoolSet
46 #define __ADS_BoolSet
47/* self-identification: #endif constant */
48
49/*--------------------------------------------------------------------------*/
50/*------------------------------ INCLUDES ----------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53#include "ADS/Contract.h"
54#include "ADS/Iterators.h"
55#include "ADS/Simd.h"
56
57#include <algorithm>
58#include <bit>
59#include <concepts>
60#include <cstddef>
61#include <cstdint>
62#include <functional>
63#include <optional>
64#include <memory>
65#include <new>
66#include <stdexcept>
67#include <type_traits>
68#include <utility>
69#include <vector>
70
71/*--------------------------------------------------------------------------*/
72/*--------------------------- NAMESPACE ------------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75namespace ADS
76{
77
78namespace detail
79{
80
83template< class T >
84struct UninitAllocator : std::allocator< T >
85{
86 template< class U >
87 struct rebind
88 {
89 using other = UninitAllocator< U >;
90 };
91 UninitAllocator( void ) = default;
92 template< class U >
93 UninitAllocator( const UninitAllocator< U > & ) noexcept { }
94 template< class U >
95 void construct( U * p ) noexcept(
96 std::is_nothrow_default_constructible_v< U > ) {
97 ::new( static_cast< void * >( p ) ) U;
98 }
99 template< class U , class... Args >
100 void construct( U * p , Args &&... args ) {
101 ::new( static_cast< void * >( p ) ) U( std::forward< Args >( args )... );
102 }
103 };
104
105} // end( namespace detail )
106
107/*--------------------------------------------------------------------------*/
108/*----------------------------- CLASS BoolSet ------------------------------*/
109/*--------------------------------------------------------------------------*/
110
113{
114
115/*--------------------------------------------------------------------------*/
116 public:
117 using Item = long;
118
122 explicit BoolSet( long N );
123
125 [[nodiscard]] std::size_t size( void ) const;
126
128 [[nodiscard]] bool empty( void ) const;
129
131 [[nodiscard]] long dim( void ) const;
132
135 [[nodiscard]] bool contains( long x ) const;
136
140 void insert( long x );
141
144 void remove( long x );
145
159 [[nodiscard]] std::optional< long > min( void ) const;
160
174 [[nodiscard]] std::optional< long > max( void ) const;
175
190 template< class F >
191 void forEach( F && f ) const;
192
193/*------------------------- THE STANDARD INTERFACE -------------------------*/
194
195 // a constant view of the items in increasing order, bidirectional; an
196 // item is computed from its bit
197
199 struct Places
200 {
202 static std::size_t size( const auto & o ) { return( o.f_N ); }
204 static bool occupied( const auto & o , std::size_t i ) {
205 return( ( o.f_W[ i / 64 ] >> ( i % 64 ) ) & 1 );
206 }
207
208 static auto read( const auto & , std::size_t i ) { return( long( i ) ); }
209 };
210
211 using value_type = long;
212 using size_type = std::size_t;
213 using difference_type = std::ptrdiff_t;
214 using reference = long;
215 using const_reference = long;
219
221 [[nodiscard]] const_iterator begin( void ) const {
222 return( const_iterator( this , 0 ) );
223 }
224
225 [[nodiscard]] const_iterator end( void ) const {
226 return( const_iterator( this , Places::size( *this ) ) );
227 }
228
229 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
231 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
232
233/*--------------------------------------------------------------------------*/
234 private:
236 [[nodiscard]] bool counted( void ) const;
237
241 template< class Op >
242 static void combine( BoolSet & C , const BoolSet & A , const BoolSet & B ,
243 Op op );
244
245 friend BoolSet setUnion( const BoolSet & A , const BoolSet & B );
246 friend BoolSet setIntersection( const BoolSet & A , const BoolSet & B );
247 friend BoolSet setDifference( const BoolSet & A , const BoolSet & B );
249 const BoolSet & B );
250
252 struct Raw
253 { };
254 BoolSet( long N , Raw );
255
257 std::vector< std::uint64_t , detail::UninitAllocator< std::uint64_t > > f_W;
258 std::size_t f_N = 0;
259 std::size_t f_size = 0;
260
261 }; // end( class BoolSet )
262
278inline BoolSet setUnion( const BoolSet & A , const BoolSet & B );
279
294inline BoolSet setIntersection( const BoolSet & A , const BoolSet & B );
295
308inline BoolSet setDifference( const BoolSet & A , const BoolSet & B );
309
325inline BoolSet setSymmetricDifference( const BoolSet & A , const BoolSet & B );
326
327} // end( namespace ADS )
328
329/*--------------------------------------------------------------------------*/
330/*-------------------------- IMPLEMENTATION --------------------------------*/
331/*--------------------------------------------------------------------------*/
332
333#include "ADS/set/impl/BoolSet.tpp"
334
335/*--------------------------------------------------------------------------*/
336
337#endif /* BoolSet.h included */
338
339/*--------------------------------------------------------------------------*/
340/*--------------------------- End File BoolSet.h ---------------------------*/
341/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
The vector kernels of ADS on Google Highway, with ADS_WITH_HIGHWAY: the loops over arrays that a stru...
a set of integers in as a boolean vector
Definition BoolSet.h:113
friend BoolSet setSymmetricDifference(const BoolSet &A, const BoolSet &B)
the symmetric difference, the items in exactly one of and , a set of dimension
std::optional< long > min(void) const
the smallest item, none if the set is empty, time
void remove(long x)
out of the set, by clearing its bit
const_iterator end(void) const
the iterator past the largest item
Definition BoolSet.h:225
bool empty(void) const
whether the set has no item
const_iterator begin(void) const
the iterator on the smallest item
Definition BoolSet.h:221
const_iterator cend(void) const
as end()
Definition BoolSet.h:231
friend BoolSet setIntersection(const BoolSet &A, const BoolSet &B)
the intersection, a set of dimension
void forEach(F &&f) const
f( x ) on the items in increasing order, time
SlotIterator< BoolSet, Places > const_iterator
the iterator, on the bits set in increasing order
Definition BoolSet.h:217
std::size_t size(void) const
the number of items, kept by the operations,
const_iterator iterator
the same, constant
Definition BoolSet.h:218
friend BoolSet setDifference(const BoolSet &A, const BoolSet &B)
the difference, a set of dimension A.dim()
long reference
an item, by value
Definition BoolSet.h:214
std::optional< long > max(void) const
the largest item, none if the set is empty, time
long value_type
the items
Definition BoolSet.h:211
std::ptrdiff_t difference_type
the distances
Definition BoolSet.h:213
void insert(long x)
into the set, by setting its bit
long const_reference
an item, by value
Definition BoolSet.h:215
const_iterator cbegin(void) const
as begin()
Definition BoolSet.h:229
long Item
the type of the items
Definition BoolSet.h:117
BoolSet(long N)
the empty set of the integers in , the Set( N ) of the book
friend BoolSet setUnion(const BoolSet &A, const BoolSet &B)
the union, a set of dimension
std::size_t size_type
the sizes
Definition BoolSet.h:212
long dim(void) const
the size of the vector
bool contains(long x) const
whether is in the set, false also for an out of , by reading its bit
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
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
the places of the table, for SlotIterator
Definition BoolSet.h:200
static bool occupied(const auto &o, std::size_t i)
whether the bit is set
Definition BoolSet.h:204
static std::size_t size(const auto &o)
the places,
Definition BoolSet.h:202
static auto read(const auto &, std::size_t i)
the item of the place , itself
Definition BoolSet.h:208