Coverage Report

Created: 2025-08-26 06:57

/src/aspell/modules/speller/default/editdist.cpp
Line
Count
Source
1
2
#include <cstring>
3
4
#include "editdist.hpp"
5
#include "matrix.hpp"
6
#include "vararray.hpp"
7
8
// edit_distance is implemented using a straight forward dynamic
9
// programming algorithm with out any special tricks.  Its space
10
// usage AND running time is tightly asymptotically bounded by
11
// strlen(a)*strlen(b)
12
13
namespace aspeller {
14
15
  short edit_distance(ParmString a0, ParmString b0,
16
          const EditDistanceWeights & w) 
17
798k
  {
18
798k
    int a_size = a0.size() + 1;
19
798k
    int b_size = b0.size() + 1;
20
798k
    VARARRAY(short, e_d, a_size * b_size);
21
798k
    ShortMatrix e(a_size,b_size,e_d);
22
798k
    e(0, 0) = 0;
23
4.48M
    for (int j = 1; j != b_size; ++j)
24
3.68M
      e(0, j) = e(0, j-1) + w.del1;
25
798k
    const char * a = a0.str() - 1;
26
798k
    const char * b = b0.str() - 1;
27
798k
    short te;
28
12.1M
    for (int i = 1; i != a_size; ++i) {
29
11.3M
      e(i, 0) = e(i-1, 0) + w.del2;
30
77.2M
      for (int j = 1; j != b_size; ++j) {
31
65.8M
  if (a[i] == b[j]) {
32
33
5.11M
    e(i, j) = e(i-1, j-1);
34
35
60.7M
  } else {
36
37
60.7M
    e(i, j) = w.sub + e(i-1, j-1);
38
39
60.7M
    if (i != 1 && j != 1 && 
40
60.7M
        a[i] == b[j-1] && a[i-1] == b[j]) 
41
65.0k
      {
42
65.0k
        te = w.swap + e(i-2, j-2);
43
65.0k
        if (te < e(i, j)) e(i, j) = te;
44
65.0k
      }
45
    
46
60.7M
    te = w.del1 + e(i-1, j);
47
60.7M
    if (te < e(i, j)) e(i, j) = te;
48
60.7M
    te = w.del2 + e(i, j-1);
49
60.7M
    if (te < e(i, j)) e(i, j) = te;
50
51
60.7M
  }
52
65.8M
      } 
53
11.3M
    }
54
798k
    return e(a_size-1, b_size-1);
55
798k
  }
56
}