In my program I need to check if I have already generated a value in a set of 2.5*10^9. I expect to generate about the half of the set and need to have a fast way to check and update it. The bitset seemed to me as a good idea as it takes not too much memory (1 bit per value) and is fast.
The problem is that when I define my set in my class, I got a segmentation fault
as the size is too big (it works with smaller sizes).
private:
std::bitset<2500000000UL> cover; // not working
std::bitset<25000UL> cover; // working
Any idea ?
Thank you
PS: I'd rather not use external library if possible. I'm already using GMP but I don't think they have a bit set implementation for large numbers.
Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.
The maximum element in the set is the size - 1st element. The default size of the bit set is 64-bit space. If the bit is set at index larger than the current BitSet size, it increases its bit space in the multiplication of 64*n, where n starts from 1, 2, 3, so on.
Memory Footprint As shown above, this boolean[] consumes around 10 KB of memory. As expected, the BitSet with the same number of bits consumes around 1 KB, which is far less than the boolean[].
Using Clang 10.0 and GCC 10.1, in both cases the array of bools is faster than bitset.
This may not be your problem, but try to allocate the bitset on the heap with new, instead of using the stack.
Some systems limit the size of the stack, which might be what causes problems for you.
I think the following solution is better than using new
std::vector<std::bitset<2500000000UL>> wrapper(1);
auto & cover = wrapper[0];//To avoide unnecessary indirection by wrapper[0]
To demonstrate
int main(){
std::vector<std::bitset<2500000000UL>> wrapper(1);
auto & cover = wrapper[0];
cover[0] = 1;
std::cout << cover[0] << " " << cover[2500000000UL - 1];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With