tlk00 / BitMagic

BitMagic Library
http://bitmagic.io
Other
412 stars 48 forks source link

Bug in bvector::flip()? #73

Open fanejonda opened 1 year ago

fanejonda commented 1 year ago

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.

tlk00 commented 11 months ago

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...