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_; }
105 [[nodiscard]]
bool none()
const {
return !
any(); }
132 if ( reserved > 0 && newSize > reserved )
134 while ( newSize > reserved )
144 if ( pos + len >
size() )
146 set( pos, len, val );
153 bool const b =
test( pos );
185 return ( (
block_type( 1 ) << firstBit ) - 1 )
199 template<
class FullBlock,
class PartialBlock>
209 std::vector<block_type> blocks_;
272 template <
typename M>
281 template <
typename M>
307 {
return static_cast<const BitSet &
>( a ) ==
static_cast<const BitSet &
>( b ); }
309template <
typename T,
typename U>
315 std::function<bool(
I )> res;
317 res = [bitset](
I id ) {
return bitset->
test(
id ); };
328 return id.valid() && ( !bitset || bitset->
test(
id ) );
334 return id.valid() && bitset.
test(
id );
354 : bitset_( &bitset ), index_( bitset.find_first() )
359 index_ = bitset_->find_next( index_ );
369 [[nodiscard]]
const T *
bitset()
const {
return bitset_; }
375 const T * bitset_ =
nullptr;
376 IndexType index_ = IndexType( ~
size_t( 0 ) );
419 for (
auto b : *this )
420 if (
auto mapped = map( b ) )
433 for (
auto b : *this )
434 if (
auto mapped = map( b ) )
#define MRMESH_API
Definition MRMeshFwd.h:80
Definition MRMesh/MRBitSet.h:24
size_t size_type
Definition MRMesh/MRBitSet.h:30
size_t IndexType
Definition MRMesh/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 MRMesh/MRBitSet.h:27
IndexType endId() const
Definition MRMesh/MRBitSet.h:167
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRMesh/MRBitSet.h:166
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:26
void reserve(size_type numBits)
Definition MRMesh/MRBitSet.h:48
MRMESH_API void resize(size_type numBits, bool fillValue=false)
BitSet() noexcept=default
creates empty bitset
bool empty() const noexcept
Definition MRMesh/MRBitSet.h:53
BitSet & set(IndexType n)
Definition MRMesh/MRBitSet.h:67
void autoResizeSet(size_t pos, bool val=true)
Definition MRMesh/MRBitSet.h:148
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:151
bool test_set(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:63
bool uncheckedTest(IndexType n) const
Definition MRMesh/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 MRMesh/MRBitSet.h:40
MRMESH_API BitSet & set()
void clear()
Definition MRMesh/MRBitSet.h:50
MRMESH_API friend std::ostream & operator<<(std::ostream &s, const BitSet &bs)
size_type size() const noexcept
Definition MRMesh/MRBitSet.h:54
bool uncheckedTestSet(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:59
BitSet & reset(IndexType n)
Definition MRMesh/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
Definition MRMesh/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 MRMesh/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 MRMesh/MRBitSet.h:142
size_type num_blocks() const noexcept
Definition MRMesh/MRBitSet.h:55
BitSet & set(IndexType n, bool val)
Definition MRMesh/MRBitSet.h:66
IndexType backId() const
returns the identifier of the back() element
Definition MRMesh/MRBitSet.h:163
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:129
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:111
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: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 MRMesh/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 MRMesh/MRBitSet.h:88
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRMesh/MRBitSet.h:160
MRMESH_API BitSet & operator|=(const BitSet &b)
bool none() const
returns true if all bits in this container are reset
Definition MRMesh/MRBitSet.h:105
MRMESH_API BitSet & operator^=(const BitSet &b)
static constexpr size_t npos
Definition MRMesh/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 MRMesh/MRBitSet.h:85
BitSet & flip(IndexType n)
Definition MRMesh/MRBitSet.h:75
void shrink_to_fit()
Definition MRMesh/MRBitSet.h:51
iterator to enumerate all indices with set bits in BitSet class or its derivatives
Definition MRMesh/MRBitSet.h:340
SetBitIteratorT operator++(int)
Definition MRMesh/MRBitSet.h:362
const IndexType * pointer
Definition MRMesh/MRBitSet.h:348
SetBitIteratorT()=default
constructs end iterator
std::forward_iterator_tag iterator_category
Definition MRMesh/MRBitSet.h:344
const T * bitset() const
Definition MRMesh/MRBitSet.h:369
IndexType value_type
Definition MRMesh/MRBitSet.h:345
const IndexType reference
intentionally not a reference
Definition MRMesh/MRBitSet.h:347
typename T::IndexType IndexType
Definition MRMesh/MRBitSet.h:342
SetBitIteratorT & operator++()
Definition MRMesh/MRBitSet.h:357
SetBitIteratorT(const T &bitset)
constructs begin iterator
Definition MRMesh/MRBitSet.h:353
std::ptrdiff_t difference_type
Definition MRMesh/MRBitSet.h:346
Definition MRMesh/MRBitSet.h:217
IndexType find_last() const
Definition MRMesh/MRBitSet.h:244
TypedBitSet & operator|=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:249
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:265
bool test_set(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:240
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:278
friend TypedBitSet operator|(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:254
friend TypedBitSet operator&(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:253
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRMesh/MRBitSet.h:292
TypedBitSet(const BitSet &src)
copies all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRMesh/MRBitSet.h:224
TypedBitSet & flip(IndexType n)
Definition MRMesh/MRBitSet.h:237
IndexType backId() const
returns the identifier of the back() element
Definition MRMesh/MRBitSet.h:289
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map, size_t resSize) const
Definition MRMesh/MRBitSet.h:285
TypedBitSet & operator-=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:251
TypedBitSet & set(IndexType n, size_type len, bool val)
Definition MRMesh/MRBitSet.h:229
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:262
TypedBitSet & reset(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:233
friend TypedBitSet operator-(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:256
TypedBitSet & operator^=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:250
TypedBitSet & set()
Definition MRMesh/MRBitSet.h:232
void autoResizeSet(IndexType pos, size_type len, bool val=true)
Definition MRMesh/MRBitSet.h:267
IndexType endId() const
Definition MRMesh/MRBitSet.h:293
I IndexType
Definition MRMesh/MRBitSet.h:221
TypedBitSet & flip()
Definition MRMesh/MRBitSet.h:238
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map, size_t resSize) const
Definition MRMesh/MRBitSet.h:283
IndexType find_first() const
Definition MRMesh/MRBitSet.h:242
friend TypedBitSet operator^(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:255
TypedBitSet & set(IndexType n, bool val)
Definition MRMesh/MRBitSet.h:230
TypedBitSet & flip(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:236
TypedBitSet & operator&=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:248
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:268
IndexType find_next(IndexType pos) const
Definition MRMesh/MRBitSet.h:243
TypedBitSet & reset()
Definition MRMesh/MRBitSet.h:235
bool autoResizeTestSet(IndexType pos, bool val=true)
Definition MRMesh/MRBitSet.h:269
TypedBitSet(BitSet &&src)
moves all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRMesh/MRBitSet.h:227
TypedBitSet & set(IndexType n)
Definition MRMesh/MRBitSet.h:231
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:259
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:274
TypedBitSet & reset(IndexType n)
Definition MRMesh/MRBitSet.h:234
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:246
bool test(IndexType n) const
Definition MRMesh/MRBitSet.h:239
TypedBitSet getMapping(const BMap< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:276
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:19
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:442
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:380
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:405
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:439
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:394
bool contains(const TypedBitSet< I > *bitset, I id)
Definition MRMesh/MRBitSet.h:326
BitSet operator|(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:440
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:382
std::function< bool(I)> makePredicate(const TypedBitSet< I > *bitset)
Definition MRMesh/MRBitSet.h:313
BitSet operator^(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:441
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRMesh/MRBitSet.h:298
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 MRMeshFwd.h:198
Color operator*(float a, const Color &b)
Definition MRMesh/MRColor.h:116
I
Definition MRMeshFwd.h:134
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:10
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMeshFwd.h:602
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