-
Notifications
You must be signed in to change notification settings - Fork 21
Add offset in dynamic_bitset #567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -334,27 +334,40 @@ namespace sparrow | |||||||||||
return std::move(m_buffer); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* @brief Returns the bit offset from the start of the buffer. | ||||||||||||
* | ||||||||||||
* This value indicates how many bits from the beginning of the underlying storage | ||||||||||||
* are skipped before the bitset's logical start. It is set during construction and | ||||||||||||
* remains constant for the lifetime of the bitset. | ||||||||||||
* | ||||||||||||
* @return The number of bits offset from the start of the buffer. | ||||||||||||
*/ | ||||||||||||
[[nodiscard]] size_t offset() const noexcept; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
|
||||||||||||
protected: | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* @brief Constructs a bitset with the given storage and size. | ||||||||||||
* @param buffer The storage buffer to use | ||||||||||||
* @param size The number of bits in the bitset | ||||||||||||
* @param offset The number of bits to offset from the start of the buffer | ||||||||||||
* @post size() == size | ||||||||||||
* @post null_count() is computed by counting unset bits | ||||||||||||
*/ | ||||||||||||
constexpr dynamic_bitset_base(storage_type buffer, size_type size); | ||||||||||||
constexpr dynamic_bitset_base(storage_type buffer, size_type size, size_type offset); | ||||||||||||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
/** | ||||||||||||
* @brief Constructs a bitset with the given storage, size, and null count. | ||||||||||||
* @param buffer The storage buffer to use | ||||||||||||
* @param size The number of bits in the bitset | ||||||||||||
* @param null_count The number of unset bits | ||||||||||||
* @param offset The number of bits to offset from the start of the buffer | ||||||||||||
* @pre null_count <= size | ||||||||||||
* @post size() == size | ||||||||||||
* @post null_count() == null_count | ||||||||||||
*/ | ||||||||||||
constexpr dynamic_bitset_base(storage_type buffer, size_type size, size_type null_count); | ||||||||||||
constexpr dynamic_bitset_base(storage_type buffer, size_type size, size_type offset, size_type null_count); | ||||||||||||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
constexpr ~dynamic_bitset_base() = default; | ||||||||||||
|
||||||||||||
|
@@ -540,6 +553,7 @@ namespace sparrow | |||||||||||
storage_type m_buffer; ///< The underlying storage for bit data | ||||||||||||
size_type m_size; ///< The number of bits in the bitset | ||||||||||||
size_type m_null_count; ///< The number of bits set to false | ||||||||||||
size_t m_offset = 0; ///< The number of bits to offset from the start of the buffer | ||||||||||||
|
||||||||||||
friend class bitset_iterator<self_type, true>; ///< Const iterator needs access to internals | ||||||||||||
friend class bitset_iterator<self_type, false>; ///< Mutable iterator needs access to internals | ||||||||||||
|
@@ -598,7 +612,8 @@ namespace sparrow | |||||||||||
{ | ||||||||||||
return true; | ||||||||||||
} | ||||||||||||
return !m_null_count || buffer().data()[block_index(pos)] & bit_mask(pos); | ||||||||||||
const size_t pos_with_offset = pos + m_offset; | ||||||||||||
return !m_null_count || buffer().data()[block_index(pos_with_offset)] & bit_mask(pos_with_offset); | ||||||||||||
} | ||||||||||||
|
||||||||||||
template <typename B> | ||||||||||||
|
@@ -627,15 +642,16 @@ namespace sparrow | |||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
block_type& block = buffer().data()[block_index(pos)]; | ||||||||||||
const bool old_value = block & bit_mask(pos); | ||||||||||||
const size_t pos_with_offset = pos + m_offset; | ||||||||||||
block_type& block = buffer().data()[block_index(pos_with_offset)]; | ||||||||||||
const bool old_value = block & bit_mask(pos_with_offset); | ||||||||||||
if (value) | ||||||||||||
{ | ||||||||||||
block |= bit_mask(pos); | ||||||||||||
block |= bit_mask(pos_with_offset); | ||||||||||||
} | ||||||||||||
else | ||||||||||||
{ | ||||||||||||
block &= block_type(~bit_mask(pos)); | ||||||||||||
block &= block_type(~bit_mask(pos_with_offset)); | ||||||||||||
} | ||||||||||||
update_null_count(old_value, value); | ||||||||||||
} | ||||||||||||
|
@@ -790,19 +806,26 @@ namespace sparrow | |||||||||||
|
||||||||||||
template <typename B> | ||||||||||||
requires std::ranges::random_access_range<std::remove_pointer_t<B>> | ||||||||||||
constexpr dynamic_bitset_base<B>::dynamic_bitset_base(storage_type buf, size_type size) | ||||||||||||
constexpr dynamic_bitset_base<B>::dynamic_bitset_base(storage_type buf, size_type size, size_type offset) | ||||||||||||
: m_buffer(std::move(buf)) | ||||||||||||
, m_size(size) | ||||||||||||
, m_null_count(m_size - count_non_null()) | ||||||||||||
, m_offset(offset) | ||||||||||||
{ | ||||||||||||
} | ||||||||||||
|
||||||||||||
template <typename B> | ||||||||||||
requires std::ranges::random_access_range<std::remove_pointer_t<B>> | ||||||||||||
constexpr dynamic_bitset_base<B>::dynamic_bitset_base(storage_type buf, size_type size, size_type null_count) | ||||||||||||
constexpr dynamic_bitset_base<B>::dynamic_bitset_base( | ||||||||||||
storage_type buf, | ||||||||||||
size_type size, | ||||||||||||
size_type offset, | ||||||||||||
size_type null_count | ||||||||||||
) | ||||||||||||
: m_buffer(std::move(buf)) | ||||||||||||
, m_size(size) | ||||||||||||
, m_null_count(null_count) | ||||||||||||
, m_offset(offset) | ||||||||||||
{ | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -914,7 +937,7 @@ namespace sparrow | |||||||||||
return; | ||||||||||||
} | ||||||||||||
size_type old_block_count = buffer().size(); | ||||||||||||
const size_type new_block_count = compute_block_count(n); | ||||||||||||
const size_type new_block_count = compute_block_count(n + m_offset); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding offset to the size for block count calculation is incorrect. The block count should be based on the actual storage needed for the bits, not the logical size plus offset. This could cause buffer overruns when the offset pushes beyond allocated memory.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
const block_type value = b ? block_type(~block_type(0)) : block_type(0); | ||||||||||||
|
||||||||||||
if (new_block_count != old_block_count) | ||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.