C++用于流压缩算法的快速位数组类

发布于 2024-09-16 09:49:22 字数 428 浏览 5 评论 0原文

实现流压缩算法,通常需要一个超快的 FIFO 位容器类,具有以下功能:

AddBits(UINT n, UINT nBits);  // Add lower nBits bits of n 
GetBitCount();                // Get the number of bits currently stored
GetBits(BYTE* n, UINT nBits); // Extract n Bits, and remove them

位的数量限制为相对较小的大小(“数据包”大小或更多)。

我正在寻找一个实现此功能的小型 C++ 类。

是的,我可以写一个(并且知道如何做),但可能有人已经写了......

注意:我不想为此而将 boost/whatever-big-lib 添加到我的项目中。

Implementing streaming compression algorithms, one usually need a super-fast FIFO bit container class with the following functions:

AddBits(UINT n, UINT nBits);  // Add lower nBits bits of n 
GetBitCount();                // Get the number of bits currently stored
GetBits(BYTE* n, UINT nBits); // Extract n Bits, and remove them

The number of bits is bounded to a relatively small size (the 'packet' size or a bit more).

I'm looking for a small C++ class which implement this functionality.

Yes, I can write one (and know how to do it), but probably someone wrote it already...

Note: I don't want to add boost / whatever-big-lib to my project just for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-09-23 09:49:22

当我总是想一次读取 16 位或更少的数据时,我在嵌入式系统中使用的一种方法是保留 32 位长,用于保存当前的部分 16 位字和下一个完整字。然后代码是这样的:

/* ui=unsigned 16-bit ul=unsigned 32-bit   LT == less-than SHL = shift-left */

ul bit_buff;
ui buff_count;
ui *bit_src;

unsigned int readbits(int numbits)
{
  if (buff_count LT numbits)
  {
    bit_buff |= ((ul)(*bit_src++)) SHL buff_ct;
    buff_ct += 16;
  }
  buff_ct -= numbits;
  return bit_buff & ((1 SHL numbits)-1);
}

这可能很容易适应与 64 位 long long 一起使用,并允许一次最多提取 32 位。

One approach I've used in embedded systems, when I always wanted to read 16 bits or less at a time, was to keep a 32-bit long which holds the current partial 16-bit word, and the next whole one. Then the code was something like:

/* ui=unsigned 16-bit ul=unsigned 32-bit   LT == less-than SHL = shift-left */

ul bit_buff;
ui buff_count;
ui *bit_src;

unsigned int readbits(int numbits)
{
  if (buff_count LT numbits)
  {
    bit_buff |= ((ul)(*bit_src++)) SHL buff_ct;
    buff_ct += 16;
  }
  buff_ct -= numbits;
  return bit_buff & ((1 SHL numbits)-1);
}

That could probably be readily adapted for use with a 64-bit long long and allow for withdrawal of up to 32 bits at a time.

初见终念 2024-09-23 09:49:22

我知道你不想,但你可以使用 增强动态位集并使用供应商/消费者语义在其之上提供 FIFO 功能。

我也不想使用 boost,但这确实没什么大不了的。您无需对库执行任何操作。您只需要在构建系统上提供可用的 boost 并包含正确的包含文件即可。

I know you don't want to, but you could use a boost dynamic bitset and provide the FIFO capability on top of it using supplier/consumer semantics.

I didn't want to use boost either, but it's really not a big deal. You don't have to do anything with libraries. You just need to have boost available on your build system and include the right include files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文