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

未关闭
#109 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp
领域
backend

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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!

主要语言
C++
星标
44
派生
65
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。