开发者

Is there a bitset class that's sized at instantiation time, but avoids boost::dynamic_bitset<>'s extra allocation call?

Is there a convenient analog of std::bitset<> that's dynamically sizable at instantiation time, but avoids the extra allocation required by boost::dynamic_bitset<>

You can create dynamically sized bit sets easily in C by doing something like :

typedef struct { node_t node; bloc开发者_如何学编程k_t bits[0]; } node_bitset_t;
p = (node_bitset_t *)malloc( (sizeof(node_t) + sizeof(block_t)*blocks) * array_size);

You could do this using std::vector<std::bitset<bits>> only if you know bits at compile time. If you use std::vector<boost::dynamic_bitset<>>, then you'll see an extra allocator call. Is there a compromise that achieves the above C code's balance?

You might for-example have some custom allocator for the std::vector<...> that leaves some extra space after each boost::dynamic_bitset<> and allocates m_block there, although that'll probably still cost you the pointer for m_block.


It would be possible to create such a class using placement new, but I'm not aware of a preexisting class which does this. The class wouldn't be instantiated directly but would be built via a factory; the factory method would use new to allocate the number of bytes necessary, then use placement new to initialize the object at the front of the buffer. The end of the buffer past the size of the class would be the bit storage.

In practice there's no need to do this, because it doesn't save anything vs. dynamic_bitset. With this method you have two parts, a pointer to the bits object and the object itself. With dynamic_bitset you have the dynamic_bitset object which contains the pointer to the bits. Same thing.


I create a ticket in boost for it as feature request: Ticket #13016 (https://svn.boost.org/trac/boost/ticket/13016)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜