Skip to content

Commit 9360f8e

Browse files
committed
restore C++20 from C++11
1 parent 34bd17f commit 9360f8e

File tree

14 files changed

+712
-717
lines changed

14 files changed

+712
-717
lines changed

__dependency_graph.dot

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
digraph {
22
CfgGrammar -> FctHelper [color=blue]
3-
CfgGrammar -> "boost/algorithm/string" [color=blue]
3+
CfgGrammar -> regex [color=blue]
44
CfgGrammar -> IPrintable [color=red]
55
CfgGrammar -> cstdint [color=red]
66
CfgGrammar -> unordered_map [color=red]
@@ -9,16 +9,17 @@ digraph {
99
CfgGrammar
1010
CfgGrammar
1111
}
12-
FctHelper -> "boost/filesystem/operations" [color=blue]
1312
FctHelper -> iostream [color=blue]
1413
FctHelper -> math [color=blue]
1514
FctHelper -> mpi [color=blue]
1615
FctHelper -> algorithm [color=red]
1716
FctHelper -> array [color=red]
17+
FctHelper -> charconv [color=red]
1818
FctHelper -> chrono [color=red]
1919
FctHelper -> cstddef [color=red]
2020
FctHelper -> cstdint [color=red]
2121
FctHelper -> deque [color=red]
22+
FctHelper -> filesystem [color=red]
2223
FctHelper -> fstream [color=red]
2324
FctHelper -> map [color=red]
2425
FctHelper -> set [color=red]
@@ -83,6 +84,7 @@ digraph {
8384
DRuleParser -> map [color=red]
8485
DRuleParser -> memory [color=red]
8586
DRuleParser -> set [color=red]
87+
DRuleParser -> string_view [color=red]
8688
DRuleParser -> string [color=red]
8789
DRuleParser -> tuple [color=red]
8890
DRuleParser -> unordered_map [color=red]
@@ -138,7 +140,6 @@ digraph {
138140
DlProofEnumerator -> DRuleParser [color=blue]
139141
DlProofEnumerator -> DlCore [color=blue]
140142
DlProofEnumerator -> DlFormula [color=blue]
141-
DlProofEnumerator -> "boost/filesystem/operations" [color=blue]
142143
DlProofEnumerator -> concurrent_map [color=blue]
143144
DlProofEnumerator -> concurrent_queue [color=blue]
144145
DlProofEnumerator -> concurrent_unordered_map [color=blue]

grammar/CfgGrammar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "../helper/FctHelper.h"
44

5-
#include <boost/algorithm/string.hpp>
5+
#include <regex>
66

77
using namespace std;
88
using namespace xamid::helper;
@@ -13,7 +13,7 @@ namespace grammar {
1313
CfgGrammar::CfgGrammar(const string& startSymbolString, const string& grammarString) :
1414
_elementCounter(0), _startSymbolString(startSymbolString), _grammarString(grammarString) {
1515
// 1. Replace "\r\n" with "\n" in the grammar string
16-
boost::replace_all(_grammarString, "\r\n", "\n");
16+
_grammarString = regex_replace(_grammarString, regex("\\r\\n"), "\n");
1717

1818
// 2. Build up nonterminals and production rules
1919
istringstream reader1(_grammarString);

helper/FctHelper.cpp

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

3-
#include <boost/filesystem/operations.hpp>
4-
53
#include <iostream>
64
#include <math.h>
75
#include <mpi.h>
@@ -30,19 +28,19 @@ bool cmpStringGrow::operator()(const string& a, const string& b) const {
3028
}
3129

3230
// Templates for using values, static arrays and dynamic arrays on MPI_Send and MPI_Recv ("block until received", with extra mode "receive only if sent")
33-
template<typename T, typename Func> void mpi_send(int rank, int count, MPI_Datatype type, const T* val, int dest, int tag, bool debug, Func printer) {
31+
template<typename T> void mpi_send(int rank, int count, MPI_Datatype type, const T* val, int dest, int tag, bool debug, auto printer) {
3432
if (debug)
3533
cout << rank << "->" << dest << ": Sending " << printer(val) << "." << endl;
3634
MPI_Send(val, count, type, dest, tag, MPI_COMM_WORLD);
3735
}
38-
template<typename T, typename Func> T mpi_recvValue(int rank, MPI_Datatype type, int source, int tag, bool debug, Func printer) {
36+
template<typename T> T mpi_recvValue(int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
3937
T val;
4038
MPI_Recv(&val, 1, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
4139
if (debug)
4240
cout << source << "->" << rank << ": Received " << printer(val) << "." << endl;
4341
return val;
4442
}
45-
template<typename T, typename Func> bool mpi_tryRecvValue(int rank, MPI_Datatype type, int source, int tag, T& result, bool debug, Func printer) {
43+
template<typename T> bool mpi_tryRecvValue(int rank, MPI_Datatype type, int source, int tag, T& result, bool debug, auto printer) {
4644
int flag;
4745
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
4846
if (flag) {
@@ -52,14 +50,14 @@ template<typename T, typename Func> bool mpi_tryRecvValue(int rank, MPI_Datatype
5250
}
5351
return flag;
5452
}
55-
template<typename T, size_t N, typename Func> array<T, N> mpi_recvArray(int rank, MPI_Datatype type, int source, int tag, bool debug, Func printer) {
53+
template<typename T, size_t N> array<T, N> mpi_recvArray(int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
5654
array<T, N> arr;
5755
MPI_Recv(arr.data(), N, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
5856
if (debug)
5957
cout << source << "->" << rank << ": Received " << printer(arr) << "." << endl;
6058
return arr;
6159
}
62-
template<typename T, size_t N, typename Func> bool mpi_tryRecvArray(int rank, MPI_Datatype type, int source, int tag, array<T, N>& result, bool debug, Func printer) {
60+
template<typename T, size_t N> bool mpi_tryRecvArray(int rank, MPI_Datatype type, int source, int tag, array<T, N>& result, bool debug, auto printer) {
6361
int flag;
6462
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
6563
if (flag) {
@@ -69,7 +67,7 @@ template<typename T, size_t N, typename Func> bool mpi_tryRecvArray(int rank, MP
6967
}
7068
return flag;
7169
}
72-
template<typename T, typename Func> ManagedArray<T> mpi_recvDynArray(int rank, MPI_Datatype type, int source, int tag, bool debug, Func printer) {
70+
template<typename T> ManagedArray<T> mpi_recvDynArray(int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
7371
MPI_Status status_recv;
7472
MPI_Probe(source, tag, MPI_COMM_WORLD, &status_recv);
7573
int size;
@@ -82,7 +80,7 @@ template<typename T, typename Func> ManagedArray<T> mpi_recvDynArray(int rank, M
8280
cout << source << "->" << rank << ": Received " << printer(arr) << "." << endl;
8381
return arr;
8482
}
85-
template<typename T, typename Func> bool mpi_tryRecvDynArray(int rank, MPI_Datatype type, int source, int tag, ManagedArray<T>& result, bool debug, Func printer) {
83+
template<typename T> bool mpi_tryRecvDynArray(int rank, MPI_Datatype type, int source, int tag, ManagedArray<T>& result, bool debug, auto printer) {
8684
int flag;
8785
MPI_Status status_recv;
8886
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, &status_recv);
@@ -249,10 +247,10 @@ string FctHelper::durationYearsToMs(const chrono::microseconds& dur, bool innerA
249247
// 2^63+1 µs = 292277 yr 0 mo 1 wk 1 d 23 h 52 min 30 s 775.807 ms
250248
// wolframAlphaMode => -2^63 µs = -292471 yr -2 mo -2 wk -1 d -8 h 0 min -54 s -775.808 ms
251249
// 2^63+1 µs = 292471 yr 2 mo 2 wk 1 d 8 h 0 min 54 s 775.807 ms
252-
constexpr intmax_t yearUs = 31556952 * chrono::microseconds::period::den; // 1 yr = 31556952000000 µs ; NOTE: 31556952 s / yr include leap years, otherwise it would be 60 * 60 * 24 * 365 = 31540000 s / yr.
253-
constexpr intmax_t monthUs = 2629746 * chrono::microseconds::period::den; // 1 mo = 2629746000000 µs ; 2629746 s / mo include leap years, otherwise it would be 31540000 / 12 = 7885000 / 3 = 2628333.333.. s / mo.
254-
constexpr intmax_t weekUs = 604800 * chrono::microseconds::period::den; // 1 wk = 604800000000 µs ; 60 * 60 * 24 * 7 = 604800 s / wk, i.e. from here down, the C++ standard's leap years do not influence values.
255-
constexpr intmax_t dayUs = 86400 * chrono::microseconds::period::den; // 1 d = 86400000000 µs
250+
constexpr intmax_t yearUs = chrono::years::period::num * chrono::microseconds::period::den; // 1 yr = 31556952000000 µs ; NOTE: 31556952 s / yr include leap years, otherwise it would be 60 * 60 * 24 * 365 = 31540000 s / yr.
251+
constexpr intmax_t monthUs = chrono::months::period::num * chrono::microseconds::period::den; // 1 mo = 2629746000000 µs ; 2629746 s / mo include leap years, otherwise it would be 31540000 / 12 = 7885000 / 3 = 2628333.333.. s / mo.
252+
constexpr intmax_t weekUs = chrono::weeks::period::num * chrono::microseconds::period::den; // 1 wk = 604800000000 µs ; 60 * 60 * 24 * 7 = 604800 s / wk, i.e. from here down, the C++ standard's leap years do not influence values.
253+
constexpr intmax_t dayUs = chrono::days::period::num * chrono::microseconds::period::den; // 1 d = 86400000000 µs
256254
constexpr intmax_t hourUs = chrono::hours::period::num * chrono::microseconds::period::den; // 1 h = 3600000000 µs
257255
constexpr intmax_t minuteUs = chrono::minutes::period::num * chrono::microseconds::period::den; // 1 min = 60000000 µs
258256
constexpr intmax_t secondUs = chrono::seconds::period::num * chrono::microseconds::period::den; // 1 s = 1000000 µs
@@ -371,10 +369,10 @@ bool FctHelper::ensureDirExists(const string& path) {
371369
string::size_type dirMarkerIndex = path.find_last_of("/\\");
372370
if (dirMarkerIndex != string::npos) { // If there is a path to another directory given, make sure that the directory exists.
373371
string dir = path.substr(0, dirMarkerIndex);
374-
if (!boost::filesystem::is_directory(dir)) { // Need to create that directory, but in order to do so, must first ensure that its parent directory exists.
372+
if (!filesystem::is_directory(dir)) { // Need to create that directory, but in order to do so, must first ensure that its parent directory exists.
375373
if (!ensureDirExists(dir))
376374
return false;
377-
if (!boost::filesystem::create_directories(dir)) {
375+
if (!filesystem::create_directories(dir)) {
378376
cerr << "Failed to create directory \"" << dir << "\"." << endl;
379377
return false;
380378
}
@@ -384,23 +382,31 @@ bool FctHelper::ensureDirExists(const string& path) {
384382
}
385383

386384
bool FctHelper::writeToFile(const string& file, const string& content, fstream::openmode mode) {
385+
return _writeToFile(filesystem::u8path(file), content, mode);
386+
}
387+
388+
bool FctHelper::_writeToFile(const filesystem::path& file, const string& content, fstream::openmode mode) {
387389
// 1. Ensure directory exists
388-
if (!boost::filesystem::exists(file) && !ensureDirExists(file))
390+
if (!filesystem::exists(file) && !ensureDirExists(file.string()))
389391
return false;
390392
// 2. Save file
391393
ofstream fout(file, mode);
392394
if (!fout.is_open()) {
393-
cerr << "Cannot write to file \"" << file << "\"." << endl;
395+
cerr << "Cannot write to file \"" << file.string() << "\"." << endl;
394396
return false;
395397
}
396398
fout << content;
397399
return true;
398400
}
399401

400402
bool FctHelper::readFile(const string& file, string& out_content, fstream::openmode mode) {
403+
return _readFile(filesystem::u8path(file), out_content, mode);
404+
}
405+
406+
bool FctHelper::_readFile(const filesystem::path& file, string& out_content, fstream::openmode mode) {
401407
ifstream fin(file, mode);
402408
if (!fin.is_open()) {
403-
cerr << "Cannot read from file \"" << file << "\"." << endl;
409+
cerr << "Cannot read from file \"" << file.string() << "\"." << endl;
404410
return false;
405411
}
406412
stringstream buffer;

helper/FctHelper.h

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
#include <algorithm>
55
#include <array>
6+
#include <charconv>
67
#include <chrono>
78
#include <cstddef>
89
#include <cstdint>
910
#include <deque>
11+
#include <filesystem>
1012
#include <fstream>
1113
#include <map>
1214
#include <set>
@@ -54,52 +56,41 @@ struct FctHelper {
5456
static std::array<std::uint64_t, 2> mpi_recvUint64Pair(int rank, int source, bool debug = false);
5557
static bool mpi_tryRecvUint64Pair(int rank, int source, std::array<std::uint64_t, 2>& result, bool debug = false);
5658

59+
// from_chars does not work as desired (i.e. accepts invalid inputs), so we use it with some additional argument checks
5760
template<typename T>
58-
static bool toUInt(const std::string& str, T& value) {
59-
if (*str.c_str() == '0' && str.length() != 1)
60-
return false;
61-
T num = 0;
62-
for (char c : str) {
63-
T before = num;
64-
switch (c) {
61+
static constexpr std::from_chars_result charsToUInt(const char* begin, const char* end, T& value) {
62+
std::from_chars_result result;
63+
if (begin > end) {
64+
result.ec = std::errc::invalid_argument;
65+
return result;
66+
}
67+
if (*begin == '0' && begin + 1 != end) {
68+
result.ec = std::errc::invalid_argument;
69+
return result;
70+
}
71+
for (const char* it = begin; it < end; it++)
72+
switch (*it) {
6573
case '0':
66-
num = 10 * num;
67-
break;
6874
case '1':
69-
num = 10 * num + 1;
70-
break;
7175
case '2':
72-
num = 10 * num + 2;
73-
break;
7476
case '3':
75-
num = 10 * num + 3;
76-
break;
7777
case '4':
78-
num = 10 * num + 4;
79-
break;
8078
case '5':
81-
num = 10 * num + 5;
82-
break;
8379
case '6':
84-
num = 10 * num + 6;
85-
break;
8680
case '7':
87-
num = 10 * num + 7;
88-
break;
8981
case '8':
90-
num = 10 * num + 8;
91-
break;
9282
case '9':
93-
num = 10 * num + 9;
9483
break;
9584
default:
96-
return false;
85+
result.ec = std::errc::invalid_argument;
86+
return result;
9787
}
98-
if (num < before) // overflow
99-
return false;
100-
}
101-
value = num;
102-
return true;
88+
result = std::from_chars(begin, end, value);
89+
return result;
90+
}
91+
template<typename T>
92+
static constexpr std::from_chars_result toUInt(const std::string& str, T& value) {
93+
return charsToUInt(str.c_str(), str.c_str() + str.length(), value);
10394
}
10495

10596
// Functions to quickly calculate to_string(n).length()
@@ -121,7 +112,10 @@ struct FctHelper {
121112

122113
static bool writeToFile(const std::string& file, const std::string& content, std::fstream::openmode mode = std::fstream::out | std::fstream::binary);
123114
static bool readFile(const std::string& file, std::string& out_content, std::fstream::openmode mode = std::fstream::in | std::fstream::binary);
124-
115+
private:
116+
static bool _writeToFile(const std::filesystem::path& file, const std::string& content, std::fstream::openmode mode = std::fstream::out | std::fstream::binary);
117+
static bool _readFile(const std::filesystem::path& file, std::string& out_content, std::fstream::openmode mode = std::fstream::in | std::fstream::binary);
118+
public:
125119
static std::wstring utf8toWide(const std::string& in);
126120
static std::wstring utf8toWide(const char* in);
127121
static std::vector<std::string> stringSplit(const std::string& str, const std::string& sep);
@@ -141,8 +135,8 @@ struct FctHelper {
141135
return ss.str();
142136
}
143137

144-
template<typename T, typename U, typename Func>
145-
static std::string vectorStringF(const std::vector<T, U>& v, const Func& f, const std::string& leftDelimiter = "[", const std::string& rightDelimiter = "]", const std::string& sep = ", ") {
138+
template<typename T, typename U>
139+
static std::string vectorStringF(const std::vector<T, U>& v, const auto& f, const std::string& leftDelimiter = "[", const std::string& rightDelimiter = "]", const std::string& sep = ", ") {
146140
std::stringstream ss;
147141
ss << leftDelimiter;
148142
for (std::size_t i = 0; i < v.size(); ++i) {
@@ -167,8 +161,8 @@ struct FctHelper {
167161
return ss.str();
168162
}
169163

170-
template<typename T, typename U, typename V, typename W, typename Func>
171-
static std::string mapStringF(const std::map<T, U, V, W>& m, const Func& f, const std::string& leftDelimiter = "{", const std::string& rightDelimiter = "}", const std::string& sep = ", ") {
164+
template<typename T, typename U, typename V, typename W>
165+
static std::string mapStringF(const std::map<T, U, V, W>& m, const auto& f, const std::string& leftDelimiter = "{", const std::string& rightDelimiter = "}", const std::string& sep = ", ") {
172166
std::stringstream ss;
173167
ss << leftDelimiter;
174168
for (typename std::map<T, U, V, W>::const_iterator it = m.begin(); it != m.end(); ++it) {

helper/FwdTbb.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
#ifndef XAMID_HELPER_FWDTBB_H
22
#define XAMID_HELPER_FWDTBB_H
33

4-
//#if __has_include(<tbb/version.h>) // NOTE: Change manually if required. There seems to be no alternative for __has_include in C++11.
4+
#if __has_include(<tbb/version.h>)
55
#include <tbb/version.h>
6-
//#else
7-
//#include <tbb/tbb_stddef.h>
8-
//#endif
6+
#else
7+
#include <tbb/tbb_stddef.h>
8+
#endif
99

1010
#include <functional>
1111
#include <utility>
1212

1313
namespace tbb {
1414
#if TBB_INTERFACE_VERSION >= 12002 // since v2021.1-beta08
15-
namespace detail { namespace d1 { template<typename T> class tbb_allocator; template<typename T> class cache_aligned_allocator; template<typename T, typename Allocator> class concurrent_vector; template<typename Key, typename T, typename Hash, typename KeyEqual, typename Allocator> class concurrent_unordered_map; template<typename Key, typename Hash, typename KeyEqual, typename Allocator> class concurrent_unordered_set; } }
15+
namespace detail::d1 { template<typename T> class tbb_allocator; template<typename T> class cache_aligned_allocator; template<typename T, typename Allocator> class concurrent_vector; template<typename Key, typename T, typename Hash, typename KeyEqual, typename Allocator> class concurrent_unordered_map; template<typename Key, typename Hash, typename KeyEqual, typename Allocator> class concurrent_unordered_set; }
1616
using detail::d1::tbb_allocator;
1717
using detail::d1::cache_aligned_allocator;
1818
using detail::d1::concurrent_vector;
1919
using detail::d1::concurrent_unordered_map;
2020
using detail::d1::concurrent_unordered_set;
2121
#if TBB_INTERFACE_VERSION >= 12040 // since v2021.4.0
22-
namespace detail { namespace d2 { template<typename Key, typename Value, typename Compare, typename Allocator> class concurrent_map; } }
22+
namespace detail::d2 { template<typename Key, typename Value, typename Compare, typename Allocator> class concurrent_map; }
2323
using detail::d2::concurrent_map;
2424
#else
25-
namespace detail { namespace d1 { template<typename Key, typename Value, typename Compare, typename Allocator> class concurrent_map; } }
25+
namespace detail::d1 { template<typename Key, typename Value, typename Compare, typename Allocator> class concurrent_map; }
2626
using detail::d1::concurrent_map;
2727
#endif
2828
#else

helper/ProgressData.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ struct ProgressData {
1616
bool maximumEstimated = false;
1717
std::chrono::time_point<std::chrono::system_clock> startTime;
1818
std::vector<std::uint64_t> progressSteps;
19-
std::atomic<std::uint64_t> progress { 0 };
20-
std::atomic<std::uint64_t> progressState { 0 };
19+
std::atomic<std::uint64_t> progress = 0;
20+
std::atomic<std::uint64_t> progressState = 0;
2121
ProgressData() = default;
2222
ProgressData(const ProgressData& other) : percentageStepSize(other.percentageStepSize), maximum(other.maximum), maximumEstimated(other.maximumEstimated), progressSteps(other.progressSteps), progress((std::uint64_t) other.progress), progressState((std::uint64_t) other.progressState) { }
2323
ProgressData(unsigned percentageStepSize, std::uint64_t maximum, bool estimated = false);

main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ int main(int argc, char* argv[]) { // argc = 1 + N, argv = { <command>, <arg1>,
230230
else {
231231
string param = string(argv[++i]);
232232
unsigned value;
233-
if (!FctHelper::toUInt(param, value))
233+
from_chars_result result = FctHelper::toUInt(param, value);
234+
if (result.ec != errc())
234235
return printUsage("Invalid parameter \"" + param + "\" for \"-m\".");
235236
tasks.emplace_back(Task::MpiFilter, value, "", "", "", "", true, false, false, false, 0, 0);
236237
mpiArg = "-m";
@@ -320,7 +321,7 @@ int main(int argc, char* argv[]) { // argc = 1 + N, argv = { <command>, <arg1>,
320321
else {
321322
string path = get<3>(t);
322323
FctHelper::ensureDirExists(path);
323-
ofstream fout(path, fstream::out | fstream::binary);
324+
ofstream fout(filesystem::u8path(path), fstream::out | fstream::binary);
324325
if (!fout.is_open())
325326
throw invalid_argument("Cannot write to file \"" + string(path) + "\".");
326327
DlProofEnumerator::printConclusionLengthPlotData(get<7>(t), get<8>(t), get<10>(t), get<11>(t), get<2>(t), &fout, get<6>(t));

0 commit comments

Comments
 (0)