added experimental plural result lookup

pull/12/head
mandlm 2018-06-13 21:14:36 +02:00
parent da2aa89059
commit dbf29d194b
4 changed files with 142 additions and 0 deletions

View File

@ -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;

View File

@ -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;
};

View File

@ -34,6 +34,55 @@ QVariant ResultModel::data(const QModelIndex &index, int role) const
return static_cast<uint>(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());
}

View File

@ -4,6 +4,72 @@
#include <vector>
#include <QAbstractTableModel>
class PluralPR
{
// clang-format off
const std::vector<Age> m_ages = {
{ 4, 0 },
{ 4, 6 },
{ 5, 6 },
{ 9, 0 }
};
const std::vector<std::vector<unsigned int>> 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;
};