Introduced utilities to gradually replace utils. (#2651)
* Replace current levenshtein with much faster implementation. * Remove usage of alias to simplify code for VS 2013.
This commit is contained in:
@@ -379,28 +379,3 @@ unsigned int get_percentage(const unsigned int A, const unsigned int B)
|
||||
|
||||
return (unsigned int)floor(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the Levenshtein distance of two strings.
|
||||
* @author http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C
|
||||
*/
|
||||
int levenshtein(const char *s1, const char *s2) {
|
||||
unsigned int s1len, s2len, x, y, lastdiag, olddiag, i;
|
||||
unsigned int *column;
|
||||
s1len = strlen(s1);
|
||||
s2len = strlen(s2);
|
||||
column = malloc((s1len+1) * sizeof(unsigned int));
|
||||
for (y = 1; y <= s1len; y++)
|
||||
column[y] = y;
|
||||
for (x = 1; x <= s2len; x++) {
|
||||
column[0] = x;
|
||||
for (y = 1, lastdiag = x-1; y <= s1len; y++) {
|
||||
olddiag = column[y];
|
||||
column[y] = min(min(column[y] + 1, column[y-1] + 1), lastdiag + (s1[y-1] == s2[x-1] ? 0 : 1));
|
||||
lastdiag = olddiag;
|
||||
}
|
||||
}
|
||||
i = column[s1len];
|
||||
free(column);
|
||||
return i;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user