我需要 C++ 中的无限位掩​​码

I need an infinite bit-mask in C++

可跳过上下文:我有一个模拟循环(using fixed update but variable rendering pattern), which instantiates entities of classes that are generated on the fly according to user-input and/or file configurations from a database of millions of components (of the container state modifying pattern)。

我实现了一个系统自动...推导(某种逻辑cell/math我不知道名字)并在任何时候应用需要的组件user-input/config 忽略了一个事实,即他们的一个选项需要额外的组件。

怎么会这样?许多组件是复杂的公式或模糊逻辑(门?)或其他复杂的科学推理,以一种可以操作我的模拟结构、对象、环境的方式进行编码,因此有时一个组件依赖于另一个组件,我需要 'deduction algorithm/system' 能够将这种依赖传递给 class 构造函数。

我在决定存储这些 'knowledge pieces' 的方式中使用了最大粒度,因为考虑到模拟的大小和计算强度以及单个实例的数量,我真的不能浪费任何内存,但现在我'm 运行 单个实例需要数千个,有时数万个组件的问题,我需要实例的 "creation-map" 既保存又仍然绑定为私有成员,这样我就可以: 1st - 知道在哪里我的推论领先于实例的构造函数,也许可以使用记忆来减少构建时间;第二 - 在模拟过程中将更改注入实时实例¹。

What I think I need: I need a possibly infinite or at least very long bit-mask, so I can iterate the creation faster and let the final component-tree of my dynamically-constructed objects logged for future use.

Possible approaches, which I have no idea will work: 1st - Manually and sequentially store values for the bit flags in each RAM cell, using the RAM wafer as my bit-mask. 2nd - Breakdown the map into smaller bit-masks of known size(hard because the final number of components is unknown until the creation is done and decoupling the deduction is something which might even be impossible without refactoring the entire system). 3rd - Figure out a way to make an infinite bit-mask, or use some library that has implemented a really long integer(5.12e+11 or bigger).

我的代码是用 C++ 编写的,我的渲染和计算内核是 Vulkan。

我的 objective 问题: 如何实现内存和计算效率高的任意长位掩码?

如果允许我提出一个额外的问题,假设我没有体系结构(软件和硬件)限制,实现这种位掩码的最有效方法是什么?

¹ 我无法在模拟期间浏览对象的树,我也不能暂停模拟并等待浏览完成后再注入修改,我需要知道并能够进行任意更改以预先安排的方式和实时方式进行的任意模拟框架。

what is the most efficient way to implement such a bit-mask

std::vector<bool> 放在那里并进行配置。 If/when 你会发现你正在花费大量时间处理该向量,请进一步阅读。

没有最有效的方法,这完全取决于您的代码对这些位的具体作用。

一种标准方法是保留整数的连续向量,并将它们视为位向量。例如,Windows 上的 std::vector<bool> 使用 32 位值,每个值包含 32 个布尔值。

但是std::vector的API太笼统了。如果你的向量有大部分 0 或大部分 1,你将能够比 std::vector<bool> 更快地实现许多操作(获取值,找到 first/next/previous 0/1)。

另外,根据您的访问模式,您可能需要更小或更大的元素。如果您决定使用大型 SIMD 类型,即 __m128i__m256i,请不要忘记它们的对齐方式。