From 172d2c2d1bee049af538b9fb2ebd5efafc38755b Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Sat, 23 Jun 2018 18:09:34 -0700 Subject: [PATCH] Miscellaneous code cleanup on "langen.cpp". NFC. $ time c++ -std=c++11 -march=native -DMAC -I/usr/local/include -DCAP_PNG=0 -O0 -w langen.cpp -o langen real 0m12.602s user 0m12.058s sys 0m0.511s --- langen.cpp | 235 +++++++++++++++++++++-------------------------------- 1 file changed, 92 insertions(+), 143 deletions(-) diff --git a/langen.cpp b/langen.cpp index 8d8d4638..4270fb35 100644 --- a/langen.cpp +++ b/langen.cpp @@ -14,35 +14,30 @@ #include #include -using std::string; -using std::map; -using std::vector; -using std::set; - template int isize(const T& x) { return x.size(); } #define NUMLAN 7 // language generator -const char *escape(string s, string dft); +const char *escape(std::string s, const std::string& dft); template struct dictionary { - map m; - void add(const string& s, const T& val) { + std::map m; + void add(const std::string& s, const T& val) { if(m.count(s)) add(s + " [repeat]", val); else m[s] = val; } - T& operator [] (const string& s) { return m[s]; } - int count(const string& s) { return m.count(s); } + T& operator [] (const std::string& s) { return m[s]; } + int count(const std::string& s) { return m.count(s); } void clear() { m.clear(); } }; -dictionary d[NUMLAN]; +dictionary d[NUMLAN]; struct noun { int genus; - string nom, nomp, acc, abl; + std::string nom, nomp, acc, abl; }; dictionary nouns[NUMLAN]; @@ -55,17 +50,16 @@ int utfsize(char c) { return 4; } -void addutftoset(set& s, string& w) { - int i = 0; -//printf("%s\n", w.c_str()); - while(i < isize(w)) { +void addutftoset(std::set& s, std::string& w) { + size_t i = 0; + while(i < w.size()) { int siz = utfsize(w[i]); s.insert(w.substr(i, siz)); i += siz; } } -void addutftoset(set& s, noun& w) { +void addutftoset(std::set& s, noun& w) { addutftoset(s, w.nom); addutftoset(s, w.nomp); addutftoset(s, w.acc); @@ -73,34 +67,22 @@ void addutftoset(set& s, noun& w) { } template -void addutftoset(set& s, dictionary& w) { - for(typename map::iterator it = w.m.begin(); it != w.m.end(); it++) - addutftoset(s, it->second); +void addutftoset(std::set& s, dictionary& w) { + for(auto&& elt : w.m) + addutftoset(s, elt.second); } -set allchars; - -void printletters(dictionary& la, dictionary& nounla, const char *lang) { - set s; - addutftoset(s, la); - addutftoset(s, nounla); - addutftoset(allchars, la); - addutftoset(allchars, nounla); -//printf("%s:", lang); -//for(set::iterator it = s.begin(); it != s.end(); it++) -// printf(" \"%s\",", it->c_str()); -//printf("\n"); - } +std::set allchars; typedef unsigned hashcode; hashcode hashval; -bool isrepeat(const string& s) { - return s.find(" [repeat]") != string::npos; +bool isrepeat(const std::string& s) { + return s.find(" [repeat]") != std::string::npos; } -hashcode langhash(const string& s) { +hashcode langhash(const std::string& s) { if(isrepeat(s)) { return langhash(s.substr(0, s.size() - 9)) + 1; } @@ -109,19 +91,19 @@ hashcode langhash(const string& s) { return r; } -map buildHashTable(set& s) { - map res; - for(set::iterator it = s.begin(); it != s.end(); it++) - res[langhash(*it)] = *it; +std::map buildHashTable(std::set& s) { + std::map res; + for(auto&& elt : s) + res[langhash(elt)] = elt; return res; } -const char *escape(string s, string dft) { +const char *escape(std::string s, const std::string& dft) { if(s == "") { printf("/*MISSING*/ "); s = dft; } - static string t; + static std::string t; t = "\""; for(int i=0; i nothe; -set plural; - -#ifdef CHECKALL -const char* allstr[] = { -#include "d" - }; -#endif - -void setstats(set& s, const char* bn) { - int tlen=0, tc = 0; - for(set::iterator it = s.begin(); it != s.end(); it++) - tc++, tlen += it->size(); - printf("// %-10s %5d %5d\n", bn, tc, tlen); - } +std::set nothe; +std::set plural; void langPL() { #define S(a,b) d[1].add(a,b); @@ -226,87 +195,68 @@ int main() { langTR(); langDE(); langPT(); // verify - set s; + std::set s; for(int i=1; i::iterator it = d[i].m.begin(); it != d[i].m.end(); it++) - s.insert(it->first); + for(auto&& elt : d[i].m) + s.insert(elt.first); printf("// DO NOT EDIT -- this file is generated automatically with langen\n\n"); - for(set::iterator x=s.begin(); x != s.end(); x++) { - string mis = "", mis1 = ""; - for(int i=1; i::iterator it = nouns[i].m.begin(); it != nouns[i].m.end(); it++) - s.insert(it->first); - - for(set::iterator x=s.begin(); x != s.end(); x++) { - string mis = "", mis1 = ""; - for(int i=1; i::iterator it = d[i].m.begin(); it != d[i].m.end(); it++) - s.insert(it->first); - - int ca = sizeof(allstr) / sizeof(char*); - for(int i=0; i::iterator x=s.begin(); x != s.end(); x++) { - bool b = false; - for(int i=0; i vchars; -//printf("ALL:"); - for(set::iterator it = allchars.begin(); it != allchars.end(); it++) { -// printf(" \"%s\",", it->c_str()); - if(isize(*it) >= 2) { javastring += (*it); vchars.push_back(*it); c++; } + std::string javastring; + std::vector vchars; + for(auto&& elt : allchars) { + if(isize(elt) >= 2) { javastring += elt; vchars.push_back(elt); } } printf("\n"); - printf("#define NUMEXTRA %d\n", c); + printf("#define NUMEXTRA %zu\n", vchars.size()); printf("#define NATCHARS {"); - for(int i=0; i::iterator it = d[i].m.begin(); it != d[i].m.end(); it++) - s.insert(it->first); + for(auto&& elt : d[i].m) + s.insert(elt.first); printf("\n//statistics\n"); - for(map::iterator it = d[1].m.begin(); it != d[1].m.end(); it++) - d[0][it->first] = it->first; - for(map::iterator it = nouns[1].m.begin(); it != nouns[1].m.end(); it++) { - noun n = it->second; - n.nom = n.nomp = n.acc = n.abl = it->first; - nouns[0][it->first] = n; + for(auto&& elt : d[1].m) + d[0][elt.first] = elt.first; + for(auto&& elt : nouns[1].m) { + noun n = elt.second; + n.nom = n.nomp = n.acc = n.abl = elt.first; + nouns[0][elt.first] = n; } - - printf("// total: %5d nouns, %5d sentences\n", isize(nouns[1].m), isize(d[1].m)); + + printf("// total: %5zu nouns, %5zu sentences\n", nouns[1].m.size(), d[1].m.size()); for(int i=0; i::iterator it = d[i].m.begin(); it != d[i].m.end(); it++) - dict += isize(it->second); - for(map::iterator it = nouns[i].m.begin(); it != nouns[i].m.end(); it++) { - noun& n = it->second; - bnouns += isize(n.nom); - bnouns += isize(n.nomp); - bnouns += isize(n.acc); - bnouns += isize(n.abl); + for(auto&& elt : d[i].m) + bdict += elt.second.size(); + for(auto&& elt : nouns[i].m) { + const noun& n = elt.second; + bnouns += n.nom.size(); + bnouns += n.nomp.size(); + bnouns += n.acc.size(); + bnouns += n.abl.size(); } - printf("// %s: %5dB nouns, %5dB sentences\n", - d[i]["EN"].c_str(), bnouns, dict); + printf("// %s: %5zuB nouns, %5zuB sentences\n", + d[i]["EN"].c_str(), bnouns, bdict); } - set allsent; - for(map::iterator it = d[1].m.begin(); it != d[1].m.end(); it++) - allsent.insert(it->first); + std::set allsent; + for(auto&& elt : d[1].m) + allsent.insert(elt.first); - set allnouns; - for(map::iterator it = nouns[1].m.begin(); it != nouns[1].m.end(); it++) - allnouns.insert(it->first); + std::set allnouns; + for(auto&& elt : nouns[1].m) + allnouns.insert(elt.first); - map ms, mn; + std::map ms, mn; do { hashval = rand(); @@ -364,16 +314,16 @@ int main() { ms = buildHashTable(allsent); mn = buildHashTable(allnouns); } - while(isize(ms) != isize(allsent) || isize(mn) != isize(allnouns)); + while(ms.size() != allsent.size() || mn.size() != allnouns.size()); printf("hashcode hashval = 0x%x;\n\n", hashval); printf("sentence all_sentences[] = {\n"); - for(map::iterator it = ms.begin(); it != ms.end(); it++) { - string s = it->second; + for(auto&& elt : ms) { + const std::string& s = elt.second; if(isrepeat(s)) printf("#if REPEATED\n"); - printf(" {0x%x, { // %s\n", it->first, escape(s, s)); + printf(" {0x%x, { // %s\n", elt.first, escape(s, s)); for(int i=1; i::iterator it = mn.begin(); it != mn.end(); it++) { - string s = it->second; + for(auto&& elt : mn) { + const std::string& s = elt.second; if(isrepeat(s)) printf("#if REPEATED\n"); - printf(" {0x%x, %d, { // \"%s\"\n", it->first, + printf(" {0x%x, %d, { // \"%s\"\n", elt.first, (nothe.count(s) ? 1:0) + (plural.count(s) ? 2:0), escape(s, s)); @@ -404,4 +354,3 @@ int main() { printf(" };\n"); } -