17using DistanceArray = MultiArray<size_t, NoneOwningMultiArray, size_t, size_t>;
31void initDistanceArray(DistanceArray &distanceArray,
const size_t size1,
const size_t size2)
33 const auto maxDistance(size1 + size2);
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wnull-dereference"
39 distanceArray.at(0, 0) = maxDistance;
41#pragma GCC diagnostic pop
43 for (
size_t i = 0;
i <= size1; ++
i) {
44 distanceArray.at(
i + 1, 1) =
i;
45 distanceArray.at(
i + 1, 0) = maxDistance;
47 for (
size_t i = 0;
i <= size2; ++
i) {
48 distanceArray.at(1,
i + 1) =
i;
49 distanceArray.at(0,
i + 1) = maxDistance;
55size_t performDamerauLevenshteinAlgorithm(
56 DistanceArray &distanceArray,
const char *
const str1,
const size_t size1,
const char *
const str2,
const size_t size2)
58 size_t dist1[std::numeric_limits<unsigned char>::max() + 1] = { 0 };
59 for (
size_t index1 = 1; index1 <= size1; ++index1) {
61 for (
size_t index2 = 1; index2 <= size2; ++index2) {
62 const size_t substitution((str1[index1 - 1] == str2[index2 - 1]) ? 0 : 1);
63 const size_t transposition1(dist1[
static_cast<unsigned char>(str2[index2 - 1])]);
64 const size_t transposition2(dist2);
70 distanceArray.at(index1, index2) + substitution,
71 distanceArray.at(index1 + 1, index2) + 1,
72 distanceArray.at(index1, index2 + 1) + 1,
73 distanceArray.at(transposition1, transposition2) + (index1 - transposition1 - 1) + 1 + (index2 - transposition2 - 1)
77 dist1[
static_cast<int>(str1[index1 - 1])] = index1;
79 return distanceArray.at(size1 + 1, size2 + 1);
83template <
typename DistanceArray>
84size_t performDamerauLevenshteinAlgorithmAllocatingOnHeap(
85 DistanceArray &distanceArray,
const char *
const str1,
const size_t size1,
const char *
const str2,
const size_t size2)
87 std::vector<size_t> buffer(distanceArray.totalSize());
88 distanceArray.buffer() = buffer.data();
89 initDistanceArray(distanceArray, size1, size2);
90 return performDamerauLevenshteinAlgorithm(distanceArray, str1, size1, str2, size2);
95template <
typename DistanceArray>
96size_t performDamerauLevenshteinAlgorithmAllocatingOnStack(
97 DistanceArray &distanceArray,
const char *
const str1,
const size_t size1,
const char *
const str2,
const size_t size2)
99 size_t buffer[128] = { 0 };
100 distanceArray.buffer() = buffer;
101 initDistanceArray(distanceArray, size1, size2);
102 return performDamerauLevenshteinAlgorithm(distanceArray, str1, size1, str2, size2);
129 if (distanceArray.totalSize() <= 128) {
130 return performDamerauLevenshteinAlgorithmAllocatingOnStack(distanceArray, str1, size1, str2, size2);
132 return performDamerauLevenshteinAlgorithmAllocatingOnHeap(distanceArray, str1, size1, str2, size2);
Contains all utilities provides by the c++utilities library.
auto makeNoneOwningMultiArray(DimensionSizes... dimensionSizes)
Constructs a new N-dimensional array using a caller-managed buffer as underlying container....
constexpr T min(T first, T second)
Returns the smallest of the given items.
CPP_UTILITIES_EXPORT std::size_t computeDamerauLevenshteinDistance(const char *str1, std::size_t size1, const char *str2, std::size_t size2)