-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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]: IndexOfAnyValues<T>.Contains(T) #78722
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationWhen updating existing code to use the new private const string Needle = ",[]&*+";
private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(Needle);
if (Needle.Contains(c))
{
// ...
} As private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(",[]&*+");
if (s_needle.Contains(c))
{
// ...
} API Proposalnamespace System.Buffers;
public class IndexOfAnyValues<T> where T : IEquatable<T>?
{
public bool Contains(T value);
} API UsageInstead of private const string Needle = ",[]&*+";
private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(Needle);
if (Needle.Contains(c))
{
// ...
} you can do private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(",[]&*+");
if (s_needle.Contains(c))
{
// ...
} Alternative DesignsNo response RisksNo response
|
cc: @stephentoub |
Should we have a different type for this? Searching for a single match in a fixed set seems to be very different operation from searching for a fixed set match in a sequence. |
We could, though the few examples I've seen so far have been related enough that I think it makes sense to have this on the same type (plus, the instance has also likely already precomputed the state to do the single check quickly due to needing it for scalar IndexOfAny operations). Examples have been of the form: int pos = data.IndexOfAny(s_values);
if (pos < 0)
return data;
data = data.Slice(pos);
foreach (char c in data)
{
if (s_values.Contains(c))
Process(c);
} so basically using the values for both a fast path and a slow path. |
Another example where this API would be really useful: improving A few other cases:
|
namespace System.Buffers;
public class IndexOfAnyValues<T> where T : IEquatable<T>?
{
public bool Contains(T value);
} |
Background and motivation
Augmenting the recently-added
IndexOfAnyValues
APIs: #68328 (comment)When updating existing code to use the new
IndexOfAnyValues
API, it is very common to replace the existing needle constant with theIndexOfAnyValues
instance.This is fine if the only operations using said needle were
{Last}IndexOfAny{Except}
calls, but it is not uncommon to also use that needle for single-value checksNeedle.Contains(c)
. In those cases, you are forced into keeping the original needle around.As
IndexOfAnyValues
is also already a representation of values optimized for searching, it can be more efficient than a full linear scan of the needle.API Proposal
API Usage
Instead of
you can do
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: