Browse Source

[add] Memory optim
[add] some tests

isundil 8 years ago
parent
commit
4feb43fc47
2 changed files with 85 additions and 16 deletions
  1. 49 16
      levenshtein.hpp
  2. 36 0
      test.cpp

+ 49 - 16
levenshtein.hpp

@@ -3,27 +3,60 @@
 #include <utility>
 #include <queue>
 
+unsigned int levenshtein_get(unsigned int * const *map, int px, int py)
+{
+    if (px == -1 && py == -1)
+        return 0;
+    if (px == -1)
+        return py +1;
+    if (py == -1)
+        return px +1;
+    return map[px][py];
+}
+
 template <class T, typename SIZE=unsigned int>
-int levenshtein(const T &a, const T &b, const SIZE aSize, const SIZE bSize)
+unsigned int levenshtein(const T *a, const T *b, const SIZE aSize, const SIZE bSize)
 {
-    int **items = new int*[aSize +1]();
+    unsigned int **items = new unsigned int*[aSize]();
 
-    for (SIZE i =0; i <= aSize; i++)
+    for (SIZE i =0; i < aSize; i++)
     {
-        items[i] = new int[bSize +1]();
-        items[i][0] = i;
-        if (i == 0)
-            for (SIZE j =1; j <= bSize; j++)
-                items[i][j] = j;
-        else
-            for (SIZE j =1; j <= bSize; j++)
-                items[i][j] = std::min(std::min(
-                            items[i][j -1] +1,
-                            items[i -1][j] +1),
-                            (items[i -1][j -1] + (a[i -1] == b[j -1] ? 0 : 1)));
+        items[i] = new unsigned int[bSize]();
+        for (SIZE j =0; j < bSize; j++)
+        {
+            unsigned int add = levenshtein_get(items, i, j -1) +1;
+            unsigned int del = levenshtein_get(items, i -1, j) +1;
+            unsigned int mod = levenshtein_get(items, i -1, j -1) +(a[i] == b[j] ? 0 : 1);
+
+            items[i][j] = std::min(std::min(add, del), mod);
+        }
+    }
+    const unsigned int levenshtein = items[aSize -1][bSize -1];
+    for (SIZE i =0; i < aSize; i++)
+        delete[] items[i];
+    delete[] items;
+    return levenshtein;
+}
+
+template <class T, typename SIZE=unsigned int>
+unsigned int levenshtein(const T &a, const T &b, const SIZE aSize, const SIZE bSize)
+{
+    unsigned int **items = new unsigned int*[aSize]();
+
+    for (SIZE i =0; i < aSize; i++)
+    {
+        items[i] = new unsigned int[bSize]();
+        for (SIZE j =0; j < bSize; j++)
+        {
+            unsigned int add = levenshtein_get(items, i, j -1) +1;
+            unsigned int del = levenshtein_get(items, i -1, j) +1;
+            unsigned int mod = levenshtein_get(items, i -1, j -1) +(a[i] == b[j] ? 0 : 1);
+
+            items[i][j] = std::min(std::min(add, del), mod);
+        }
     }
-    const int levenshtein = items[aSize][bSize];
-    for (SIZE i =0; i < aSize +1; i++)
+    const unsigned int levenshtein = items[aSize -1][bSize -1];
+    for (SIZE i =0; i < aSize; i++)
         delete[] items[i];
     delete[] items;
     return levenshtein;

+ 36 - 0
test.cpp

@@ -0,0 +1,36 @@
+#include <iostream>
+#include "levenshtein.hpp"
+
+#define FAILED { std::cerr << "test failed at " << __LINE__ << std::endl; exit(EXIT_FAILURE); }
+
+int main()
+{
+    if (levenshtein("abcdef", "abcdef", 6, 6) != 0)
+        FAILED;
+    if (levenshtein("abcdef", "abcdf", 6, 5) != 1)
+        FAILED;
+    if (levenshtein("abcdf", "abcdef", 5, 6) != 1)
+        FAILED;
+    if (levenshtein("abcdf", "abcef", 5, 5) != 1)
+        FAILED;
+    if (levenshtein("abcf", "abdcf", 4, 5) != 1)
+        FAILED;
+    if (levenshtein("lorem", "liroem", 5, 6) != 2)
+        FAILED;
+
+    if (levenshtein<std::string, int>("abcdef", "abcdef", 6, 6) != 0)
+        FAILED;
+    if (levenshtein<std::string, int>("abcdef", "abcdf", 6, 5) != 1)
+        FAILED;
+    if (levenshtein<std::string, int>("abcdf", "abcdef", 5, 6) != 1)
+        FAILED;
+    if (levenshtein<std::string, int>("abcdf", "abcef", 5, 5) != 1)
+        FAILED;
+    if (levenshtein<std::string, int>("abcf", "abdcf", 4, 5) != 1)
+        FAILED;
+    if (levenshtein<std::string, int>("lorem", "liroem", 5, 6) != 2)
+        FAILED;
+
+    std::cout << "success" << std::endl;
+}
+