<commit_before>#include "ScoreBoard.h"
#include <algorithm>        // for_each
#include <iomanip>          // setw, left
#include <iostream>         // cout, endl

///////////////////////////// Score ////////////////////////////////////////////

Score::Score(void)
    : level(DEFAULT_SCORE), score(DEFAULT_SCORE), time(DEFAULT_SCORE),
      nick(DEFAULT_NICK)
{
}

Score::Score(size_t lv, size_t s, size_t t, const std::string& n)
    : level(lv), score(s), time(t), nick(n)
{
}

Score& Score::update(size_t lv, size_t s, size_t t, const std::string& n)
    noexcept
{
    level = lv;
    score = s;
    time  = t;
    nick  = n;

    return *this;
}

bool Score::operator>(const Score& s) const noexcept
{
    return score == s.score? time < s.time : score > s.score;
}

std::ostream& operator<<(std::ostream& os, const Score& obj) noexcept
{
    os << std::setw(NICK_WIDTH)  << obj.nick
       << std::setw(SCORE_WIDTH) << obj.score
       << std::setw(LEVEL_WIDTH) << obj.level
       << std::setw(TIME_WIDTH)  << obj.time;

    return os;
}


///////////////////////////// ScoreBoard ///////////////////////////////////////

ScoreBoard::ScoreBoard(const std::string& hsf, size_t hss)
    : highScoreFile(hsf), highScore(hss)
{
}

/** TODO: Test/confirm no off-by-errors
 */
ScoreBoard& ScoreBoard::update(const Score& sc) noexcept
{
    // Decide where (if) to insert the new high score
    auto rit = --highScore.rbegin();
    std::for_each(highScore.rbegin(), highScore.rend(), [&](const Score& s) {
        if (sc > s) ++rit; } );

    // Insert new highscore and remove the previously last high score
    if (rit != highScore.rbegin() - 1) {
        highScore.insert((rit + 1).base(), sc);
        highScore.pop_back();
    }

    return *this;
}

const ScoreBoard& ScoreBoard::print(void) const noexcept
{
    // Print table frame and then all valid scores
    std::cout << std::left << std::setw(TAB_WIDTH)   << ""
              << std::left << std::setw(NICK_WIDTH)  << PLAYER
              << std::left << std::setw(SCORE_WIDTH) << SCORE
              << std::left << std::setw(LEVEL_WIDTH) << LEVEL<commit_msg>soreBoard::update confirmed to work properly<commit_after>#include "ScoreBoard.h"
#include <algorithm>        // for_each
#include <iomanip>          // setw, left
#include <iostream>         // cout, endl

///////////////////////////// Score ////////////////////////////////////////////

Score::Score(void)
    : level(DEFAULT_SCORE), score(DEFAULT_SCORE), time(DEFAULT_SCORE),
      nick(DEFAULT_NICK)
{
}

Score::Score(size_t lv, size_t s, size_t t, const std::string& n)
    : level(lv), score(s), time(t), nick(n)
{
}

Score& Score::update(size_t lv, size_t s, size_t t, const std::string& n)
    noexcept
{
    level = lv;
    score = s;
    time  = t;
    nick  = n;

    return *this;
}

bool Score::operator>(const Score& s) const noexcept
{
    return score == s.score? time < s.time : score > s.score;
}

std::ostream& operator<<(std::ostream& os, const Score& obj) noexcept
{
    os << std::setw(NICK_WIDTH)  << obj.nick
       << std::setw(SCORE_WIDTH) << obj.score
       << std::setw(LEVEL_WIDTH) << obj.level
       << std::setw(TIME_WIDTH)  << obj.time;

    return os;
}


///////////////////////////// ScoreBoard ///////////////////////////////////////

ScoreBoard::ScoreBoard(const std::string& hsf, size_t hss)
    : highScoreFile(hsf), highScore(hss)
{
}

ScoreBoard& ScoreBoard::update(const Score& sc) noexcept
{
    // Decide where (if) to insert the new high score
    auto rit = --highScore.rbegin();
    std::for_each(highScore.rbegin(), highScore.rend(), [&](const Score& s) {
        if (sc > s) ++rit; } );

    // Insert new highscore and remove the previously last high score
    if (rit != highScore.rbegin() - 1) {
        highScore.insert((rit + 1).base(), sc);
        highScore.pop_back();
    }

    return *this;
}

const ScoreBoard& ScoreBoard::print(void) const noexcept
{
    // Print table frame and then all valid scores
    std::cout << std::left << std::setw(TAB_WIDTH)   << ""
              << std::left << std::setw(NICK_WIDTH)  << PLAYER
              << std::left << std::setw(SCORE_WIDTH) << SCORE
              << std::left << std::setw(LEVEL_WIDTH) << LEVEL
{'fraction_non_alphanumeric': 0.21558441558441557, 'fraction_numerical': 0.0009445100354191264, 'mean_word_length': 2.353919239904988, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3684071', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1383, 'n_words': 333}