From 8fecda2a81ad299bd9fcd92129b2bf8ade837a7a Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Thu, 19 Nov 2020 16:02:02 +0100 Subject: [PATCH] first commit --- Makefile | 68 +++++++++ autogram.1 | 57 ++++++++ numerals.db | Bin 0 -> 40960 bytes premake.make | 1 + src/engine.cpp | 284 ++++++++++++++++++++++++++++++++++++++ src/engine.hpp | 56 ++++++++ src/main.cpp | 68 +++++++++ src/robinsonizer_mode.hpp | 13 ++ tests/Makefile | 51 +++++++ tests/src/main.cpp | 8 ++ 10 files changed, 606 insertions(+) create mode 100644 Makefile create mode 100644 autogram.1 create mode 100644 numerals.db create mode 100644 premake.make create mode 100644 src/engine.cpp create mode 100644 src/engine.hpp create mode 100644 src/main.cpp create mode 100644 src/robinsonizer_mode.hpp create mode 100644 tests/Makefile create mode 100644 tests/src/main.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7394e58 --- /dev/null +++ b/Makefile @@ -0,0 +1,68 @@ +include premake.make + +BIN := $(shell basename $$(pwd)) + +MANSECTION := 1 +MANPAGE := $(BIN).$(MANSECTION) + +SRCS := $(notdir $(wildcard src/*.cpp)) +OBJS := $(SRCS:.cpp=.o) +DEPS := $(SRCS:.cpp=.d) + +BUILDDIR := build/intermediates/ +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin +CONFIGDIR ?= $(PREFIX)/etc +DATADIR ?= $(PREFIX)/share +MANDIR ?= $(DATADIR)/man/man +DOCDIR ?= $(DATADIR)/$(BIN)/doc + +CXX ?= g++ +RM := /bin/rm -rf +INSTALL := /usr/bin/install -c + +CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -pthread +ifeq ($(DEBUG),1) + CXXFLAGS += -D DEBUG -O0 + CONFIG := debug +else + CXXFLAGS += -D NDEBUG -O3 + CONFIG := release +endif +OUTDIR := build/$(CONFIG)/ + +vpath %.cpp src +vpath %.d $(BUILDDIR) +vpath %.o $(BUILDDIR) + +.PHONY: all clean install prebuild test + +all: prebuild $(OUTDIR)$(BIN) + +prebuild: + @mkdir -p $(BUILDDIR) $(OUTDIR) + +$(OUTDIR)$(BIN): $(OBJS) $(DEPS) + $(CXX) $(addprefix $(BUILDDIR),$(OBJS)) $(LDFLAGS) $(LDLIBS) -o $(OUTDIR)$(BIN) + @ln -sf $(OUTDIR)$(BIN) $(BIN) + +%.o: %.cpp %.d + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + @mv $@ $*.d $(BUILDDIR) + +-include $(BUILDDIR)*.d + +%.d: ; + +test: + $(MAKE) -C tests && tests/tests + +clean: + $(RM) build $(BIN) + $(MAKE) -C tests clean + +install: $(OUTDIR)$(BIN) + $(INSTALL) -d $(BINDIR) + $(INSTALL) $(OUTDIR)$(BIN) $(BINDIR) + $(INSTALL) -d $(MANDIR)$(MANSECTION) + $(INSTALL) $(MANPAGE) $(MANDIR)$(MANSECTION) diff --git a/autogram.1 b/autogram.1 new file mode 100644 index 0000000..6c7dc38 --- /dev/null +++ b/autogram.1 @@ -0,0 +1,57 @@ +.Dd $Mdocdate$ +.Dt autogram 1 +.Os +.Sh NAME +.Nm autogram +.Nd one line about what it does +.\" .Sh LIBRARY +.\" For sections 2, 3, and 9 only. +.\" Not used in OpenBSD. +.Sh SYNOPSIS +.Nm +.Fl h | v +.Nm +.Op Ar +.Sh DESCRIPTION +The +.Nm +utility processes files ... +When no file arguments are given, +.Nm +will read from the standard input. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl h, \-help +Print help text and exit. +.It Fl v, \-version +Print version info and exit. +.El +.\" .Sh CONTEXT +.\" For section 9 functions only. +.\" .Sh IMPLEMENTATION NOTES +.\" Not used in OpenBSD. +.\" .Sh RETURN VALUES +.\" For sections 2, 3, and 9 function return values only. +.\" .Sh ENVIRONMENT +.\" For sections 1, 6, 7, and 8 only. +.\" .Sh FILES +.Sh EXIT STATUS +.\" For sections 1, 6, and 8 only. +.Nm +exits 0 on success, and 1 if an error occurs. +.\" .Sh EXAMPLES +.\" .Sh DIAGNOSTICS +.\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. +.\" .Sh ERRORS +.\" For sections 2, 3, 4, and 9 errno settings only. +.\" .Sh SEE ALSO +.\" .Xr foobar 1 +.\" .Sh STANDARDS +.\" .Sh HISTORY +.Sh AUTHORS +Bob Polis +.\" .Sh CAVEATS +.\" .Sh BUGS +.\" .Sh SECURITY CONSIDERATIONS +.\" Not used in OpenBSD. diff --git a/numerals.db b/numerals.db new file mode 100644 index 0000000000000000000000000000000000000000..17215f89c387c72ea3581dfbfe4b19a6ecff2350 GIT binary patch literal 40960 zcmeHwcX$-n)&9M+b-Gpsl|>*Sgb-bb-h1!8gMgI~2ni&CME8uq2IGzkxOcF@#=T&` zU}KZmN$fawV#ki1;+DjA?AS@1*!l9Ed(NE&ZJzJ%U%u}*JdbnUdEdGBp8M{s_TIBI zo7pp`R5vCHD(mXkS2Pxsvoyvz8&Oce7{8t|ron&SKLtS0JAlY9!v6z=K6;ElV%ptV z&>GF`%j~|^XzTm`;bfoo9q=9S9q=9S9q=9S9q=9S9q=9S9q=9a&vwA+7l>r%=W};> zW5vpvMD3>aiTa9~1}_Fj&l)>&&e(!EBS%dcTi~$;owWk`S5r}2wW*>ivAlY9L3M3o zqAF2eP@kww)F*0JB^nAmxuKiaU~@&yri6EDl7)^78WYWpNn%5N_41ErH1WK!JlJo@^ zFEa1qiPLme)hE^`YA;wL+M`)osVU}@CjQ6&=R4p#;5*PK_@E!0S@E!0S@E!0S@E!0S@E!0S@E!0S@E!Qy?|>Q&a;_*Edqdp+f1Iyp z_TTK!?BCcww_ml-+E3aK+PB*W?0xojyVhQA&#*_?z3n19!wy-0w%)g1v7WN-wyw6e zT9wvJYmC*~YHu0l-_4KBH_bEVL*^lKzu9cAHfNYa%+6+($&Am8Ul>0$o-*z>t}?b7 zmBwsin9;?^GNSrl_222Q>!fZv*cJP6zG^>hn4-x24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ake.make new file mode 100644 index 0000000..48ede85 --- /dev/null +++ b/premake.make @@ -0,0 +1 @@ +LDLIBS := -lm -lpthread diff --git a/src/engine.cpp b/src/engine.cpp new file mode 100644 index 0000000..4f7b0e7 --- /dev/null +++ b/src/engine.cpp @@ -0,0 +1,284 @@ +// robinsonizer engine by Bob Polis +// copyright (c) 1994-2019 + +// TODO Use correct plurals: adjust numeral file format to include this. + +// TODO Don't assume 26-letter alphabet, but allow for arbitrary character list. + +// TODO Switch to get_long_options in main. + +// C++ +#include +#include +#include +#include +using namespace std; + +// libcommon +#include + +// 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} +{ + // 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(abs(maxseed), _numerals.size())}); + + // now we know how many numerals we have, so we can allocate our efficient buffers + int n; + for (n = 0; n < _numerals.size(); ++n) { + vector vec; + vec.resize(26, 0); + _frequencies.push_back(vec); + _freq.push_back(_frequencies.back().data()); + } + _start_freq.resize(26, 0); + _old.resize(26, 0); + _new.resize(26, 0); + _used.resize(26, 0); + + // init numeral letter frequency table + for (n = 0; n < _numerals.size(); n++) { + for (char c : _numerals[n]) { + if (c >= 'a' && c <= 'z') { + ++_freq[n][c - 'a']; + } + } + } + if (_verbosity_level > 1) { + numeral_frequencies(cerr); + } + + // get letter frequencies from sentence start + for (char c : start) { + // optionally translate upper- to lowercase + const char up_lo_dif = 'a' - 'A'; + if (c >= 'A' && c <= 'Z') { + c += up_lo_dif; + } + // skip non-alphabetical chars + if (c >= 'a' && c <= 'z') { + ++_start_freq[c - 'a']; + } + } + // add letters from 'and', reset 'and' vector + for (n = 0; n < 26; n++) { + _start_freq[n] += _freq[0][n]; + _freq[0][n] = 0; + } + // build 'used' table for correct autogram seeding + for (n = 0; n < 26; ++n) { + _used[n] = _start_freq[n]; + } + for (n = 0; n < _numerals.size(); ++n) { + for (int i = 0; i < 26; ++i) { + _used[i] += _freq[n][i]; + } + } +} + + +void engine::run() +{ + auto prev = _old.data(); + auto next = _new.data(); + auto freq = _freq.data(); + auto start = _start_freq.data(); + + do { + // setup + int num_iter = 0; + unsigned int k, n; + + // 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 + 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(prev)); + } +#endif + + do { + // setup + num_iter++; + _total_iterations++; + memcpy(next, start, 26 * sizeof(int)); + + // count letters in resulting sentence by incrementing result freqmap elements + for (n = 0; n < 26; n++) { + if (static_cast(prev[n]) < _numerals.size()) { + auto p = freq[prev[n]]; + for (k = 0; k < 26; k++) { + 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; + } + } + + // increment frequency for 's' for every letter which occurs more than once, + // 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]) { + ++next[n]; + } + break; + case robinsonizer_mode::lax_autogram: + if (prev[n]) { + ++next[n]; + } + break; + default: + break; + } + if (next[n] > 1) { + ++next[s_index]; + } + } + +#if DEBUG + // debug output, only if verbosity level is high enough + if (_verbosity_level > 1) { + write_result(cerr); + cerr << 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; + + if (_found) { + break; + } + if (num_iter == _maxiter) { + break; + } else { + memcpy(prev, next, 26 * sizeof(int)); + } + + } while (true); + + } while (!_found); +} + +void engine::frequencies(ostream& os, const int fm[]) const +{ + bool output = false; + for (unsigned int n = 0; n < 26; n++) { + if (fm[n]) { + if (output) { + os << ", "; + } + char c = n + 'a'; + os << c << " (" << fm[n] << ")"; + output = true; + } + } + os << endl; +} + +void engine::numeral_frequencies(ostream& os) const +{ + for (unsigned int i = 0; i < _numerals.size(); i++) { + os << _numerals[i] << ": "; + frequencies(os, _freq[i]); + } +} + +void engine::write_result(ostream& os) const +{ + if (_easy_parsing) { + os << "RSLT[" << _engine_id << "] "; + } + os << _start << " "; + unsigned int n; + unsigned int first = 0; + unsigned int last = 25; + bool first_found = false; + for (n = 0; n < 26; n++) { // pre-scan + if (_new[n]) { + if (!first_found) { + first = n; + first_found = true; + } + last = n; + } + } + for (n = 0; n < 26; n++) { + if (_new[n]) { + if (n == last) { + os << " " << _numerals[0] << " "; + } else if (n > first) { + os << ", "; + } + char c = n + 'a'; + os << _numerals[_new[n]] << " " << c; + if (_new[n] > 1) { + os << "'s"; + } + } + } + os << "."; +} + +ostream& operator<<(ostream& os, const engine& engine) { + engine.write_result(os); + return os; +} diff --git a/src/engine.hpp b/src/engine.hpp new file mode 100644 index 0000000..3fdf353 --- /dev/null +++ b/src/engine.hpp @@ -0,0 +1,56 @@ +// robinsonizer engine by Bob Polis +// copyright (c) 1994-2019 + +#ifndef __engine__ +#define __engine__ + +#include +#include +#include +#include +#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; + + 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 _numerals; + std::vector> _frequencies; // for memory mgmt only + std::vector _freq; + std::vector _start_freq; + std::vector _old; + std::vector _new; + std::vector _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 _dist; +}; + +std::ostream& operator<<(std::ostream& os, const engine& engine); + +#endif // __engine__ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8abf701 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,68 @@ +// +// main.cpp +// autogram +// +// Created by Bob Polis at 2020-11-19 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#include +#include +#include +#include +#include + +void print_help() { + std::cout << "usage: autogram [-h|--version]\n"; + std::cout << " -h, --help show this help text and exit\n"; + std::cout << " --version show version number and exit\n"; +} + +void print_version() { + std::cout << "autogram version 1.0\n"; +} + +int main(int argc, const char * argv[]) { + try { + int opt_char, opt_val; + struct option long_options[] = { + {"help", no_argument, nullptr, 'h'}, + {"version", no_argument, &opt_val, 1}, + {nullptr, 0, nullptr, 0} + }; + while ((opt_char = getopt_long(argc, const_cast(argv), "h", long_options, nullptr)) != -1) { + std::string arg {optarg ? optarg : ""}; + switch (opt_char) { + case 0: { + // handle long-only options here + switch (opt_val) { + case 1: + print_version(); + return EXIT_SUCCESS; + } + break; + } + case 'h': + print_help(); + return EXIT_SUCCESS; + case '?': + throw std::runtime_error("unrecognized option"); + } + } + if (optind == argc) { + // here when no file args + } + for (int i = optind; i < argc; ++i) { + try { + // process file argv[i] + } catch (const std::runtime_error& ex) { + std::cerr << "autogram: " << ex.what() << '\n'; + } + } + std::cout << "hello, autogram\n"; + } catch (const std::exception& ex) { + std::cerr << "autogram: " << ex.what() << '\n'; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} diff --git a/src/robinsonizer_mode.hpp b/src/robinsonizer_mode.hpp new file mode 100644 index 0000000..6adf643 --- /dev/null +++ b/src/robinsonizer_mode.hpp @@ -0,0 +1,13 @@ +// +// robinsonizer_mode.hpp +// robinsonizer +// +// Created by Bob Polis at 2019-02-02 +// Copyright (c) 2019 SwiftCoder. All rights reserved. +// + +enum class robinsonizer_mode { + pangram, + lax_autogram, + strict_autogram +}; diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..ae065a3 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,51 @@ +include ../premake.make +LDLIBS += -lboost_unit_test_framework + +BIN := $(shell basename $$(pwd)) + +SRCS := $(notdir $(wildcard src/*.cpp)) +SRCS += $(notdir $(filter-out ../src/main.cpp,$(wildcard ../src/*.cpp))) +OBJS := $(SRCS:.cpp=.o) +DEPS := $(SRCS:.cpp=.d) + +BUILDDIR := build/intermediates/ + +CXX ?= g++ +RM := /bin/rm -rf +INSTALL := /usr/bin/install -c + +CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -pthread -I../src +ifeq ($(DEBUG),1) + CXXFLAGS += -D DEBUG -O0 + CONFIG := debug +else + CXXFLAGS += -D NDEBUG -O3 + CONFIG := release +endif +OUTDIR := build/$(CONFIG)/ + +vpath %.cpp src ../src +vpath %.d $(BUILDDIR) +vpath %.o $(BUILDDIR) + +.PHONY: all clean prebuild + +all: prebuild $(OUTDIR)$(BIN) + +prebuild: + @mkdir -p $(BUILDDIR) $(OUTDIR) + +$(OUTDIR)$(BIN): $(OBJS) $(DEPS) + $(CXX) $(addprefix $(BUILDDIR),$(OBJS)) $(LDFLAGS) $(LDLIBS) -o $(OUTDIR)$(BIN) + @ln -sf $(OUTDIR)$(BIN) $(BIN) + +%.o: %.cpp %.d + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + @mv $@ $*.d $(BUILDDIR) + +-include $(BUILDDIR)*.d + +%.d: ; + +clean: + $(RM) build $(BIN) diff --git a/tests/src/main.cpp b/tests/src/main.cpp new file mode 100644 index 0000000..533ab2a --- /dev/null +++ b/tests/src/main.cpp @@ -0,0 +1,8 @@ +#define BOOST_TEST_MODULE My Test +#define BOOST_TEST_DYN_LINK +#include + +BOOST_AUTO_TEST_CASE(first_test) +{ + BOOST_TEST(1 == 1); +}