Sunday, January 29, 2012

Dynamic bitsets not supported in C++ STL

It was annoying and frustrating to come across the fact that an essential feature was missing in the C++ STL library that I was using. I was writing code that would create a bit array of length ranging from 1 to anything like a thousand or say ten thousand. This needed to be dynamically allocated. So, if I said:
bitset* b;
...    
b = new bitset(number_of_bits); // somewhere in the program
This would create a bitset of "number_of_bits" bits. This would have been cool, but, this isn't the way bitsets are supposed to be used. The usage of bitsets is quite rigid. They are based on templates which need the size of the bit array at compile time which is done something like this.
bitset<1000> b;   
or
bitset<1000> *b = new bitset<1000>(); 
Now, how does that help me? What if I need more than 1000 bits someday? I can't just give it the highest possible constant value. Everything would fail one day if the question of scalability arises. I don't understand why the folks who developed STL didn't have this in mind. They did it for all the other data structures but, couldn't do the same for bitsets? Why? The question might sound strange but, the answer to this might even be more strange.

So, what's the solution?
Use Boost libraries - which have their implementation of dynamic_bitset. Damn! I can't use Boost as the firm that I work for doesn't want to.
Use vector<bool> and implement overload the bitwise operator to act on that. Well, that's as good as creating a new implementation for my own dynamic bitset.
 
So, I've decided that I'll create my own dynamic version of <bitset> as the guys at MSDN forums told me to.

No comments:

Post a Comment