7#include "MRPch/MRBindingMacros.h"
27 inline static constexpr size_t npos = (size_t)-1;
36 explicit
BitSet(
size_t numBits,
bool fillValue = false ) {
resize( numBits, fillValue ); }
42 res.blocks_ = std::move( blocks );
49 void clear() { numBits_ = 0; blocks_.clear(); }
52 [[nodiscard]]
bool empty() const noexcept {
return numBits_ == 0; }
81 void push_back(
bool val ) {
auto n = numBits_++;
if ( bitIndex_( n ) == 0 ) blocks_.push_back(
block_type{} );
set( n, val ); }
84 void pop_back() { assert( numBits_ > 0 ); {
if ( bitIndex_( numBits_ ) == 1 ) blocks_.pop_back();
else reset( numBits_ - 1 ); } --numBits_; }
87 [[nodiscard]]
const auto &
bits()
const {
return blocks_; }
104 [[nodiscard]]
bool none()
const {
return !
any(); }
131 if ( reserved > 0 && newSize > reserved )
133 while ( newSize > reserved )
143 if ( pos + len >
size() )
145 set( pos, len, val );
152 bool const b =
test( pos );
184 return ( (
block_type( 1 ) << firstBit ) - 1 )
198 template<
class FullBlock,
class PartialBlock>
205 std::vector<block_type> blocks_;
268 template <
typename M>
277 template <
typename M>
303 {
return static_cast<const BitSet &
>( a ) ==
static_cast<const BitSet &
>( b ); }
305template <
typename T,
typename U>
311 std::function<bool(
I )> res;
313 res = [bitset](
I id ) {
return bitset->
test(
id ); };
324 return id.valid() && ( !bitset || bitset->
test(
id ) );
330 return id.valid() && bitset.
test(
id );
350 : bitset_( &bitset ), index_( bitset.find_first() )
355 index_ = bitset_->find_next( index_ );
365 [[nodiscard]]
const T *
bitset()
const {
return bitset_; }
371 const T * bitset_ =
nullptr;
372 IndexType index_ = IndexType( ~
size_t( 0 ) );
415 for (
auto b : *this )
416 if (
auto mapped = map( b ) )
429 for (
auto b : *this )
430 if (
auto mapped = map( b ) )
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:80
Definition MRMesh/MRBitSet.h:23
size_t size_type
Definition MRMesh/MRBitSet.h:29
size_t IndexType
Definition MRMesh/MRBitSet.h:30
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 MRMesh/MRBitSet.h:26
IndexType endId() const
Definition MRMesh/MRBitSet.h:166
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRMesh/MRBitSet.h:165
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 MRMesh/MRBitSet.h:25
void reserve(size_type numBits)
Definition MRMesh/MRBitSet.h:47
MRMESH_API void resize(size_type numBits, bool fillValue=false)
BitSet() noexcept=default
creates empty bitset
bool empty() const noexcept
Definition MRMesh/MRBitSet.h:52
BitSet & set(IndexType n)
Definition MRMesh/MRBitSet.h:66
void autoResizeSet(size_t pos, bool val=true)
Definition MRMesh/MRBitSet.h:147
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 MRMesh/MRBitSet.h:150
bool test_set(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:62
bool uncheckedTest(IndexType n) const
Definition MRMesh/MRBitSet.h:57
static BitSet fromBlocks(std::vector< block_type > &&blocks)
creates bitset from the given blocks of bits
Definition MRMesh/MRBitSet.h:39
MRMESH_API BitSet & set()
void clear()
Definition MRMesh/MRBitSet.h:49
size_type size() const noexcept
Definition MRMesh/MRBitSet.h:53
bool uncheckedTestSet(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:58
BitSet & reset(IndexType n)
Definition MRMesh/MRBitSet.h:70
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
Definition MRMesh/MRBitSet.h:61
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 MRMesh/MRBitSet.h:113
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 MRMesh/MRBitSet.h:141
size_type num_blocks() const noexcept
Definition MRMesh/MRBitSet.h:54
BitSet & set(IndexType n, bool val)
Definition MRMesh/MRBitSet.h:65
IndexType backId() const
returns the identifier of the back() element
Definition MRMesh/MRBitSet.h:162
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 MRMesh/MRBitSet.h:128
IndexType find_first() const
return the smallest index i such that bit i is set, or npos if *this has no on bits.
Definition MRMesh/MRBitSet.h:110
void push_back(bool val)
adds one more bit with the given value in the container, increasing its size on 1
Definition MRMesh/MRBitSet.h:81
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 MRMesh/MRBitSet.h:55
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 MRMesh/MRBitSet.h:87
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRMesh/MRBitSet.h:159
MRMESH_API BitSet & operator|=(const BitSet &b)
bool none() const
returns true if all bits in this container are reset
Definition MRMesh/MRBitSet.h:104
MRMESH_API BitSet & operator^=(const BitSet &b)
static constexpr size_t npos
Definition MRMesh/MRBitSet.h:27
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 MRMesh/MRBitSet.h:84
BitSet & flip(IndexType n)
Definition MRMesh/MRBitSet.h:74
void shrink_to_fit()
Definition MRMesh/MRBitSet.h:50
iterator to enumerate all indices with set bits in BitSet class or its derivatives
Definition MRMesh/MRBitSet.h:336
SetBitIteratorT operator++(int)
Definition MRMesh/MRBitSet.h:358
const IndexType * pointer
Definition MRMesh/MRBitSet.h:344
SetBitIteratorT()=default
constructs end iterator
std::forward_iterator_tag iterator_category
Definition MRMesh/MRBitSet.h:340
const T * bitset() const
Definition MRMesh/MRBitSet.h:365
IndexType value_type
Definition MRMesh/MRBitSet.h:341
const IndexType reference
intentionally not a reference
Definition MRMesh/MRBitSet.h:343
typename T::IndexType IndexType
Definition MRMesh/MRBitSet.h:338
SetBitIteratorT & operator++()
Definition MRMesh/MRBitSet.h:353
SetBitIteratorT(const T &bitset)
constructs begin iterator
Definition MRMesh/MRBitSet.h:349
std::ptrdiff_t difference_type
Definition MRMesh/MRBitSet.h:342
Definition MRMesh/MRBitSet.h:213
IndexType find_last() const
Definition MRMesh/MRBitSet.h:240
TypedBitSet & operator|=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:245
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 MRMesh/MRBitSet.h:261
bool test_set(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:236
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:274
friend TypedBitSet operator|(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:250
friend TypedBitSet operator&(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:249
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRMesh/MRBitSet.h:288
TypedBitSet(const BitSet &src)
copies all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRMesh/MRBitSet.h:220
TypedBitSet & flip(IndexType n)
Definition MRMesh/MRBitSet.h:233
IndexType backId() const
returns the identifier of the back() element
Definition MRMesh/MRBitSet.h:285
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map, size_t resSize) const
Definition MRMesh/MRBitSet.h:281
TypedBitSet & operator-=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:247
TypedBitSet & set(IndexType n, size_type len, bool val)
Definition MRMesh/MRBitSet.h:225
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 MRMesh/MRBitSet.h:258
TypedBitSet & reset(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:229
friend TypedBitSet operator-(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:252
TypedBitSet & operator^=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:246
TypedBitSet & set()
Definition MRMesh/MRBitSet.h:228
void autoResizeSet(IndexType pos, size_type len, bool val=true)
Definition MRMesh/MRBitSet.h:263
IndexType endId() const
Definition MRMesh/MRBitSet.h:289
I IndexType
Definition MRMesh/MRBitSet.h:217
TypedBitSet & flip()
Definition MRMesh/MRBitSet.h:234
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map, size_t resSize) const
Definition MRMesh/MRBitSet.h:279
IndexType find_first() const
Definition MRMesh/MRBitSet.h:238
friend TypedBitSet operator^(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:251
TypedBitSet & set(IndexType n, bool val)
Definition MRMesh/MRBitSet.h:226
TypedBitSet & flip(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:232
TypedBitSet & operator&=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:244
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 MRMesh/MRBitSet.h:264
IndexType find_next(IndexType pos) const
Definition MRMesh/MRBitSet.h:239
TypedBitSet & reset()
Definition MRMesh/MRBitSet.h:231
bool autoResizeTestSet(IndexType pos, bool val=true)
Definition MRMesh/MRBitSet.h:265
TypedBitSet(BitSet &&src)
moves all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRMesh/MRBitSet.h:223
TypedBitSet & set(IndexType n)
Definition MRMesh/MRBitSet.h:227
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 MRMesh/MRBitSet.h:255
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:270
TypedBitSet & reset(IndexType n)
Definition MRMesh/MRBitSet.h:230
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 MRMesh/MRBitSet.h:242
bool test(IndexType n) const
Definition MRMesh/MRBitSet.h:235
TypedBitSet getMapping(const BMap< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:272
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:19
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:438
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:376
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 MRMesh/MRBitSet.h:401
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 MRMesh/MRBitSet.h:435
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 MRMesh/MRBitSet.h:390
bool contains(const TypedBitSet< I > *bitset, I id)
Definition MRMesh/MRBitSet.h:322
BitSet operator|(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:436
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:378
std::function< bool(I)> makePredicate(const TypedBitSet< I > *bitset)
Definition MRMesh/MRBitSet.h:309
BitSet operator^(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:437
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRMesh/MRBitSet.h:294
Definition MRCameraOrientationPlugin.h:8
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:119
std::uint64_t Uint64
Definition MRMesh/MRMeshFwd.h:194
Color operator*(float a, const Color &b)
Definition MRMesh/MRColor.h:116
I
Definition MRMesh/MRMeshFwd.h:130
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:10
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMesh/MRMeshFwd.h:592
flat map: I -> T
Definition MRBuffer.h:143
Buffer< T, I > b
Definition MRBuffer.h:144
size_t tsize
target size, all values inside b must be less than this value
Definition MRBuffer.h:145