omw  0.2.1-beta
algorithm.h
1 /*
2 author Oliver Blaser
3 date 28.02.2023
4 copyright MIT - Copyright (c) 2023 Oliver Blaser
5 */
6 
7 #ifndef IG_OMW_ALGORITHM_H
8 #define IG_OMW_ALGORITHM_H
9 
10 #include <algorithm>
11 #include <cstddef>
12 #include <cstdint>
13 #include <cstring>
14 #include <string>
15 #include <vector>
16 
17 #include "../omw/int.h"
18 
19 namespace omw
20 {
25  std::vector<uint8_t> doubleDabble128(uint32_t valueHH, uint32_t valueLH, uint32_t valueHL, uint32_t valueLL);
28  std::vector<uint8_t> doubleDabble128(uint64_t valueH, uint64_t valueL);
29 
30  std::vector<uint8_t> doubleDabble(const omw::uint128_t& value);
32 
35  template <typename T>
36  inline size_t levenshteinDistance(const T* a, size_t aCount, const T* b, size_t bCount)
37  {
38  size_t r;
39 
40  if (a && b)
41  {
42  std::vector<std::vector<size_t>> m(aCount + 1, std::vector<size_t>(bCount + 1, 0));
43 
44  for (size_t j = 1; j < m[0].size(); ++j) m[0][j] = j;
45 
46  for (size_t i = 1; i < m.size(); ++i)
47  {
48  m[i][0] = i;
49 
50  for (size_t j = 1; j < m[i].size(); ++j)
51  {
52  const size_t sub = m[i - 1][j - 1] + (*(a + i - 1) == *(b + j - 1) ? 0 : 1); // substitution
53  const size_t ins = m[i][j - 1] + 1; // insertion
54  const size_t del = m[i - 1][j] + 1; // deletion
55 
56  m[i][j] = std::min(std::min(sub, ins), del);
57  }
58  }
59 
60  r = m[aCount][bCount];
61  }
62  else r = SIZE_MAX;
63 
64  return r;
65  }
66 
67  inline size_t levenshteinDistance(const char* a, const char* b)
68  {
69  size_t r;
70  if (a && b) r = levenshteinDistance(a, std::strlen(a), b, std::strlen(b));
71  else r = SIZE_MAX;
72  return r;
73  }
74 
75  inline size_t levenshteinDistance(const std::string& a, const std::string& b) { return levenshteinDistance(a.data(), a.size(), b.data(), b.size()); }
76 
77  template <typename T>
78  inline size_t levenshteinDistance(const std::vector<T>& a, const std::vector<T>& b)
79  {
80  size_t r;
81 
82  if (a.empty() && b.empty()) r = 0;
83  else if (a.empty()) r = levenshteinDistance<T>(b.data(), 0, b.data(), b.size());
84  else if (b.empty()) r = levenshteinDistance<T>(a.data(), a.size(), a.data(), 0);
85  else r = levenshteinDistance<T>(a.data(), a.size(), b.data(), b.size());
86 
87  return r;
88  }
90 
92 }
93 
94 #endif // IG_OMW_ALGORITHM_H
std::string
C++ standard string. See std::basic_string.
Definition: linkToStd.dox:19
omw::UnsignedInt128
Definition: int.h:105
omw
Main namespace.