MeshLib C++ Docs
Loading...
Searching...
No Matches
MRBitSet.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRAllocator.h"
5#include "MRId.h"
6#include "MRphmap.h"
7#include "MRVector.h"
8#include "MRPch/MRBindingMacros.h"
9#include <atomic>
10#include <functional>
11#include <iosfwd>
12#include <iterator>
13
14namespace MR
15{
16
22
24template <typename T>
25class MR_BIND_IGNORE_PY SetBitIteratorT
26{
27public:
28 using IndexType = typename T::IndexType;
29
30 using iterator_category = std::forward_iterator_tag;
32 using difference_type = std::ptrdiff_t;
34 using pointer = const IndexType *;
35
37 SetBitIteratorT() = default;
38
41 : bitset_( &bitset ), index_( bitset.find_first() )
42 {
43 }
44
46 {
47 index_ = bitset_->find_next( index_ );
48 return * this;
49 }
50
51 [[nodiscard]] SetBitIteratorT operator++( int )
52 {
53 SetBitIteratorT ret = *this;
54 operator++();
55 return ret;
56 }
57
58 [[nodiscard]] const T * bitset() const { return bitset_; }
59 [[nodiscard]] reference operator *() const { return index_; }
60
61 [[nodiscard]] friend bool operator ==( const SetBitIteratorT<T> & a, const SetBitIteratorT<T> & b ) { return *a == *b; }
62
63private:
64 const T * bitset_ = nullptr;
65 IndexType index_ = IndexType( ~size_t( 0 ) );
66};
67
70class BitSet
71{
72public:
73 using block_type = std::uint64_t;
74 using Blocks = std::vector<block_type, Allocator<block_type>>;
75 inline static constexpr size_t bits_per_block = sizeof( block_type ) * 8;
76 inline static constexpr size_t npos = (size_t)-1;
77
78 using size_type = size_t;
79 using IndexType = size_t;
80
82 BitSet() noexcept = default;
83
85 explicit BitSet( size_t numBits, bool fillValue = false ) { resize( numBits, fillValue ); }
86
88 static BitSet fromBlocks( Blocks && blocks )
89 {
90 BitSet res;
91 res.blocks_ = std::move( blocks );
92 res.numBits_ = res.blocks_.size() * bits_per_block;
93 return res;
94 }
95
96 void reserve( size_type numBits ) { blocks_.reserve( calcNumBlocks_( numBits ) ); }
97 MRMESH_API void resize( size_type numBits, bool fillValue = false );
98 void clear() { numBits_ = 0; blocks_.clear(); }
99 void shrink_to_fit() { blocks_.shrink_to_fit(); }
100
101 [[nodiscard]] bool empty() const noexcept { return numBits_ == 0; }
102 [[nodiscard]] size_type size() const noexcept { return numBits_; }
103 [[nodiscard]] size_type num_blocks() const noexcept { return blocks_.size(); }
104 [[nodiscard]] size_type capacity() const noexcept { return blocks_.capacity() * bits_per_block; }
105
106 [[nodiscard]] bool uncheckedTest( IndexType n ) const { assert( n < size() ); return blocks_[blockIndex_( n )] & bitMask_( n ); }
107 [[nodiscard]] bool uncheckedTestSet( IndexType n, bool val = true ) { assert( n < size() ); bool b = uncheckedTest( n ); if ( b != val ) set( n, val ); return b; }
108
110 [[nodiscard]] bool test( IndexType n ) const { return n < size() ? uncheckedTest( n ) : false; }
111 [[nodiscard]] bool test_set( IndexType n, bool val = true ) { return ( val || n < size() ) ? uncheckedTestSet( n, val ) : false; }
112
113 MR_BIND_OVERLOAD_SUFFIX(range) MRMESH_API BitSet & set( IndexType n, size_type len, bool val );
114 MR_BIND_OVERLOAD_SUFFIX() BitSet & set( IndexType n, bool val ) { return val ? set( n ) : reset( n ); }
115 MR_BIND_OVERLOAD_SUFFIX(bit) BitSet & set( IndexType n ) { assert( n < size() ); blocks_[blockIndex_( n )] |= bitMask_( n ); return * this; }
116 MR_BIND_OVERLOAD_SUFFIX(all) MRMESH_API BitSet & set();
117
121 {
122 assert( n < size() );
123 const auto mask = bitMask_( n );
124#ifdef __cpp_lib_atomic_ref
125 return std::atomic_ref<block_type>( blocks_[blockIndex_( n )] ).fetch_or( mask, std::memory_order_relaxed ) & mask;
126#else
127 return __atomic_fetch_or( &blocks_[blockIndex_( n )], mask, __ATOMIC_RELAXED ) & mask;
128#endif
129 }
130
132 BitSet & reset( IndexType n ) { if ( n < size() ) blocks_[blockIndex_( n )] &= ~bitMask_( n ); return * this; }
134
136 BitSet & flip( IndexType n ) { assert( n < size() ); blocks_[blockIndex_( n )] ^= bitMask_( n ); return * this; }
138
141
143 void push_back( bool val ) { auto n = numBits_++; if ( bitIndex_( n ) == 0 ) blocks_.push_back( block_type{} ); set( n, val ); }
144
146 void pop_back() { assert( numBits_ > 0 ); { if ( bitIndex_( numBits_ ) == 1 ) blocks_.pop_back(); else reset( numBits_ - 1 ); } --numBits_; }
147
149 [[nodiscard]] const auto & bits() const { return blocks_; }
150
154
157 MRMESH_API BitSet & subtract( const BitSet & b, int bShiftInBlocks );
158
160 [[nodiscard]] MRMESH_API bool all() const;
161
163 [[nodiscard]] MRMESH_API bool any() const;
164
166 [[nodiscard]] bool none() const { return !any(); }
167
169 [[nodiscard]] MRMESH_API size_type count() const noexcept;
170
173
176 [[nodiscard]] MRMESH_API Counts countBitsAndBlocks( size_type maxBits = ~size_type{} ) const noexcept;
177
179 [[nodiscard]] IndexType find_first() const { return findSetBitAfter_( 0 ); }
180
182 [[nodiscard]] IndexType find_next( IndexType n ) const { return findSetBitAfter_( n + 1 ); }
183
185 [[nodiscard]] MRMESH_API IndexType find_last() const;
186
188 [[nodiscard]] IndexType find_first_not_set() const { return findNonSetBitAfter_( 0 ); }
189
191 [[nodiscard]] IndexType find_next_not_set( IndexType n ) const { return findNonSetBitAfter_( n + 1 ); }
192
195
197 [[nodiscard]] MRMESH_API size_t nthSetBit( size_t n ) const;
198
200 [[nodiscard]] MRMESH_API bool is_subset_of( const BitSet& a ) const;
201
203 [[nodiscard]] MRMESH_API bool intersects( const BitSet & a ) const;
204
206 [[deprecated( "use resize() instead" )]]
207 void resizeWithReserve( size_t newSize ) { resize( newSize ); }
208
210 MR_BIND_OVERLOAD_SUFFIX(range) void autoResizeSet( size_t pos, size_type len, bool val = true )
211 {
212 if ( pos + len > size() )
213 resize( pos + len );
214 set( pos, len, val );
215 }
216 MR_BIND_OVERLOAD_SUFFIX() void autoResizeSet( size_t pos, bool val = true ) { autoResizeSet( pos, 1, val ); }
217
219 [[nodiscard]] bool autoResizeTestSet( size_t pos, bool val = true )
220 {
221 bool const b = test( pos );
222 if ( b != val )
223 autoResizeSet( pos, val );
224 return b;
225 }
226
228 [[nodiscard]] size_t heapBytes() const { return capacity() / 8; }
229
231 [[nodiscard]] IndexType backId() const { assert( !empty() ); return IndexType{ size() - 1 }; }
232
234 [[nodiscard]] static IndexType beginId() { return IndexType{ 0 }; }
235 [[nodiscard]] IndexType endId() const { return IndexType{ size() }; }
236
237private:
239 [[nodiscard]] static size_type calcNumBlocks_( size_type numBits ) noexcept { return ( numBits + bits_per_block - 1 ) / bits_per_block; }
240
242 [[nodiscard]] static size_type blockIndex_( IndexType n ) noexcept { return n / bits_per_block; }
243
245 [[nodiscard]] static size_type bitIndex_( IndexType n ) noexcept { return n % bits_per_block; }
246
248 [[nodiscard]] static block_type bitMask_( IndexType n ) noexcept { return block_type( 1 ) << bitIndex_( n ); }
249
251 [[nodiscard]] static block_type bitMask_( IndexType firstBit, IndexType lastBit ) noexcept
252 {
253 return ( ( block_type( 1 ) << firstBit ) - 1 )
254 ^
255 ( lastBit == bits_per_block ? ~block_type{} : ( ( block_type( 1 ) << lastBit ) - 1 ) );
256 }
257
259 MRMESH_API void setUnusedBits_();
260
262 MRMESH_API void resetUnusedBits_();
263
267 template<class FullBlock, class PartialBlock>
268 BitSet & rangeOp_( IndexType n, size_type len, FullBlock&&, PartialBlock&& );
269
271 MRMESH_API IndexType findSetBitAfter_( IndexType n ) const;
272
274 MRMESH_API IndexType findNonSetBitAfter_( IndexType n ) const;
275
276 MRMESH_API friend std::ostream& operator<<( std::ostream& s, const BitSet & bs );
277 MRMESH_API friend std::istream& operator>>( std::istream& s, BitSet & bs );
278
279 [[nodiscard]] MR_BIND_IGNORE_PY friend auto begin( const BitSet & a ) { return SetBitIteratorT<BitSet>(a); }
280 [[nodiscard]] MR_BIND_IGNORE_PY friend auto end( const BitSet & ) { return SetBitIteratorT<BitSet>(); }
281
282private:
283 Blocks blocks_;
284 size_type numBits_ = 0;
285};
286
288[[nodiscard]] MR_BIND_IGNORE MRMESH_API size_t countSetBits( const BitSet::block_type* blocks, size_t numBlocks ) noexcept;
289
292template <typename I>
293class TypedBitSet : public BitSet
294{
295 using base = BitSet;
296public:
297 using base::base;
298 using IndexType = I;
299
301 explicit TypedBitSet( const BitSet & src ) : BitSet( src ) {}
302
304 explicit TypedBitSet( BitSet && src ) : BitSet( std::move( src ) ) {}
305
306 MR_BIND_OVERLOAD_SUFFIX(range) TypedBitSet & set( IndexType n, size_type len, bool val ) { base::set( n, len, val ); return * this; }
307 MR_BIND_OVERLOAD_SUFFIX() TypedBitSet & set( IndexType n, bool val ) { base::set( n, val ); return * this; }
308 MR_BIND_OVERLOAD_SUFFIX(bit) TypedBitSet & set( IndexType n ) { base::set( n ); return * this; }
309 MR_BIND_OVERLOAD_SUFFIX(all) TypedBitSet & set() { base::set(); return * this; }
310 bool atomicTestSet( IndexType n ) { return base::atomicTestSet( n ); }
311 TypedBitSet & reset( IndexType n, size_type len ) { base::reset( n, len ); return * this; }
312 TypedBitSet & reset( IndexType n ) { base::reset( n ); return * this; }
313 TypedBitSet & reset() { base::reset(); return * this; }
314 TypedBitSet & flip( IndexType n, size_type len ) { base::flip( n, len ); return * this; }
315 TypedBitSet & flip( IndexType n ) { base::flip( n ); return * this; }
316 TypedBitSet & flip() { base::flip(); return * this; }
317 [[nodiscard]] bool test( IndexType n ) const { return base::test( n ); }
318 [[nodiscard]] bool test_set( IndexType n, bool val = true ) { return base::test_set( n, val ); }
319
320 [[nodiscard]] IndexType find_first() const { return IndexType( base::find_first() ); }
321 [[nodiscard]] IndexType find_next( IndexType pos ) const { return IndexType( base::find_next( pos ) ); }
322 [[nodiscard]] IndexType find_last() const { return IndexType( base::find_last() ); }
323 [[nodiscard]] IndexType find_first_not_set() const { return IndexType( base::find_first_not_set() ); }
324 [[nodiscard]] IndexType find_next_not_set( IndexType pos ) const { return IndexType( base::find_next_not_set( pos ) ); }
325 [[nodiscard]] IndexType find_last_not_set() const { return IndexType( base::find_last_not_set() ); }
327 [[nodiscard]] IndexType nthSetBit( size_t n ) const { return IndexType( base::nthSetBit( n ) ); }
328
329 TypedBitSet & operator &= ( const TypedBitSet & b ) { base::operator &= ( b ); return * this; }
330 TypedBitSet & operator |= ( const TypedBitSet & b ) { base::operator |= ( b ); return * this; }
331 TypedBitSet & operator ^= ( const TypedBitSet & b ) { base::operator ^= ( b ); return * this; }
332 TypedBitSet & operator -= ( const TypedBitSet & b ) { base::operator -= ( b ); return * this; }
333
334 [[nodiscard]] friend TypedBitSet operator & ( const TypedBitSet & a, const TypedBitSet & b ) { auto res{ a }; res &= b; return res; }
335 [[nodiscard]] friend TypedBitSet operator | ( const TypedBitSet & a, const TypedBitSet & b ) { auto res{ a }; res |= b; return res; }
336 [[nodiscard]] friend TypedBitSet operator ^ ( const TypedBitSet & a, const TypedBitSet & b ) { auto res{ a }; res ^= b; return res; }
337 [[nodiscard]] friend TypedBitSet operator - ( const TypedBitSet & a, const TypedBitSet & b ) { auto res{ a }; res -= b; return res; }
338
339 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator & ( TypedBitSet && a, const TypedBitSet & b ) { return std::move( a &= b ); }
340 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator | ( TypedBitSet && a, const TypedBitSet & b ) { return std::move( a |= b ); }
341 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator ^ ( TypedBitSet && a, const TypedBitSet & b ) { return std::move( a ^= b ); }
342 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator - ( TypedBitSet && a, const TypedBitSet & b ) { return std::move( a -= b ); }
343
344 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator & ( const TypedBitSet & a, TypedBitSet && b ) { return std::move( b &= a ); }
345 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator | ( const TypedBitSet & a, TypedBitSet && b ) { return std::move( b |= a ); }
346 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator ^ ( const TypedBitSet & a, TypedBitSet && b ) { return std::move( b ^= a ); }
347
348 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator & ( TypedBitSet && a, TypedBitSet && b ) { return std::move( a &= b ); }
349 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator | ( TypedBitSet && a, TypedBitSet && b ) { return std::move( a |= b ); }
350 [[nodiscard]] MR_BIND_IGNORE friend TypedBitSet operator ^ ( TypedBitSet && a, TypedBitSet && b ) { return std::move( a ^= b ); }
351
353 TypedBitSet & subtract( const TypedBitSet & b, int bShiftInBlocks ) { base::subtract( b, bShiftInBlocks ); return * this; }
354
356 [[nodiscard]] bool is_subset_of( const TypedBitSet& a ) const { return base::is_subset_of( a ); }
357
359 [[nodiscard]] bool intersects( const TypedBitSet & a ) const { return base::intersects( a ); }
360
361 MR_BIND_OVERLOAD_SUFFIX(range) void autoResizeSet( IndexType pos, size_type len, bool val = true ) { base::autoResizeSet( pos, len, val ); }
362 MR_BIND_OVERLOAD_SUFFIX() void autoResizeSet( IndexType pos, bool val = true ) { base::autoResizeSet( pos, val ); }
363 [[nodiscard]] bool autoResizeTestSet( IndexType pos, bool val = true ) { return base::autoResizeTestSet( pos, val ); }
364
366 template <typename M>
367 [[nodiscard]] TypedBitSet getMapping( const M & map ) const;
368 [[nodiscard]] TypedBitSet getMapping( const Vector<IndexType, IndexType> & map ) const
369 { return getMapping( [&map]( IndexType i ) { return map[i]; } ); }
370 [[nodiscard]] TypedBitSet getMapping( const BMap<IndexType, IndexType> & map ) const
371 { return getMapping( [&map]( IndexType i ) { return map.b[i]; }, map.tsize ); }
372 [[nodiscard]] TypedBitSet getMapping( const HashMap<IndexType, IndexType> & map ) const
373 { return getMapping( [&map]( IndexType i ) { return getAt( map, i ); } ); }
374
375 template <typename M>
376 [[nodiscard]] TypedBitSet getMapping( const M & map, size_t resSize ) const;
377 [[nodiscard]] TypedBitSet getMapping( const Vector<IndexType, IndexType> & map, size_t resSize ) const
378 { return getMapping( [&map]( IndexType i ) { return map[i]; }, resSize ); }
379 [[nodiscard]] TypedBitSet getMapping( const HashMap<IndexType, IndexType> & map, size_t resSize ) const
380 { return getMapping( [&map]( IndexType i ) { return getAt( map, i ); }, resSize ); }
381
383 [[nodiscard]] IndexType backId() const { assert( !empty() ); return IndexType{ size() - 1 }; }
384
386 [[nodiscard]] static IndexType beginId() { return IndexType{ size_t( 0 ) }; }
387 [[nodiscard]] IndexType endId() const { return IndexType{ size() }; }
388
389 [[nodiscard]] MR_BIND_IGNORE_PY friend auto begin( const TypedBitSet & a ) { return SetBitIteratorT<TypedBitSet>(a); }
390 [[nodiscard]] MR_BIND_IGNORE_PY friend auto end( const TypedBitSet & ) { return SetBitIteratorT<TypedBitSet>(); }
391};
392
393
395[[nodiscard]] inline size_t heapBytes( const BitSet& bs )
396{
397 return bs.heapBytes();
398}
399
401[[nodiscard]] MRMESH_API bool operator == ( const BitSet & a, const BitSet & b );
402template <typename I>
403[[nodiscard]] inline bool operator == ( const TypedBitSet<I> & a, const TypedBitSet<I> & b )
404 { return static_cast<const BitSet &>( a ) == static_cast<const BitSet &>( b ); }
405
406template <typename T, typename U>
407void operator == ( const TypedBitSet<T> & a, const TypedBitSet<U> & b ) = delete;
408
409template <typename I>
410[[nodiscard]] inline std::function<bool( I )> makePredicate( const TypedBitSet<I> * bitset )
411{
412 std::function<bool( I )> res;
413 if ( bitset )
414 res = [bitset]( I id ) { return bitset->test( id ); };
415 return res;
416}
417
418template <typename I>
419[[nodiscard]] inline std::function<bool( I )> makePredicate( const TypedBitSet<I> & bitset )
420 { return makePredicate( &bitset ); }
421
422template <typename I>
423[[nodiscard]] inline bool contains( const TypedBitSet<I> * bitset, I id )
424{
425 return id.valid() && ( !bitset || bitset->test( id ) );
426}
427
428template <typename I>
429[[nodiscard]] inline bool contains( const TypedBitSet<I> & bitset, I id )
430{
431 return id.valid() && bitset.test( id );
432}
433
434
436template <typename I>
438{
439 int n = 0;
440 for ( auto v : bs )
441 vec[v] = n++;
442}
443
445template <typename I>
447{
448 Vector<int, I> res( bs.size(), -1 );
449 fillVectorWithSeqNums( bs, res );
450 return res;
451}
452
454template <typename I>
455[[nodiscard]] Vector<I> listSetBits( const TypedBitSet<I> & bs )
456{
457 Vector<I> res;
458 res.resizeNoInit( bs.count() );
459 size_t n = 0;
460 for ( auto v : bs )
461 res[n++] = v;
462 return res;
463}
464
467
469template <typename I>
471{
472 HashMap<I, int> res;
473 int n = 0;
474 for ( auto v : bs )
475 res[v] = n++;
476 return res;
477}
478
479template <typename I>
480template <typename M>
481[[nodiscard]] TypedBitSet<I> TypedBitSet<I>::getMapping( const M & map ) const
482{
483 TypedBitSet<I> res;
484 for ( auto b : *this )
485 if ( auto mapped = map( b ) )
486 res.autoResizeSet( mapped );
487 return res;
488}
489
490template <typename I>
491template <typename M>
492[[nodiscard]] TypedBitSet<I> TypedBitSet<I>::getMapping( const M & map, size_t resSize ) const
493{
494 TypedBitSet<I> res;
495 if ( !any() )
496 return res;
497 res.resize( resSize );
498 for ( auto b : *this )
499 if ( auto mapped = map( b ) )
500 res.set( mapped );
501 return res;
502}
503
504[[nodiscard]] inline BitSet operator & ( const BitSet & a, const BitSet & b ) { BitSet res{ a }; res &= b; return res; }
505[[nodiscard]] inline BitSet operator | ( const BitSet & a, const BitSet & b ) { BitSet res{ a }; res |= b; return res; }
506[[nodiscard]] inline BitSet operator ^ ( const BitSet & a, const BitSet & b ) { BitSet res{ a }; res ^= b; return res; }
507[[nodiscard]] inline BitSet operator - ( const BitSet & a, const BitSet & b ) { BitSet res{ a }; res -= b; return res; }
508
509[[nodiscard]] MR_BIND_IGNORE inline BitSet operator & ( BitSet && a, const BitSet & b ) { return std::move( a &= b ); }
510[[nodiscard]] MR_BIND_IGNORE inline BitSet operator | ( BitSet && a, const BitSet & b ) { return std::move( a |= b ); }
511[[nodiscard]] MR_BIND_IGNORE inline BitSet operator ^ ( BitSet && a, const BitSet & b ) { return std::move( a ^= b ); }
512[[nodiscard]] MR_BIND_IGNORE inline BitSet operator - ( BitSet && a, const BitSet & b ) { return std::move( a -= b ); }
513
514[[nodiscard]] MR_BIND_IGNORE inline BitSet operator & ( const BitSet & a, BitSet && b ) { return std::move( b &= a ); }
515[[nodiscard]] MR_BIND_IGNORE inline BitSet operator | ( const BitSet & a, BitSet && b ) { return std::move( b |= a ); }
516[[nodiscard]] MR_BIND_IGNORE inline BitSet operator ^ ( const BitSet & a, BitSet && b ) { return std::move( b ^= a ); }
517
518[[nodiscard]] MR_BIND_IGNORE inline BitSet operator & ( BitSet && a, BitSet && b ) { return std::move( a &= b ); }
519[[nodiscard]] MR_BIND_IGNORE inline BitSet operator | ( BitSet && a, BitSet && b ) { return std::move( a |= b ); }
520[[nodiscard]] MR_BIND_IGNORE inline BitSet operator ^ ( BitSet && a, BitSet && b ) { return std::move( a ^= b ); }
521
523
524}
constexpr bool operator==(ImVec2 a, ImVec2 b)
Definition MRImGuiVectorOperators.h:117
constexpr auto operator*(A a, B b)
Definition MRImGuiVectorOperators.h:107
#define MRMESH_API
Definition MRMeshFwd.h:85
#define M(T)
Definition MRBitSet.h:71
size_t size_type
Definition MRBitSet.h:78
IndexType find_first_not_set() const
return the smallest index i such that bit i is NOT set, or npos if *this has no off bits.
Definition MRBitSet.h:188
size_t IndexType
Definition MRBitSet.h:79
friend auto end(const BitSet &)
Definition MRBitSet.h:280
bool all() const
returns true if all bits in this container are set
static constexpr size_t bits_per_block
Definition MRBitSet.h:75
IndexType endId() const
Definition MRBitSet.h:235
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRBitSet.h:234
BitSet & reset()
bool atomicTestSet(IndexType n)
Definition MRBitSet.h:120
friend auto begin(const BitSet &a)
Definition MRBitSet.h:279
void reserve(size_type numBits)
Definition MRBitSet.h:96
BitSet() noexcept=default
creates empty bitset
bool any() const
returns true if at least one bits in this container is set
IndexType find_next_not_set(IndexType n) const
return the smallest index i>n such that bit i is NOT set, or npos if *this has no off bits.
Definition MRBitSet.h:191
IndexType find_last() const
return the highest index i such that bit i is set, or npos if *this has no on bits.
BitSet & set(IndexType n, size_type len, bool val)
Counts countBitsAndBlocks(size_type maxBits=~size_type{}) const noexcept
BitSet & operator|=(const BitSet &b)
BitSet & set()
bool empty() const noexcept
Definition MRBitSet.h:101
friend std::ostream & operator<<(std::ostream &s, const BitSet &bs)
BitSet & set(IndexType n)
Not using a default argument for val to get better C bindings.
Definition MRBitSet.h:115
void autoResizeSet(size_t pos, bool val=true)
Definition MRBitSet.h:216
BitSet & reset(IndexType n, size_type len)
BitSet & operator-=(const BitSet &b)
static BitSet fromBlocks(Blocks &&blocks)
creates bitset from the given blocks of bits
Definition MRBitSet.h:88
bool autoResizeTestSet(size_t pos, bool val=true)
same as autoResizeSet and returns previous value of pos-bit
Definition MRBitSet.h:219
bool test_set(IndexType n, bool val=true)
return n < size() && uncheckedTest( n ); was compiled with extra conditional jump inside uncheckedTes...
Definition MRBitSet.h:111
bool uncheckedTest(IndexType n) const
Definition MRBitSet.h:106
bool intersects(const BitSet &a) const
returns true if, there is a bit which is set in this bitset, such that the corresponding bit in bitse...
void clear()
Definition MRBitSet.h:98
BitSet & flip(IndexType n, size_type len)
void resize(size_type numBits, bool fillValue=false)
IndexType find_last_not_set() const
return the highest index i such that bit i is NOT set, or npos if *this has no off bits.
size_type size() const noexcept
Definition MRBitSet.h:102
bool uncheckedTestSet(IndexType n, bool val=true)
Definition MRBitSet.h:107
BitSet & reset(IndexType n)
Definition MRBitSet.h:132
bool test(IndexType n) const
all bits after size() we silently consider as not-set
Definition MRBitSet.h:110
BitSet & flip()
friend std::istream & operator>>(std::istream &s, BitSet &bs)
std::vector< block_type, Allocator< block_type > > Blocks
Definition MRBitSet.h:74
void reverse()
changes the order of bits on the opposite
IndexType find_next(IndexType n) const
return the smallest index i>n such that bit i is set, or npos if *this has no on bits.
Definition MRBitSet.h:182
void autoResizeSet(size_t pos, size_type len, bool val=true)
sets elements [pos, pos+len) to given value, adjusting the size of the set to include new elements
Definition MRBitSet.h:210
size_type num_blocks() const noexcept
Definition MRBitSet.h:103
BitSet & set(IndexType n, bool val)
Definition MRBitSet.h:114
IndexType backId() const
returns the identifier of the back() element
Definition MRBitSet.h:231
void resizeWithReserve(size_t newSize)
same as resize(newSize), which already grows the capacity geometrically
Definition MRBitSet.h:207
IndexType find_first() const
return the smallest index i such that bit i is set, or npos if *this has no on bits.
Definition MRBitSet.h:179
std::uint64_t block_type
Definition MRBitSet.h:73
void push_back(bool val)
adds one more bit with the given value in the container, increasing its size on 1
Definition MRBitSet.h:143
size_type capacity() const noexcept
Definition MRBitSet.h:104
BitSet & subtract(const BitSet &b, int bShiftInBlocks)
subtracts b from this, considering that bits in b are shifted right on bShiftInBlocks*bits_per_block
size_t nthSetBit(size_t n) const
returns the location of nth set bit (where the first bit corresponds to n=0) or npos if there are les...
const auto & bits() const
read-only access to all bits stored as a vector of uint64 blocks
Definition MRBitSet.h:149
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRBitSet.h:228
bool none() const
returns true if all bits in this container are reset
Definition MRBitSet.h:166
BitSet & operator&=(const BitSet &b)
static constexpr size_t npos
Definition MRBitSet.h:76
bool is_subset_of(const BitSet &a) const
returns true if, for every bit that is set in this bitset, the corresponding bit in bitset a is also ...
void pop_back()
removes last bit from the container, decreasing its size on 1
Definition MRBitSet.h:146
BitSet & flip(IndexType n)
Definition MRBitSet.h:136
void shrink_to_fit()
Definition MRBitSet.h:99
size_type count() const noexcept
computes the number of set bits in the whole set
BitSet & operator^=(const BitSet &b)
iterator to enumerate all indices with set bits in BitSet class or its derivatives
Definition MRBitSet.h:26
SetBitIteratorT operator++(int)
Definition MRBitSet.h:51
const IndexType * pointer
Definition MRBitSet.h:34
SetBitIteratorT()=default
constructs end iterator
std::forward_iterator_tag iterator_category
Definition MRBitSet.h:30
const T * bitset() const
Definition MRBitSet.h:58
IndexType value_type
Definition MRBitSet.h:31
typename T::IndexType IndexType
Definition MRBitSet.h:28
SetBitIteratorT & operator++()
Definition MRBitSet.h:45
SetBitIteratorT(const T &bitset)
constructs begin iterator
Definition MRBitSet.h:40
IndexType reference
intentionally not a reference
Definition MRBitSet.h:33
std::ptrdiff_t difference_type
Definition MRBitSet.h:32
Definition MRBitSet.h:294
IndexType find_last() const
Definition MRBitSet.h:322
TypedBitSet & operator|=(const TypedBitSet &b)
Definition MRBitSet.h:330
bool intersects(const TypedBitSet &a) const
returns true if, there is a bit which is set in this bitset, such that the corresponding bit in bitse...
Definition MRBitSet.h:359
bool test_set(IndexType n, bool val=true)
Definition MRBitSet.h:318
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map) const
Definition MRBitSet.h:372
friend TypedBitSet operator|(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:335
friend TypedBitSet operator&(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:334
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRBitSet.h:386
TypedBitSet(const BitSet &src)
copies all bits from another BitSet (or a descending class, e.g. TypedBitSet<U>)
Definition MRBitSet.h:301
TypedBitSet & flip(IndexType n)
Definition MRBitSet.h:315
IndexType backId() const
returns the identifier of the back() element
Definition MRBitSet.h:383
friend auto end(const TypedBitSet &)
Definition MRBitSet.h:390
friend auto begin(const TypedBitSet &a)
Definition MRBitSet.h:389
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map, size_t resSize) const
Definition MRBitSet.h:379
bool atomicTestSet(IndexType n)
Definition MRBitSet.h:310
TypedBitSet & operator-=(const TypedBitSet &b)
Definition MRBitSet.h:332
TypedBitSet & set(IndexType n, size_type len, bool val)
Definition MRBitSet.h:306
bool is_subset_of(const TypedBitSet &a) const
returns true if, for every bit that is set in this bitset, the corresponding bit in bitset a is also ...
Definition MRBitSet.h:356
TypedBitSet & reset(IndexType n, size_type len)
Definition MRBitSet.h:311
friend TypedBitSet operator-(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:337
TypedBitSet & operator^=(const TypedBitSet &b)
Definition MRBitSet.h:331
TypedBitSet & set()
Definition MRBitSet.h:309
void autoResizeSet(IndexType pos, size_type len, bool val=true)
Definition MRBitSet.h:361
IndexType endId() const
Definition MRBitSet.h:387
I IndexType
Definition MRBitSet.h:298
TypedBitSet & flip()
Definition MRBitSet.h:316
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map, size_t resSize) const
Definition MRBitSet.h:377
IndexType find_first_not_set() const
Definition MRBitSet.h:323
IndexType find_first() const
Definition MRBitSet.h:320
friend TypedBitSet operator^(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:336
IndexType find_next_not_set(IndexType pos) const
Definition MRBitSet.h:324
TypedBitSet & flip(IndexType n, size_type len)
Definition MRBitSet.h:314
TypedBitSet & operator&=(const TypedBitSet &b)
Definition MRBitSet.h:329
TypedBitSet getMapping(const M &map, size_t resSize) const
this is a faster version if the result size is known beforehand
IndexType find_next(IndexType pos) const
Definition MRBitSet.h:321
TypedBitSet & reset()
Definition MRBitSet.h:313
bool autoResizeTestSet(IndexType pos, bool val=true)
Definition MRBitSet.h:363
IndexType find_last_not_set() const
Definition MRBitSet.h:325
TypedBitSet(BitSet &&src)
moves all bits from another BitSet (or a descending class, e.g. TypedBitSet<U>)
Definition MRBitSet.h:304
TypedBitSet & set(IndexType n)
Not using a default argument for val to get better C bindings.
Definition MRBitSet.h:308
TypedBitSet & subtract(const TypedBitSet &b, int bShiftInBlocks)
subtracts b from this, considering that bits in b are shifted right on bShiftInBlocks*bits_per_block
Definition MRBitSet.h:353
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map) const
Definition MRBitSet.h:368
TypedBitSet & reset(IndexType n)
Definition MRBitSet.h:312
TypedBitSet getMapping(const M &map) const
constructs another bit set from this where every set bit index is transformed using given map
IndexType nthSetBit(size_t n) const
returns the location of nth set bit (where the first bit corresponds to n=0) or IndexType(npos) if th...
Definition MRBitSet.h:327
bool test(IndexType n) const
Definition MRBitSet.h:317
TypedBitSet getMapping(const BMap< IndexType, IndexType > &map) const
Definition MRBitSet.h:370
Vector<T> is indexed by size_t.
Definition MRVector.h:24
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:507
HashMap< size_t, size_t > makeHashMapWithSeqNums(const BitSet &bs)
creates a HashMap where for each set bit of input bitset its sequential number starting from 0 is ret...
BitSet operator&(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:504
Vector< I > listSetBits(const TypedBitSet< I > &bs)
returns the ids of all set bits in increasing order, i.e. the inverse of makeVectorWithSeqNums(....
Definition MRBitSet.h:455
size_t countSetBits(const BitSet::block_type *blocks, size_t numBlocks) noexcept
returns the number of set bits in given blocks; uses the popcnt instruction on x86-64 CPUs having it
Vector< int, I > makeVectorWithSeqNums(const TypedBitSet< I > &bs)
creates a Vector where for each set bit of input bitset its sequential number starting from 0 is retu...
Definition MRBitSet.h:446
bool contains(const TypedBitSet< I > *bitset, I id)
Definition MRBitSet.h:423
void fillVectorWithSeqNums(const TypedBitSet< I > &bs, Vector< int, I > &vec)
for each set bit of input bitset, writes its sequential number starting from 0 in the given vector th...
Definition MRBitSet.h:437
BitSet operator|(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:505
std::function< bool(I)> makePredicate(const TypedBitSet< I > *bitset)
Definition MRBitSet.h:410
BitSet operator^(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:506
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRBitSet.h:395
bool operator==(const Allocator< T > &, const Allocator< U > &) noexcept
Definition MRAllocator.h:93
Buffer< T, I > b
Definition MRBuffer.h:149
struct
Definition MRMeshFwd.h:189
T getAt(const Buffer< T, I > &bmap, I key, T def={})
given some buffer map and a key, returns the value associated with the key, or default value if key i...
Definition MRBuffer.h:140
void resizeNoInit(size_t targetSize, ReserveMode mode=ReserveMode::Exact)
resizes the vector skipping initialization of its elements (more precisely initializing them using ( ...
Definition MRVector.h:78
size_t tsize
target size, all values inside b must be less than this value
Definition MRBuffer.h:150
phmap::flat_hash_map< K, V, Hash, Eq, Allocator< std::pair< const K, V > > > HashMap
Definition MRMeshFwd.h:631
SetBitIteratorT< BitSet >(FaceSetBitIterator, SetBitIteratorT< FaceBitSet >)(VertSetBitIterator
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
flat map: I -> T
Definition MRBuffer.h:148
the numbers of set bits and of non-empty blocks
Definition MRBitSet.h:172
size_type blocks
Definition MRBitSet.h:172
size_type bits
Definition MRBitSet.h:172