Skip to content

Commit

Permalink
changed several iterators to references (performance improvement)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kelley committed Jun 5, 2022
1 parent ef8bd84 commit 7fa68a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion WordleArchive/WordleArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class WordlePlayer {
void printPrevGuesses() {
prepKeyboard();

for (auto guess : valid_guesses) {
for (string& guess : valid_guesses) {
for (std::string::size_type i = 0; i < (59 - current_word.word.size()) / 2; ++i) {
cout << Color::defaults << " ";
}
Expand Down
24 changes: 12 additions & 12 deletions WordleArchive/wordle_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void WordleStatistics::readFromFile(string filename) {
if (game.state == won) {
++guess_frequencies.at(game.guesses.size() - 1);
} else {
++guess_frequencies[7 - 1];
++guess_frequencies[6];
}
}
games.emplace(word, game);
Expand Down Expand Up @@ -101,17 +101,17 @@ void WordleStatistics::writeToFile() {
// categorize games
std::vector<WordleGame> finished_games;
std::vector<WordleGame> unfinished_games;
for (auto pair : games) {
for (auto& pair : games) {
WordleGame game = pair.second;
if (game.state != unfinished) finished_games.push_back(game);
else unfinished_games.push_back(game);
}
ofs << "Completed Games\n";
ofs << "Wordle number (-1 for custom),Correct word,Won? (1 for yes; 0 for no),Guesses---->\n";
if (!finished_games.empty()) {
for (auto game : finished_games) {
for (auto& game : finished_games) {
ofs << game.word_number << ',' << game.word << ',' << (game.state == won);
for (auto guess : game.guesses) {
for (auto& guess : game.guesses) {
ofs << ',' << guess;
}
ofs << '\n';
Expand All @@ -122,9 +122,9 @@ void WordleStatistics::writeToFile() {
if (!unfinished_games.empty()) {
ofs << "Unfinished Games\n";
ofs << "Wordle number,Correct word,Guesses---->\n";
for (auto game : unfinished_games) {
for (auto& game : unfinished_games) {
ofs << game.word_number << ',' << game.word;
for (auto guess : game.guesses) {
for (auto& guess : game.guesses) {
ofs << ',' << guess;
}
ofs << '\n';
Expand Down Expand Up @@ -157,7 +157,7 @@ void WordleStatistics::addEntry(WordleWord word, std::vector<string> guesses) {
else state = lost;
WordleGame game = {word.number, word.word, guesses, state};
if (state == won && word.word.size() == 5) ++guess_frequencies[guesses.size() - 1];
else if (state == lost && word.word.size() == 5) ++guess_frequencies[7 - 1];
else if (state == lost && word.word.size() == 5) ++guess_frequencies[6];
if (!inFile(word)) {
// completely new game never played before
games.emplace(word.word, game);
Expand All @@ -169,7 +169,7 @@ void WordleStatistics::addEntry(WordleWord word, std::vector<string> guesses) {
--guess_frequencies[games[word.word].guesses.size() - 1];
break;
case lost:
--guess_frequencies[7 - 1];
--guess_frequencies[6];
break;
default:
break;
Expand All @@ -191,7 +191,7 @@ WordleGame WordleStatistics::loadEntry(WordleWord word) {
};
return game;
}
for (auto pair : games) {
for (auto& pair : games) {
WordleGame game = pair.second;
if (game.word == word.word) {
return game;
Expand All @@ -202,7 +202,7 @@ WordleGame WordleStatistics::loadEntry(WordleWord word) {

size_t WordleStatistics::numFinishedGames() {
size_t num_finished = 0;
for (auto pair : games) {
for (auto& pair : games) {
WordleGame game = pair.second;
if (game.state != unfinished) ++num_finished;
}
Expand All @@ -212,7 +212,7 @@ size_t WordleStatistics::numFinishedGames() {
double WordleStatistics::calcWinRate() {
double num_won = 0.0;
double num_finished = (double)games.size();
for (auto pair : games) {
for (auto& pair : games) {
WordleGame game = pair.second;
if (game.state == won) ++num_won;
else if (game.state == unfinished) --num_finished;
Expand All @@ -223,7 +223,7 @@ double WordleStatistics::calcWinRate() {
double WordleStatistics::calcAverageNumGuesses() {
double num_guesses = 0.0;
double num_finished = 0.0;
for (auto pair : games) {
for (auto& pair : games) {
WordleGame game = pair.second;
num_guesses += (double)game.guesses.size();
if (game.state == lost) ++num_guesses;
Expand Down

0 comments on commit 7fa68a0

Please sign in to comment.