Coverage Report

Created: 2026-03-26 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
1.13M
  {
18
1.13M
    int a_size = a0.size() + 1;
19
1.13M
    int b_size = b0.size() + 1;
20
1.13M
    VARARRAY(short, e_d, a_size * b_size);
21
1.13M
    ShortMatrix e(a_size,b_size,e_d);
22
1.13M
    e(0, 0) = 0;
23
6.13M
    for (int j = 1; j != b_size; ++j)
24
4.99M
      e(0, j) = e(0, j-1) + w.del1;
25
1.13M
    const char * a = a0.str() - 1;
26
1.13M
    const char * b = b0.str() - 1;
27
1.13M
    short te;
28
13.4M
    for (int i = 1; i != a_size; ++i) {
29
12.3M
      e(i, 0) = e(i-1, 0) + w.del2;
30
80.2M
      for (int j = 1; j != b_size; ++j) {
31
67.9M
  if (a[i] == b[j]) {
32
33
3.66M
    e(i, j) = e(i-1, j-1);
34
35
64.2M
  } else {
36
37
64.2M
    e(i, j) = w.sub + e(i-1, j-1);
38
39
64.2M
    if (i != 1 && j != 1 && 
40
48.9M
        a[i] == b[j-1] && a[i-1] == b[j]) 
41
83.9k
      {
42
83.9k
        te = w.swap + e(i-2, j-2);
43
83.9k
        if (te < e(i, j)) e(i, j) = te;
44
83.9k
      }
45
    
46
64.2M
    te = w.del1 + e(i-1, j);
47
64.2M
    if (te < e(i, j)) e(i, j) = te;
48
64.2M
    te = w.del2 + e(i, j-1);
49
64.2M
    if (te < e(i, j)) e(i, j) = te;
50
51
64.2M
  }
52
67.9M
      } 
53
12.3M
    }
54
1.13M
    return e(a_size-1, b_size-1);
55
1.13M
  }
56
}