Add find_prev() and find_last() and their _off to boost::dynamic_bitset
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
Research direction
Read the existing find_first(), find_next(), find_first_off(), and find_next_off() implementations and their tests in the dynamic_bitset repository; the issue does not name file paths. Check how those methods handle boundary positions and npos, then implement and test the four reverse-search methods with the examples in the issue as expected behavior.
Written by the indexing model from the issue text.
Description
Description
Currently, boost::dynamic_bitset provides find_first() and find_next() to efficiently iterate over set bits (1) in ascending order, as well as find_first_off() and find_next_off() for unset bits (0).
However, there are no equivalent methods for searching bits in reverse order.
I would like to propose adding four methods:
find_last(): Returns the index of the last (highest) set bit.find_prev(pos): Returns the index of the previous set bit strictly beforepos.find_last_off(): Returns the index of the last (highest) unset bit.find_prev_off(pos): Returns the index of the previous unset bit strictly beforepos.
All methods should return npos when no matching bit exists.
Proposed API
size_type find_last() const;
size_type find_prev(size_type pos) const;
size_type find_last_off() const;
size_type find_prev_off(size_type pos) const;
Example usage
boost::dynamic_bitset<> bs(10);
bs.set(2);
bs.set(5);
bs.set(8);
// Finding set bits (1)
assert(bs.find_last() == 8);
assert(bs.find_prev(8) == 5);
assert(bs.find_prev(5) == 2);
assert(bs.find_prev(2) == bs.npos);
// Finding unset bits (0)
assert(bs.find_last_off() == 9);
assert(bs.find_prev_off(9) == 7);
assert(bs.find_prev_off(7) == 6);
assert(bs.find_prev_off(1) == 0);
assert(bs.find_prev_off(0) == bs.npos);
Motivation
These methods would provide a symmetric API with the existing find_first(), find_next(), find_first_off(), and find_next_off() methods, allowing efficient traversal of both set and unset bits in either direction.
Although reverse searches can be implemented manually, having native implementations would allow Boost to optimize them at the block level, avoiding bit-by-bit scanning or unnecessary temporary bitset allocations.
This would be particularly useful for algorithms that frequently need to find the highest set or unset bit, or iterate over bits in descending order.
Thanks for considering this enhancement!
- Dominant language
- C++
- Stars
- 44
- Forks
- 65
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Similar issues
-
agent:WSL code-review linux pri:medium refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
kind/enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
psi29a/d2decomped#691 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
CachyOS/packageinstaller#144 ·
Maintainers usually reply within 1 day
-
(S1 - Need confirmation)
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
CleverRaven/Cataclysm-DDA#89715 ·
Maintainers usually reply within 1 day