ATLAS Offline Software
Loading...
Searching...
No Matches
EventContainers::IdentifierMask Class Reference

A class for a dynamic bit mask specialised for fast, ordered iteration using the forEachSetBit method. More...

#include <IdentifierMask.h>

Collaboration diagram for EventContainers::IdentifierMask:

Public Member Functions

 IdentifierMask (size_t maxHash)
void set (size_t hash)
 Mark a hash as present.
bool test (size_t hash) const
 Check if a hash is present.
void unset (size_t hash)
 Remove a hash from the mask (Set bit to 0).
void clear ()
 Reset all bits to 0.
template<typename Func>
void forEachSetBit (Func &&f) const
 Execute a lambda for every set bit The lambda should accept a size_t index.
size_t size () const
 Return the total size of the mask.
size_t count () const
 Return a count of the bits set to 1.

Static Public Attributes

static constexpr size_t BitsPerWord = 64
 Bits per storage unit (64).
static constexpr size_t Mask = BitsPerWord - 1
 Mask for the bit index (63).
static constexpr size_t MaskShift = std::countr_one(Mask)
 Mask shift value for this word size.

Private Attributes

std::vector< uint64_t > m_bits
size_t m_maxHash

Detailed Description

A class for a dynamic bit mask specialised for fast, ordered iteration using the forEachSetBit method.

Definition at line 17 of file IdentifierMask.h.

Constructor & Destructor Documentation

◆ IdentifierMask()

EventContainers::IdentifierMask::IdentifierMask ( size_t maxHash)
inlineexplicit

Definition at line 26 of file IdentifierMask.h.

30 : m_bits((maxHash + Mask) / BitsPerWord, 0), m_maxHash(maxHash) {}
static constexpr size_t BitsPerWord
Bits per storage unit (64).
std::vector< uint64_t > m_bits
static constexpr size_t Mask
Mask for the bit index (63).

Member Function Documentation

◆ clear()

void EventContainers::IdentifierMask::clear ( )
inline

Reset all bits to 0.

Definition at line 53 of file IdentifierMask.h.

53 {
54 std::fill(m_bits.begin(), m_bits.end(), 0);
55 }

◆ count()

size_t EventContainers::IdentifierMask::count ( ) const
inline

Return a count of the bits set to 1.

Definition at line 87 of file IdentifierMask.h.

87 {
88 size_t total = 0;
89 for (uint64_t word : m_bits) total += std::popcount(word);
90 return total;
91 }

◆ forEachSetBit()

template<typename Func>
void EventContainers::IdentifierMask::forEachSetBit ( Func && f) const
inline

Execute a lambda for every set bit The lambda should accept a size_t index.

Definition at line 60 of file IdentifierMask.h.

60 {
61 for (size_t block_idx = 0; block_idx < m_bits.size(); ++block_idx) {
62 uint64_t word = m_bits[block_idx];
63
64 // Skip empty blocks of 64 bits entirely
65 while (word != 0) {
66 // Returns the number of trailing zeros, which is the index of the first '1' bit
67 // countr_zero uses the TZCNT or BSF instruction (1 clock cycle)
68 int bit_idx = std::countr_zero(word);
69
70 // (block_idx << 6): Multiply block index by 64 to get the base bit position
71 // Then add the bit_idx within that block to get the absolute hash value
72 // Execute user logic
73 f((block_idx << MaskShift) + bit_idx);
74
75 // Brian Kernighan’s Algorithm: Clears the least significant bit set to 1.
76 // This allows the 'while' loop to jump directly to the next set bit.
77 // Suggested by Google Gemini and verified by maskTest.cxx
78 word &= (word - 1);
79 }
80 }
81 }
static constexpr size_t MaskShift
Mask shift value for this word size.

◆ set()

void EventContainers::IdentifierMask::set ( size_t hash)
inline

Mark a hash as present.

Definition at line 33 of file IdentifierMask.h.

33 {
34 assert (hash < m_maxHash); //change to a contract in c++26
35 // hash >> 6: Efficiently divides by 64 to find the vector index
36 // hash & Mask: Efficiently computes hash % 64 to find the bit position within the word
37 m_bits[hash >> MaskShift] |= (1ULL << (hash & Mask));
38 }

◆ size()

size_t EventContainers::IdentifierMask::size ( ) const
inline

Return the total size of the mask.

Definition at line 84 of file IdentifierMask.h.

84{ return m_maxHash; }

◆ test()

bool EventContainers::IdentifierMask::test ( size_t hash) const
inline

Check if a hash is present.

Definition at line 41 of file IdentifierMask.h.

41 {
42 assert (hash < m_maxHash); //change to a contract in c++26
43 return (m_bits[hash >> MaskShift] >> (hash & Mask)) & 1ULL;
44 }

◆ unset()

void EventContainers::IdentifierMask::unset ( size_t hash)
inline

Remove a hash from the mask (Set bit to 0).

Definition at line 47 of file IdentifierMask.h.

47 {
48 assert (hash < m_maxHash); //change to a contract in c++26
49 m_bits[hash >> MaskShift] &= ~(1ULL << (hash & Mask));
50 }

Member Data Documentation

◆ BitsPerWord

size_t EventContainers::IdentifierMask::BitsPerWord = 64
staticconstexpr

Bits per storage unit (64).

Definition at line 20 of file IdentifierMask.h.

◆ m_bits

std::vector<uint64_t> EventContainers::IdentifierMask::m_bits
private

Definition at line 94 of file IdentifierMask.h.

◆ m_maxHash

size_t EventContainers::IdentifierMask::m_maxHash
private

Definition at line 95 of file IdentifierMask.h.

◆ Mask

size_t EventContainers::IdentifierMask::Mask = BitsPerWord - 1
staticconstexpr

Mask for the bit index (63).

Definition at line 22 of file IdentifierMask.h.

◆ MaskShift

size_t EventContainers::IdentifierMask::MaskShift = std::countr_one(Mask)
staticconstexpr

Mask shift value for this word size.

Definition at line 24 of file IdentifierMask.h.


The documentation for this class was generated from the following file: