[poincare] Define and use Unit::NumberOfBaseUnits instead of magic number

This commit is contained in:
Ruben Dashyan
2020-02-19 11:06:37 +01:00
committed by Émilie Feral
parent be2892eae5
commit 2fa71e4350
2 changed files with 10 additions and 9 deletions

View File

@@ -11,9 +11,6 @@ public:
/* The units having the same physical dimension are grouped together.
* Each such group has a standard representative with a standard prefix.
*
* A standard unit is a derived unit, when defined from base units
* or otherwise a base unit (if no definition is provided).
*
* Each representative has
* - a root symbol
* - a definition
@@ -22,6 +19,9 @@ public:
* allowed for that representative, one may get a symbol and an Expression.
*/
// There are 7 base units from which all other units are derived.
static constexpr size_t NumberOfBaseUnits = 7;
class Prefix {
public:
constexpr Prefix(const char * symbol, int8_t exponent) :
@@ -74,7 +74,7 @@ public:
class Dimension {
public:
template<typename T>
using Vector = T[7];
using Vector = T[NumberOfBaseUnits];
template <size_t N>
constexpr Dimension(const Representative (&representatives)[N], const Prefix * stdRepresentativePrefix) :
m_representatives(representatives),
@@ -220,6 +220,7 @@ public:
GigaPrefix,
TeraPrefix,
};
static constexpr size_t NumberOfBaseUnits = UnitNode::NumberOfBaseUnits;
static constexpr const Representative
TimeRepresentatives[] = {
Representative("s", nullptr,

View File

@@ -303,14 +303,14 @@ Expression Multiplication::shallowReduce(ExpressionNode::ReductionContext reduct
}
static void ExponentsCopy(Unit::Dimension::Vector<Integer> &dst, const Unit::Dimension::Vector<Integer> &src) {
for (int i = 0; i < 7; i++) {
for (size_t i = 0; i < Unit::NumberOfBaseUnits; i++) {
dst[i] = src[i];
}
}
static void ExponentsMetrics(const Unit::Dimension::Vector<Integer> &exponents, size_t & supportSize, Integer & norm) {
assert(supportSize == 0 && norm.isZero());
for (int i = 0; i < 7; i++) {
for (size_t i = 0; i < Unit::NumberOfBaseUnits; i++) {
Integer unsignedExponent = exponents[i];
unsignedExponent.setNegative(false);
if (!unsignedExponent.isZero()) {
@@ -346,7 +346,7 @@ static void ExponentsOfBaseUnits(const Expression units, Unit::Dimension::Vector
// Fill the exponents array with the unit's exponent
const int indexInTable = static_cast<Unit &>(factor).dimension() - Unit::DimensionTable;
assert(0 <= indexInTable && indexInTable < 7);
assert(0 <= indexInTable && indexInTable < Unit::NumberOfBaseUnits);
exponents[indexInTable] = exponent;
}
}
@@ -368,7 +368,7 @@ static bool CanSimplifyUnitProduct(
Integer(0),
Integer(0),
};
for (int i = 0; i < 7; i++) {
for (size_t i = 0; i < Unit::NumberOfBaseUnits; i++) {
simplifiedExponents[i] = operationOnExponents(unitsExponents[i], entryUnitExponents[i]);
}
size_t simplifiedSupportSize = 0;
@@ -445,7 +445,7 @@ Expression Multiplication::shallowBeautify(ExpressionNode::ReductionContext redu
Integer bestUnitNorm(0);
size_t bestRemainderSupportSize = unitsSupportSize - 1;
Integer bestRemainderNorm = unitsNorm;
for (const Unit::Dimension * dim = Unit::DimensionTable + 7; dim < Unit::DimensionTableUpperBound; dim++) {
for (const Unit::Dimension * dim = Unit::DimensionTable + Unit::NumberOfBaseUnits; dim < Unit::DimensionTableUpperBound; dim++) {
Unit entryUnit = Unit::Builder(dim, dim->stdRepresentative(), dim->stdRepresentativePrefix());
Unit::Dimension::Vector<Integer> entryUnitExponents = {
Integer(0),