changed constructor params

This commit is contained in:
Bob Polis
2020-11-20 18:04:38 +01:00
parent ba714c52ca
commit f941a7019e
2 changed files with 87 additions and 124 deletions

View File

@ -5,55 +5,32 @@
// TODO Don't assume 26-letter alphabet, but allow for arbitrary character list. // TODO Don't assume 26-letter alphabet, but allow for arbitrary character list.
// TODO Switch to get_long_options in main.
// C++
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
using namespace std;
// libcommon
#include <libcommon.hpp>
// Project
#include "engine.hpp" #include "engine.hpp"
const int s_index = 's' - 'a'; const int s_index = 's' - 'a';
engine::engine(std::string start, engine::engine(const std::string& start,
int maxiter, std::vector<std::string>&& numerals,
int maxseed, int maxiter,
std::string numerals_file, robinsonizer_mode mode,
std::string characters_file, unsigned int verbose) :
unsigned int vl, _start {start},
robinsonizer_mode mode, _numerals {numerals},
bool easy_parsing, _maxiter {maxiter},
int engine_id) : _mode {mode},
_start {start}, _verbosity_level {verbose}
_maxiter {maxiter},
_verbosity_level {vl},
_mode {mode},
_easy_parsing {easy_parsing},
_engine_id {engine_id}
{ {
// read numerals for desired language from text file, init letter frequency table
{
ifstream file {numerals_file};
string line;
while (getline(file, line)) {
_numerals.push_back(line);
}
}
// setup random distribution // setup random distribution
_dist.param(uniform_int_distribution<>::param_type {0, min<int>(abs(maxseed), _numerals.size())}); _dist.param(std::uniform_int_distribution<>::param_type {0, static_cast<int>(_numerals.size())});
// now we know how many numerals we have, so we can allocate our efficient buffers // now we know how many numerals we have, so we can allocate our efficient buffers
int n; unsigned int n;
for (n = 0; n < _numerals.size(); ++n) { for (n = 0; n < _numerals.size(); ++n) {
vector<int> vec; std::vector<int> vec;
vec.resize(26, 0); vec.resize(26, 0);
_frequencies.push_back(vec); _frequencies.push_back(vec);
_freq.push_back(_frequencies.back().data()); _freq.push_back(_frequencies.back().data());
@ -72,7 +49,7 @@ _engine_id {engine_id}
} }
} }
if (_verbosity_level > 1) { if (_verbosity_level > 1) {
numeral_frequencies(cerr); numeral_frequencies(std::cerr);
} }
// get letter frequencies from sentence start // get letter frequencies from sentence start
@ -118,29 +95,29 @@ void engine::run()
// create random seed vector // create random seed vector
switch (_mode) { switch (_mode) {
case robinsonizer_mode::pangram: case robinsonizer_mode::pangram:
case robinsonizer_mode::strict_autogram: case robinsonizer_mode::strict_autogram:
for (n = 0; n < 26; n++) { for (n = 0; n < 26; n++) {
prev[n] = _dist(_random_engine);
}
break;
case robinsonizer_mode::lax_autogram:
for (n = 0; n < 26; ++n) {
if (_used[n]) { // only if letter occurs in numerals or sentence start
prev[n] = _dist(_random_engine); prev[n] = _dist(_random_engine);
} else {
prev[n] = 0;
} }
} break;
break; case robinsonizer_mode::lax_autogram:
default: for (n = 0; n < 26; ++n) {
break; if (_used[n]) { // only if letter occurs in numerals or sentence start
prev[n] = _dist(_random_engine);
} else {
prev[n] = 0;
}
}
break;
default:
break;
} }
#if DEBUG #if DEBUG
// logging, if desired // logging, if desired
if (_verbosity_level > 1) { if (_verbosity_level > 1) {
frequencies(cerr, const_cast<const int*>(prev)); frequencies(std::cerr, const_cast<const int*>(prev));
} }
#endif #endif
@ -148,7 +125,7 @@ void engine::run()
// setup // setup
num_iter++; num_iter++;
_total_iterations++; _total_iterations++;
memcpy(next, start, 26 * sizeof(int)); std::memcpy(next, start, 26 * sizeof(int));
// count letters in resulting sentence by incrementing result freqmap elements // count letters in resulting sentence by incrementing result freqmap elements
for (n = 0; n < 26; n++) { for (n = 0; n < 26; n++) {
@ -158,13 +135,10 @@ void engine::run()
next[k] += p[k]; next[k] += p[k];
} }
} else { } else {
char c = 'a' + n; // FIXME continuously reports overflow
if (_easy_parsing) { // char c = 'a' + n;
cout << "OVFL[" << _engine_id << "] " << c << " (" << prev[n] << ")" << endl; // std::cerr << std::endl << "overflow: " << c << " (" << prev[n] << ")";
} else { // break;
cerr << endl << "overflow: " << c << " (" << prev[n] << ")";
}
break;
} }
} }
@ -172,21 +146,21 @@ void engine::run()
// and increment the count for every letter which is (or should be) mentioned // and increment the count for every letter which is (or should be) mentioned
for (n = 0; n < 26; n++) { for (n = 0; n < 26; n++) {
switch (_mode) { switch (_mode) {
case robinsonizer_mode::pangram: case robinsonizer_mode::pangram:
++next[n];
break;
case robinsonizer_mode::strict_autogram:
if (next[n]) {
++next[n]; ++next[n];
} break;
break; case robinsonizer_mode::strict_autogram:
case robinsonizer_mode::lax_autogram: if (next[n]) {
if (prev[n]) { ++next[n];
++next[n]; }
} break;
break; case robinsonizer_mode::lax_autogram:
default: if (prev[n]) {
break; ++next[n];
}
break;
default:
break;
} }
if (next[n] > 1) { if (next[n] > 1) {
++next[s_index]; ++next[s_index];
@ -196,13 +170,13 @@ void engine::run()
#if DEBUG #if DEBUG
// debug output, only if verbosity level is high enough // debug output, only if verbosity level is high enough
if (_verbosity_level > 1) { if (_verbosity_level > 1) {
write_result(cerr); write_result(std::cerr);
cerr << endl; std::cerr << std::endl;
} }
#endif #endif
// test if our result equals the previous one (if so, we found a valid sentence) // test if our result equals the previous one (if so, we found a valid sentence)
_found = memcmp(next, prev, 26 * sizeof(int)) == 0; _found = std::memcmp(next, prev, 26 * sizeof(int)) == 0;
if (_found) { if (_found) {
break; break;
@ -210,7 +184,7 @@ void engine::run()
if (num_iter == _maxiter) { if (num_iter == _maxiter) {
break; break;
} else { } else {
memcpy(prev, next, 26 * sizeof(int)); std::memcpy(prev, next, 26 * sizeof(int));
} }
} while (true); } while (true);
@ -218,7 +192,7 @@ void engine::run()
} while (!_found); } while (!_found);
} }
void engine::frequencies(ostream& os, const int fm[]) const void engine::frequencies(std::ostream& os, const int fm[]) const
{ {
bool output = false; bool output = false;
for (unsigned int n = 0; n < 26; n++) { for (unsigned int n = 0; n < 26; n++) {
@ -231,10 +205,10 @@ void engine::frequencies(ostream& os, const int fm[]) const
output = true; output = true;
} }
} }
os << endl; os << std::endl;
} }
void engine::numeral_frequencies(ostream& os) const void engine::numeral_frequencies(std::ostream& os) const
{ {
for (unsigned int i = 0; i < _numerals.size(); i++) { for (unsigned int i = 0; i < _numerals.size(); i++) {
os << _numerals[i] << ": "; os << _numerals[i] << ": ";
@ -242,11 +216,8 @@ void engine::numeral_frequencies(ostream& os) const
} }
} }
void engine::write_result(ostream& os) const void engine::write_result(std::ostream& os) const
{ {
if (_easy_parsing) {
os << "RSLT[" << _engine_id << "] ";
}
os << _start << " "; os << _start << " ";
unsigned int n; unsigned int n;
unsigned int first = 0; unsigned int first = 0;
@ -278,7 +249,7 @@ void engine::write_result(ostream& os) const
os << "."; os << ".";
} }
ostream& operator<<(ostream& os, const engine& engine) { std::ostream& operator<<(std::ostream& os, const engine& engine) {
engine.write_result(os); engine.write_result(os);
return os; return os;
} }

View File

@ -11,44 +11,36 @@
#include "robinsonizer_mode.hpp" #include "robinsonizer_mode.hpp"
class engine { class engine {
public: public:
engine(std::string start, engine(const std::string& start,
int maxiter, std::vector<std::string>&& numerals,
int maxseed, int maxiter,
std::string numerals_file, robinsonizer_mode mode,
std::string characters_file, unsigned int verbose);
unsigned int vl,
robinsonizer_mode mode,
bool easy_parsing,
int engine_id);
engine() = delete;
void run(); void run();
void write_result(std::ostream& os) const; void write_result(std::ostream& os) const;
void frequencies(std::ostream& os, const int fm[]) const; void frequencies(std::ostream& os, const int fm[]) const;
void numeral_frequencies(std::ostream& os) const; void numeral_frequencies(std::ostream& os) const;
unsigned long long total_iterations() const {return _total_iterations; } unsigned long long total_iterations() const {return _total_iterations; }
bool found() const { return _found; } bool found() const { return _found; }
protected: protected:
std::string _start; std::string _start;
int _maxiter; std::vector<std::string> _numerals;
unsigned long long _total_iterations {0}; int _maxiter {10};
std::vector<std::string> _numerals; robinsonizer_mode _mode {robinsonizer_mode::lax_autogram};
std::vector<std::vector<int>> _frequencies; // for memory mgmt only unsigned long long _total_iterations {0};
std::vector<int*> _freq; std::vector<std::vector<int>> _frequencies; // for memory mgmt only
std::vector<int> _start_freq; std::vector<int*> _freq;
std::vector<int> _old; std::vector<int> _start_freq;
std::vector<int> _new; std::vector<int> _old;
std::vector<int> _used; std::vector<int> _new;
unsigned int _verbosity_level; std::vector<int> _used;
bool _found {false}; unsigned int _verbosity_level {0};
robinsonizer_mode _mode; bool _found {false};
bool _easy_parsing; std::default_random_engine _random_engine {std::random_device {}()};
int _engine_id; std::uniform_int_distribution<int> _dist;
std::default_random_engine _random_engine {std::random_device {}()};
std::uniform_int_distribution<int> _dist;
}; };
std::ostream& operator<<(std::ostream& os, const engine& engine); std::ostream& operator<<(std::ostream& os, const engine& engine);