Coverage Report

Created: 2018-07-12 09:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/ADT/edit_distance.h
Line
Count
Source
1
//===-- llvm/ADT/edit_distance.h - Array edit distance function --- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file defines a Levenshtein distance function that works for any two
11
// sequences, with each element of each sequence being analogous to a character
12
// in a string.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#ifndef LLVM_ADT_EDIT_DISTANCE_H
17
#define LLVM_ADT_EDIT_DISTANCE_H
18
19
#include "llvm/ADT/ArrayRef.h"
20
#include <algorithm>
21
#include <memory>
22
23
namespace llvm {
24
25
/// Determine the edit distance between two sequences.
26
///
27
/// \param FromArray the first sequence to compare.
28
///
29
/// \param ToArray the second sequence to compare.
30
///
31
/// \param AllowReplacements whether to allow element replacements (change one
32
/// element into another) as a single operation, rather than as two operations
33
/// (an insertion and a removal).
34
///
35
/// \param MaxEditDistance If non-zero, the maximum edit distance that this
36
/// routine is allowed to compute. If the edit distance will exceed that
37
/// maximum, returns \c MaxEditDistance+1.
38
///
39
/// \returns the minimum number of element insertions, removals, or (if
40
/// \p AllowReplacements is \c true) replacements needed to transform one of
41
/// the given sequences into the other. If zero, the sequences are identical.
42
template<typename T>
43
unsigned ComputeEditDistance(ArrayRef<T> FromArray, ArrayRef<T> ToArray,
44
                             bool AllowReplacements = true,
45
1.60M
                             unsigned MaxEditDistance = 0) {
46
1.60M
  // The algorithm implemented below is the "classic"
47
1.60M
  // dynamic-programming algorithm for computing the Levenshtein
48
1.60M
  // distance, which is described here:
49
1.60M
  //
50
1.60M
  //   http://en.wikipedia.org/wiki/Levenshtein_distance
51
1.60M
  //
52
1.60M
  // Although the algorithm is typically described using an m x n
53
1.60M
  // array, only one row plus one element are used at a time, so this
54
1.60M
  // implementation just keeps one vector for the row.  To update one entry,
55
1.60M
  // only the entries to the left, top, and top-left are needed.  The left
56
1.60M
  // entry is in Row[x-1], the top entry is what's in Row[x] from the last
57
1.60M
  // iteration, and the top-left entry is stored in Previous.
58
1.60M
  typename ArrayRef<T>::size_type m = FromArray.size();
59
1.60M
  typename ArrayRef<T>::size_type n = ToArray.size();
60
1.60M
61
1.60M
  const unsigned SmallBufferSize = 64;
62
1.60M
  unsigned SmallBuffer[SmallBufferSize];
63
1.60M
  std::unique_ptr<unsigned[]> Allocated;
64
1.60M
  unsigned *Row = SmallBuffer;
65
1.60M
  if (n + 1 > SmallBufferSize) {
66
10
    Row = new unsigned[n + 1];
67
10
    Allocated.reset(Row);
68
10
  }
69
1.60M
70
19.1M
  for (unsigned i = 1; i <= n; 
++i17.5M
)
71
17.5M
    Row[i] = i;
72
1.60M
73
11.8M
  for (typename ArrayRef<T>::size_type y = 1; y <= m; 
++y10.2M
) {
74
11.7M
    Row[0] = y;
75
11.7M
    unsigned BestThisRow = Row[0];
76
11.7M
77
11.7M
    unsigned Previous = y - 1;
78
182M
    for (typename ArrayRef<T>::size_type x = 1; x <= n; 
++x171M
) {
79
171M
      int OldRow = Row[x];
80
171M
      if (AllowReplacements) {
81
168M
        Row[x] = std::min(
82
168M
            Previous + (FromArray[y-1] == ToArray[x-1] ? 
0u9.71M
:
1u158M
),
83
168M
            std::min(Row[x-1], Row[x])+1);
84
168M
      }
85
2.84M
      else {
86
2.84M
        if (FromArray[y-1] == ToArray[x-1]) 
Row[x] = Previous191k
;
87
2.65M
        else Row[x] = std::min(Row[x-1], Row[x]) + 1;
88
2.84M
      }
89
171M
      Previous = OldRow;
90
171M
      BestThisRow = std::min(BestThisRow, Row[x]);
91
171M
    }
92
11.7M
93
11.7M
    if (MaxEditDistance && 
BestThisRow > MaxEditDistance11.7M
)
94
1.42M
      return MaxEditDistance + 1;
95
11.7M
  }
96
1.60M
97
1.60M
  unsigned Result = Row[n];
98
173k
  return Result;
99
1.60M
}
unsigned int llvm::ComputeEditDistance<char>(llvm::ArrayRef<char>, llvm::ArrayRef<char>, bool, unsigned int)
Line
Count
Source
45
1.59M
                             unsigned MaxEditDistance = 0) {
46
1.59M
  // The algorithm implemented below is the "classic"
47
1.59M
  // dynamic-programming algorithm for computing the Levenshtein
48
1.59M
  // distance, which is described here:
49
1.59M
  //
50
1.59M
  //   http://en.wikipedia.org/wiki/Levenshtein_distance
51
1.59M
  //
52
1.59M
  // Although the algorithm is typically described using an m x n
53
1.59M
  // array, only one row plus one element are used at a time, so this
54
1.59M
  // implementation just keeps one vector for the row.  To update one entry,
55
1.59M
  // only the entries to the left, top, and top-left are needed.  The left
56
1.59M
  // entry is in Row[x-1], the top entry is what's in Row[x] from the last
57
1.59M
  // iteration, and the top-left entry is stored in Previous.
58
1.59M
  typename ArrayRef<T>::size_type m = FromArray.size();
59
1.59M
  typename ArrayRef<T>::size_type n = ToArray.size();
60
1.59M
61
1.59M
  const unsigned SmallBufferSize = 64;
62
1.59M
  unsigned SmallBuffer[SmallBufferSize];
63
1.59M
  std::unique_ptr<unsigned[]> Allocated;
64
1.59M
  unsigned *Row = SmallBuffer;
65
1.59M
  if (n + 1 > SmallBufferSize) {
66
10
    Row = new unsigned[n + 1];
67
10
    Allocated.reset(Row);
68
10
  }
69
1.59M
70
19.1M
  for (unsigned i = 1; i <= n; 
++i17.5M
)
71
17.5M
    Row[i] = i;
72
1.59M
73
11.8M
  for (typename ArrayRef<T>::size_type y = 1; y <= m; 
++y10.2M
) {
74
11.7M
    Row[0] = y;
75
11.7M
    unsigned BestThisRow = Row[0];
76
11.7M
77
11.7M
    unsigned Previous = y - 1;
78
182M
    for (typename ArrayRef<T>::size_type x = 1; x <= n; 
++x170M
) {
79
170M
      int OldRow = Row[x];
80
170M
      if (AllowReplacements) {
81
168M
        Row[x] = std::min(
82
168M
            Previous + (FromArray[y-1] == ToArray[x-1] ? 
0u9.70M
:
1u158M
),
83
168M
            std::min(Row[x-1], Row[x])+1);
84
168M
      }
85
2.84M
      else {
86
2.84M
        if (FromArray[y-1] == ToArray[x-1]) 
Row[x] = Previous191k
;
87
2.65M
        else Row[x] = std::min(Row[x-1], Row[x]) + 1;
88
2.84M
      }
89
170M
      Previous = OldRow;
90
170M
      BestThisRow = std::min(BestThisRow, Row[x]);
91
170M
    }
92
11.7M
93
11.7M
    if (MaxEditDistance && 
BestThisRow > MaxEditDistance11.7M
)
94
1.42M
      return MaxEditDistance + 1;
95
11.7M
  }
96
1.59M
97
1.59M
  unsigned Result = Row[n];
98
164k
  return Result;
99
1.59M
}
unsigned int llvm::ComputeEditDistance<clang::IdentifierInfo const*>(llvm::ArrayRef<clang::IdentifierInfo const*>, llvm::ArrayRef<clang::IdentifierInfo const*>, bool, unsigned int)
Line
Count
Source
45
8.78k
                             unsigned MaxEditDistance = 0) {
46
8.78k
  // The algorithm implemented below is the "classic"
47
8.78k
  // dynamic-programming algorithm for computing the Levenshtein
48
8.78k
  // distance, which is described here:
49
8.78k
  //
50
8.78k
  //   http://en.wikipedia.org/wiki/Levenshtein_distance
51
8.78k
  //
52
8.78k
  // Although the algorithm is typically described using an m x n
53
8.78k
  // array, only one row plus one element are used at a time, so this
54
8.78k
  // implementation just keeps one vector for the row.  To update one entry,
55
8.78k
  // only the entries to the left, top, and top-left are needed.  The left
56
8.78k
  // entry is in Row[x-1], the top entry is what's in Row[x] from the last
57
8.78k
  // iteration, and the top-left entry is stored in Previous.
58
8.78k
  typename ArrayRef<T>::size_type m = FromArray.size();
59
8.78k
  typename ArrayRef<T>::size_type n = ToArray.size();
60
8.78k
61
8.78k
  const unsigned SmallBufferSize = 64;
62
8.78k
  unsigned SmallBuffer[SmallBufferSize];
63
8.78k
  std::unique_ptr<unsigned[]> Allocated;
64
8.78k
  unsigned *Row = SmallBuffer;
65
8.78k
  if (n + 1 > SmallBufferSize) {
66
0
    Row = new unsigned[n + 1];
67
0
    Allocated.reset(Row);
68
0
  }
69
8.78k
70
22.6k
  for (unsigned i = 1; i <= n; 
++i13.8k
)
71
13.8k
    Row[i] = i;
72
8.78k
73
19.0k
  for (typename ArrayRef<T>::size_type y = 1; y <= m; 
++y10.2k
) {
74
10.2k
    Row[0] = y;
75
10.2k
    unsigned BestThisRow = Row[0];
76
10.2k
77
10.2k
    unsigned Previous = y - 1;
78
26.6k
    for (typename ArrayRef<T>::size_type x = 1; x <= n; 
++x16.4k
) {
79
16.4k
      int OldRow = Row[x];
80
16.4k
      if (AllowReplacements) {
81
16.4k
        Row[x] = std::min(
82
16.4k
            Previous + (FromArray[y-1] == ToArray[x-1] ? 
0u1.13k
:
1u15.3k
),
83
16.4k
            std::min(Row[x-1], Row[x])+1);
84
16.4k
      }
85
0
      else {
86
0
        if (FromArray[y-1] == ToArray[x-1]) Row[x] = Previous;
87
0
        else Row[x] = std::min(Row[x-1], Row[x]) + 1;
88
0
      }
89
16.4k
      Previous = OldRow;
90
16.4k
      BestThisRow = std::min(BestThisRow, Row[x]);
91
16.4k
    }
92
10.2k
93
10.2k
    if (MaxEditDistance && 
BestThisRow > MaxEditDistance0
)
94
0
      return MaxEditDistance + 1;
95
10.2k
  }
96
8.78k
97
8.78k
  unsigned Result = Row[n];
98
8.78k
  return Result;
99
8.78k
}
100
101
} // End llvm namespace
102
103
#endif