Skip to content

Commit 4abf34b

Browse files
committed
Fix compilation on old compilers
1 parent 123ea49 commit 4abf34b

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ NAME := complexity
44

55
# Commands
66

7-
override CPPC := clang++
7+
override CPPC := c++
88
override CPPFLAGS := -std=c++20 -Wall -Wextra -O3
99

1010
# Sources

src/args.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "complexity.hpp"
22

33
#include <iostream>
4-
#include <format>
4+
5+
#include <version>
56

67
#include <getopt.h>
78

@@ -18,11 +19,19 @@ static auto parseNumber(const char* str, const int min) -> uint32_t
1819
}
1920
catch (...)
2021
{
22+
#ifdef USE_FORMAT
2123
throw std::invalid_argument(std::format("{} is not a valid number", str));
24+
#else
25+
throw std::invalid_argument(std::string() + str + " is not a valid number");
26+
#endif
2227
}
2328

2429
if (number < min)
30+
#ifdef USE_FORMAT
2531
throw std::invalid_argument(std::format("{} must be at least equal to {}", str, std::to_string(min)));
32+
#else
33+
throw std::invalid_argument(std::string() + str + " must be at least equal to " + std::to_string(min));
34+
#endif
2635
return number;
2736
}
2837

src/complexity.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include <optional>
66
#include <random>
77

8+
#if __cpp_lib_format >= 201907L
9+
#define USE_FORMAT
10+
#include <format>
11+
#endif
12+
813
struct program_opts
914
{
1015
bool version;

src/main.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include <atomic>
99
#include <mutex>
1010
#include <thread>
11-
#include <format>
11+
#include <algorithm>
1212
#include <iomanip>
1313
#include <limits>
1414
#include <unistd.h>
15+
#include <signal.h>
1516

1617
#include "complexity.hpp"
1718
#include "executor.hpp"
@@ -39,10 +40,17 @@ auto worker(const program_opts& opts, const program_params& params) -> void
3940
auto& buffer = executor.execute("/Users/simoncros/Projects/push_swap/push_swap",
4041
generator.generate(std::numeric_limits<int>::min(),
4142
std::numeric_limits<int>::max()));
42-
const auto lines = std::ranges::count(buffer, '\n');
43-
if (lines >= std::numeric_limits<unsigned int>::max())
44-
throw std::runtime_error(std::format("push_swap printed more than {} lines",
45-
std::numeric_limits<unsigned int>::max()));
43+
// const auto lines = std::ranges::count(buffer, '\n');
44+
const auto lines = std::count(buffer.begin(), buffer.end(), '\n');
45+
if (lines >= std::numeric_limits<unsigned int>::max()) {
46+
throw std::runtime_error(
47+
#ifdef USE_FORMAT
48+
std::format("push_swap printed more than {} lines", std::numeric_limits<unsigned int>::max())
49+
#else
50+
std::string("push_swap printed more than ") + std::to_string(std::numeric_limits<unsigned int>::max()) + "lines"
51+
#endif
52+
);
53+
}
4654

4755
{
4856
std::scoped_lock lock(results_access);

src/print.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//
44

55
#include <iostream>
6-
#include <format>
6+
#ifndef USE_FORMAT
7+
#include <iomanip>
8+
#endif
79

810
#include "complexity.hpp"
911

@@ -69,11 +71,17 @@ auto printStart(const program_opts& opts, const program_params& params) -> void
6971
if (prettyPrint())
7072
{
7173
std::cout << getVersion() << '\n';
74+
#ifdef USE_FORMAT
7275
std::cout << std::format(
7376
"\033[97mStarting the test : \033[95m{}\033[97m elements, \033[95m{}\033[97m iterations\033[0m",
7477
params.numbers, params.iterations);
7578
if (opts.seed.has_value())
7679
std::cout << std::format(" (seed {})", opts.seed.value());
80+
#else
81+
std::cout << "\033[97mStarting the test : \033[95m" << params.numbers << "\033[97m elements, \033[95m" << params.iterations << "\033[97m iterations\033[0m";
82+
if (opts.seed.has_value())
83+
std::cout << " (seed " << opts.seed.value() << ")";
84+
#endif
7785
std::cout << std::endl;
7886
}
7987
}
@@ -97,6 +105,7 @@ auto printStatus(const program_params& params, const results_t& results) -> void
97105

98106
if (prettyPrint())
99107
{
108+
#ifdef USE_FORMAT
100109
std::cout << std::format("Worst = \033[31m{}\033[0m instructions\n", results.worst);
101110
std::cout << std::format("Mean = \033[33m{:.1f}\033[0m instructions\n", mean);
102111
std::cout << std::format("Best = \033[36m{}\033[0m instructions\n", results.best);
@@ -115,9 +124,32 @@ auto printStatus(const program_params& params, const results_t& results) -> void
115124
// std::cout << "Precision = enter a tester as the fourth argument\n";
116125
std::cout << "Failed = currently not available\n";
117126
std::cout << std::format("\033[32m{}\033[0m % effective\n", percentDone);
127+
#else
128+
std::cout << "Worst = \033[31m" << results.worst << "\033[0m instructions\n";
129+
std::cout << "Mean = \033[33m" << std::fixed << std::setprecision(1) << mean << "\033[0m instructions\n";
130+
std::cout << "Best = \033[36m" << results.best << "\033[0m instructions\n";
131+
std::cout << "Std. deviation = \033[93m" << std::fixed << std::setprecision(1) << stddev << "\033[0m instructions\n";
132+
133+
if (params.objective.has_value()) {
134+
std::cout << "Objective = \033[94m" << underObjective
135+
<< "\033[0m % under \033[94m" << params.objective.value()
136+
<< "\033[0m (\033[91m" << results.aboveObjective << "\033[0m above)\n";
137+
} else {
138+
std::cout << "Objective = enter a number as the third argument\n";
139+
}
140+
141+
// if (params.checker.has_value())
142+
// std::cout << "Precision = \033[97m" << (ok * 100 / done) << "\033[0m % OK (\033[91m" << (done - ok) << "\033[0m KO) " << std::endl;
143+
// else
144+
// std::cout << "Precision = enter a tester as the fourth argument\n";
145+
146+
std::cout << "Failed = currently not available\n";
147+
std::cout << "\033[32m" << percentDone << "\033[0m % effective\n";
148+
#endif
118149
}
119150
else
120151
{
152+
#ifdef USE_FORMAT
121153
std::cout << std::format(
122154
"{{\n"
123155
" \"elements\": {},\n"
@@ -137,5 +169,17 @@ auto printStatus(const program_params& params, const results_t& results) -> void
137169
results.best,
138170
stddev,
139171
results.aboveObjective);
172+
#else
173+
std::cout << "{\n"
174+
<< " \"elements\": " << params.numbers << ",\n"
175+
<< " \"iterations\": " << params.iterations << ",\n"
176+
<< " \"objective\": " << params.objective.value_or(-1) << ",\n"
177+
<< " \"worst\": " << results.worst << ",\n"
178+
<< " \"mean\": " << std::fixed << std::setprecision(6) << mean << ",\n"
179+
<< " \"best\": " << results.best << ",\n"
180+
<< " \"stddev\": " << std::fixed << std::setprecision(6) << stddev << ",\n"
181+
<< " \"aboveObjective\": " << results.aboveObjective << "\n"
182+
<< "}\n";
183+
#endif
140184
}
141185
}

0 commit comments

Comments
 (0)