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

Modify BKTree.Lookup to pass the threshold to GetEditDistance when there will not be a need for the actual edit distance #70567

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,48 @@ private void Lookup(
return;
}

// We always want to compute the real edit distance (ignoring any thresholds). This is
// because we need that edit distance to appropriately determine which edges to walk
// We may need to compute the real edit distance (ignoring any thresholds) in the case
// where edges exist as we need that edit distance to appropriately determine which edges to walk
// in the tree.
var characterSpan = currentNode.WordSpan;

// The GetEditDistance call below serves two purposes:
// 1) To determine whether currentNode should be added to the result
// 2) To determine whether children need to be searched
//
// If there are no edges, we don't need the information to determine case 2. So, as a
// performance optimization, in that case we send in a threshold to GetEditDistance
// that indicates only the work necessary to determine case 1 need be performed.
var edgesExist = currentNode.EdgeCount > 0;
var editDistance = EditDistance.GetEditDistance(
_concatenatedLowerCaseWords.AsSpan(characterSpan.Start, characterSpan.Length),
queryCharacters.AsSpan(0, queryLength));
queryCharacters.AsSpan(0, queryLength),
edgesExist ? int.MaxValue : threshold);

// Case 1
if (editDistance <= threshold)
{
// Found a match.
result.Add(new string(_concatenatedLowerCaseWords, characterSpan.Start, characterSpan.Length));
}

var min = editDistance - threshold;
var max = editDistance + threshold;

var startInclusive = currentNode.FirstEdgeIndex;
var endExclusive = startInclusive + currentNode.EdgeCount;
for (var i = startInclusive; i < endExclusive; i++)
// Case 2
if (edgesExist)
{
var childEditDistance = _edges[i].EditDistance;
if (min <= childEditDistance && childEditDistance <= max)
var min = editDistance - threshold;
var max = editDistance + threshold;

var startInclusive = currentNode.FirstEdgeIndex;
var endExclusive = startInclusive + currentNode.EdgeCount;
for (var i = startInclusive; i < endExclusive; i++)
{
Lookup(_nodes[_edges[i].ChildNodeIndex],
queryCharacters, queryLength, threshold, ref result,
recursionCount + 1);
var childEditDistance = _edges[i].EditDistance;
if (min <= childEditDistance && childEditDistance <= max)
{
Lookup(_nodes[_edges[i].ChildNodeIndex],
queryCharacters, queryLength, threshold, ref result,
recursionCount + 1);
}
}
}
}
Expand Down
Loading