-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add a BitArray.Contains method #72999
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsBackground and motivationThe input.Cast<bool>().Contains(true); which is far from optimal. The type could really benefit from a dedicated API Proposalnamespace System.Collections;
public class BitArray
{
public bool Contains(bool value);
} API UsageBitArray value;
BitArray mask;
value.And(mask).Contains(true); Alternative DesignsNo response RisksNo response
|
I'd only recommend renaming Contains into |
Exposing an public AllBitsEqual(bool value) => !Contains(!value); and vice versa. I feel |
I had the same expectation as @krwq. (not that means much) What do other platforms/implementations name it? Also, if I was translating an algorithm would I generally be thinking in terms of "are any bits set" (in which case Contains might be what I look for) or "are all bits false"? |
Java only implements IsEmpty() -> https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html#isEmpty() The other way round is not implemented, there is a discussion around that here: https://stackoverflow.com/questions/36308666/check-if-all-bits-in-bitset-are-set-to-true |
Alternatives: bool AllBitsSet { get; }
bool AllBitsUnset => !AllBitsSet; |
We wouldn't want this as a property. Properties are typically used for things that are "as cheap as a field read" and not for some potentially expensive algorithm that needs to check every element in the collection.
While you can express it as this, I personally find comparing against a negative much harder to read/understand. Not only can There are several other cases where I really wish we had both |
FWIW If we do ship both variants, I think the names should complement each other. Following existing .NET terminology that could look as follows: public bool Any(bool value);
public bool All(bool value); |
|
Right. I meant for the case people are requesting of
|
|
Yes, you're right. Probably, these methods are subjects to different proposal. |
You'd need something like But, that's also not strictly speaking the best or optimal implementation. |
namespace System.Collections
{
public partial class BitArray
{
public bool HasAllSet();
public bool HasAnySet();
}
} |
Can you elaborate? Generally speaking "All" quantifiers return |
Heh, should've left it as my original "needs careful attention". Enumerable.All is the only "all" I know of in .NET, and it returns true on empty, so I guess true makes sense here, too. The trouble is, with an empty set, All is the same as divide by zero to a mathematician: it means "this isn't answerable in a general context, but you might have a specific interpretation to fall back on" (so, had I been the author if it, the LINQ All it would have thrown on empty). ( |
See https://en.wikipedia.org/wiki/Universal_quantification#The_empty_set By convention/definition, "forall" over empty is true and "exists" over empty is false. This makes sense if you look at inductive definitions of such quantifiers. |
|
Background and motivation
The
BitArray
class has been optimized to take advantage of hardware vectorization but it doesn't appear to have an efficient way to check if all flags within the array are either set or unset. The only suggested workaround I could find is to use Linq, i.e.which is far from optimal. The type could really benefit from a dedicated
Contains
implementation that uses vectorization.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: