8#include "MRPch/MRBindingMacros.h"
47 index_ = bitset_->find_next( index_ );
58 [[nodiscard]]
const T *
bitset()
const {
return bitset_; }
64 const T * bitset_ =
nullptr;
65 IndexType index_ = IndexType( ~
size_t( 0 ) );
74 using Blocks = std::vector<block_type, Allocator<block_type>>;
76 inline static constexpr size_t npos = (size_t)-1;
85 explicit
BitSet(
size_t numBits,
bool fillValue = false ) {
resize( numBits, fillValue ); }
91 res.blocks_ = std::move( blocks );
98 void clear() { numBits_ = 0; blocks_.clear(); }
101 [[nodiscard]]
bool empty() const noexcept {
return numBits_ == 0; }
115 MR_BIND_OVERLOAD_SUFFIX(bit)
BitSet &
set(
IndexType n ) { assert( n <
size() ); blocks_[blockIndex_( n )] |= bitMask_( n );
return *
this; }
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;
127 return __atomic_fetch_or( &blocks_[blockIndex_( n )], mask, __ATOMIC_RELAXED ) & mask;
143 void push_back(
bool val ) {
auto n = numBits_++;
if ( bitIndex_( n ) == 0 ) blocks_.push_back(
block_type{} );
set( n, val ); }
146 void pop_back() { assert( numBits_ > 0 ); {
if ( bitIndex_( numBits_ ) == 1 ) blocks_.pop_back();
else reset( numBits_ - 1 ); } --numBits_; }
149 [[nodiscard]]
const auto &
bits()
const {
return blocks_; }
166 [[nodiscard]]
bool none()
const {
return !
any(); }
206 [[deprecated(
"use resize() instead" )]]
212 if ( pos + len >
size() )
214 set( pos, len, val );
221 bool const b =
test( pos );
253 return ( (
block_type( 1 ) << firstBit ) - 1 )
267 template<
class FullBlock,
class PartialBlock>
366 template <
typename M>
375 template <
typename M>
404 {
return static_cast<const BitSet &
>( a ) ==
static_cast<const BitSet &
>( b ); }
406template <
typename T,
typename U>
412 std::function<bool( I )> res;
414 res = [bitset]( I id ) {
return bitset->
test(
id ); };
425 return id.valid() && ( !bitset || bitset->
test(
id ) );
431 return id.valid() && bitset.
test(
id );
484 for (
auto b : *
this )
485 if (
auto mapped = map( b ) )
498 for (
auto b : *
this )
499 if (
auto mapped = map( b ) )
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
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
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)
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
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