0

API 不允许对不同大小的 dynamic_bitsets 应用逻辑操作,而我的应用程序要求如果其中一个位集大于另一个,则相应地调整结果大小。我必须创建一个临时副本来解决问题:

using column_mask = boost::dynamic_bitset<uint64_t>;                                                
void column_mask_union(column_mask& to, const column_mask& with) {

    if (to.size() > with.size()) {
        if (with.size()) {
            column_mask tmp = with;
            tmp.resize(to.size());
            to |= tmp;
        }
        return;
    }
    if (to.size() < with.size()) {
        to.resize(with.size());
    }
    to |= with;
}

我可以在 API 中看到函数 to_block_range() 和 from_block_range() 理论上可用于访问位集,但它们似乎太有限而无法实现不同大小的位集的逻辑或。

4

1 回答 1

0

如果您考虑一下,您无法在位集中的任意偏移处创建视图而不会受到一些惩罚,因为如果它不与字节边界对齐,则每个操作都需要首先移动所有位,以便它们正确与另一个操作数中的位对齐。

您是出于性能、内存的原因还是因为您(仅)需要二进制操作而使用它?

因为如果它不是完全对性能至关重要,我建议您改用它并收工:

column_mask column_mask_union(const column_mask & to, const column_mask & with) {
    column_mask to2 = to;
    column_mask with2 = with;
    std::size_t size = std::max(with.size(), to.size());
    to2.resize(size);
    with2.resize(size);
    return to2 | with2;
}

或者,您可以过度使用所有集合以具有相同的大小。

我不认为boost::dynamic_bitset支持使用现有内存。也许还有其他一些库允许这样做。

于 2019-09-28T13:35:36.773 回答