From 6cfb0e4b2bd7904c771914dd5030c54a2540e156 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Wed, 6 Jun 2018 21:37:05 +0200 Subject: Move damerauLevenshteinDistance from qt-config's Util to CRSM's own Util --- src/CRSMUtil.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ src/CRSMUtil.hpp | 6 ++++++ src/CrServerManager.pro | 6 ++++-- src/Util.cpp | 1 - src/Util.hpp | 1 - src/crsm.cpp | 2 ++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/CRSMUtil.cpp create mode 100644 src/CRSMUtil.hpp delete mode 120000 src/Util.cpp delete mode 120000 src/Util.hpp (limited to 'src') diff --git a/src/CRSMUtil.cpp b/src/CRSMUtil.cpp new file mode 100644 index 0000000..91fa057 --- /dev/null +++ b/src/CRSMUtil.cpp @@ -0,0 +1,50 @@ +#include "CRSMUtil.hpp" + +namespace Util { + int damerauLevenshteinDistance(const QString& s1, const QString& s2) + { + int length1 = s1.length(); + int length2 = s2.length(); + int d[length1+1][length2+1]; + + int i; + int j; + int cost; + + for(i = 0; i <= length1; i++) + { + d[i][0] = i; + } + for(j = 0; j <= length2; j++) + { + d[0][j] = j; + } + for (i = 0; i < length1;i++) + { + for(j = 0; j < length2; j++) + { + if(s1[i] == s2[j]) + { + cost = 0; + } + else + { + cost = 1; + } + d[i+1][j+1] = std::min({ + d[i][j+1] + 1, // delete + d[i+1][j] + 1, // insert + d[i][j] + cost // substitution + }); + if((i > 0) && (j > 0) && (s1[i] == s2[j-1]) && (s1[i-1] == s2[j])) + { + d[i+1][j+1] = std::min( + d[i+1][j+1], + d[i-1][j-1] + cost // transposition + ); + } + } + } + return d[length1][length2]; + } +} diff --git a/src/CRSMUtil.hpp b/src/CRSMUtil.hpp new file mode 100644 index 0000000..99ae916 --- /dev/null +++ b/src/CRSMUtil.hpp @@ -0,0 +1,6 @@ +#pragma once +#include + +namespace Util { + int damerauLevenshteinDistance(const QString& s1, const QString& s2); +} diff --git a/src/CrServerManager.pro b/src/CrServerManager.pro index b5a3459..be9f8c4 100644 --- a/src/CrServerManager.pro +++ b/src/CrServerManager.pro @@ -21,7 +21,8 @@ SOURCES += main.cpp \ crsm.cpp \ ProcessManager.cpp \ qt-config/ConfigBase.cpp \ - Util.cpp \ + qt-config/Util.cpp \ + CRSMUtil.cpp \ CRSMStats.cpp \ CRSMPackCompatibility.cpp \ CRSMLogging.cpp \ @@ -38,7 +39,8 @@ HEADERS += \ ProcessManager.hpp \ CRSMConfig.hpp \ qt-config/ConfigBase.hpp \ - Util.hpp \ + qt-config/Util.hpp \ + CRSMUtil.hpp \ CRSMStats.hpp \ CRSMPackCompatibility.hpp \ CRSMLogging.hpp \ diff --git a/src/Util.cpp b/src/Util.cpp deleted file mode 120000 index bbc718a..0000000 --- a/src/Util.cpp +++ /dev/null @@ -1 +0,0 @@ -qt-config/Util.cpp \ No newline at end of file diff --git a/src/Util.hpp b/src/Util.hpp deleted file mode 120000 index bcc063c..0000000 --- a/src/Util.hpp +++ /dev/null @@ -1 +0,0 @@ -qt-config/Util.hpp \ No newline at end of file diff --git a/src/crsm.cpp b/src/crsm.cpp index 1ba1d0e..023b0cf 100644 --- a/src/crsm.cpp +++ b/src/crsm.cpp @@ -9,6 +9,8 @@ #include #include +#include "CRSMUtil.hpp" + #define MGMT_BUFFER_FILENAME "CRSM-MGMT-Buffer" CRSM::CRSM(QObject *parent) : -- cgit v1.2.3-54-g00ecf