Can I check in C(++) if an array is all 0 (or false)?

后端 未结 8 588
执念已碎
执念已碎 2020-12-21 01:34

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 memc

相关标签:
8条回答
  • 2020-12-21 01:34

    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.

    0 讨论(0)
  • 2020-12-21 01:35

    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.

    0 讨论(0)
  • 2020-12-21 01:38

    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.

    0 讨论(0)
  • 2020-12-21 01:41

    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.)

    0 讨论(0)
  • 2020-12-21 01:43

    You can use the following condition:

    (myvector.end() == std::find(myvector.begin(), myvector.end(), true))
    

    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 讨论(0)
  • 2020-12-21 01:45

    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.

    0 讨论(0)
提交回复
热议问题