Skip to content

Commit d2da104

Browse files
committed
ci/cd workflow fixes and algorithm improvements
1 parent 43530e0 commit d2da104

4 files changed

Lines changed: 75 additions & 79 deletions

File tree

src/cli/wagner_fischer.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "wagner_fischer.hpp"
2+
3+
#include <iostream>
4+
5+
u8 wagner_fischer(const std::string& a_input, const std::string& b_input) {
6+
std::string a = a_input;
7+
std::string b = b_input;
8+
9+
u8 a_length = static_cast<u8>(a.length());
10+
u8 b_length = static_cast<u8>(b.length());
11+
12+
if (a_length > b_length) {
13+
std::swap(a, b);
14+
std::swap(a_length, b_length);
15+
}
16+
17+
std::vector<u8> curr_row(a_length + 1);
18+
std::vector<u8> prev_row(a_length + 1);
19+
20+
for (u8 j = 0; j <= a_length; ++j) {
21+
prev_row[j] = j;
22+
}
23+
24+
for (u8 i = 1; i <= b_length; ++i) {
25+
curr_row[0] = i;
26+
27+
for (u8 j = 1; j <= a_length; ++j) {
28+
u8 add = prev_row[j] + 1;
29+
u8 del = curr_row[j - 1] + 1;
30+
u8 change = prev_row[j - 1];
31+
32+
if (a[j - 1] != b[i - 1]) {
33+
change += 1;
34+
}
35+
36+
curr_row[j] = std::min({add, del, change});
37+
}
38+
39+
std::swap(prev_row, curr_row);
40+
}
41+
42+
return prev_row[a_length];
43+
}
44+
45+
void manage_output(const std::vector<std::string>& suggestions) {
46+
if (suggestions.empty()) {
47+
return;
48+
}
49+
50+
std::cerr << "Did you mean: \"";
51+
52+
for (std::size_t i = 0; i < suggestions.size(); ++i) {
53+
if (i > 0) {
54+
std::cerr << ", ";
55+
}
56+
std::cerr << bold << suggestions.at(i) << ansi_exit;
57+
}
58+
59+
std::cerr << "\"?\n";
60+
}

src/cli/wagner_fischer.hpp

Lines changed: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,25 @@
1+
#pragma once
2+
13
#include "globals.hpp"
24
#include "types.hpp"
35

4-
56
#include <string>
6-
#include <string_view>
77
#include <vector>
8+
#include <array>
89
#include <algorithm>
9-
#include <iostream>
10-
11-
using sv = std::string_view;
1210

1311
// https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
14-
// this function is used for argument corrections in CLI tools.
12+
// this function is used for argument corrections in CLI tools.
1513
// The performance might be questionable, but it's nothing critical.
1614
// It's only used for finding unrecognised arguments anyway, it's
17-
// a debug utility for crashes.
18-
u8 wagner_fischer(
19-
sv a,
20-
sv b
21-
) {
22-
u8 a_length = static_cast<u8>(a.length());
23-
u8 b_length = static_cast<u8>(b.length());
24-
25-
if (a_length > b_length) {
26-
std::swap(a, b);
27-
std::swap(a_length, b_length);
28-
}
29-
30-
std::vector<u8> curr_row(a_length + 1);
31-
std::vector<u8> prev_row(a_length + 1);
32-
33-
curr_row.reserve(std::max(a_length, b_length));
34-
prev_row.reserve(std::max(a_length, b_length));
35-
36-
for (u8 j = 0; j <= a_length; ++j) {
37-
prev_row[j] = j;
38-
}
39-
40-
for (u8 i = 1; i <= b_length; ++i) {
41-
curr_row[0] = i;
42-
43-
for (u8 j = 1; j <= a_length; ++j) {
44-
u8 add = prev_row[j] + 1;
45-
u8 del = curr_row[j - 1] + 1;
46-
u8 change = prev_row[j - 1];
47-
48-
if (a[j - 1] != b[i - 1]) {
49-
change += 1;
50-
}
51-
52-
curr_row[j] = std::min({add, del, change});
53-
}
54-
55-
std::swap(prev_row, curr_row);
56-
}
57-
58-
return prev_row[a_length];
59-
}
60-
15+
// a debug utility for crashes.
16+
u8 wagner_fischer(const std::string& a_input, const std::string& b_input);
17+
void manage_output(const std::vector<std::string>& suggestions);
6118

6219
template <std::size_t N>
6320
std::vector<std::string> suggest(
64-
const sv misspelled_word,
65-
const std::array<std::pair<const char*, arg_enum>, N> &dictionary
21+
const std::string& misspelled_word,
22+
const std::array<std::pair<const char*, arg_enum>, N>& dictionary
6623
) {
6724
std::vector<std::pair<u8, std::string>> candidates;
6825
candidates.reserve(N);
@@ -75,38 +32,19 @@ std::vector<std::string> suggest(
7532
}
7633

7734
std::sort(
78-
candidates.begin(),
35+
candidates.begin(),
7936
candidates.end(),
80-
[](const auto& a, const auto& b) {
37+
[](const std::pair<u8, std::string>& a, const std::pair<u8, std::string>& b) {
8138
return a.first < b.first;
8239
}
8340
);
8441

85-
std::vector<std::string> suggestions = {};
42+
std::vector<std::string> suggestions;
8643
suggestions.reserve(candidates.size());
8744

88-
for (const auto& [distance, word] : candidates) {
89-
suggestions.push_back(word);
45+
for (const auto& candidate : candidates) {
46+
suggestions.push_back(candidate.second); // word
9047
}
9148

9249
return suggestions;
93-
}
94-
95-
96-
void manage_output(const std::vector<std::string>& suggestions) {
97-
if (suggestions.empty()) {
98-
return;
99-
}
100-
101-
std::cerr << "Did you mean: \"";
102-
103-
for (std::size_t i = 0; i < suggestions.size(); ++i) {
104-
if (i > 0) {
105-
std::cerr << ", ";
106-
}
107-
std::cerr << bold + suggestions.at(i);
108-
std::cerr << ansi_exit;
109-
}
110-
111-
std::cerr << "\"?\n";
11250
}

src/cli/windows_cli.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "types.hpp"
12

23
#if (CLI_WINDOWS)
34
#include "globals.hpp"

src/cli/windows_cli.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#pragma once
22

3-
43
#include <iostream>
54
#include <sstream>
65

7-
86
#if (CLI_WINDOWS)
9-
107
#define WIN32_LEAN_AND_MEAN
118
#define NOMINMAX
129
#include <windows.h>

0 commit comments

Comments
 (0)