xref: /OK3568_Linux_fs/kernel/tools/perf/util/levenshtein.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include "levenshtein.h"
3*4882a593Smuzhiyun #include <errno.h>
4*4882a593Smuzhiyun #include <stdlib.h>
5*4882a593Smuzhiyun #include <string.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun /*
8*4882a593Smuzhiyun  * This function implements the Damerau-Levenshtein algorithm to
9*4882a593Smuzhiyun  * calculate a distance between strings.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Basically, it says how many letters need to be swapped, substituted,
12*4882a593Smuzhiyun  * deleted from, or added to string1, at least, to get string2.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The idea is to build a distance matrix for the substrings of both
15*4882a593Smuzhiyun  * strings.  To avoid a large space complexity, only the last three rows
16*4882a593Smuzhiyun  * are kept in memory (if swaps had the same or higher cost as one deletion
17*4882a593Smuzhiyun  * plus one insertion, only two rows would be needed).
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * At any stage, "i + 1" denotes the length of the current substring of
20*4882a593Smuzhiyun  * string1 that the distance is calculated for.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * row2 holds the current row, row1 the previous row (i.e. for the substring
23*4882a593Smuzhiyun  * of string1 of length "i"), and row0 the row before that.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * In other words, at the start of the big loop, row2[j + 1] contains the
26*4882a593Smuzhiyun  * Damerau-Levenshtein distance between the substring of string1 of length
27*4882a593Smuzhiyun  * "i" and the substring of string2 of length "j + 1".
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * All the big loop does is determine the partial minimum-cost paths.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * It does so by calculating the costs of the path ending in characters
32*4882a593Smuzhiyun  * i (in string1) and j (in string2), respectively, given that the last
33*4882a593Smuzhiyun  * operation is a substition, a swap, a deletion, or an insertion.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * This implementation allows the costs to be weighted:
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * - w (as in "sWap")
38*4882a593Smuzhiyun  * - s (as in "Substitution")
39*4882a593Smuzhiyun  * - a (for insertion, AKA "Add")
40*4882a593Smuzhiyun  * - d (as in "Deletion")
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * Note that this algorithm calculates a distance _iff_ d == a.
43*4882a593Smuzhiyun  */
levenshtein(const char * string1,const char * string2,int w,int s,int a,int d)44*4882a593Smuzhiyun int levenshtein(const char *string1, const char *string2,
45*4882a593Smuzhiyun 		int w, int s, int a, int d)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	int len1 = strlen(string1), len2 = strlen(string2);
48*4882a593Smuzhiyun 	int *row0 = malloc(sizeof(int) * (len2 + 1));
49*4882a593Smuzhiyun 	int *row1 = malloc(sizeof(int) * (len2 + 1));
50*4882a593Smuzhiyun 	int *row2 = malloc(sizeof(int) * (len2 + 1));
51*4882a593Smuzhiyun 	int i, j;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	for (j = 0; j <= len2; j++)
54*4882a593Smuzhiyun 		row1[j] = j * a;
55*4882a593Smuzhiyun 	for (i = 0; i < len1; i++) {
56*4882a593Smuzhiyun 		int *dummy;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 		row2[0] = (i + 1) * d;
59*4882a593Smuzhiyun 		for (j = 0; j < len2; j++) {
60*4882a593Smuzhiyun 			/* substitution */
61*4882a593Smuzhiyun 			row2[j + 1] = row1[j] + s * (string1[i] != string2[j]);
62*4882a593Smuzhiyun 			/* swap */
63*4882a593Smuzhiyun 			if (i > 0 && j > 0 && string1[i - 1] == string2[j] &&
64*4882a593Smuzhiyun 					string1[i] == string2[j - 1] &&
65*4882a593Smuzhiyun 					row2[j + 1] > row0[j - 1] + w)
66*4882a593Smuzhiyun 				row2[j + 1] = row0[j - 1] + w;
67*4882a593Smuzhiyun 			/* deletion */
68*4882a593Smuzhiyun 			if (row2[j + 1] > row1[j + 1] + d)
69*4882a593Smuzhiyun 				row2[j + 1] = row1[j + 1] + d;
70*4882a593Smuzhiyun 			/* insertion */
71*4882a593Smuzhiyun 			if (row2[j + 1] > row2[j] + a)
72*4882a593Smuzhiyun 				row2[j + 1] = row2[j] + a;
73*4882a593Smuzhiyun 		}
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 		dummy = row0;
76*4882a593Smuzhiyun 		row0 = row1;
77*4882a593Smuzhiyun 		row1 = row2;
78*4882a593Smuzhiyun 		row2 = dummy;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	i = row1[len2];
82*4882a593Smuzhiyun 	free(row0);
83*4882a593Smuzhiyun 	free(row1);
84*4882a593Smuzhiyun 	free(row2);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	return i;
87*4882a593Smuzhiyun }
88