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 Switch to get_long_options in main.
// C++
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
// libcommon
#include <libcommon.hpp>
// Project
#include "engine.hpp"
const int s_index = 's' - 'a';
engine::engine(std::string start,
int maxiter,
int maxseed,
std::string numerals_file,
std::string characters_file,
unsigned int vl,
robinsonizer_mode mode,
bool easy_parsing,
int engine_id) :
_start {start},
_maxiter {maxiter},
_verbosity_level {vl},
_mode {mode},
_easy_parsing {easy_parsing},
_engine_id {engine_id}
engine::engine(const std::string& start,
std::vector<std::string>&& numerals,
int maxiter,
robinsonizer_mode mode,
unsigned int verbose) :
_start {start},
_numerals {numerals},
_maxiter {maxiter},
_mode {mode},
_verbosity_level {verbose}
{
// 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
_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
int n;
unsigned int n;
for (n = 0; n < _numerals.size(); ++n) {
vector<int> vec;
std::vector<int> vec;
vec.resize(26, 0);
_frequencies.push_back(vec);
_freq.push_back(_frequencies.back().data());
@ -72,7 +49,7 @@ _engine_id {engine_id}
}
}
if (_verbosity_level > 1) {
numeral_frequencies(cerr);
numeral_frequencies(std::cerr);
}
// get letter frequencies from sentence start
@ -118,29 +95,29 @@ void engine::run()
// create random seed vector
switch (_mode) {
case robinsonizer_mode::pangram:
case robinsonizer_mode::strict_autogram:
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
case robinsonizer_mode::pangram:
case robinsonizer_mode::strict_autogram:
for (n = 0; n < 26; n++) {
prev[n] = _dist(_random_engine);
} else {
prev[n] = 0;
}
}
break;
default:
break;
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);
} else {
prev[n] = 0;
}
}
break;
default:
break;
}
#if DEBUG
// logging, if desired
if (_verbosity_level > 1) {
frequencies(cerr, const_cast<const int*>(prev));
frequencies(std::cerr, const_cast<const int*>(prev));
}
#endif
@ -148,7 +125,7 @@ void engine::run()
// setup
num_iter++;
_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
for (n = 0; n < 26; n++) {
@ -158,13 +135,10 @@ void engine::run()
next[k] += p[k];
}
} else {
char c = 'a' + n;
if (_easy_parsing) {
cout << "OVFL[" << _engine_id << "] " << c << " (" << prev[n] << ")" << endl;
} else {
cerr << endl << "overflow: " << c << " (" << prev[n] << ")";
}
break;
// FIXME continuously reports overflow
// char c = 'a' + n;
// std::cerr << std::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
for (n = 0; n < 26; n++) {
switch (_mode) {
case robinsonizer_mode::pangram:
++next[n];
break;
case robinsonizer_mode::strict_autogram:
if (next[n]) {
case robinsonizer_mode::pangram:
++next[n];
}
break;
case robinsonizer_mode::lax_autogram:
if (prev[n]) {
++next[n];
}
break;
default:
break;
break;
case robinsonizer_mode::strict_autogram:
if (next[n]) {
++next[n];
}
break;
case robinsonizer_mode::lax_autogram:
if (prev[n]) {
++next[n];
}
break;
default:
break;
}
if (next[n] > 1) {
++next[s_index];
@ -196,13 +170,13 @@ void engine::run()
#if DEBUG
// debug output, only if verbosity level is high enough
if (_verbosity_level > 1) {
write_result(cerr);
cerr << endl;
write_result(std::cerr);
std::cerr << std::endl;
}
#endif
// 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) {
break;
@ -210,7 +184,7 @@ void engine::run()
if (num_iter == _maxiter) {
break;
} else {
memcpy(prev, next, 26 * sizeof(int));
std::memcpy(prev, next, 26 * sizeof(int));
}
} while (true);
@ -218,7 +192,7 @@ void engine::run()
} while (!_found);
}
void engine::frequencies(ostream& os, const int fm[]) const
void engine::frequencies(std::ostream& os, const int fm[]) const
{
bool output = false;
for (unsigned int n = 0; n < 26; n++) {
@ -231,10 +205,10 @@ void engine::frequencies(ostream& os, const int fm[]) const
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++) {
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 << " ";
unsigned int n;
unsigned int first = 0;
@ -278,7 +249,7 @@ void engine::write_result(ostream& os) const
os << ".";
}
ostream& operator<<(ostream& os, const engine& engine) {
std::ostream& operator<<(std::ostream& os, const engine& engine) {
engine.write_result(os);
return os;
}

View File

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