From dbf29d194b8d2bf37345384e352d2b7ac20693ea Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 13 Jun 2018 21:14:36 +0200 Subject: [PATCH] added experimental plural result lookup --- source/Age.cpp | 10 +++++ source/Age.h | 2 + source/ResultWidget/ResultModel.cpp | 60 +++++++++++++++++++++++++ source/ResultWidget/ResultModel.h | 70 +++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) diff --git a/source/Age.cpp b/source/Age.cpp index 1069357..2f406c6 100644 --- a/source/Age.cpp +++ b/source/Age.cpp @@ -37,6 +37,16 @@ Age::Age(const QDate &birth, const QDate &reference) m_months = months; } +bool Age::operator<(const Age &cmp) const +{ + if (m_years == cmp.m_years) + { + return m_months < cmp.m_months; + } + + return m_years < cmp.m_years; +} + unsigned int Age::years() const { return m_years; diff --git a/source/Age.h b/source/Age.h index 93b4e5e..1ad28a2 100644 --- a/source/Age.h +++ b/source/Age.h @@ -13,6 +13,8 @@ public: Age(unsigned int years, unsigned int months); Age(const QDate &birth, const QDate &reference); + bool operator<(const Age &cmp) const; + unsigned int years() const; unsigned int months() const; }; diff --git a/source/ResultWidget/ResultModel.cpp b/source/ResultWidget/ResultModel.cpp index 6eb8216..e750760 100644 --- a/source/ResultWidget/ResultModel.cpp +++ b/source/ResultWidget/ResultModel.cpp @@ -34,6 +34,55 @@ QVariant ResultModel::data(const QModelIndex &index, int role) const return static_cast(points); } } + break; + case 1: + switch (index.column()) + { + case 8: + { + auto pR = getPluralPR(); + if (pR >= 84) + { + return pR; + } + } + break; + default: + break; + } + break; + case 2: + switch (index.column()) + { + case 8: + { + auto pR = getPluralPR(); + if (pR < 84 && pR > 16) + { + return pR; + } + } + break; + default: + break; + } + break; + case 3: + switch (index.column()) + { + case 8: + { + auto pR = getPluralPR(); + if (pR <= 16) + { + return pR; + } + } + break; + default: + break; + } + break; default: break; } @@ -84,6 +133,7 @@ void ResultModel::setAge(const Age &age) { qDebug() << "Age:" << age.years() << "years" << age.months() << "months"; m_age = age; + emit dataChanged(index(1, 0), index(4, 8)); } void ResultModel::setPluralResult(size_t points) @@ -94,3 +144,13 @@ void ResultModel::setPluralResult(size_t points) emit dataChanged(index(0, 8), index(4, 8)); } } + +unsigned int ResultModel::getPluralPoints() const +{ + return m_results[8].points(); +} + +unsigned int ResultModel::getPluralPR() const +{ + return PluralPR().lookup(m_age, getPluralPoints()); +} diff --git a/source/ResultWidget/ResultModel.h b/source/ResultWidget/ResultModel.h index f626d3b..178ce0a 100644 --- a/source/ResultWidget/ResultModel.h +++ b/source/ResultWidget/ResultModel.h @@ -4,6 +4,72 @@ #include #include +class PluralPR +{ + // clang-format off + const std::vector m_ages = { + { 4, 0 }, + { 4, 6 }, + { 5, 6 }, + { 9, 0 } + }; + + const std::vector> m_PRs = { + { 0, 0, 0 }, + { 0, 1, 0 }, + { 0, 1, 0 }, + { 1, 1, 0 }, + { 7, 2, 1 }, + { 10, 4, 1}, + { 26, 10, 2 }, + { 57, 25, 7 }, + { 79, 56, 27 }, + { 100, 100, 100 } + }; + // clang-format on + +public: + unsigned int lookup(const Age &age, const unsigned int &points) + { + if (points >= m_PRs.size()) + { + return 0; + } + + auto ageIndex = [&]() -> size_t { + if (m_ages.empty()) + { + return 0; + } + + if (age < m_ages.front()) + { + return 0; + } + + if (m_ages.back() < age) + { + return m_ages.size() - 1; + } + + for (size_t index = 1; index < m_ages.size(); ++index) + { + if (age < m_ages.at(index)) + { + return index - 1; + } + } + }(); + + if (ageIndex >= m_PRs.at(points).size()) + { + return 0; + } + + return m_PRs.at(points).at(ageIndex); + } +}; + class TestResult { private: @@ -55,4 +121,8 @@ public: void setAge(const Age &age); void setPluralResult(size_t points); + +private: + unsigned int getPluralPoints() const; + unsigned int getPluralPR() const; };