12#ifndef CL_DYNAMICBITSET_HPP
13#define CL_DYNAMICBITSET_HPP
69 uint64_t* mSummary1 = nullptr ;
75 uint64_t* mSummary2 = nullptr ;
271 "Bitsets don't have the same size (%lu vs. %lu)",
272 (
long unsigned int)this->size(),
273 (
long unsigned int) aRhs.
size());
275 if ( this->hash() != aRhs.
hash() )
281 for (
index_t k = 0; k < mMemorySize; ++k)
283 if (mData[k] != aRhs.
data()[k])
296 return !(*
this == aRhs);
318 recompute_summaries();
327 select_to_int_function();
330 to_int_partial()
const ;
333 to_int_full()
const ;
338 to_int_zero()
const ;
341 to_int_fail()
const ;
352 "Bitsets don't have the same size (%lu vs. %lu)",
353 (
long unsigned int)this->size(),
354 (
long unsigned int)aRhs.
size());
358 const uint64_t* tSrcA = mData;
359 const uint64_t* tSrcB = aRhs.
data();
360 uint64_t* tDst = aResult.mData;
361 const uint64_t* tEnd = tSrcA + mMemorySize;
365 *tDst++ = *tSrcA++ | *tSrcB++;
370 for(
index_t i = 0; i < mSummary1Size; ++i )
372 aResult.mSummary1[ i ] = mSummary1[ i ] | aRhs.mSummary1[ i ];
374 for(
index_t i = 0; i < mSummary2Size; ++i )
376 aResult.mSummary2[ i ] = mSummary2[ i ] | aRhs.mSummary2[ i ];
386 "Bitsets don't have the same size (%lu vs. %lu)",
387 (
long unsigned int)this->size(),
388 (
long unsigned int) aRhs.
size());
390 BELFEM_ERROR( mHash == 0,
"Can't modify a locked bitset" );
392 const uint64_t* tData = aRhs.
data();
394 for(
index_t i = 0; i < mMemorySize; ++i)
396 mData[i] |= tData[i];
400 for(
index_t i = 0; i < mSummary1Size; ++i )
402 mSummary1[ i ] |= aRhs.mSummary1[ i ];
404 for(
index_t i = 0; i < mSummary2Size; ++i )
406 mSummary2[ i ] |= aRhs.mSummary2[ i ];
416 "Bitsets don't have the same size (%lu vs. %lu)",
417 (
long unsigned int)this->size(),
418 (
long unsigned int)aRhs.
size());
422 const uint64_t* tData = aRhs.
data();
424 for(
index_t i = 0; i < mMemorySize; ++i)
426 aResult.mData[i] = mData[i] ^ tData[i];
431 aResult.recompute_summaries();
440 "Bitsets don't have the same size (%lu vs. %lu)",
441 (
long unsigned int)this->size(),
442 (
long unsigned int)aRhs.
size());
444 BELFEM_ERROR( mHash == 0,
"Can't modify a locked bitset");
446 for(
index_t i = 0; i < mMemorySize; ++i)
448 mData[i] ^= aRhs.mData[i];
452 this->recompute_summaries();
461 "Bitsets don't have the same size (%lu vs. %lu)",
462 (
long unsigned int)this->size(),
463 (
long unsigned int)aRhs.
size());
467 const uint64_t* tData = aRhs.
data();
469 for(
index_t i = 0; i < mMemorySize; ++i)
471 tResult.mData[i] = mData[i] & tData[i];
475 tResult.recompute_summaries();
484 "Bitsets don't have the same size (%lu vs. %lu)",
485 (
long unsigned int)this->size(),
486 (
long unsigned int)aRhs.
size());
488 const uint64_t* tData = aRhs.
data();
490 BELFEM_ERROR( mHash == 0,
"Can't modify a locked bitset");
492 for(
index_t i = 0; i < mMemorySize; ++i)
494 mData[i] &= tData[i];
498 this->recompute_summaries();
512 return mNumberOfBits;
527 "Index %lu out of range (expect < %lu)",
528 (
long unsigned int)aPos,
529 (
long unsigned int)mNumberOfBits);
532 "can't write on a locked bitset");
536 const index_t tWord = aPos / 64 ;
537 const index_t tS1 = tWord / 64 ;
539 mData [ tWord ] |= uint64_t( 1 ) << ( aPos % 64 );
540 mSummary1[ tS1 ] |= uint64_t( 1 ) << ( tWord % 64 );
541 mSummary2[ tS1 / 64 ] |= uint64_t( 1 ) << ( tS1 % 64 );
549 "Index %lu out of range (expect < %lu)",
550 (
long unsigned int)aPos,
551 (
long unsigned int)mNumberOfBits);
554 "can't reset an single digit of a locked bitset" );
561 const index_t tWord = aPos / 64 ;
563 mData[ tWord ] &= ~( uint64_t( 1 ) << ( aPos % 64 ) );
565 if ( mData[ tWord ] == 0 )
567 const index_t tS1 = tWord / 64 ;
569 mSummary1[ tS1 ] &= ~( uint64_t( 1 ) << ( tWord % 64 ) );
571 if ( mSummary1[ tS1 ] == 0 )
573 mSummary2[ tS1 / 64 ] &= ~( uint64_t( 1 ) << ( tS1 % 64 ) );
604 "Index %lu out of range (expect < %lu)",
605 (
long unsigned int)aPos,
606 (
long unsigned int)mNumberOfBits);
609 "Can't flip a bit on a locked bitset" );
614 const index_t tWord = aPos / 64 ;
615 const index_t tS1 = tWord / 64 ;
617 mData[ tWord ] ^= uint64_t( 1 ) << ( aPos % 64 );
619 if ( mData[ tWord ] != 0 )
621 mSummary1[ tS1 ] |= uint64_t( 1 ) << ( tWord % 64 );
622 mSummary2[ tS1 / 64 ] |= uint64_t( 1 ) << ( tS1 % 64 );
626 mSummary1[ tS1 ] &= ~( uint64_t( 1 ) << ( tWord % 64 ) );
628 if ( mSummary1[ tS1 ] == 0 )
630 mSummary2[ tS1 / 64 ] &= ~( uint64_t( 1 ) << ( tS1 % 64 ) );
640 "Index %lu out of range (expect < %lu)",
641 (
long unsigned int)aPos,
642 (
long unsigned int)mNumberOfBits);
644 return ( mData[aPos / 64] & (uint64_t(1) << (aPos % 64))) != 0;
659 mHash = 14695981039346656037ULL;
660 mHash ^= std::hash<index_t>{}(mNumberOfBits);
661 mHash *= 1099511628211ULL;
662 for (
index_t i = 0; i < mMemorySize; ++i)
664 mHash ^= std::hash<uint64_t>{}(mData[i]);
665 mHash *= 1099511628211ULL;
697 BELFEM_ASSERT( mHash != 0,
"can't return the hash of a writable bitset" );
706 if ( mNumberOfBits == 0 )
715 ( void ) aAssumeSparse ;
717 this->where_sparse( aBits ) ;
725 return ( this->*mFunToInt )();
731 DynamicBitset::to_int_partial()
const
733 uint64_t mask = (uint64_t(1) << mNumberOfBits) - 1;
734 return static_cast< index_t >(mData[0] & mask);
740 DynamicBitset::to_int_full()
const
742 return static_cast<index_t>(mData[0]);
748 DynamicBitset::to_int_zero()
const
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
void clear()
clear the memory
Definition cl_Cell.hpp:240
Runtime-sized bitset; one bit per flag, packed into 64-bit words.
Definition cl_DynamicBitset.hpp:33
void lock()
makes the bitset non-writable and computes the hash
Definition cl_DynamicBitset.hpp:657
DynamicBitset operator&(const DynamicBitset &aRhs) const
Bitwise AND operator.
Definition cl_DynamicBitset.hpp:458
const uint64_t * data() const
Definition cl_DynamicBitset.hpp:649
DynamicBitset & operator&=(const DynamicBitset &aRhs)
Bitwise AND assignment operator.
Definition cl_DynamicBitset.hpp:481
index_t count() const
Counts the number of bits set to 1.
Definition cl_DynamicBitset.cpp:187
string to_string() const
Definition cl_DynamicBitset.cpp:231
string to_raw_string() const
Definition cl_DynamicBitset.cpp:316
string to_hex() const
Definition cl_DynamicBitset.cpp:252
size_t hash() const
Returns a hash function for fast comparison.
Definition cl_DynamicBitset.hpp:695
void set_index(const index_t aIndex)
Definition cl_DynamicBitset.hpp:756
void set_from_hex(const string &aString)
Definition cl_DynamicBitset.cpp:334
void where(Cell< index_t > &aBits, const bool aAssumeSparse=true) const
Definition cl_DynamicBitset.hpp:704
index_t index() const
Definition cl_DynamicBitset.hpp:764
DynamicBitset operator^(const DynamicBitset &aRhs) const
Bitwise XOR operator.
Definition cl_DynamicBitset.hpp:413
DynamicBitset operator|(const DynamicBitset &aRhs) const
Bitwise OR operator.
Definition cl_DynamicBitset.hpp:349
void reset(const index_t aPos)
Resets (clears) the bit at the given position.
Definition cl_DynamicBitset.hpp:546
void flip()
Flips all bits at once.
Definition cl_DynamicBitset.cpp:559
index_t to_int() const
Definition cl_DynamicBitset.hpp:723
void unlock()
makes the bitset writable and resets the hash
Definition cl_DynamicBitset.hpp:679
void set(const index_t aPos)
Sets the bit at the given position to 1.
Definition cl_DynamicBitset.hpp:524
index_t memory() const
Returns the number of 64-bit blocks used (memory size).
Definition cl_DynamicBitset.hpp:517
void flip(const index_t aPos)
Flips (toggles) the bit at the given position.
Definition cl_DynamicBitset.hpp:601
bool operator==(const DynamicBitset &aRhs) const
Definition cl_DynamicBitset.hpp:268
bool test(const index_t aPos) const
Tests whether the bit at the given position is set.
Definition cl_DynamicBitset.hpp:637
void reset()
Resets (clears) all bits.
Definition cl_DynamicBitset.cpp:384
bool operator!=(const DynamicBitset &aRhs) const
Comparison operator: Checks if two bitsets are not equal.
Definition cl_DynamicBitset.hpp:294
DynamicBitset & operator^=(const DynamicBitset &aRhs)
Bitwise XOR assignment operator.
Definition cl_DynamicBitset.hpp:437
DynamicBitset & operator=(const DynamicBitset &aRhs)
Assignment operator.
Definition cl_DynamicBitset.cpp:599
bool is_locked() const
checks if the bitset is writable
Definition cl_DynamicBitset.hpp:686
DynamicBitset & operator|=(const DynamicBitset &aRhs)
Assignment or operator.
Definition cl_DynamicBitset.hpp:383
bool summaries_are_tight() const
Definition cl_DynamicBitset.cpp:448
index_t size() const
Returns the number of bits in the bitset.
Definition cl_DynamicBitset.hpp:510
DynamicBitset(const index_t aNumberOfBits)
Constructor: Initializes the bitset with the given number of bits.
Definition cl_DynamicBitset.cpp:72
USER GUIDES:
Definition cl_Capacitor.cpp:16
constexpr index_t gNoIndex
Definition typedefs.hpp:57
uint32_t index_t
Definition typedefs.hpp:52