LLVM: include/llvm/ADT/edit_distance.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef LLVM_ADT_EDIT_DISTANCE_H
17#define LLVM_ADT_EDIT_DISTANCE_H
18
20#include
21
22namespace llvm {
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44template <typename T, typename Functor>
46 Functor Map, bool AllowReplacements = true,
47 unsigned MaxEditDistance = 0) {
48
49
50
51
52
53
54
55
56
57
58
59
62
63 if (MaxEditDistance) {
64
65
66
68 if (AbsDiff > MaxEditDistance)
69 return MaxEditDistance + 1;
70 }
71
73 for (unsigned i = 1; i < Row.size(); ++i)
74 Row[i] = i;
75
77 Row[0] = y;
78 unsigned BestThisRow = Row[0];
79
80 unsigned Previous = y - 1;
81 const auto &CurItem = Map(FromArray[y - 1]);
83 int OldRow = Row[x];
84 if (AllowReplacements) {
85 Row[x] = std::min(Previous + (CurItem == Map(ToArray[x - 1]) ? 0u : 1u),
86 std::min(Row[x - 1], Row[x]) + 1);
87 }
88 else {
89 if (CurItem == Map(ToArray[x - 1]))
90 Row[x] = Previous;
91 else Row[x] = std::min(Row[x-1], Row[x]) + 1;
92 }
93 Previous = OldRow;
94 BestThisRow = std::min(BestThisRow, Row[x]);
95 }
96
97 if (MaxEditDistance && BestThisRow > MaxEditDistance)
98 return MaxEditDistance + 1;
99 }
100
101 unsigned Result = Row[n];
102 return Result;
103}
104
105template
107 bool AllowReplacements = true,
108 unsigned MaxEditDistance = 0) {
110 FromArray, ToArray, [](const T &X) -> const T & { return X; },
111 AllowReplacements, MaxEditDistance);
112}
113
114}
115
116#endif
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.
unsigned ComputeEditDistance(ArrayRef< T > FromArray, ArrayRef< T > ToArray, bool AllowReplacements=true, unsigned MaxEditDistance=0)
Definition edit_distance.h:106
unsigned ComputeMappedEditDistance(ArrayRef< T > FromArray, ArrayRef< T > ToArray, Functor Map, bool AllowReplacements=true, unsigned MaxEditDistance=0)
Determine the edit distance between two sequences.
Definition edit_distance.h:45