1
1

levenshteinMatrice.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include "levenshtein.hpp"
  3. enum eLevenshteinOperator: char
  4. {
  5. add = '+',
  6. rem = '-',
  7. mod = '!',
  8. equ = '='
  9. };
  10. class LevenshteinMatrice_base
  11. {
  12. public:
  13. virtual ~LevenshteinMatrice_base() {}
  14. };
  15. template<typename T>
  16. class LevenshteinMatrice: public LevenshteinMatrice_base
  17. {
  18. public:
  19. LevenshteinMatrice(size_t n, size_t m)
  20. {
  21. this->n = n;
  22. this->m = m;
  23. this->matrice = new T*[n +1]();
  24. this->subMatrice = new LevenshteinMatrice_base**[n +1]();
  25. matrice[0] = new T[m +1];
  26. for (size_t i =1; i <= m; ++i)
  27. matrice[0][i] = i;
  28. for (size_t i=1; i <= n; ++i)
  29. {
  30. matrice[i] = new T[m +1];
  31. matrice[i][0] = i;
  32. }
  33. for (size_t i=0; i <= n; ++i)
  34. {
  35. subMatrice[i] = new LevenshteinMatrice_base*[m +1];
  36. for (size_t j=0; j <= m; ++j)
  37. subMatrice[i][j] = nullptr;
  38. }
  39. };
  40. ~LevenshteinMatrice()
  41. {
  42. for (size_t i=0; i <= n; ++i)
  43. {
  44. delete []matrice[i];
  45. for (size_t j=0; j <= m; ++j)
  46. if (subMatrice[i][j])
  47. delete subMatrice[i][j];
  48. delete []subMatrice[i];
  49. }
  50. delete []matrice;
  51. delete []subMatrice;
  52. };
  53. void prune()
  54. {
  55. //TODO
  56. }
  57. T get(size_t a, size_t b) const
  58. {
  59. return matrice[a][b];
  60. };
  61. void set(size_t a, size_t b, T value, LevenshteinMatrice_base *subMatrice =nullptr)
  62. {
  63. matrice[a][b] = value;
  64. this->subMatrice[a][b] = subMatrice;
  65. };
  66. std::list<eLevenshteinOperator> shortestPath() const
  67. {
  68. std::list<eLevenshteinOperator> result;
  69. size_t i = n;
  70. size_t j = m;
  71. while (i || j)
  72. {
  73. if (i && (!j || matrice[i][j] > matrice[i-1][j]))
  74. {
  75. result.push_front(eLevenshteinOperator::add);
  76. --i;
  77. }
  78. else if (j && (!i || matrice[i][j] > matrice[i][j -1]))
  79. {
  80. result.push_front(eLevenshteinOperator::rem);
  81. --j;
  82. }
  83. else if (i && j)
  84. {
  85. result.push_front(matrice[i][j] == matrice[i-1][j-1] ? eLevenshteinOperator::equ : eLevenshteinOperator::mod);
  86. --i;
  87. --j;
  88. }
  89. else if (i)
  90. {
  91. result.push_front(eLevenshteinOperator::add);
  92. --i;
  93. }
  94. else if (j)
  95. {
  96. result.push_front(eLevenshteinOperator::rem);
  97. --j;
  98. }
  99. }
  100. return result;
  101. }
  102. T result() const
  103. {
  104. return matrice[n][m];
  105. };
  106. private:
  107. T **matrice;
  108. LevenshteinMatrice_base ***subMatrice;
  109. size_t n;
  110. size_t m;
  111. };