Coverage Report

Created: 2019-03-22 08:08

/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
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines a Levenshtein distance function that works for any two
10
// sequences, with each element of each sequence being analogous to a character
11
// in a string.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_ADT_EDIT_DISTANCE_H
16
#define LLVM_ADT_EDIT_DISTANCE_H
17
18
#include "llvm/ADT/ArrayRef.h"
19
#include <algorithm>
20
#include <memory>
21
22
namespace llvm {
23
24
/// Determine the edit distance between two sequences.
25
///
26
/// \param FromArray the first sequence to compare.
27
///
28
/// \param ToArray the second sequence to compare.
29
///
30
/// \param AllowReplacements whether to allow element replacements (change one
31
/// element into another) as a single operation, rather than as two operations
32
/// (an insertion and a removal).
33
///
34
/// \param MaxEditDistance If non-zero, the maximum edit distance that this
35
/// routine is allowed to compute. If the edit distance will exceed that
36
/// maximum, returns \c MaxEditDistance+1.
37
///
38
/// \returns the minimum number of element insertions, removals, or (if
39
/// \p AllowReplacements is \c true) replacements needed to transform one of
40
/// the given sequences into the other. If zero, the sequences are identical.
41
template<typename T>
42
unsigned ComputeEditDistance(ArrayRef<T> FromArray, ArrayRef<T> ToArray,
43
                             bool AllowReplacements = true,
44
1.90M
                             unsigned MaxEditDistance = 0) {
45
1.90M
  // The algorithm implemented below is the "classic"
46
1.90M
  // dynamic-programming algorithm for computing the Levenshtein
47
1.90M
  // distance, which is described here:
48
1.90M
  //
49
1.90M
  //   http://en.wikipedia.org/wiki/Levenshtein_distance
50
1.90M
  //
51
1.90M
  // Although the algorithm is typically described using an m x n
52
1.90M
  // array, only one row plus one element are used at a time, so this
53
1.90M
  // implementation just keeps one vector for the row.  To update one entry,
54
1.90M
  // only the entries to the left, top, and top-left are needed.  The left
55
1.90M
  // entry is in Row[x-1], the top entry is what's in Row[x] from the last
56
1.90M
  // iteration, and the top-left entry is stored in Previous.
57
1.90M
  typename ArrayRef<T>::size_type m = FromArray.size();
58
1.90M
  typename ArrayRef<T>::size_type n = ToArray.size();
59
1.90M
60
1.90M
  const unsigned SmallBufferSize = 64;
61
1.90M
  unsigned SmallBuffer[SmallBufferSize];
62
1.90M
  std::unique_ptr<unsigned[]> Allocated;
63
1.90M
  unsigned *Row = SmallBuffer;
64
1.90M
  if (n + 1 > SmallBufferSize) {
65
1.51k
    Row = new unsigned[n + 1];
66
1.51k
    Allocated.reset(Row);
67
1.51k
  }
68
1.90M
69
21.9M
  for (unsigned i = 1; i <= n; 
++i20.0M
)
70
20.0M
    Row[i] = i;
71
1.90M
72
12.2M
  for (typename ArrayRef<T>::size_type y = 1; y <= m; 
++y10.3M
) {
73
12.1M
    Row[0] = y;
74
12.1M
    unsigned BestThisRow = Row[0];
75
12.1M
76
12.1M
    unsigned Previous = y - 1;
77
189M
    for (typename ArrayRef<T>::size_type x = 1; x <= n; 
++x176M
) {
78
176M
      int OldRow = Row[x];
79
176M
      if (AllowReplacements) {
80
171M
        Row[x] = std::min(
81
171M
            Previous + (FromArray[y-1] == ToArray[x-1] ? 
0u9.98M
:
1u161M
),
82
171M
            std::min(Row[x-1], Row[x])+1);
83
171M
      }
84
5.56M
      else {
85
5.56M
        if (FromArray[y-1] == ToArray[x-1]) 
Row[x] = Previous353k
;
86
5.21M
        else Row[x] = std::min(Row[x-1], Row[x]) + 1;
87
5.56M
      }
88
176M
      Previous = OldRow;
89
176M
      BestThisRow = std::min(BestThisRow, Row[x]);
90
176M
    }
91
12.1M
92
12.1M
    if (MaxEditDistance && 
BestThisRow > MaxEditDistance12.1M
)
93
1.74M
      return MaxEditDistance + 1;
94
12.1M
  }
95
1.90M
96
1.90M
  unsigned Result = Row[n];
97
153k
  return Result;
98
1.90M
}
unsigned int llvm::ComputeEditDistance<char>(llvm::ArrayRef<char>, llvm::ArrayRef<char>, bool, unsigned int)
Line
Count
Source
44
1.89M
                             unsigned MaxEditDistance = 0) {
45
1.89M
  // The algorithm implemented below is the "classic"
46
1.89M
  // dynamic-programming algorithm for computing the Levenshtein
47
1.89M
  // distance, which is described here:
48
1.89M
  //
49
1.89M
  //   http://en.wikipedia.org/wiki/Levenshtein_distance
50
1.89M
  //
51
1.89M
  // Although the algorithm is typically described using an m x n
52
1.89M
  // array, only one row plus one element are used at a time, so this
53
1.89M
  // implementation just keeps one vector for the row.  To update one entry,
54
1.89M
  // only the entries to the left, top, and top-left are needed.  The left
55
1.89M
  // entry is in Row[x-1], the top entry is what's in Row[x] from the last
56
1.89M
  // iteration, and the top-left entry is stored in Previous.
57
1.89M
  typename ArrayRef<T>::size_type m = FromArray.size();
58
1.89M
  typename ArrayRef<T>::size_type n = ToArray.size();
59
1.89M
60
1.89M
  const unsigned SmallBufferSize = 64;
61
1.89M
  unsigned SmallBuffer[SmallBufferSize];
62
1.89M
  std::unique_ptr<unsigned[]> Allocated;
63
1.89M
  unsigned *Row = SmallBuffer;
64
1.89M
  if (n + 1 > SmallBufferSize) {
65
1.51k
    Row = new unsigned[n + 1];
66
1.51k
    Allocated.reset(Row);
67
1.51k
  }
68
1.89M
69
21.8M
  for (unsigned i = 1; i <= n; 
++i19.9M
)
70
19.9M
    Row[i] = i;
71
1.89M
72
12.2M
  for (typename ArrayRef<T>::size_type y = 1; y <= m; 
++y10.3M
) {
73
12.1M
    Row[0] = y;
74
12.1M
    unsigned BestThisRow = Row[0];
75
12.1M
76
12.1M
    unsigned Previous = y - 1;
77
189M
    for (typename ArrayRef<T>::size_type x = 1; x <= n; 
++x176M
) {
78
176M
      int OldRow = Row[x];
79
176M
      if (AllowReplacements) {
80
171M
        Row[x] = std::min(
81
171M
            Previous + (FromArray[y-1] == ToArray[x-1] ? 
0u9.98M
:
1u161M
),
82
171M
            std::min(Row[x-1], Row[x])+1);
83
171M
      }
84
5.56M
      else {
85
5.56M
        if (FromArray[y-1] == ToArray[x-1]) 
Row[x] = Previous353k
;
86
5.21M
        else Row[x] = std::min(Row[x-1], Row[x]) + 1;
87
5.56M
      }
88
176M
      Previous = OldRow;
89
176M
      BestThisRow = std::min(BestThisRow, Row[x]);
90
176M
    }
91
12.1M
92
12.1M
    if (MaxEditDistance && 
BestThisRow > MaxEditDistance12.1M
)
93
1.74M
      return MaxEditDistance + 1;
94
12.1M
  }
95
1.89M
96
1.89M
  unsigned Result = Row[n];
97
144k
  return Result;
98
1.89M
}
unsigned int llvm::ComputeEditDistance<clang::IdentifierInfo const*>(llvm::ArrayRef<clang::IdentifierInfo const*>, llvm::ArrayRef<clang::IdentifierInfo const*>, bool, unsigned int)
Line
Count
Source
44
9.00k
                             unsigned MaxEditDistance = 0) {
45
9.00k
  // The algorithm implemented below is the "classic"
46
9.00k
  // dynamic-programming algorithm for computing the Levenshtein
47
9.00k
  // distance, which is described here:
48
9.00k
  //
49
9.00k
  //   http://en.wikipedia.org/wiki/Levenshtein_distance
50
9.00k
  //
51
9.00k
  // Although the algorithm is typically described using an m x n
52
9.00k
  // array, only one row plus one element are used at a time, so this
53
9.00k
  // implementation just keeps one vector for the row.  To update one entry,
54
9.00k
  // only the entries to the left, top, and top-left are needed.  The left
55
9.00k
  // entry is in Row[x-1], the top entry is what's in Row[x] from the last
56
9.00k
  // iteration, and the top-left entry is stored in Previous.
57
9.00k
  typename ArrayRef<T>::size_type m = FromArray.size();
58
9.00k
  typename ArrayRef<T>::size_type n = ToArray.size();
59
9.00k
60
9.00k
  const unsigned SmallBufferSize = 64;
61
9.00k
  unsigned SmallBuffer[SmallBufferSize];
62
9.00k
  std::unique_ptr<unsigned[]> Allocated;
63
9.00k
  unsigned *Row = SmallBuffer;
64
9.00k
  if (n + 1 > SmallBufferSize) {
65
0
    Row = new unsigned[n + 1];
66
0
    Allocated.reset(Row);
67
0
  }
68
9.00k
69
23.2k
  for (unsigned i = 1; i <= n; 
++i14.2k
)
70
14.2k
    Row[i] = i;
71
9.00k
72
19.4k
  for (typename ArrayRef<T>::size_type y = 1; y <= m; 
++y10.4k
) {
73
10.4k
    Row[0] = y;
74
10.4k
    unsigned BestThisRow = Row[0];
75
10.4k
76
10.4k
    unsigned Previous = y - 1;
77
27.2k
    for (typename ArrayRef<T>::size_type x = 1; x <= n; 
++x16.8k
) {
78
16.8k
      int OldRow = Row[x];
79
16.8k
      if (AllowReplacements) {
80
16.8k
        Row[x] = std::min(
81
16.8k
            Previous + (FromArray[y-1] == ToArray[x-1] ? 
0u1.14k
:
1u15.6k
),
82
16.8k
            std::min(Row[x-1], Row[x])+1);
83
16.8k
      }
84
0
      else {
85
0
        if (FromArray[y-1] == ToArray[x-1]) Row[x] = Previous;
86
0
        else Row[x] = std::min(Row[x-1], Row[x]) + 1;
87
0
      }
88
16.8k
      Previous = OldRow;
89
16.8k
      BestThisRow = std::min(BestThisRow, Row[x]);
90
16.8k
    }
91
10.4k
92
10.4k
    if (MaxEditDistance && 
BestThisRow > MaxEditDistance0
)
93
0
      return MaxEditDistance + 1;
94
10.4k
  }
95
9.00k
96
9.00k
  unsigned Result = Row[n];
97
9.00k
  return Result;
98
9.00k
}
99
100
} // End llvm namespace
101
102
#endif