Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Implement Span LastIndexOf extension method and add tests#25748

Merged
ahsonkhan merged 10 commits into
dotnet:masterfrom
ahsonkhan:LastIndexOf
Dec 7, 2017
Merged

Implement Span LastIndexOf extension method and add tests#25748
ahsonkhan merged 10 commits into
dotnet:masterfrom
ahsonkhan:LastIndexOf

Conversation

@ahsonkhan
Copy link
Copy Markdown

@ahsonkhan ahsonkhan commented Dec 6, 2017

Part of https://github.com/dotnet/corefx/issues/24839

Following IndexOf vectorization from this PR: #17143

Left to do: LastIndexOfAny APIs

Also did a lot of cleaning and code formatting.

cc @benaadams, @KrzysztofCwalina, @GrabYourPitchforks, @jkotas, @stephentoub

public static int IndexOfAny(this ReadOnlySpan<byte> span, byte value0, byte value1, byte value2) { throw null; }
public static int IndexOfAny(this ReadOnlySpan<byte> span, ReadOnlySpan<byte> values) { throw null; }

public static int LastIndexOf<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T> { throw null; }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have LastIndexOfAny for parity?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I will add those in a later PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you file an issue? Thanks.

int valueTailLength = valueLength - 1;

int index = 0;
for (;;)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if span is "abaaaaaaaaaaa..." and I search for "ab", it will do lots of loops. Maybe there is nothing we can do.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same would apply to something like "...aaaaaaaaab" and you search for "ab" when you call IndexOf (which is where I borrowed the template for LastIndexOf). There may be a different approaches that balance things better but I haven't thought too deeply about it yet since it would require a bigger change (to all our IndexOf APIs).


using Xunit;

namespace System.SpanTests
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding tests that use a reference type T implementing IEquatable. I think all the current tests would work regardless of the code using IEquatable or simply comparing bits in the structs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for {RO}Span<string>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's better than just testing the structs, but unfortunately String.Equals(string) does ordinal comparison, i.e. compares bits in the string :-). Maybe it's ok though.

Also, I just realized we should probably add LastIndexOf(T, Comparer) to let people chose whatever string comparison they want (not this PR of course).

@ahsonkhan
Copy link
Copy Markdown
Author

Is this good to merge?

@KrzysztofCwalina
Copy link
Copy Markdown
Member

It looks good to me.

Also, thanks for all the cleanup, though next time it might be better to have two PRs: one for cleanup and one for the feature.

@ahsonkhan ahsonkhan merged commit d219378 into dotnet:master Dec 7, 2017
@ahsonkhan ahsonkhan deleted the LastIndexOf branch December 7, 2017 18:56
@karelz karelz added this to the 2.1.0 milestone Dec 28, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…efx#25748)

* Implement Span LastIndexOf and add tests

* Add LastIndexOfSequence tests and fix implementation

* Vectorize LastIndexOf<byte> similar to IndexOf<byte>

* Add LastIndexOf performance tests

* Adding IndexOf and LastIndexOf tests for reference type (string).

* Use abbreviated version of 'default' literal

* Remove unnecessary type specifiers in generic function calls.

* Cleanup and format all files in the solution to follow coding style.

* Cleaning up leftover unused using directives and extra spaces


Commit migrated from dotnet/corefx@d219378
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants