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 decPlaces(double d)
6 {
7 constexpr double precision = 1e-7;
8 double temp = 0.0;
9 int decimal_places = 0;
10
11 do
12 {
13 d *= 10;
14 temp = d - static_cast<long>(d);
15 decimal_places++;
16 } while (temp > precision && decimal_places < std::numeric_limits<double>::digits10);
17
18 return decimal_places;
19 }
20
21 int digPlaces(double d)
22 {
23 int digit_places = 0;
24 int i = static_cast<int>(d);
25 while (i != 0)
26 {
27 digit_places++;
28 i /= 10;
29 }
30 return digit_places;
31 }
32
33 int levenshteinDistance(const std::string& str1, const std::string& str2)
34 {
35 std::size_t str1_len = str1.size();
36 std::size_t str2_len = str2.size();
37 std::vector<std::vector<int>> edit_distances(str1_len + 1, std::vector<int>(str2_len + 1, 0));
38
39 for (std::size_t i = 0; i < str1_len + 1; i++)
40 edit_distances[i][0] = i;
41
42 for (std::size_t j = 0; j < str2_len + 1; j++)
43 edit_distances[0][j] = j;
44
45 for (std::size_t i = 1; i < str1_len + 1; i++)
46 {
47 for (std::size_t j = 1; j < str2_len + 1; j++)
48 {
49 int indicator = str1[i - 1] == str2[j - 1] ? 0 : 1;
50 edit_distances[i][j] = std::min({
51 edit_distances[i - 1][j] + 1, // deletion
52 edit_distances[i][j - 1] + 1, // insertion
53 edit_distances[i - 1][j - 1] + indicator // substitution
54 });
55 }
56 }
57
58 return edit_distances[str1_len][str2_len];
59 }
60}
Lots of utilities about string, filesystem and more.
int decPlaces(double d)
Count the number of decimals for a double.
Definition: Utils.cpp:5
int digPlaces(double d)
Count the number of digits for a double.
Definition: Utils.cpp:21
int levenshteinDistance(const std::string &str1, const std::string &str2)
Calculate the Levenshtein distance between two strings.
Definition: Utils.cpp:33