BitMagic icon indicating copy to clipboard operation
BitMagic copied to clipboard

Bug in bvector::flip()?

Open fanejonda opened this issue 1 year ago • 1 comments

Hi,

It appears that there is a bug in the implementation of the flip function (full vector flip). The flipping seems to assume that there is a large tail of zeroes at the end of every bvector and flipping causes that implicit tail to become real.

To reproduce:

#include <iostream>
#include "bm.h"

int main() {
  bm::bvector<> bv;
  bv.set_range(10, 11);
  std::cout << "Count pre flip=" << bv.count() << std::endl;
  bv.flip();
  std::cout << "Count post flip=" << bv.count() << std::endl;
}

The output

Count pre flip=2
Count post flip=4294967293

I would have expected that a flipping operation would not change the size of the vector and the post flip count to be 9.

This also affects other APIs, because the rest of the code now believes there is a big set of 1s at the end of the vector.

fanejonda avatar Oct 19 '23 14:10 fanejonda

It is indeed the case, bvector<> is actually size-less and technically it contains a whole tail of zeroes at the end. It is possible to use keep_range() to clip the vector. I guess the API really needs flip_range() method...

tlk00 avatar Nov 20 '23 04:11 tlk00