Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6c3b231

Browse files
committed
Re-architect the way openleetcode.py communicates with the Solution binary
1 parent 8ffeef1 commit 6c3b231

File tree

14 files changed

+397
-169
lines changed

14 files changed

+397
-169
lines changed

‎.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ build/
4545
*.vcxproj.filters
4646
*.sln
4747
problem_builds/
48-
.vscode/
48+
.vscode/
49+
50+
# Imported modules
51+
nlohmann/

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ This project requires the following to run:
9292

9393
- Python
9494
- CMake 3.12
95+
- Git
9596

9697
## Contributing
9798
Feel free to contribute with code, test cases, or even code reviews.

‎data/languages/cpp/CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
cmake_minimum_required(VERSION 3.12)
22

33
set(CMAKE_CXX_STANDARD 20)
4-
set(CMAKE_CXX_STANDARD_REQUIRED True)
4+
set(CMAKE_CXX_STANDARD_REQUIRED True)
5+
set(JSON_BuildTests OFF CACHE INTERNAL "")
56

67
project(solution
78
DESCRIPTION "Open Source version of LeetCode"
89
LANGUAGES CXX
9-
)
10+
)
11+
12+
include(FetchContent)
13+
14+
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
15+
FetchContent_MakeAvailable(json)
1016

1117
# Set the build type explicitly
1218
set(CMAKE_BUILD_TYPE Release)
@@ -15,7 +21,8 @@ add_executable(solution_cpp)
1521

1622
target_sources(solution_cpp PRIVATE
1723
# headers
18-
binder.h
24+
binder.h
25+
comparator.h
1926
parser.h
2027
printer.h
2128
problemtest.h
@@ -28,6 +35,8 @@ target_sources(solution_cpp PRIVATE
2835
problemtest.cpp
2936
)
3037

38+
target_link_libraries(solution_cpp PRIVATE nlohmann_json::nlohmann_json)
39+
3140
set(CMAKE_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bin)
3241

3342
install(TARGETS solution_cpp

‎data/languages/cpp/binder.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
using SolutionFun = decltype(fun);
1616

1717
class Binder {
18+
public:
1819
using return_type = typename function_traits<SolutionFun>::return_type;
19-
using arg_tuple_type = typename function_traits<SolutionFun>::arg_tuple_type;
20+
using arg_tuple_type =
21+
typename function_traits<SolutionFun>::arg_tuple_type;
22+
23+
constexpr static size_t func_arg_size_v =
24+
std::tuple_size_v<arg_tuple_type>;
25+
26+
private:
2027

2128
template <std::size_t... Is>
2229
static return_type callFunction(Solution& solution,

‎data/languages/cpp/comparator.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef COMPARATOR_H
2+
#define COMPARATOR_H
3+
4+
#include <string>
5+
#include <vector>
6+
7+
struct Comparator {
8+
// template <typename T>
9+
// inline static bool compare(const T& lhs, const T& rhs, bool order = false);
10+
11+
template <typename T,
12+
typename = std::enable_if_t<std::is_integral_v<T> ||
13+
std::is_same_v<T, std::string>>>
14+
inline static bool compare(const T& lhs, const T& rhs, bool order) {
15+
return lhs == rhs;
16+
}
17+
18+
template <typename T,
19+
typename = std::enable_if_t<std::is_integral_v<T> ||
20+
std::is_same_v<T, std::string>>>
21+
inline static bool compare(std::vector<T>& lhs,
22+
std::vector<T>& rhs,
23+
bool order) {
24+
if (lhs.size() != rhs.size()) {
25+
return false;
26+
}
27+
28+
if (order) {
29+
return lhs == rhs;
30+
} else {
31+
std::ranges::sort(lhs);
32+
std::ranges::sort(rhs);
33+
return compare(lhs, rhs, true);
34+
}
35+
}
36+
};
37+
38+
#endif // COMPARATOR_H

‎data/languages/cpp/main.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "problemtest.h"
22

3+
#include <algorithm>
4+
#include <cctype>
35
#include <filesystem>
46
#include <fstream>
57
#include <iostream>
@@ -26,31 +28,51 @@ bool openFileForWriting(const std::string &filename, std::ofstream &out) {
2628
return true;
2729
}
2830

31+
std::string to_lower(std::string s) {
32+
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
33+
return s;
34+
}
35+
2936
int main(int argc, char *argv[]) {
30-
if (argc < 3) {
31-
std::cerr << "Usage: " << argv[0] << " <input file> <output file>"
32-
<< std::endl;
37+
if (argc < 4) {
38+
std::cerr << "Usage: " << argv[0] << " <input file> <output file>"
39+
<< "<testcase name>" << std::endl;
3340
return 1;
3441
}
3542

36-
const std::string inputFile = argv[1];
37-
const std::string outputFile = argv[2];
43+
const std::string test_dir_name = argv[1];
44+
const std::string results_file_name = argv[2];
45+
const std::string testcase_name = argv[3];
3846

39-
std::ifstream in(inputFile);
40-
if (!in) {
41-
std::cerr << "Error: Could not open input file: "<< inputFile
47+
if (!std::filesystem::is_directory(test_dir_name)) {
48+
std::cerr << "Error, test directory " << test_dir_name
49+
<< " does not exist."
4250
<< std::endl;
4351
return 1;
4452
}
4553

46-
std::ofstream out;
47-
if (!openFileForWriting(outputFile, out)) {
48-
std::cerr << "Error: Could not open output file: "<< outputFile
54+
if (std::filesystem::exists(results_file_name)) {
55+
std::cerr << "Error, results file " << results_file_name
56+
<< " already exists."
4957
<< std::endl;
5058
return 1;
5159
}
5260

53-
ProblemTest problemTest(std::move(in), std::move(out));
61+
std::string testcase_file_name;
62+
if (to_lower(testcase_name) != "all") {
63+
testcase_file_name = test_dir_name + "/" +
64+
testcase_name + ".test";
65+
if (!std::filesystem::exists(testcase_file_name)) {
66+
std::cerr << "Error, testcase file " << testcase_file_name
67+
<< " does not exist."
68+
<< std::endl;
69+
return 1;
70+
}
71+
}
72+
73+
ProblemTest problemTest(test_dir_name,
74+
results_file_name,
75+
testcase_file_name);
5476

5577
const bool success = problemTest.run();
5678

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /