15#ifndef CXXUTILS_CONCURRENTBITSET_H
16#define CXXUTILS_CONCURRENTBITSET_H
154 typedef unsigned long Block_t;
157 static const size_t BLOCKSIZE =
sizeof(Block_t) * CHAR_BIT;
160 static const size_t MASK = BLOCKSIZE-1;
163 static_assert (std::atomic<Block_t>::is_always_lock_free);
167 typedef size_t bit_t;
531 template <
class ITERATOR,
532 typename =
typename std::enable_if<
534 typename std::forward_iterator_tag,
535 typename std::iterator_traits<ITERATOR>::iterator_category
716 const std::atomic<Block_t>*
data,
717 const std::atomic<Block_t>*
end);
852 void*
operator new (
size_t , bit_t
nbits);
859 void operator delete (
void* p);
936 std::atomic<Block_t>*
block (bit_t bit);
ElementLink & operator=(const ElementLink &)=default
bool operator!=(const DataVector< T > &a, const DataVector< T > &b)
Based on operator==.
bool operator==(const DataVector< T > &a, const DataVector< T > &b)
Vector equality comparison.
char data[hepevt_bytes_allocation_ATLAS]
Atomic min/max functions.
void assign(const Impl &other)
Copy from another instance.
bool none() const
Return true if there are no 1 bits in the set.
size_t m_nblocks
Number of blocks in the container.
bit_t count() const
Count the number of 1 bits in the set.
void flip(bit_t bit)
Flip the value of one bit.
bool all() const
Return true if all bits in the set are 1.
Impl(bit_t nbits)
Constructor.
void flip()
Flip the state of all bits in the set.
std::atomic< Block_t > * block(bit_t bit)
Return a pointer to the block containing bit.
void operator_t(std::atomic< Block_t > &a, Block_t v)
std::atomic< size_t > m_hwm
High-water mark: index of last block with a 1 bit.
void operate(operator_t op, const Impl &other)
Apply a binary operation.
const_iterator begin() const
Return an iterator referencing the first 1 bit.
void reset(bit_t bit)
Turn off one bit.
Impl(const Impl &other, bit_t nbits=0)
Copy constructor.
const_iterator find(bit_t bit) const
If bit bit is set, return an iterator pointing to it.
bool test(bit_t bit) const
Test to see if a bit is set.
const_iterator end() const
Return the end iterator.
void set(bit_t bit)
Turn on one bit.
void set()
Turn on all bits in the set.
bit_t nbits() const
Return the number of bits in the set.
size_t m_nbits
Number of bits in the container.
void clear()
Clear all bits in the set.
std::atomic< Block_t > m_data[1]
The set data. The implementation objects are allocated such that there are actually m_nblocks entries...
A reference to one bit in a set.
Block_t m_mask
Mask of the referenced bit within the block.
reference(Impl &impl, bit_t bit)
Constructor.
std::atomic< Block_t > * m_block
Pointer to the block containing the referenced bit.
friend class ConcurrentBitset
reference & flip() noexcept
Invert the referenced bit.
Some additional feature test macros.
std::string find(const std::string &s)
return a remapped string
void * newImpl(std::size_t size)
bool none() const
Return true if there are no 1 bits in the set.
void emptyGarbage()
Clean up old versions of the set.
std::mutex mutex_t
Mutex used for synchronization when switching to a new implementation object.
ConcurrentBitset & operator-=(const ConcurrentBitset &other)
Subtract another set from this set.
constexpr std::enable_if_t< is_bitmask_v< E >, bool > test(E lhs, E rhs)
Convenience function to test bits in a class enum bitmask.
const_iterator begin() const
Return a begin iterator.
static bit_t nBlocks(bit_t nbits)
Find number of blocks needed to hold a given number of bits.
bool empty() const
Return true if there are no 1 bits in the set.
ConcurrentBitset & set()
Turn on all bits in the set.
void expandOol(bit_t new_nbits)
Expand the container: out-of-line portion.
bool all() const
Return true if all bits in the set are 1.
ConcurrentBitset operator~() const
Return a new set that is the complement of this set.
bit_t size() const
Count the number of 1 bits in the set.
ConcurrentBitset & operator^=(const ConcurrentBitset &other)
XOR this set with another set.
~ConcurrentBitset()
Destructor.
ConcurrentBitset & erase(bit_t bit)
Turn off one bit.
ConcurrentBitset & flip()
Flip the state of all bits in the set.
ConcurrentBitset & reset()
Clear all bits in the set.
bit_t capacity() const
The number of bits that this container can hold.
ConcurrentBitset(bit_t nbits=0)
Constructor.
ConcurrentBitset & operator|=(const ConcurrentBitset &other)
OR this set with another set.
ConcurrentBitset & operator=(const ConcurrentBitset &other)
Assignment.
mutex_t m_mutex
Mutex protecting the container.
ConcurrentBitset & clear()
Clear all bits in the set.
const_iterator end() const
Return an end iterator.
bool operator==(const ClassName &other) const
Test two expressions for equality.
ConcurrentBitset & insert(bit_t bit, bit_t new_nbits=0)
Set a bit to 1.
bool operator!=(const ClassName &other) const
Test two expressions for inequality.
ConcurrentBitset & operator&=(const ConcurrentBitset &other)
AND this set with another set.
std::lock_guard< mutex_t > lock_t
void expand(bit_t new_nbits)
Expand the container.
std::atomic< Impl * > m_impl
The current implementation object.
bit_t count() const
Count the number of 1 bits in the set.
std::vector< Impl * > m_garbage
Old implementation objects, pending deletion.
bool any() const
Return true if there are any 1 bits in the set.
Iterator over all 1 bits in the set.
const_iterator operator++(int)
Advance the iterator to the next set bit (postincrement).
std::forward_iterator_tag iterator_category
const value_type * pointer
const_iterator(Block_t cache, bit_t bit, const std::atomic< Block_t > *data, const std::atomic< Block_t > *end)
Constructor.
const value_type & reference
Block_t m_cache
Cache of the block to which we're currently pointing. Should be shifted such that the bit we're refer...
bit_t m_bit
Bit number which we're currently referencing.
bit_t operator*() const
Return the bit number which the iterator is currently referencing.
ptrdiff_t difference_type
const_iterator & operator++()
Advance the iterator to the next set bit (preincrement).
const std::atomic< Block_t > * m_data
Pointer to the block containing the bit which we're currently referencing.
const std::atomic< Block_t > * m_end
Pointer to one past the last block in the set.