7#include "MRPch/MRBindingMacros.h"
28 inline static constexpr size_t npos = (size_t)-1;
37 explicit
BitSet(
size_t numBits,
bool fillValue = false ) {
resize( numBits, fillValue ); }
43 res.blocks_ = std::move( blocks );
50 void clear() { numBits_ = 0; blocks_.clear(); }
53 [[nodiscard]]
bool empty() const noexcept {
return numBits_ == 0; }
82 void push_back(
bool val ) {
auto n = numBits_++;
if ( bitIndex_( n ) == 0 ) blocks_.push_back(
block_type{} );
set( n, val ); }
85 void pop_back() { assert( numBits_ > 0 ); {
if ( bitIndex_( numBits_ ) == 1 ) blocks_.pop_back();
else reset( numBits_ - 1 ); } --numBits_; }
88 [[nodiscard]]
const auto &
bits()
const {
return blocks_; }
99 [[nodiscard]] MRMESH_API
bool all()
const;
102 [[nodiscard]] MRMESH_API
bool any()
const;
105 [[nodiscard]]
bool none()
const {
return !
any(); }
129 [[nodiscard]] MRMESH_API
size_t nthSetBit(
size_t n )
const;
141 if ( reserved > 0 && newSize > reserved )
143 while ( newSize > reserved )
153 if ( pos + len >
size() )
155 set( pos, len, val );
162 bool const b =
test( pos );
194 return ( (
block_type( 1 ) << firstBit ) - 1 )
200 MRMESH_API
void setUnusedBits_();
203 MRMESH_API
void resetUnusedBits_();
208 template<
class FullBlock,
class PartialBlock>
221 std::vector<block_type> blocks_;
287 template <
typename M>
296 template <
typename M>
322 {
return static_cast<const BitSet &
>( a ) ==
static_cast<const BitSet &
>( b ); }
324template <
typename T,
typename U>
330 std::function<bool( I )> res;
332 res = [bitset]( I id ) {
return bitset->
test(
id ); };
343 return id.valid() && ( !bitset || bitset->
test(
id ) );
349 return id.valid() && bitset.
test(
id );
369 : bitset_( &bitset ), index_( bitset.find_first() )
374 index_ = bitset_->find_next( index_ );
384 [[nodiscard]]
const T *
bitset()
const {
return bitset_; }
390 const T * bitset_ =
nullptr;
391 IndexType index_ = IndexType( ~
size_t( 0 ) );
395[[nodiscard]] MR_BIND_IGNORE_PY
inline auto begin(
const BitSet & a )
397[[nodiscard]] MR_BIND_IGNORE_PY
inline auto end(
const BitSet & )
434 for (
auto b : *this )
435 if (
auto mapped = map( b ) )
448 for (
auto b : *this )
449 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
size_t size_type
Definition MRBitSet.h:30
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:120
size_t IndexType
Definition MRBitSet.h:31
MRMESH_API BitSet & flip(IndexType n, size_type len)
MRMESH_API size_type count() const noexcept
computes the number of set bits in the whole set
static constexpr size_t bits_per_block
Definition MRBitSet.h:27
IndexType endId() const
Definition MRBitSet.h:176
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRBitSet.h:175
MRMESH_API IndexType find_last() const
return the highest index i such that bit i is set, or npos if *this has no on bits.
MRMESH_API 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...
MRMESH_API BitSet & subtract(const BitSet &b, int bShiftInBlocks)
subtracts b from this, considering that bits in b are shifted right on bShiftInBlocks*bits_per_block
MRMESH_API BitSet & operator-=(const BitSet &b)
Uint64 block_type
Definition MRBitSet.h:26
void reserve(size_type numBits)
Definition MRBitSet.h:48
MRMESH_API void resize(size_type numBits, bool fillValue=false)
BitSet() noexcept=default
creates empty bitset
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:123
MRMESH_API 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.
bool empty() const noexcept
Definition MRBitSet.h:53
BitSet & set(IndexType n)
Not using a default argument for val to get better C bindings.
Definition MRBitSet.h:67
void autoResizeSet(size_t pos, bool val=true)
Definition MRBitSet.h:157
MRMESH_API BitSet & reset()
MRMESH_API BitSet & flip()
bool autoResizeTestSet(size_t pos, bool val=true)
same as autoResizeSet and returns previous value of pos-bit
Definition MRBitSet.h:160
bool test_set(IndexType n, bool val=true)
return n < size() && uncheckedTest( n ); was compiled with extra conditional jump inside uncheckedTes...
Definition MRBitSet.h:63
bool uncheckedTest(IndexType n) const
Definition MRBitSet.h:58
MRMESH_API friend std::istream & operator>>(std::istream &s, BitSet &bs)
static BitSet fromBlocks(std::vector< block_type > &&blocks)
creates bitset from the given blocks of bits
Definition MRBitSet.h:40
MRMESH_API BitSet & set()
void clear()
Definition MRBitSet.h:50
MRMESH_API friend std::ostream & operator<<(std::ostream &s, const BitSet &bs)
size_type size() const noexcept
Definition MRBitSet.h:54
bool uncheckedTestSet(IndexType n, bool val=true)
Definition MRBitSet.h:59
BitSet & reset(IndexType n)
Definition MRBitSet.h:71
MRMESH_API bool all() const
returns true if all bits in this container are set
MRMESH_API void reverse()
changes the order of bits on the opposite
bool test(IndexType n) const
all bits after size() we silently consider as not-set
Definition MRBitSet.h:62
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:114
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:151
size_type num_blocks() const noexcept
Definition MRBitSet.h:55
BitSet & set(IndexType n, bool val)
Definition MRBitSet.h:66
IndexType backId() const
returns the identifier of the back() element
Definition MRBitSet.h:172
MRMESH_API 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...
MRMESH_API BitSet & set(IndexType n, size_type len, bool val)
void resizeWithReserve(size_t newSize)
doubles reserved memory until resize(newSize) can be done without reallocation
Definition MRBitSet.h:138
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:111
void push_back(bool val)
adds one more bit with the given value in the container, increasing its size on 1
Definition MRBitSet.h:82
MRMESH_API 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 ...
size_type capacity() const noexcept
Definition MRBitSet.h:56
MRMESH_API BitSet & reset(IndexType n, size_type len)
const auto & bits() const
read-only access to all bits stored as a vector of uint64 blocks
Definition MRBitSet.h:88
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRBitSet.h:169
MRMESH_API BitSet & operator|=(const BitSet &b)
bool none() const
returns true if all bits in this container are reset
Definition MRBitSet.h:105
MRMESH_API BitSet & operator^=(const BitSet &b)
static constexpr size_t npos
Definition MRBitSet.h:28
MRMESH_API bool any() const
returns true if at least one bits in this container is set
MRMESH_API BitSet & operator&=(const BitSet &b)
void pop_back()
removes last bit from the container, decreasing its size on 1
Definition MRBitSet.h:85
BitSet & flip(IndexType n)
Definition MRBitSet.h:75
void shrink_to_fit()
Definition MRBitSet.h:51
iterator to enumerate all indices with set bits in BitSet class or its derivatives
Definition MRBitSet.h:355
SetBitIteratorT operator++(int)
Definition MRBitSet.h:377
const IndexType * pointer
Definition MRBitSet.h:363
SetBitIteratorT()=default
constructs end iterator
std::forward_iterator_tag iterator_category
Definition MRBitSet.h:359
const T * bitset() const
Definition MRBitSet.h:384
IndexType value_type
Definition MRBitSet.h:360
typename T::IndexType IndexType
Definition MRBitSet.h:357
SetBitIteratorT & operator++()
Definition MRBitSet.h:372
SetBitIteratorT(const T &bitset)
constructs begin iterator
Definition MRBitSet.h:368
IndexType reference
intentionally not a reference
Definition MRBitSet.h:362
std::ptrdiff_t difference_type
Definition MRBitSet.h:361
Definition MRBitSet.h:229
IndexType find_last() const
Definition MRBitSet.h:256
TypedBitSet & operator|=(const TypedBitSet &b)
Definition MRBitSet.h:264
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:280
bool test_set(IndexType n, bool val=true)
Definition MRBitSet.h:252
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map) const
Definition MRBitSet.h:293
friend TypedBitSet operator|(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:269
friend TypedBitSet operator&(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:268
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRBitSet.h:307
TypedBitSet(const BitSet &src)
copies all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRBitSet.h:236
TypedBitSet & flip(IndexType n)
Definition MRBitSet.h:249
IndexType backId() const
returns the identifier of the back() element
Definition MRBitSet.h:304
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map, size_t resSize) const
Definition MRBitSet.h:300
TypedBitSet & operator-=(const TypedBitSet &b)
Definition MRBitSet.h:266
TypedBitSet & set(IndexType n, size_type len, bool val)
Definition MRBitSet.h:241
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:277
TypedBitSet & reset(IndexType n, size_type len)
Definition MRBitSet.h:245
friend TypedBitSet operator-(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:271
TypedBitSet & operator^=(const TypedBitSet &b)
Definition MRBitSet.h:265
TypedBitSet & set()
Definition MRBitSet.h:244
void autoResizeSet(IndexType pos, size_type len, bool val=true)
Definition MRBitSet.h:282
IndexType endId() const
Definition MRBitSet.h:308
I IndexType
Definition MRBitSet.h:233
TypedBitSet & flip()
Definition MRBitSet.h:250
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map, size_t resSize) const
Definition MRBitSet.h:298
IndexType find_first_not_set() const
Definition MRBitSet.h:257
IndexType find_first() const
Definition MRBitSet.h:254
friend TypedBitSet operator^(const TypedBitSet &a, const TypedBitSet &b)
Definition MRBitSet.h:270
IndexType find_next_not_set(IndexType pos) const
Definition MRBitSet.h:258
TypedBitSet & set(IndexType n, bool val)
Definition MRBitSet.h:242
TypedBitSet & flip(IndexType n, size_type len)
Definition MRBitSet.h:248
TypedBitSet & operator&=(const TypedBitSet &b)
Definition MRBitSet.h:263
TypedBitSet getMapping(const M &map, size_t resSize) const
this is a faster version if the result size is known beforehand
void autoResizeSet(IndexType pos, bool val=true)
Definition MRBitSet.h:283
IndexType find_next(IndexType pos) const
Definition MRBitSet.h:255
TypedBitSet & reset()
Definition MRBitSet.h:247
bool autoResizeTestSet(IndexType pos, bool val=true)
Definition MRBitSet.h:284
IndexType find_last_not_set() const
Definition MRBitSet.h:259
TypedBitSet(BitSet &&src)
moves all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRBitSet.h:239
TypedBitSet & set(IndexType n)
Not using a default argument for val to get better C bindings.
Definition MRBitSet.h:243
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:274
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map) const
Definition MRBitSet.h:289
TypedBitSet & reset(IndexType n)
Definition MRBitSet.h:246
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:261
bool test(IndexType n) const
Definition MRBitSet.h:251
TypedBitSet getMapping(const BMap< IndexType, IndexType > &map) const
Definition MRBitSet.h:291
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:457
HashMap< I, int > makeHashMapWithSeqNums(const TypedBitSet< I > &bs)
creates a HashMap where for each set bit of input bitset its sequential number starting from 0 is ret...
Definition MRBitSet.h:420
MRMESH_API bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
BitSet operator&(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:454
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:409
bool contains(const TypedBitSet< I > *bitset, I id)
Definition MRBitSet.h:341
BitSet operator|(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:455
MR_BIND_IGNORE_PY auto end(const BitSet &)
Definition MRBitSet.h:397
MR_BIND_IGNORE_PY auto begin(const BitSet &a)
Definition MRBitSet.h:395
std::function< bool(I)> makePredicate(const TypedBitSet< I > *bitset)
Definition MRBitSet.h:328
BitSet operator^(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:456
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRBitSet.h:313
Buffer< T, I > b
Definition MRBuffer.h:148
T getAt(const Buffer< T, I > &bmap MR_LIFETIMEBOUND_NESTED, 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:123
size_t tsize
target size, all values inside b must be less than this value
Definition MRBuffer.h:149
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
flat map: I -> T
Definition MRBuffer.h:147