20#ifndef FASTDDS_UTILS__FIXED_SIZE_BITMAP_HPP
21#define FASTDDS_UTILS__FIXED_SIZE_BITMAP_HPP
32#pragma push_macro("max")
34#define FASTDDS_RESTORE_MAX
38#pragma push_macro("min")
40#define FASTDDS_RESTORE_MIN
45#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
74template<
class T,
class Diff = DiffFunction<T>, u
int32_t NBITS = 256>
77 #define NITEMS ((NBITS + 31u) / 32u)
119 uint32_t max_bits) noexcept
162 uint32_t max_bits)
noexcept
190 shift_map_left(n_bits);
196 shift_map_right(n_bits);
233 uint32_t n_longs = (
num_bits_ + 31u) / 32u;
234 for (uint32_t i = 0; i < n_longs; i++)
245 _BitScanReverse(&bit, bits);
246 uint32_t offset = 31u ^ bit;
248 uint32_t offset =
static_cast<uint32_t
>(__builtin_clz(bits));
252 return item + offset;
270 const T& item)
const noexcept
277 uint32_t diff = d_func(item,
base_);
280 uint32_t pos = diff >> 5;
282 return (
bitmap_[pos] & (1u << (31u - diff))) != 0;
298 const T& item)
noexcept
305 uint32_t diff = d_func(item,
base_);
307 uint32_t pos = diff >> 5;
309 bitmap_[pos] |= (1u << (31u - diff));
329 constexpr uint32_t full_mask = (std::numeric_limits<uint32_t>::max)();
343 uint32_t offset = d_func(
min,
base_);
344 uint32_t n_bits = d_func(
max,
min);
348 uint32_t pos = offset >> 5;
350 uint32_t mask = full_mask;
352 uint32_t bits_in_mask = 32u - offset;
355 while (n_bits >= bits_in_mask)
359 n_bits -= bits_in_mask;
367 bitmap_[pos] |= mask & (full_mask << (bits_in_mask - n_bits));
378 const T& item)
noexcept
382 if ((item >=
base_) && (max_value >= item))
386 uint32_t diff = d_func(item,
base_);
387 uint32_t pos = diff >> 5;
389 bitmap_[pos] &= ~(1u << (31u - diff));
391 if (item == max_value)
393 calc_maximum_bit_set(pos + 1, 0);
409 uint32_t& num_longs_used)
const noexcept
412 num_longs_used = (
num_bits_ + 31u) / 32u;
425 uint32_t n_longs = (
num_bits_ + 31u) / 32u;
426 for (uint32_t i = 0; i < n_longs; i++)
432 bits = bits - ((bits >> 1) & 0x55555555);
433 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
434 bits = (bits + (bits >> 4)) & 0x0F0F0F0F;
435 total += (bits * 0x1010101) >> 24;
450 const uint32_t* bitmap)
noexcept
453 uint32_t num_items = ((
num_bits_ + 31u) / 32u);
454 uint32_t num_bytes = num_items *
static_cast<uint32_t
>(
sizeof(uint32_t));
456 memcpy(
bitmap_.data(), bitmap, num_bytes);
458 short shift = num_bits & 31u;
459 if (0 < num_bits && shift != 0)
461 bitmap_[num_items - 1] &= ~((std::numeric_limits<uint32_t>::max)() >> shift);
463 calc_maximum_bit_set(num_items, 0);
471 template<
class UnaryFunc>
478 uint32_t n_longs = (
num_bits_ + 31u) / 32u;
479 for (uint32_t i = 0; i < n_longs; i++)
491 _BitScanReverse(&bit, bits);
492 uint32_t offset = 31u ^ bit;
494 uint32_t offset =
static_cast<uint32_t
>(__builtin_clz(bits));
495 uint32_t bit = 31u ^ offset;
502 bits &= ~(1u << bit);
534 uint32_t n_items = n_bits >> 5;
540 std::fill_n(
bitmap_.rbegin(), n_items, 0);
550 uint32_t overflow_bits = 32u - n_bits;
551 size_t last_index = NITEMS - 1u;
552 for (
size_t i = 0, n = n_items; n < last_index; i++, n++)
559 std::fill_n(
bitmap_.rbegin(), n_items, 0);
564 void shift_map_right(
577 uint32_t new_num_bits =
num_bits_ + n_bits;
578 bool find_new_max = new_num_bits > NBITS;
581 uint32_t n_items = n_bits >> 5;
587 std::fill_n(
bitmap_.begin(), n_items, 0);
598 uint32_t overflow_bits = 32u - n_bits;
599 size_t last_index = NITEMS - 1u;
600 for (
size_t i = last_index, n = last_index - n_items; n > 0; i--, n--)
607 std::fill_n(
bitmap_.begin(), n_items, 0);
613 calc_maximum_bit_set(NITEMS, n_items);
618 void calc_maximum_bit_set(
619 uint32_t starting_index,
623 for (uint32_t i = starting_index; i > min_index;)
629 bits = (bits & ~(bits - 1));
632 _BitScanReverse(&bit, bits);
633 uint32_t offset = (31u ^ bit) + 1;
635 uint32_t offset =
static_cast<uint32_t
>(__builtin_clz(bits)) + 1u;
652#if defined(FASTDDS_RESTORE_MIN)
653#pragma pop_macro("min")
654#undef FASTDDS_RESTORE_MIN
657#if defined(FASTDDS_RESTORE_MAX)
658#pragma pop_macro("max")
659#undef FASTDDS_RESTORE_MAX
bitmap_type bitmap_
Definition fixed_size_bitmap.hpp:514
FragmentNumber_t base() const noexcept
Definition fixed_size_bitmap.hpp:133
uint32_t num_bits_
Definition fixed_size_bitmap.hpp:515
bool empty() const noexcept
Returns whether the range is empty (i.e.
Definition fixed_size_bitmap.hpp:209
bool add(const T &item) noexcept
Adds an element to the range.
Definition fixed_size_bitmap.hpp:297
void bitmap_get(uint32_t &num_bits, bitmap_type &bitmap, uint32_t &num_longs_used) const noexcept
Gets the current value of the bitmap.
Definition fixed_size_bitmap.hpp:406
T min() const noexcept
Returns the lowest value set in the range.
Definition fixed_size_bitmap.hpp:229
std::array< uint32_t, NITEMS > bitmap_type
Definition fixed_size_bitmap.hpp:82
void bitmap_set(uint32_t num_bits, const uint32_t *bitmap) noexcept
Sets the current value of the bitmap.
Definition fixed_size_bitmap.hpp:448
FragmentNumber_t base_
Definition fixed_size_bitmap.hpp:512
void add_range(const T &from, const T &to)
Adds a range of elements to the range.
Definition fixed_size_bitmap.hpp:325
void base_update(T base) noexcept
Set a new base for the range, keeping old values where possible.
Definition fixed_size_bitmap.hpp:176
FragmentNumber_t range_max_
Definition fixed_size_bitmap.hpp:513
BitmapRange(T base) noexcept
Base-specific constructor.
Definition fixed_size_bitmap.hpp:102
void base(T base, uint32_t max_bits) noexcept
Set a new base and maximum bits for the range.
Definition fixed_size_bitmap.hpp:160
void for_each(UnaryFunc f) const
Apply a function on every item on the range.
Definition fixed_size_bitmap.hpp:472
uint32_t count() const noexcept
Counts the number of elements set in the bitmap.
Definition fixed_size_bitmap.hpp:421
bool is_set(const T &item) const noexcept
Checks if an element is present in the bitmap.
Definition fixed_size_bitmap.hpp:269
BitmapRange(T base, uint32_t max_bits) noexcept
Range specific constructor.
Definition fixed_size_bitmap.hpp:117
BitmapRange() noexcept
Default constructor.
Definition fixed_size_bitmap.hpp:88
void base(T base) noexcept
Set a new base for the range.
Definition fixed_size_bitmap.hpp:144
T max() const noexcept
Returns the highest value set in the range.
Definition fixed_size_bitmap.hpp:219
void remove(const T &item) noexcept
Removes an element from the range.
Definition fixed_size_bitmap.hpp:377
Definition fixed_size_bitmap.hpp:53
constexpr auto operator()(T a, T b) const -> decltype(a - b)
Definition fixed_size_bitmap.hpp:54