Skip to content
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

Optimize StemmerUtil for ReadOnlySpan<char>/Span<char>, #1140 #1144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 49 additions & 41 deletions src/Lucene.Net.Analysis.Common/Analysis/Util/StemmerUtil.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Lucene version compatibility level 4.8.1
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using System;
using System.Diagnostics;

namespace Lucene.Net.Analysis.Util
{
Expand All @@ -25,7 +23,7 @@ namespace Lucene.Net.Analysis.Util

/// <summary>
/// Some commonly-used stemming functions
///
///
/// @lucene.internal
/// </summary>
public static class StemmerUtil // LUCENENET specific: CA1052 Static holder types should be Static or NotInheritable
Expand All @@ -37,21 +35,34 @@ public static class StemmerUtil // LUCENENET specific: CA1052 Static holder type
/// <param name="len"> length of input buffer </param>
/// <param name="prefix"> Prefix string to test </param>
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
public static bool StartsWith(char[] s, int len, string prefix)
/// <remarks>
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
/// </remarks>
public static bool StartsWith(ReadOnlySpan<char> s, int len, string prefix)
{
return StartsWith(s, len, prefix.AsSpan());
}

/// <summary>
/// Returns true if the character array starts with the prefix.
/// </summary>
/// <param name="s"> Input Buffer </param>
/// <param name="len"> length of input buffer </param>
/// <param name="prefix"> Prefix string to test </param>
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
/// <remarks>
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
/// </remarks>
public static bool StartsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char> prefix)
{
int prefixLen = prefix.Length;
if (prefixLen > len)
{
return false;
}
for (int i = 0; i < prefixLen; i++)
{
if (s[i] != prefix[i])
{
return false;
}
}
return true;

// LUCENENET: use more efficient implementation in MemoryExtensions
return s.StartsWith(prefix, StringComparison.Ordinal);
}

/// <summary>
Expand All @@ -61,22 +72,12 @@ public static bool StartsWith(char[] s, int len, string prefix)
/// <param name="len"> length of input buffer </param>
/// <param name="suffix"> Suffix string to test </param>
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
public static bool EndsWith(char[] s, int len, string suffix)
/// <remarks>
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
/// </remarks>
public static bool EndsWith(ReadOnlySpan<char> s, int len, string suffix)
{
int suffixLen = suffix.Length;
if (suffixLen > len)
{
return false;
}
for (int i = suffixLen - 1; i >= 0; i--)
{
if (s[len - (suffixLen - i)] != suffix[i])
{
return false;
}
}

return true;
return EndsWith(s, len, suffix.AsSpan());
}

/// <summary>
Expand All @@ -86,37 +87,40 @@ public static bool EndsWith(char[] s, int len, string suffix)
/// <param name="len"> length of input buffer </param>
/// <param name="suffix"> Suffix string to test </param>
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
public static bool EndsWith(char[] s, int len, char[] suffix)
/// <remarks>
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
/// </remarks>
public static bool EndsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char> suffix)
{
int suffixLen = suffix.Length;
if (suffixLen > len)
{
return false;
}
for (int i = suffixLen - 1; i >= 0; i--)
{
if (s[len - (suffixLen - i)] != suffix[i])
{
return false;
}
}

return true;
// LUCENENET: use more efficient implementation in MemoryExtensions
return s.Slice(0, len).EndsWith(suffix, StringComparison.Ordinal);
}

// LUCENENET NOTE: char[] overload of EndsWith removed because the ReadOnlySpan<char> overload can be used instead

/// <summary>
/// Delete a character in-place
/// </summary>
/// <param name="s"> Input Buffer </param>
/// <param name="pos"> Position of character to delete </param>
/// <param name="len"> length of input buffer </param>
/// <returns> length of input buffer after deletion </returns>
public static int Delete(char[] s, int pos, int len)
/// <remarks>
/// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
/// </remarks>
public static int Delete(Span<char> s, int pos, int len)
{
if (Debugging.AssertsEnabled) Debugging.Assert(pos < len);
if (pos < len - 1) // don't arraycopy if asked to delete last character
{
Arrays.Copy(s, pos + 1, s, pos, len - pos - 1);
// Arrays.Copy(s, pos + 1, s, pos, len - pos - 1);
s.Slice(pos + 1, len - pos - 1).CopyTo(s.Slice(pos, len - pos - 1));
}
return len - 1;
}
Expand All @@ -129,14 +133,18 @@ public static int Delete(char[] s, int pos, int len)
/// <param name="len"> Length of input buffer </param>
/// <param name="nChars"> number of characters to delete </param>
/// <returns> length of input buffer after deletion </returns>
public static int DeleteN(char[] s, int pos, int len, int nChars)
/// <remarks>
/// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
/// </remarks>
public static int DeleteN(Span<char> s, int pos, int len, int nChars)
{
if (Debugging.AssertsEnabled) Debugging.Assert(pos + nChars <= len);
if (pos + nChars < len) // don't arraycopy if asked to delete the last characters
{
Arrays.Copy(s, pos + nChars, s, pos, len - pos - nChars);
// Arrays.Copy(s, pos + nChars, s, pos, len - pos - nChars);
s.Slice(pos + nChars, len - pos - nChars).CopyTo(s.Slice(pos, len - pos - nChars));
}
return len - nChars;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Lucene.Net.Attributes;
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using Assert = Lucene.Net.TestFramework.Assert;

namespace Lucene.Net.Analysis.Util
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <summary>
/// Tests for <see cref="StemmerUtil"/>
/// </summary>
[TestFixture]
[LuceneNetSpecific]
public class TestStemmerUtil : LuceneTestCase
{
[Test]
[TestCase("foobar", 6, "foo", true)]
[TestCase("foobar", 3, "foo", true)]
[TestCase("foobar", 6, "bar", false)]
[TestCase("foobar", 2, "foo", false)]
public void TestStartsWith(string input, int len, string prefix, bool expected)
{
Assert.AreEqual(expected, StemmerUtil.StartsWith(input.AsSpan(), len, prefix));
}

[Test]
[TestCase("foobar", 6, "bar", true)]
[TestCase("foobar", 3, "bar", false)]
[TestCase("foobar", 6, "foo", false)]
[TestCase("foobar", 2, "bar", false)]
[TestCase("foobar", 3, "foo", true)]
public void TestEndsWith(string input, int len, string prefix, bool expected)
{
Assert.AreEqual(expected, StemmerUtil.EndsWith(input.AsSpan(), len, prefix));
}

[Test]
[TestCase("foobar", 3, 6, "fooar", 5)]
[TestCase("foobar", 0, 6, "oobar", 5)]
[TestCase("foobar", 0, 3, "oo", 2)]
[TestCase("foobar", 5, 6, "fooba", 5)]
public void TestDelete(string input, int pos, int len, string expected, int expectedLen)
{
char[] buffer = input.ToCharArray();
Assert.AreEqual(expectedLen, StemmerUtil.Delete(buffer, pos, len));
Assert.AreEqual(expected, new string(buffer, 0, expectedLen));
}

[Test]
[TestCase("foobar", 3, 6, 2, "foor", 4)]
[TestCase("foobar", 0, 6, 2, "obar", 4)]
[TestCase("foobar", 0, 3, 2, "o", 1)]
[TestCase("foobar", 4, 6, 2, "foob", 4)]
public void TestDeleteN(string input, int pos, int len, int nChars, string expected, int expectedLen)
{
char[] buffer = input.ToCharArray();
Assert.AreEqual(expectedLen, StemmerUtil.DeleteN(buffer, pos, len, nChars));
Assert.AreEqual(expected, new string(buffer, 0, expectedLen));
}
}
}