Skip to content

Commit

Permalink
Enhance fuzzy matcher performance
Browse files Browse the repository at this point in the history
  • Loading branch information
funap committed Dec 22, 2024
1 parent 5545174 commit 42fcb4f
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 135 deletions.
224 changes: 113 additions & 111 deletions src/Explorer/FuzzyMatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,113 +1,75 @@
/*
The MIT License (MIT)
Copyright (c) 2019 funap
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// The MIT License (MIT)
//
// Copyright (c) 2019-2024 funap
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "FuzzyMatcher.h"

#include <cwctype>
#include <memory>
#include <algorithm>

FuzzyMatcher::FuzzyMatcher(std::wstring_view pattern)
: pattern_(pattern)
{
}

FuzzyMatcher::~FuzzyMatcher()
{
}

int FuzzyMatcher::ScoreMatch(std::wstring_view target, std::vector<size_t>* positions)
{
if (0 == pattern_.length()) {
return 0;
}
if (0 == target.length()) {
return 0;
}
if (pattern_.length() > target.length()) {
return 0;
namespace {
struct ScoringConstants {
static constexpr int CHARACTER_MATCH_BONUS = 1;
static constexpr int SAME_CASE_BONUS = 1;
static constexpr int FIRST_LETTER_BONUS = 8;
static constexpr int CONSECUTIVE_MATCH_BONUS = 5;
static constexpr int START_OF_EXTENSION_BONUS = 3;
static constexpr int CAMEL_CASE_BONUS = 4;
static constexpr int SEPARATOR_BONUS = 4;
static constexpr int DIRECTORY_SEPARATOR_BONUS = 5;
};

bool ValidateInputs(const std::wstring_view& pattern, const std::wstring_view& target)
{
return !(pattern.empty() || target.empty() || pattern.length() > target.length());
}

auto scoreMatrix = std::make_unique<int[]>(pattern_.length() * target.length());
auto matcheMatrix = std::make_unique<int[]>(pattern_.length() * target.length());
void RestoreMatchPositions(std::vector<size_t>* positions,
const int* matchMatrix,
size_t patternLength,
size_t targetLength)
{
if (!positions) return;

for (size_t patternIndex = 0; patternIndex < pattern_.length(); ++patternIndex) {
const bool patternIsFirstIndex = (0 == patternIndex);
const size_t patternIndexOffset = patternIndex * target.length();
const size_t patternIndexPreviousOffset = patternIndexOffset - target.length();

for (size_t targetIndex = 0; targetIndex < target.length(); ++targetIndex) {
const bool targetIsFirstIndex = (0 == targetIndex);
const size_t currentIndex = patternIndexOffset + targetIndex;
const size_t leftIndex = currentIndex - 1;
const size_t diagIndex = patternIndexPreviousOffset + (targetIndex - 1);

const int leftScore = targetIsFirstIndex ? 0 : scoreMatrix[leftIndex];
const int diagScore = (patternIsFirstIndex || targetIsFirstIndex) ? 0 : scoreMatrix[diagIndex];
const int matchesSequenceLength = (patternIsFirstIndex || targetIsFirstIndex) ? 0 : matcheMatrix[diagIndex];

int score;
if (!diagScore && !patternIsFirstIndex) {
score = 0;
}
else {
score = CalculateScore(pattern_[patternIndex], target, targetIndex, matchesSequenceLength);
}

if (score && (leftScore <= diagScore + score)) {
matcheMatrix[currentIndex] = matchesSequenceLength + 1;
scoreMatrix[currentIndex] = diagScore + score;
}
else {
matcheMatrix[currentIndex] = 0;
scoreMatrix[currentIndex] = leftScore;
}
}
}
const int result = scoreMatrix[pattern_.length() * target.length() - 1];
size_t patternIndex = patternLength - 1;
size_t targetIndex = targetLength - 1;

// Restore Positions
if (positions) {
size_t patternIndex = pattern_.length() - 1;
size_t targetIndex = target.length() - 1;
while ((0 <= patternIndex) && (0 <= targetIndex)) {
const size_t currentIndex = patternIndex * target.length() + targetIndex;
const int match = matcheMatrix[currentIndex];
const size_t currentIndex = patternIndex * targetLength + targetIndex;
const int match = matchMatrix[currentIndex];

if (0 == match) {
if (0 < targetIndex) {
--targetIndex; // go left
--targetIndex;
}
else {
break;
}
}
else {
positions->emplace_back(targetIndex);

if ((0 < patternIndex) && (0 < targetIndex)) {
--patternIndex;
--targetIndex; // go up and left
--targetIndex;
}
else {
break;
Expand All @@ -116,63 +78,103 @@ int FuzzyMatcher::ScoreMatch(std::wstring_view target, std::vector<size_t>* posi
}
std::reverse(positions->begin(), positions->end());
}
} // namespace

return result;
FuzzyMatcher::FuzzyMatcher(std::wstring_view pattern)
: pattern_(pattern)
, scoreMatrix_()
, matchMatrix_()
{
}

FuzzyMatcher::~FuzzyMatcher() = default;

int FuzzyMatcher::CalculateScore(wchar_t patternChar, const std::wstring_view &target, size_t targetIndex, int matchesSequenceLength)
int FuzzyMatcher::ScoreMatch(std::wstring_view target, std::vector<size_t>* positions)
{
int score = 0;
if (!ValidateInputs(pattern_, target)) {
return 0;
}

constexpr int CHARACTER_MATCH_BONUS = 1;
constexpr int SAME_CASE_BONUS = 1;
constexpr int FIRST_LETTER_BONUS = 8;
constexpr int CONSECUTIVE_MATCH_BONUS = 5;
constexpr int START_OF_EXTENSION_BONUS = 3;
constexpr int CAMEL_CASE_BONUS = 4;
constexpr int SEPARATOR_BONUS = 4;
constexpr int DIRECTORY_SEPARATOR_BONUS = 5;
scoreMatrix_.resize(pattern_.length() * target.length());
matchMatrix_.resize(pattern_.length() * target.length());
for (size_t patternIndex = 0; patternIndex < pattern_.length(); ++patternIndex) {
const bool patternIsFirstIndex = (0 == patternIndex);
const size_t patternIndexOffset = patternIndex * target.length();
const size_t patternIndexPreviousOffset = patternIndexOffset - target.length();

for (size_t targetIndex = 0; targetIndex < target.length(); ++targetIndex) {
const bool targetIsFirstIndex = (0 == targetIndex);
const size_t currentIndex = patternIndexOffset + targetIndex;
const size_t leftIndex = currentIndex - 1;
const size_t diagIndex = patternIndexPreviousOffset + (targetIndex - 1);

const int leftScore = targetIsFirstIndex ? 0 : scoreMatrix_[leftIndex];
const int diagScore = (patternIsFirstIndex || targetIsFirstIndex) ? 0 : scoreMatrix_[diagIndex];
const int matchesSequenceLength = (patternIsFirstIndex || targetIsFirstIndex) ? 0 : matchMatrix_[diagIndex];

const int score = (!diagScore && !patternIsFirstIndex)
? 0
: CalculateScore(pattern_[patternIndex], target, targetIndex, matchesSequenceLength);

if (score && (leftScore <= diagScore + score)) {
matchMatrix_[currentIndex] = matchesSequenceLength + 1;
scoreMatrix_[currentIndex] = diagScore + score;
}
else {
matchMatrix_[currentIndex] = 0;
scoreMatrix_[currentIndex] = leftScore;
}
}
}

const int result = scoreMatrix_[pattern_.length() * target.length() - 1];
RestoreMatchPositions(positions, matchMatrix_.data(), pattern_.length(), target.length());
return result;
}

int FuzzyMatcher::CalculateScore(wchar_t patternChar, const std::wstring_view& target, size_t targetIndex, int matchesSequenceLength)
{
int score = 0;

const wchar_t patternLowerChar = std::towlower(patternChar);
const wchar_t targetLowerChar = std::towlower(target[targetIndex]);

if (patternLowerChar != targetLowerChar) {
return score; // no match
return score;
}
score += CHARACTER_MATCH_BONUS;

score += ScoringConstants::CHARACTER_MATCH_BONUS;

if (0 < matchesSequenceLength) {
score += (matchesSequenceLength * CONSECUTIVE_MATCH_BONUS);
score += (matchesSequenceLength * ScoringConstants::CONSECUTIVE_MATCH_BONUS);
}

if (patternChar == target[targetIndex]) {
score += SAME_CASE_BONUS;
score += ScoringConstants::SAME_CASE_BONUS;
}

if (0 == targetIndex) {
score += FIRST_LETTER_BONUS;
score += ScoringConstants::FIRST_LETTER_BONUS;
}
else {
switch (target[targetIndex - 1]) {
case '\\':
score += DIRECTORY_SEPARATOR_BONUS;
score += ScoringConstants::DIRECTORY_SEPARATOR_BONUS;
break;
case ' ':
case '_':
score += SEPARATOR_BONUS;
score += ScoringConstants::SEPARATOR_BONUS;
break;
case '.':
score += START_OF_EXTENSION_BONUS;
score += ScoringConstants::START_OF_EXTENSION_BONUS;
break;
default:
if (std::iswlower(target[targetIndex - 1])) {
if (std::iswupper(target[targetIndex])) {
score += CAMEL_CASE_BONUS;
}
if (std::iswlower(target[targetIndex - 1]) && std::iswupper(target[targetIndex])) {
score += ScoringConstants::CAMEL_CASE_BONUS;
}
break;
}
}

return score;
};
}
52 changes: 28 additions & 24 deletions src/Explorer/FuzzyMatcher.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*
The MIT License (MIT)
Copyright (c) 2019 funap
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// The MIT License (MIT)
//
// Copyright (c) 2019-2024 funap
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#pragma once
#ifndef FUZZY_MATCHER_H_
#define FUZZY_MATCHER_H_

#include <memory>
#include <string_view>
#include <vector>

Expand All @@ -37,4 +37,8 @@ class FuzzyMatcher
private:
int CalculateScore(wchar_t patternChar, const std::wstring_view& target, size_t targetIndex, int matchesSequenceLength);
std::wstring_view pattern_;
std::vector<int> scoreMatrix_;
std::vector<int> matchMatrix_;
};

#endif // FUZZY_MATCHER_H_

0 comments on commit 42fcb4f

Please sign in to comment.