Add find_prev() and find_last() and their _off to boost::dynamic_bitset

Open
#109 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
backend

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 before pos.
  • 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 before pos.

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.