7#include "MRPch/MRBindingMacros.h"
8#define BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
10#pragma warning(disable: 4643)
11#include <boost/dynamic_bitset.hpp>
27class BitSet :
public boost::dynamic_bitset<std::uint64_t>
30 using base = boost::dynamic_bitset<std::uint64_t>;
35 explicit BitSet(
size_t numBits,
bool fillValue ) { resize( numBits, fillValue ); }
38 explicit BitSet(
size_t,
unsigned long ) =
delete;
39 template<class T, std::enable_if_t<std::is_arithmetic<T>::value, std::nullptr_t> =
nullptr>
44 [[nodiscard]]
bool test_set(
IndexType n,
bool val =
true ) {
return ( val || n <
size() ) ? base::test_set( n, val ) :
false; }
46 BitSet &
set(
IndexType n, size_type len,
bool val ) { base::set( n, len, val );
return *
this; }
58 const auto &
bits()
const {
return m_bits; }
83 auto reserved = capacity();
84 if ( reserved > 0 && newSize > reserved )
86 while ( newSize > reserved )
96 if ( pos + len >
size() )
105 bool const b =
test( pos );
112 [[nodiscard]]
size_t heapBytes()
const {
return capacity() / 8; }
122 #if defined(MR_PARSING_FOR_ANY_BINDINGS) || defined(MR_COMPILING_ANY_BINDINGS)
123 std::size_t
size()
const {
return dynamic_bitset::size(); }
124 std::size_t count()
const {
return dynamic_bitset::count(); }
125 void resize( std::size_t num_bits,
bool value =
false ) { dynamic_bitset::resize( num_bits, value ); }
126 void clear() { dynamic_bitset::clear(); }
127 void push_back(
bool bit ) { dynamic_bitset::push_back( bit ); }
128 void pop_back() { dynamic_bitset::pop_back(); }
132 using base::m_highest_block;
134 using base::m_num_bits;
200 template <
typename M>
209 template <
typename M>
235 {
return static_cast<const BitSet &
>( a ) ==
static_cast<const BitSet &
>( b ); }
237template <
typename T,
typename U>
243 std::function<bool(
I )> res;
245 res = [bitset](
I id ) {
return bitset->
test(
id ); };
256 return id.valid() && ( !bitset || bitset->
test(
id ) );
262 return id.valid() && bitset.
test(
id );
282 : bitset_( &bitset ), index_( bitset.find_first() )
287 index_ = bitset_->find_next( index_ );
297 [[nodiscard]]
const T *
bitset()
const {
return bitset_; }
303 const T * bitset_ =
nullptr;
304 IndexType index_ = IndexType( ~
size_t( 0 ) );
347 for (
auto b : *this )
348 if (
auto mapped = map( b ) )
360 res.resize( resSize );
361 for (
auto b : *this )
362 if (
auto mapped = map( b ) )
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:80
Definition MRMesh/MRBitSet.h:28
size_t IndexType
Definition MRMesh/MRBitSet.h:32
IndexType endId() const
Definition MRMesh/MRBitSet.h:119
boost::dynamic_bitset< std::uint64_t > base
Definition MRMesh/MRBitSet.h:30
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRMesh/MRBitSet.h:118
MRMESH_API IndexType find_last() const
return the highest index i such as bit i is set, or npos if *this has no on bits.
BitSet & reset()
Definition MRMesh/MRBitSet.h:52
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)
bool is_proper_subset_of(const BitSet &a) const =delete
returns true if, for every bit that is set in this bitset, the corresponding bit in bitset a is also ...
BitSet(size_t, unsigned long)=delete
prohibit these constructors inherited from boost::dynamic_bitset, which can initialize only few initi...
BitSet & set(IndexType n, size_type len, bool val)
Definition MRMesh/MRBitSet.h:46
BitSet & set()
Definition MRMesh/MRBitSet.h:49
BitSet & set(IndexType n)
Definition MRMesh/MRBitSet.h:48
void autoResizeSet(size_t pos, bool val=true)
Definition MRMesh/MRBitSet.h:100
BitSet & reset(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:50
bool autoResizeTestSet(size_t pos, bool val=true)
same as autoResizeSet and returns previous value of pos-bit
Definition MRMesh/MRBitSet.h:103
bool test_set(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:44
BitSet & flip(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:53
BitSet & reset(IndexType n)
Definition MRMesh/MRBitSet.h:51
bool test(IndexType n) const
Definition MRMesh/MRBitSet.h:43
BitSet & flip()
Definition MRMesh/MRBitSet.h:55
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:94
BitSet & set(IndexType n, bool val)
Definition MRMesh/MRBitSet.h:47
IndexType backId() const
returns the identifier of the back() element
Definition MRMesh/MRBitSet.h:115
void resizeWithReserve(size_t newSize)
doubles reserved memory until resize(newSize) can be done without reallocation
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 ...
const auto & bits() const
read-only access to all bits stored as a vector of uint64 blocks
Definition MRMesh/MRBitSet.h:58
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRMesh/MRBitSet.h:112
BitSet(size_t numBits, bool fillValue)
creates bitset of given size filled with given value
Definition MRMesh/MRBitSet.h:35
MRMESH_API BitSet & operator|=(const BitSet &b)
MRMESH_API BitSet & operator^=(const BitSet &b)
MRMESH_API BitSet & operator&=(const BitSet &b)
BitSet & flip(IndexType n)
Definition MRMesh/MRBitSet.h:54
iterator to enumerate all indices with set bits in BitSet class or its derivatives
Definition MRMesh/MRBitSet.h:268
SetBitIteratorT operator++(int)
Definition MRMesh/MRBitSet.h:290
const IndexType * pointer
Definition MRMesh/MRBitSet.h:276
SetBitIteratorT()=default
constructs end iterator
std::forward_iterator_tag iterator_category
Definition MRMesh/MRBitSet.h:272
const T * bitset() const
Definition MRMesh/MRBitSet.h:297
IndexType value_type
Definition MRMesh/MRBitSet.h:273
const IndexType reference
intentionally not a reference
Definition MRMesh/MRBitSet.h:275
typename T::IndexType IndexType
Definition MRMesh/MRBitSet.h:270
SetBitIteratorT & operator++()
Definition MRMesh/MRBitSet.h:285
SetBitIteratorT(const T &bitset)
constructs begin iterator
Definition MRMesh/MRBitSet.h:281
std::ptrdiff_t difference_type
Definition MRMesh/MRBitSet.h:274
Definition MRMesh/MRBitSet.h:141
IndexType find_last() const
Definition MRMesh/MRBitSet.h:172
TypedBitSet & operator|=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:177
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:193
bool test_set(IndexType n, bool val=true)
Definition MRMesh/MRBitSet.h:164
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:206
friend TypedBitSet operator|(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:182
friend TypedBitSet operator&(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:181
static IndexType beginId()
[beginId(), endId()) is the range of all bits in the set
Definition MRMesh/MRBitSet.h:220
TypedBitSet(const BitSet &src)
copies all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRMesh/MRBitSet.h:148
TypedBitSet & flip(IndexType n)
Definition MRMesh/MRBitSet.h:161
IndexType backId() const
returns the identifier of the back() element
Definition MRMesh/MRBitSet.h:217
MR_BIND_IGNORE reference operator[](IndexType pos)
Definition MRMesh/MRBitSet.h:167
TypedBitSet getMapping(const HashMap< IndexType, IndexType > &map, size_t resSize) const
Definition MRMesh/MRBitSet.h:213
TypedBitSet & operator-=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:179
TypedBitSet & set(IndexType n, size_type len, bool val)
Definition MRMesh/MRBitSet.h:153
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:190
TypedBitSet & reset(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:157
friend TypedBitSet operator-(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:184
TypedBitSet & operator^=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:178
TypedBitSet & set()
Definition MRMesh/MRBitSet.h:156
void autoResizeSet(IndexType pos, size_type len, bool val=true)
Definition MRMesh/MRBitSet.h:195
bool operator[](IndexType pos) const
Definition MRMesh/MRBitSet.h:168
IndexType endId() const
Definition MRMesh/MRBitSet.h:221
I IndexType
Definition MRMesh/MRBitSet.h:145
TypedBitSet & flip()
Definition MRMesh/MRBitSet.h:162
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map, size_t resSize) const
Definition MRMesh/MRBitSet.h:211
IndexType find_first() const
Definition MRMesh/MRBitSet.h:170
friend TypedBitSet operator^(const TypedBitSet &a, const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:183
TypedBitSet & set(IndexType n, bool val)
Definition MRMesh/MRBitSet.h:154
TypedBitSet & flip(IndexType n, size_type len)
Definition MRMesh/MRBitSet.h:160
TypedBitSet & operator&=(const TypedBitSet &b)
Definition MRMesh/MRBitSet.h:176
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:196
IndexType find_next(IndexType pos) const
Definition MRMesh/MRBitSet.h:171
TypedBitSet & reset()
Definition MRMesh/MRBitSet.h:159
bool autoResizeTestSet(IndexType pos, bool val=true)
Definition MRMesh/MRBitSet.h:197
TypedBitSet(BitSet &&src)
moves all bits from another BitSet (or a descending class, e.g. TypedBitSet)
Definition MRMesh/MRBitSet.h:151
TypedBitSet & set(IndexType n)
Definition MRMesh/MRBitSet.h:155
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:187
TypedBitSet getMapping(const Vector< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:202
TypedBitSet & reset(IndexType n)
Definition MRMesh/MRBitSet.h:158
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:174
bool test(IndexType n) const
Definition MRMesh/MRBitSet.h:163
TypedBitSet getMapping(const BMap< IndexType, IndexType > &map) const
Definition MRMesh/MRBitSet.h:204
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:370
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:308
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:333
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:367
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:322
bool contains(const TypedBitSet< I > *bitset, I id)
Definition MRMesh/MRBitSet.h:254
BitSet operator|(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:368
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:310
std::function< bool(I)> makePredicate(const TypedBitSet< I > *bitset)
Definition MRMesh/MRBitSet.h:241
BitSet operator^(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:369
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRMesh/MRBitSet.h:226
Definition MRCameraOrientationPlugin.h:8
class MRMESH_CLASS BitSet
Definition MRMesh/MRMeshFwd.h:144
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:29
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
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