ArkScript
A small, fast, functional and scripting language for video games
Utils.cpp
Go to the documentation of this file.
1#include <Ark/Utils.hpp>
2
3namespace Ark::Utils
4{
5 int levenshteinDistance(const std::string& str1, const std::string& str2)
6 {
7 const std::size_t str1_len = str1.size();
8 const std::size_t str2_len = str2.size();
9 std::vector edit_distances(str1_len + 1, std::vector<int>(str2_len + 1, 0));
10
11 for (std::size_t i = 0; i < str1_len + 1; i++)
12 edit_distances[i][0] = i;
13
14 for (std::size_t j = 0; j < str2_len + 1; j++)
15 edit_distances[0][j] = j;
16
17 for (std::size_t i = 1; i < str1_len + 1; i++)
18 {
19 for (std::size_t j = 1; j < str2_len + 1; j++)
20 {
21 const int indicator = str1[i - 1] == str2[j - 1] ? 0 : 1;
22 edit_distances[i][j] = std::min({
23 edit_distances[i - 1][j] + 1, // deletion
24 edit_distances[i][j - 1] + 1, // insertion
25 edit_distances[i - 1][j - 1] + indicator // substitution
26 });
27 }
28 }
29
30 return edit_distances[str1_len][str2_len];
31 }
32}
Lots of utilities about string, filesystem and more.
int levenshteinDistance(const std::string &str1, const std::string &str2)
Calculate the Levenshtein distance between two strings.
Definition: Utils.cpp:5