如果数组全为 0(或 false),我可以检查 C(++) 吗?
我可以在 C(++) 中检查数组是否全为 0(或 false),而不迭代/循环每个值并且不分配相同大小的新数组(使用 memcmp
)?
我滥用布尔数组在运行时拥有任意大的位集并对其进行一些位翻转
Can I check in C(++) if an array is all 0 (or false) without iterating/looping over every single value and without allocating a new array of the same size (to use memcmp
)?
I'm abusing an array of bools to have arbitrary large bitsets at runtime and do some bitflipping on it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用以下条件:
显然,在内部,此循环遍历所有值。
另一种方法(实际上应该避免循环)是覆盖所有写访问函数,并跟踪
true
是否已写入向量。更新
Lie Ryan 在下面的评论中描述了一种基于相同原理的更稳健的方法。
You can use the following condition:
Obviously, internally, this loops over all values.
The alternative (which really should avoid looping) is to override all write-access functions, and keep track of whether
true
has ever been written to your vector.UPDATE
Lie Ryan's comments below describe a more robust method of doing this, based on the same principle.
如果没有排序的话,就没有。您打算如何实现这一目标?您需要检查每个元素以查看它是否为 0!当然,memcmp 也会检查每个元素。它只会更昂贵,因为它还读取另一个数组。
当然,一旦遇到非 0 元素,您就可以提前退出。
您唯一的选择是使用 SIMD(从技术上讲,它仍然检查每个元素,但使用更少的指令),但您通常不会在通用数组中这样做。
(顺便说一句,我的回答假设您有一个简单的静态 C/C++ 数组。如果您可以指定您拥有哪种数组,我们可以更具体。)
If it's not sorted, no. How would you plan on accomplishing that? You would need to inspect every element to see if it's 0 or not! memcmp, of course, would also check every element. It would just be much more expensive since it reads another array as well.
Of course, you can early-out as soon as you hit a non-0 element.
Your only option would be to use SIMD (which technically still checks every element, but using fewer instructions), but you generally don't do that in a generic array.
(Btw, my answer assumes that you have a simple static C/C++ array. If you can specify what kind of array you have, we could be more specific.)
如果您知道这将是一个要求,您可以构建一个由数组(可能是动态的)和计数或当前非零单元组成的数据结构。显然,单元格的设置必须通过抽象来实现,但这在带有重载的 C++ 中是很自然的,并且您可以在 C 中使用不透明类型。
If you know that this is going to be a requirement, you could build a data structure consisting of an array (possibly dynamic) and a count or currently non-zero cells. Obviously the setting of cells must be abstracted through, but that is natural in c++ with overloading, and you can use an opaque type in c.
假设您有一个包含 N 个元素的数组,您可以对一组基向量进行位检查。
例如,您想要测试一个包含 15 个元素的数组。
您可以针对 8 元素零数组、4 元素零数组、2 元素零数组和 1 元素零数组对其进行测试。
一旦您知道要测试的数组的最大大小,您只需分配这些元素。此外,测试可以并行完成(如果需要,可以使用内部装配)。
仅使用 8 元素数组即可进一步改进内存分配,因为 4 元素零数组只是 8 元素零数组的前半部分。
Assume that you have an array of N element, you can do a bit check against a set of base vectors.
For example, you have a 15-element array you want to test.
You can test it against an 8-element zero array, an 4-element zero array, a 2-element zero array and a 1-element zero array.
You only have to allocate these elements once given that you know the maximum size of arrays you want to test. Furthermore, the test can be done in parallel (and with assembly intrinsic if necessary).
Further improvement in term of memory allocation can be done with using only an 8-element array since a 4-element zero array is simply the first half of the 8-element zero array.
考虑使用
boost::dynamic_bitset
相反。它有一个none
成员和其他几个类似std::bitset
的操作,但它的长度可以在运行时设置。Consider using
boost::dynamic_bitset
instead. It has anone
member and several otherstd::bitset
-like operations, but its length can be set at runtime.不,您可以将数组与
memcmp
进行比较,但无法将一个值与一块内存进行比较。您可以做的是使用 C++ 中的算法,但这仍然涉及内部循环。
No, you can compare arrays with
memcmp
, but you can't compare one value against a block of memory.What you can do is use algorithms in C++ but that still involves a loop internally.
您不必迭代整个过程,只需在第一个非零值上停止循环即可。
除了依次检查每个值之外,我想不出任何方法来检查一组值 - 您可以通过检查底层内存作为大于
bool
(__int64 说)但是对齐是一个问题。
编辑:
您可以保留设置位的单独计数,并检查其是否非零。您必须小心维护这一点,以便设置一个设置位不会
++
它等等。You don't have to iterate over the entire thing, just stop looping on the first non-zero value.
I can't think of any way to check a set of values other than inspecting them each in turn - you could play games with checking the underlying memory as something larger than
bool
(__int64
say) but alignment is then an issue.EDIT:
You could keep a separate count of set bits, and check that is non-zero. You'd have to be careful about maintenance of this, so that setting a set bit did not
++
it and so on.knittl,
我不认为您可以访问目标计算机上的一些奇特的 DMA 硬件吗?有时 DMA 硬件完全支持您需要的操作,即“该内存区域是否全为零?”在处理大位缓冲区时,这种硬件加速比较是一种常见的解决方案。例如,某些 RAID 控制器使用此机制进行奇偶校验。
knittl,
I don't suppose you have access to some fancy DMA hardware on the target computer? Sometimes DMA hardware supports exactly the operation you require, i.e. "Is this region of memory all-zero?" This sort of hardware-accelerated comparison is a common solution when dealing with large bit-buffers. For example, some RAID controllers use this mechanism for parity checking.