开发者

Efficient reading/writing of streams of bits in C++

I'm writing a data compression library and I need to write a sequence of encodings of integers (in a variety of integer encoders) in memory, store it in a file and then read all of them later.

Integer encodings have to be stored consecutively. Since generally their size in bits isn't a multiple of 8, I don't have them aligned in memory.

In short, what I need is something which exposes functions like these:

unsigned int BitReader::read_bits(size_t bits);
unsigned int BitWriter::write_bits(unsigned int num, size_t bits);
void BitWriter::get_array(char** array);
BitReader::BitReader(char *array);

Since I need to invoke those functions in a (very) tight loop, efficiency is of paramount interest (especially in reading).

Do you know some C++ libraries which does what I want? T开发者_StackOverflow社区hanks.


If efficiency is your only requirement then get the address of the storage for the data and write it directly to storage. Then on restore allocate the same storage and perform the reverse operation. It's simple, fast, and has no learning curve.


Openning a stream to whatever input / output is the most affective although not efficient for huge amount of data. Streams provide a portable way of carrying out read/write operations thats why you can open stream in memory or disk. If you want to take control of the stream to the disk itself i would recommend using _bios_disk function google "_bios_disk" for more info.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜