diff CachedEditDistance.cpp @ 78:9744ec195be3

Encapsulate EditDistance with caching.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Thu, 10 Oct 2013 01:07:52 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CachedEditDistance.cpp	Thu Oct 10 01:07:52 2013 +0200
@@ -0,0 +1,33 @@
+#include "CachedEditDistance.hpp"
+
+#include "CompileTimeConstants.h"
+#include "ConfigurationProcessing.hpp"
+
+CachedEditDistance::cacheType* CachedEditDistance::cache = 0;
+//CachedEditDistance::cacheType CachedEditDistance::cache;
+
+int CachedEditDistance::Compute(QString a, QString b, bool remove) {
+  if (remove) {
+    removeDiacriticsNoCopy(a);
+    removeDiacriticsNoCopy(b);
+  }
+
+  if ( a == b)
+    return 0;
+
+  OrderedPair<UniqueString> lup(a, b);
+
+  if (cache == 0) {
+    QString cacheLocation = processSetupVariables(EDITDISTANCE_CACHE_LOCATION);
+    CachedEditDistance::cache = new cacheType(cacheLocation, "EditLUT");
+  }
+  boost::optional<int> res = cache->value(lup);
+  if (res)
+    return *res;
+
+  int retVal = EditDistance::Compute(a, b, false);
+
+  cache->insert(lup, retVal);
+
+  return retVal;
+}