Mercurial > dedupe
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 77:a827f3687c4a | 78:9744ec195be3 |
|---|---|
| 1 #include "CachedEditDistance.hpp" | |
| 2 | |
| 3 #include "CompileTimeConstants.h" | |
| 4 #include "ConfigurationProcessing.hpp" | |
| 5 | |
| 6 CachedEditDistance::cacheType* CachedEditDistance::cache = 0; | |
| 7 //CachedEditDistance::cacheType CachedEditDistance::cache; | |
| 8 | |
| 9 int CachedEditDistance::Compute(QString a, QString b, bool remove) { | |
| 10 if (remove) { | |
| 11 removeDiacriticsNoCopy(a); | |
| 12 removeDiacriticsNoCopy(b); | |
| 13 } | |
| 14 | |
| 15 if ( a == b) | |
| 16 return 0; | |
| 17 | |
| 18 OrderedPair<UniqueString> lup(a, b); | |
| 19 | |
| 20 if (cache == 0) { | |
| 21 QString cacheLocation = processSetupVariables(EDITDISTANCE_CACHE_LOCATION); | |
| 22 CachedEditDistance::cache = new cacheType(cacheLocation, "EditLUT"); | |
| 23 } | |
| 24 boost::optional<int> res = cache->value(lup); | |
| 25 if (res) | |
| 26 return *res; | |
| 27 | |
| 28 int retVal = EditDistance::Compute(a, b, false); | |
| 29 | |
| 30 cache->insert(lup, retVal); | |
| 31 | |
| 32 return retVal; | |
| 33 } |
