-
Notifications
You must be signed in to change notification settings - Fork 9
/
generator.cc
82 lines (66 loc) · 2.23 KB
/
generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "generator.h"
#include "streams.h"
#include <eacirc-core/logger.h>
#include <eacirc-core/random.h>
#include <pcg/pcg_random.hpp>
#include <fstream>
#include <iomanip>
#include <sstream>
static std::ifstream open_config_file(const std::string path) {
std::ifstream file(path);
if (!file.is_open())
throw std::runtime_error("can't open config file " + path);
return file;
}
static std::string out_name(json const &config) {
auto fname_it = config.find("file_name");
if (fname_it != config.end()) {
return *fname_it;
}
std::stringstream ss;
json config_ref = config.at("stream");
// this allows finding name hidden in postprocessing streams
while (config_ref.find("algorithm") == config_ref.end()) {
config_ref = config_ref.at("source");
}
std::string a = config_ref.at("algorithm");
ss << a;
auto round = config_ref.value("round", -1);
if (round != -1) {
ss << "_r";
ss << std::setw(2) << std::setfill('0') << round;
}
auto block_size = config_ref.value("block_size", -1);
if (block_size != -1) {
ss << "_b" << block_size;
}
ss << ".bin";
return ss.str();
}
generator::generator(const std::string config)
: generator(open_config_file(config)) {}
generator::generator(json const &config)
: _config(config)
, _seed(seed::create(config.at("seed")))
, _tv_count(config.at("tv_count"))
, _o_file_name(out_name(config)) {
seed_seq_from<pcg32> main_seeder(_seed);
std::unordered_map<std::string, std::shared_ptr<std::unique_ptr<stream>>> map;
_stream_a = make_stream(config.at("stream"), main_seeder, map, std::size_t(config.at("tv_size")));
}
void generator::generate() {
auto stdout_it = _config.find("stdout");
std::unique_ptr<std::ostream> ofstream_ptr;
std::ostream *o_file = nullptr;
if (stdout_it == _config.end() || stdout_it->get<bool>() == false) {
ofstream_ptr = std::make_unique<std::ofstream>(_o_file_name, std::ios::binary);
o_file = ofstream_ptr.get();
} else {
o_file = &(std::cout);
}
for (std::size_t i = 0; i < _tv_count; ++i) {
vec_cview n = _stream_a->next();
for (auto o : n)
*o_file << o;
}
}