src/utilities/units/Unit.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Unit.hpp" | ||
| 7 | #include "Unit_Impl.hpp" | ||
| 8 | #include "Quantity.hpp" | ||
| 9 | #include "QuantityConverter.hpp" | ||
| 10 | |||
| 11 | #include "ScaleFactory.hpp" | ||
| 12 | #include "QuantityRegex.hpp" | ||
| 13 | #include "UnitFactory.hpp" | ||
| 14 | |||
| 15 | #include "../core/Compare.hpp" | ||
| 16 | #include "../core/Assert.hpp" | ||
| 17 | #include "../core/Containers.hpp" | ||
| 18 | |||
| 19 | #include <algorithm> | ||
| 20 | #include <sstream> | ||
| 21 | |||
| 22 | namespace openstudio { | ||
| 23 | namespace detail { | ||
| 24 | |||
| 25 | 142871 | Unit_Impl::Unit_Impl(int scaleExponent, const std::string& prettyString) | |
| 26 |
4/4✓ Branch 3 taken 142871 times.
✓ Branch 6 taken 142871 times.
✓ Branch 9 taken 142871 times.
✓ Branch 12 taken 142871 times.
|
142871 | : m_units(), m_scale(ScaleFactory::instance().createScale(scaleExponent)), m_prettyString(prettyString), m_system(UnitSystem::Mixed) { |
| 27 |
3/3✓ Branch 1 taken 142871 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 142870 times.
|
142871 | if (scale().value == 0.0) { |
| 28 |
17/17✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 37 taken 1 time.
✓ Branch 40 taken 1 time.
✓ Branch 43 taken 1 time.
✓ Branch 46 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
|
1 | LOG_AND_THROW("Unit constructed with invalid scale exponent. " << "ScaleFactory::instance().registeredScales(), or << ScaleFactory::instance() " |
| 29 | << "to see what is available."); | ||
| 30 | } | ||
| 31 | 142874 | } | |
| 32 | |||
| 33 | 6 | Unit_Impl::Unit_Impl(const std::string& scaleAbbreviation, const std::string& prettyString) | |
| 34 |
4/4✓ Branch 3 taken 6 times.
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
✓ Branch 12 taken 6 times.
|
6 | : m_units(), m_scale(ScaleFactory::instance().createScale(scaleAbbreviation)), m_prettyString(prettyString), m_system(UnitSystem::Mixed) { |
| 35 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 5 times.
|
6 | if (scale().value == 0.0) { |
| 36 |
17/17✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 37 taken 1 time.
✓ Branch 40 taken 1 time.
✓ Branch 43 taken 1 time.
✓ Branch 46 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
|
1 | LOG_AND_THROW("Unit constructed with invalid scaleAbbreviation. " |
| 37 | << "ScaleFactory::instance().registeredScales(), or << ScaleFactory::instance() " << "to see what is available."); | ||
| 38 | } | ||
| 39 | 9 | } | |
| 40 | |||
| 41 | 42716 | Unit_Impl::Unit_Impl(int scaleExponent, const std::string& prettyString, int numBaseUnits, UnitSystem system) | |
| 42 |
4/4✓ Branch 2 taken 42716 times.
✓ Branch 5 taken 42716 times.
✓ Branch 8 taken 42716 times.
✓ Branch 11 taken 42716 times.
|
128148 | : m_units(numBaseUnits), m_scale(ScaleFactory::instance().createScale(scaleExponent)), m_prettyString(prettyString), m_system(system) { |
| 43 |
2/3✓ Branch 1 taken 42716 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 42716 times.
|
42716 | if (scale().value == 0.0) { |
| 44 | ✗ | LOG_AND_THROW("Unit constructed with invalid scale exponent. " << "ScaleFactory::instance().registeredScales(), or << ScaleFactory::instance() " | |
| 45 | << "to see what is available."); | ||
| 46 | } | ||
| 47 | 42716 | } | |
| 48 | |||
| 49 | 2 | Unit_Impl::Unit_Impl(const std::string& scaleAbbreviation, const std::string& prettyString, int numBaseUnits, UnitSystem system) | |
| 50 |
4/4✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 11 taken 2 times.
|
6 | : m_units(numBaseUnits), m_scale(ScaleFactory::instance().createScale(scaleAbbreviation)), m_prettyString(prettyString), m_system(system) { |
| 51 |
2/3✓ Branch 1 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (scale().value == 0.0) { |
| 52 | ✗ | LOG_AND_THROW("Unit constructed with invalid scaleAbbreviation. " | |
| 53 | << "ScaleFactory::instance().registeredScales(), or << ScaleFactory::instance() " << "to see what is available."); | ||
| 54 | } | ||
| 55 | 2 | } | |
| 56 | |||
| 57 | 1618 | Unit Unit_Impl::clone() const { | |
| 58 |
3/5✓ Branch 1 taken 1618 times.
✓ Branch 4 taken 1618 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1618 times.
✗ Branch 10 not taken.
|
1618 | OS_ASSERT(system() == UnitSystem::Mixed); |
| 59 |
3/5✓ Branch 1 taken 1618 times.
✓ Branch 4 taken 1618 times.
✓ Branch 7 taken 1618 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
1618 | std::shared_ptr<Unit_Impl> impl(new Unit_Impl(*this)); |
| 60 |
1/1✓ Branch 2 taken 1618 times.
|
3236 | return Unit(impl); |
| 61 | 1618 | } | |
| 62 | |||
| 63 | 301 | Unit Unit_Impl::cloneToMixed() const { | |
| 64 |
3/5✓ Branch 1 taken 301 times.
✓ Branch 4 taken 301 times.
✓ Branch 7 taken 301 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
301 | std::shared_ptr<Unit_Impl> impl(new Unit_Impl(*this)); |
| 65 |
1/1✓ Branch 1 taken 301 times.
|
301 | impl->m_system = UnitSystem::Mixed; |
| 66 |
1/1✓ Branch 2 taken 301 times.
|
602 | return Unit(impl); |
| 67 | 301 | } | |
| 68 | |||
| 69 | 135968 | std::vector<std::string> Unit_Impl::baseUnits() const { | |
| 70 | // create vector of correct size to hold baseUnit strings | ||
| 71 |
1/1✓ Branch 2 taken 135968 times.
|
135968 | std::vector<std::string> result(m_units.size()); |
| 72 | 135968 | std::vector<std::string>::iterator resultIter; | |
| 73 | |||
| 74 | 135968 | std::vector<UnitElement>::const_iterator unitsIter; | |
| 75 | 135968 | auto unitsEnd = m_units.end(); | |
| 76 |
2/2✓ Branch 5 taken 324369 times.
✓ Branch 6 taken 135968 times.
|
460337 | for (resultIter = result.begin(), unitsIter = m_units.begin(); unitsIter != unitsEnd; ++unitsIter, ++resultIter) { |
| 77 |
1/1✓ Branch 3 taken 324369 times.
|
324369 | *resultIter = unitsIter->first; // copy baseUnit into result vector |
| 78 | } | ||
| 79 | |||
| 80 | 271936 | return result; | |
| 81 | ✗ | } | |
| 82 | |||
| 83 | 144294 | bool Unit_Impl::isBaseUnit(const std::string& baseUnit) const { | |
| 84 |
1/1✓ Branch 1 taken 144294 times.
|
144294 | auto loc = findBaseUnit(baseUnit); |
| 85 |
2/2✓ Branch 2 taken 144274 times.
✓ Branch 3 taken 20 times.
|
144294 | if (loc == m_units.end()) { |
| 86 | 144274 | return false; | |
| 87 | } | ||
| 88 | 20 | return true; | |
| 89 | } | ||
| 90 | |||
| 91 | 569776 | int Unit_Impl::baseUnitExponent(const std::string& baseUnit) const { | |
| 92 |
1/1✓ Branch 1 taken 569776 times.
|
569776 | auto loc = findBaseUnit(baseUnit); |
| 93 |
2/2✓ Branch 2 taken 797 times.
✓ Branch 3 taken 568979 times.
|
569776 | if (loc == m_units.end()) { |
| 94 | 797 | return 0; | |
| 95 | } else { | ||
| 96 | 568979 | return loc->second; | |
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | 146046 | void Unit_Impl::setBaseUnitExponent(const std::string& baseUnit, int exponent) { | |
| 101 |
1/1✓ Branch 1 taken 146046 times.
|
146046 | auto loc = findBaseUnit(baseUnit); |
| 102 |
2/2✓ Branch 2 taken 27 times.
✓ Branch 3 taken 146019 times.
|
146046 | if (loc != m_units.end()) { |
| 103 | 27 | loc->second = exponent; | |
| 104 | } else { | ||
| 105 |
2/2✓ Branch 1 taken 146019 times.
✓ Branch 4 taken 146019 times.
|
146019 | m_units.push_back(UnitElement(baseUnit, exponent)); |
| 106 | } | ||
| 107 | 146046 | } | |
| 108 | |||
| 109 | 221433 | Scale Unit_Impl::scale() const { | |
| 110 | 221433 | return m_scale(); | |
| 111 | } | ||
| 112 | |||
| 113 | 1670 | bool Unit_Impl::setScale(int scaleExponent) { | |
| 114 |
2/2✓ Branch 1 taken 1670 times.
✓ Branch 4 taken 1670 times.
|
1670 | ScaleConstant candidate = ScaleFactory::instance().createScale(scaleExponent); |
| 115 |
3/3✓ Branch 1 taken 1670 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1669 times.
|
1670 | if (candidate().value == 0.0) { |
| 116 | 1 | return false; | |
| 117 | } | ||
| 118 |
1/1✓ Branch 1 taken 1669 times.
|
1669 | m_scale = candidate; |
| 119 | 1669 | return true; | |
| 120 | 1670 | } | |
| 121 | |||
| 122 | 92 | bool Unit_Impl::setScale(const std::string& scaleAbbreviation) { | |
| 123 |
2/2✓ Branch 1 taken 92 times.
✓ Branch 4 taken 92 times.
|
92 | ScaleConstant candidate = ScaleFactory::instance().createScale(scaleAbbreviation); |
| 124 |
2/3✓ Branch 1 taken 92 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 92 times.
|
92 | if (candidate().value == 0.0) { |
| 125 | ✗ | return false; | |
| 126 | } | ||
| 127 |
1/1✓ Branch 1 taken 92 times.
|
92 | m_scale = candidate; |
| 128 | 92 | return true; | |
| 129 | 92 | } | |
| 130 | |||
| 131 | 47969 | std::string Unit_Impl::standardString(bool withScale) const { | |
| 132 |
9/11✓ Branch 0 taken 451 times.
✓ Branch 1 taken 47518 times.
✓ Branch 3 taken 451 times.
✓ Branch 6 taken 398 times.
✓ Branch 7 taken 53 times.
✓ Branch 8 taken 451 times.
✓ Branch 9 taken 47518 times.
✓ Branch 11 taken 398 times.
✓ Branch 12 taken 47571 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
47969 | if (withScale && (scale().abbr.empty())) { |
| 133 | 398 | withScale = false; | |
| 134 | } | ||
| 135 | |||
| 136 |
1/1✓ Branch 1 taken 47969 times.
|
47969 | std::stringstream result; |
| 137 | 47969 | bool parentheses(false); | |
| 138 | |||
| 139 | // determine number of non-zero, positive, and negative baseUnits | ||
| 140 | 47969 | int nnz(0); | |
| 141 | 47969 | int npos(0); | |
| 142 | 47969 | std::vector<UnitElement>::const_iterator unitsIter; | |
| 143 | 47969 | std::vector<UnitElement>::const_iterator firstPosIter; | |
| 144 | 47969 | std::vector<UnitElement>::const_iterator firstNegIter; | |
| 145 | 47969 | auto unitsEnd = m_units.end(); | |
| 146 | |||
| 147 | 47969 | firstPosIter = unitsEnd; | |
| 148 | 47969 | firstNegIter = unitsEnd; | |
| 149 | |||
| 150 |
2/2✓ Branch 3 taken 462893 times.
✓ Branch 4 taken 47969 times.
|
510862 | for (unitsIter = m_units.begin(); unitsIter != unitsEnd; ++unitsIter) { |
| 151 |
2/2✓ Branch 1 taken 44374 times.
✓ Branch 2 taken 418519 times.
|
462893 | if (unitsIter->second > 0) { |
| 152 | 44374 | ++npos; | |
| 153 | 44374 | ++nnz; | |
| 154 |
2/2✓ Branch 1 taken 37096 times.
✓ Branch 2 taken 7278 times.
|
44374 | if (firstPosIter == unitsEnd) { |
| 155 | 37096 | firstPosIter = unitsIter; | |
| 156 | } | ||
| 157 | } | ||
| 158 |
2/2✓ Branch 1 taken 33938 times.
✓ Branch 2 taken 428955 times.
|
462893 | if (unitsIter->second < 0) { |
| 159 | 33938 | ++nnz; | |
| 160 |
2/2✓ Branch 1 taken 25384 times.
✓ Branch 2 taken 8554 times.
|
33938 | if (firstNegIter == unitsEnd) { |
| 161 | 25384 | firstNegIter = unitsIter; | |
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 47916 times.
|
47969 | if (withScale) { |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
|
53 | if (nnz == 0) { |
| 168 | ✗ | parentheses = true; | |
| 169 | ✗ | result << scale().abbr << "("; | |
| 170 | } else { | ||
| 171 |
1/1✓ Branch 1 taken 53 times.
|
53 | ScaleConstant printScale = m_scale; |
| 172 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 3 times.
|
53 | if (npos > 0) { |
| 173 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 44 times.
|
50 | if (firstPosIter->second > 1) { |
| 174 | 6 | parentheses = true; | |
| 175 | // see if can pull scale into exponent | ||
| 176 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2 times.
|
6 | if (scale().exponent % firstPosIter->second == 0) { |
| 177 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 8 taken 4 times.
|
4 | ScaleConstant candidateScale = ScaleFactory::instance().createScale(scale().exponent / firstPosIter->second); |
| 178 |
2/3✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | if (candidateScale().value != 0.0) { |
| 179 |
1/1✓ Branch 1 taken 4 times.
|
4 | printScale = candidateScale; |
| 180 | 4 | parentheses = false; | |
| 181 | } | ||
| 182 | 4 | } | |
| 183 | } | ||
| 184 |
2/2✓ Branch 1 taken 50 times.
✓ Branch 4 taken 50 times.
|
50 | result << printScale().abbr; |
| 185 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 48 times.
|
50 | if (parentheses) { |
| 186 |
1/1✓ Branch 1 taken 2 times.
|
2 | result << "("; |
| 187 | } | ||
| 188 | } else { | ||
| 189 | // all baseUnits in the denominator | ||
| 190 | // see if can pull scale into exponent of first baseUnit in denominator | ||
| 191 | 3 | parentheses = true; | |
| 192 |
2/3✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
3 | if (scale().exponent % -firstNegIter->second == 0) { |
| 193 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
|
3 | ScaleConstant candidateScale = ScaleFactory::instance().createScale(scale().exponent / firstNegIter->second); |
| 194 |
2/3✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
3 | if (candidateScale().value != 0.0) { |
| 195 |
1/1✓ Branch 1 taken 3 times.
|
3 | printScale = candidateScale; |
| 196 | 3 | parentheses = false; | |
| 197 | } | ||
| 198 | 3 | } | |
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (parentheses) { |
| 200 | ✗ | result << printScale().abbr << "("; | |
| 201 | } else { | ||
| 202 |
4/4✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
|
3 | result << 1 << "/" << printScale().abbr; |
| 203 | // go ahead and print denominator of standard string | ||
| 204 |
2/2✓ Branch 2 taken 14 times.
✓ Branch 3 taken 3 times.
|
17 | for (unitsIter = firstNegIter; unitsIter != unitsEnd; ++unitsIter) { |
| 205 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 11 times.
|
14 | if (unitsIter->second < 0) { |
| 206 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (unitsIter != firstNegIter) { |
| 207 | ✗ | result << "*"; | |
| 208 | } | ||
| 209 |
1/1✓ Branch 2 taken 3 times.
|
3 | result << unitsIter->first; |
| 210 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (unitsIter->second < -1) { |
| 211 | ✗ | result << "^" << -unitsIter->second; | |
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 |
1/1✓ Branch 1 taken 3 times.
|
3 | return result.str(); |
| 216 | } | ||
| 217 | } | ||
| 218 | 53 | } | |
| 219 | } | ||
| 220 | |||
| 221 | // print numerator of standard string | ||
| 222 |
2/2✓ Branch 2 taken 329908 times.
✓ Branch 3 taken 47966 times.
|
377874 | for (unitsIter = firstPosIter; unitsIter != unitsEnd; ++unitsIter) { |
| 223 | 329908 | int exp = unitsIter->second; // determines action and may be output | |
| 224 |
2/2✓ Branch 0 taken 44374 times.
✓ Branch 1 taken 285534 times.
|
329908 | if (exp > 0) { |
| 225 |
2/2✓ Branch 1 taken 7278 times.
✓ Branch 2 taken 37096 times.
|
44374 | if (unitsIter != firstPosIter) { |
| 226 |
1/1✓ Branch 1 taken 7278 times.
|
7278 | result << "*"; |
| 227 | } | ||
| 228 |
1/1✓ Branch 2 taken 44374 times.
|
44374 | result << unitsIter->first; |
| 229 |
2/2✓ Branch 0 taken 10484 times.
✓ Branch 1 taken 33890 times.
|
44374 | if (exp > 1) { |
| 230 |
2/2✓ Branch 1 taken 10484 times.
✓ Branch 4 taken 10484 times.
|
10484 | result << "^" << exp; |
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | // print denominator of standard string | ||
| 236 |
2/2✓ Branch 2 taken 157613 times.
✓ Branch 3 taken 47966 times.
|
205579 | for (unitsIter = firstNegIter; unitsIter != unitsEnd; ++unitsIter) { |
| 237 | 157613 | int exp = unitsIter->second; | |
| 238 |
2/2✓ Branch 0 taken 33935 times.
✓ Branch 1 taken 123678 times.
|
157613 | if (exp < 0) { |
| 239 |
2/2✓ Branch 1 taken 8554 times.
✓ Branch 2 taken 25381 times.
|
33935 | if (unitsIter != firstNegIter) { |
| 240 |
1/1✓ Branch 1 taken 8554 times.
|
8554 | result << "*"; |
| 241 | } else { | ||
| 242 |
2/2✓ Branch 0 taken 14730 times.
✓ Branch 1 taken 10651 times.
|
25381 | if (npos > 0) { |
| 243 |
1/1✓ Branch 1 taken 14730 times.
|
14730 | result << "/"; |
| 244 | } else { | ||
| 245 |
1/1✓ Branch 1 taken 10651 times.
|
10651 | result << "1/"; |
| 246 | } | ||
| 247 | } | ||
| 248 |
3/3✓ Branch 2 taken 33935 times.
✓ Branch 4 taken 409 times.
✓ Branch 5 taken 33526 times.
|
33935 | if (unitsIter->first == "people") { |
| 249 |
1/1✓ Branch 1 taken 409 times.
|
409 | result << "person"; |
| 250 | } else { | ||
| 251 |
1/1✓ Branch 2 taken 33526 times.
|
33526 | result << unitsIter->first; |
| 252 | } | ||
| 253 |
2/2✓ Branch 0 taken 7845 times.
✓ Branch 1 taken 26090 times.
|
33935 | if (exp < -1) { |
| 254 |
2/2✓ Branch 1 taken 7845 times.
✓ Branch 4 taken 7845 times.
|
7845 | result << "^" << -exp; |
| 255 | } | ||
| 256 | } // if | ||
| 257 | } // for | ||
| 258 | |||
| 259 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 47964 times.
|
47966 | if (parentheses) { |
| 260 |
1/1✓ Branch 1 taken 2 times.
|
2 | result << ")"; |
| 261 | } | ||
| 262 | |||
| 263 |
1/1✓ Branch 1 taken 47966 times.
|
47966 | return result.str(); |
| 264 | 47969 | } | |
| 265 | |||
| 266 | 29128 | std::string Unit_Impl::prettyString(bool withScale) const { | |
| 267 | |||
| 268 |
11/13✓ Branch 0 taken 199 times.
✓ Branch 1 taken 28929 times.
✓ Branch 3 taken 161 times.
✓ Branch 4 taken 38 times.
✓ Branch 6 taken 161 times.
✓ Branch 8 taken 13 times.
✓ Branch 9 taken 148 times.
✓ Branch 10 taken 161 times.
✓ Branch 11 taken 28967 times.
✓ Branch 13 taken 13 times.
✓ Branch 14 taken 29115 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
29128 | if (withScale && !m_prettyString.empty() && (scale().exponent != 0)) { |
| 269 |
1/1✓ Branch 1 taken 13 times.
|
13 | Unit wUnit = parseUnitString(m_prettyString); |
| 270 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 4 taken 13 times.
|
13 | wUnit.setScale(scale().exponent); |
| 271 |
1/1✓ Branch 1 taken 13 times.
|
13 | return wUnit.standardString(withScale); |
| 272 | 13 | } | |
| 273 | |||
| 274 | 29115 | return m_prettyString; | |
| 275 | } | ||
| 276 | |||
| 277 | 6351 | void Unit_Impl::setPrettyString(const std::string& str) { | |
| 278 | 6351 | m_prettyString = str; | |
| 279 | 6351 | } | |
| 280 | |||
| 281 | 95 | std::string Unit_Impl::print(bool withScale) const { | |
| 282 |
3/3✓ Branch 1 taken 95 times.
✓ Branch 5 taken 21 times.
✓ Branch 6 taken 74 times.
|
95 | if (!prettyString(false).empty()) { |
| 283 | 21 | return prettyString(withScale); | |
| 284 | } | ||
| 285 | 74 | return standardString(withScale); | |
| 286 | } | ||
| 287 | |||
| 288 | 2550641 | UnitSystem Unit_Impl::system() const { | |
| 289 | 2550641 | return m_system; | |
| 290 | } | ||
| 291 | |||
| 292 | 8583 | bool Unit_Impl::operator==(const Unit& rUnit) const { | |
| 293 | |||
| 294 | // construct union of base units | ||
| 295 | 8583 | StringSet allBaseUnits; | |
| 296 | 8583 | StringVector baseUnits; | |
| 297 |
1/1✓ Branch 1 taken 8583 times.
|
8583 | baseUnits = this->baseUnits(); |
| 298 |
1/1✓ Branch 3 taken 8583 times.
|
8583 | allBaseUnits.insert(baseUnits.begin(), baseUnits.end()); |
| 299 |
1/1✓ Branch 1 taken 8583 times.
|
8583 | baseUnits = rUnit.baseUnits(); |
| 300 |
1/1✓ Branch 3 taken 8583 times.
|
8583 | allBaseUnits.insert(baseUnits.begin(), baseUnits.end()); |
| 301 | |||
| 302 | // loop through and see if exponents are equal | ||
| 303 |
2/2✓ Branch 5 taken 87725 times.
✓ Branch 6 taken 8419 times.
|
96144 | for (const std::string& baseUnit : allBaseUnits) { |
| 304 |
4/4✓ Branch 1 taken 87725 times.
✓ Branch 4 taken 87725 times.
✓ Branch 6 taken 164 times.
✓ Branch 7 taken 87561 times.
|
87725 | if (baseUnitExponent(baseUnit) != rUnit.baseUnitExponent(baseUnit)) { |
| 305 | 164 | return false; | |
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | 8419 | return true; | |
| 310 | 8583 | } | |
| 311 | |||
| 312 | 1124 | void Unit_Impl::operator*=(const Unit& rUnit) { | |
| 313 | |||
| 314 |
6/8✓ Branch 1 taken 1124 times.
✓ Branch 4 taken 721 times.
✓ Branch 5 taken 403 times.
✓ Branch 7 taken 721 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 721 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1124 times.
|
1124 | if ((m_system != rUnit.system()) && (m_system != UnitSystem::Mixed)) { |
| 315 | ✗ | LOG_AND_THROW("Cannot " << m_system.valueName() << " *= " << rUnit.system().valueName() | |
| 316 | << ". Must have same systems, or UnitSystem::Mixed on the left-hand side."); | ||
| 317 | } | ||
| 318 | |||
| 319 |
2/2✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1123 times.
|
1124 | if (this == rUnit.getImpl<Unit_Impl>().get()) { |
| 320 | // u1 * u1 | ||
| 321 |
1/1✓ Branch 1 taken 1 time.
|
1 | this->pow(2); |
| 322 | 1 | return; | |
| 323 | } | ||
| 324 | |||
| 325 | // u1 * u2, loop through both | ||
| 326 | 1123 | std::vector<UnitElement>::iterator lUnitsIter; | |
| 327 | 1123 | auto lUnitsEnd = m_units.end(); | |
| 328 | 1123 | std::vector<UnitElement>::const_iterator rUnitsIter; | |
| 329 | 1123 | auto rUnitsEnd = rUnit.getImpl<detail::Unit_Impl>()->m_units.end(); | |
| 330 | 1123 | bool ordered = true; | |
| 331 | 1123 | for (lUnitsIter = m_units.begin(), rUnitsIter = rUnit.getImpl<detail::Unit_Impl>()->m_units.begin(); | |
| 332 |
6/6✓ Branch 2 taken 5343 times.
✓ Branch 3 taken 353 times.
✓ Branch 5 taken 5287 times.
✓ Branch 6 taken 56 times.
✓ Branch 7 taken 5287 times.
✓ Branch 8 taken 409 times.
|
5696 | (lUnitsIter != lUnitsEnd) && (rUnitsIter != rUnitsEnd); ++lUnitsIter, ++rUnitsIter) { |
| 333 |
2/2✓ Branch 3 taken 714 times.
✓ Branch 4 taken 4573 times.
|
5287 | if (lUnitsIter->first != rUnitsIter->first) { |
| 334 | // different order | ||
| 335 | 714 | ordered = false; | |
| 336 | 714 | break; | |
| 337 | } else { | ||
| 338 | 4573 | lUnitsIter->second += rUnitsIter->second; | |
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 |
6/6✓ Branch 0 taken 409 times.
✓ Branch 1 taken 714 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 396 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 1110 times.
|
1123 | if (ordered && (rUnitsIter != rUnitsEnd)) { |
| 343 | // finish looping through rUnits and append any non-zero exponented baseUnits | ||
| 344 |
2/2✓ Branch 2 taken 116 times.
✓ Branch 3 taken 13 times.
|
129 | for (; rUnitsIter != rUnitsEnd; ++rUnitsIter) { |
| 345 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 2 taken 100 times.
|
116 | if (rUnitsIter->second != 0) { |
| 346 |
1/1✓ Branch 3 taken 16 times.
|
16 | setBaseUnitExponent(rUnitsIter->first, rUnitsIter->second); |
| 347 | } | ||
| 348 | } | ||
| 349 |
2/2✓ Branch 0 taken 714 times.
✓ Branch 1 taken 396 times.
|
1110 | } else if (!ordered) { |
| 350 | // mark baseUnits as checked | ||
| 351 | 714 | std::vector<std::string> checkedBaseUnits; | |
| 352 | 714 | std::vector<UnitElement>::const_iterator tmpLUnitsIter; | |
| 353 |
2/2✓ Branch 4 taken 3 times.
✓ Branch 5 taken 714 times.
|
717 | for (tmpLUnitsIter = m_units.begin(); tmpLUnitsIter != lUnitsIter; ++tmpLUnitsIter) { |
| 354 |
1/1✓ Branch 2 taken 3 times.
|
3 | checkedBaseUnits.push_back(tmpLUnitsIter->first); |
| 355 | } | ||
| 356 | // pick up with lUnits | ||
| 357 |
2/2✓ Branch 2 taken 5702 times.
✓ Branch 3 taken 714 times.
|
6416 | for (; lUnitsIter != lUnitsEnd; ++lUnitsIter) { |
| 358 |
1/1✓ Branch 4 taken 5702 times.
|
5702 | rUnitsIter = rUnit.getImpl<detail::Unit_Impl>()->findBaseUnit(lUnitsIter->first); |
| 359 |
2/2✓ Branch 1 taken 3442 times.
✓ Branch 2 taken 2260 times.
|
5702 | if (rUnitsIter != rUnitsEnd) { |
| 360 | 3442 | lUnitsIter->second += rUnitsIter->second; | |
| 361 |
1/1✓ Branch 2 taken 3442 times.
|
3442 | checkedBaseUnits.push_back(lUnitsIter->first); |
| 362 | } | ||
| 363 | } | ||
| 364 | // check rUnits | ||
| 365 | 714 | auto checkedBaseUnitsEnd = checkedBaseUnits.end(); | |
| 366 |
2/2✓ Branch 7 taken 8073 times.
✓ Branch 8 taken 714 times.
|
8787 | for (rUnitsIter = rUnit.getImpl<detail::Unit_Impl>()->m_units.begin(); rUnitsIter != rUnitsEnd; ++rUnitsIter) { |
| 367 |
1/1✓ Branch 3 taken 8073 times.
|
8073 | auto loc = std::find(checkedBaseUnits.begin(), checkedBaseUnitsEnd, rUnitsIter->first); |
| 368 |
2/2✓ Branch 1 taken 4628 times.
✓ Branch 2 taken 3445 times.
|
8073 | if (loc == checkedBaseUnitsEnd) { |
| 369 | // this base unit not checked yet, and is not in lUnits | ||
| 370 |
2/2✓ Branch 1 taken 692 times.
✓ Branch 2 taken 3936 times.
|
4628 | if (rUnitsIter->second != 0) { |
| 371 |
1/1✓ Branch 3 taken 692 times.
|
692 | setBaseUnitExponent(rUnitsIter->first, rUnitsIter->second); |
| 372 | } | ||
| 373 | } // if | ||
| 374 | } // for | ||
| 375 | 714 | } | |
| 376 | |||
| 377 |
3/3✓ Branch 1 taken 1123 times.
✓ Branch 4 taken 1123 times.
✓ Branch 7 taken 1123 times.
|
1123 | ScaleOpReturnType resultScale = scale() * rUnit.scale(); |
| 378 |
2/2✓ Branch 1 taken 1123 times.
✓ Branch 4 taken 1123 times.
|
1123 | setScale(resultScale.first().exponent); |
| 379 | |||
| 380 |
3/3✓ Branch 1 taken 1123 times.
✓ Branch 4 taken 1123 times.
✓ Branch 7 taken 1123 times.
|
1123 | std::string prettyStringFromFactory = UnitFactory::instance().lookupPrettyString(standardString(false)); |
| 381 |
2/2✓ Branch 1 taken 174 times.
✓ Branch 2 taken 949 times.
|
1123 | if (!prettyStringFromFactory.empty()) { |
| 382 |
1/1✓ Branch 1 taken 174 times.
|
174 | setPrettyString(prettyStringFromFactory); |
| 383 | } else { | ||
| 384 | // otherwise need to handle pretty string | ||
| 385 |
3/3✓ Branch 1 taken 949 times.
✓ Branch 5 taken 283 times.
✓ Branch 6 taken 666 times.
|
949 | if (!prettyString(false).empty()) { |
| 386 |
2/2✓ Branch 1 taken 283 times.
✓ Branch 4 taken 283 times.
|
283 | Unit wThisPretty = parseUnitString(prettyString(false)); |
| 387 |
3/3✓ Branch 1 taken 283 times.
✓ Branch 5 taken 52 times.
✓ Branch 6 taken 231 times.
|
283 | if (!rUnit.prettyString(false).empty()) { |
| 388 |
2/2✓ Branch 1 taken 52 times.
✓ Branch 4 taken 52 times.
|
52 | Unit wRUnitPretty = parseUnitString(rUnit.prettyString(false)); |
| 389 |
1/1✓ Branch 1 taken 52 times.
|
52 | wThisPretty *= wRUnitPretty; |
| 390 | 52 | } else { | |
| 391 |
1/1✓ Branch 1 taken 231 times.
|
231 | wThisPretty *= rUnit; |
| 392 | } | ||
| 393 |
2/2✓ Branch 1 taken 283 times.
✓ Branch 4 taken 283 times.
|
283 | setPrettyString(wThisPretty.standardString(false)); |
| 394 | 283 | } | |
| 395 | } | ||
| 396 | 1123 | } | |
| 397 | |||
| 398 | 673 | void Unit_Impl::operator/=(const Unit& rUnit) { | |
| 399 |
6/8✓ Branch 1 taken 673 times.
✓ Branch 4 taken 370 times.
✓ Branch 5 taken 303 times.
✓ Branch 7 taken 370 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 370 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 673 times.
|
673 | if ((m_system != rUnit.system()) && (m_system != UnitSystem::Mixed)) { |
| 400 | ✗ | LOG_AND_THROW("Cannot " << m_system.valueName() << " /= " << rUnit.system().valueName() | |
| 401 | << ". Must have same systems, or UnitSystem::Mixed on the left-hand side."); | ||
| 402 | } | ||
| 403 | |||
| 404 | // to avoid making any copies, use the identity: | ||
| 405 | // this/rUnit = (rUnit/this)^{-1} = (this^{-1} * rUnit)^{-1} | ||
| 406 | 673 | this->pow(-1); | |
| 407 | 673 | (*this) *= rUnit; | |
| 408 | 673 | this->pow(-1); | |
| 409 | 673 | } | |
| 410 | |||
| 411 | 11602 | void Unit_Impl::pow(int expNum, int expDenom, bool okToCallFactory) { | |
| 412 | 11602 | std::vector<UnitElement>::iterator unitsIter; | |
| 413 | 11602 | auto unitsEnd = m_units.end(); | |
| 414 | |||
| 415 | // check for errors first | ||
| 416 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 11594 times.
|
11602 | if (expDenom != 1) { |
| 417 | // baseUnit exponents ok in integer arithmetic? | ||
| 418 |
2/2✓ Branch 3 taken 46 times.
✓ Branch 4 taken 7 times.
|
53 | for (unitsIter = m_units.begin(); unitsIter != unitsEnd; ++unitsIter) { |
| 419 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 2 taken 45 times.
|
46 | if ((unitsIter->second * expNum) % expDenom != 0) { |
| 420 |
23/23✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 20 taken 1 time.
✓ Branch 23 taken 1 time.
✓ Branch 26 taken 1 time.
✓ Branch 29 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
✓ Branch 44 taken 1 time.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
✓ Branch 56 taken 1 time.
✓ Branch 59 taken 1 time.
✓ Branch 62 taken 1 time.
✓ Branch 66 taken 1 time.
✓ Branch 70 taken 1 time.
✓ Branch 73 taken 1 time.
|
1 | LOG_AND_THROW("Unit raised to fractional power " << expNum << "/" << expDenom << " that resulted in non-integer exponent on baseUnit " |
| 421 | << unitsIter->first); | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | // scale exponent ok in integer arithmetic? | ||
| 426 |
2/3✓ Branch 1 taken 7 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
|
7 | if ((scale().exponent * expNum) % expDenom != 0) { |
| 427 | ✗ | LOG_AND_THROW("Scale " << scale().name << " cannot be raised to the power " << expNum << "/" << expDenom | |
| 428 | << " since that results in a fractional exponent."); | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | // baseUnit exponents | ||
| 433 |
2/2✓ Branch 2 taken 87412 times.
✓ Branch 3 taken 11601 times.
|
99013 | for (unitsIter = m_units.begin(); unitsIter != unitsEnd; ++unitsIter) { |
| 434 | 87412 | unitsIter->second *= expNum; | |
| 435 | 87412 | unitsIter->second /= expDenom; | |
| 436 | } | ||
| 437 | |||
| 438 | // scale | ||
| 439 |
1/1✓ Branch 1 taken 11601 times.
|
11601 | const Scale& sc = scale(); |
| 440 |
2/2✓ Branch 0 taken 273 times.
✓ Branch 1 taken 11328 times.
|
11601 | if (sc.exponent != 0) { |
| 441 |
1/1✓ Branch 1 taken 273 times.
|
273 | ScaleOpReturnType resultScale = openstudio::pow(sc, expNum, expDenom); |
| 442 |
1/1✓ Branch 1 taken 273 times.
|
273 | const Scale& sc2 = resultScale.first(); |
| 443 | 273 | int exponent = sc2.exponent; | |
| 444 | // DLM: check that there are no extra multipliers not accounted for in scale | ||
| 445 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
|
273 | if (resultScale.second != 1.0) { |
| 446 | ✗ | LOG_AND_THROW("Cannot raise scale '" << sc << "' to the " << expNum << "/" << expDenom << " power.") | |
| 447 | } | ||
| 448 |
1/1✓ Branch 1 taken 273 times.
|
273 | bool test = setScale(exponent); |
| 449 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
✗ Branch 3 not taken.
|
273 | OS_ASSERT(test); |
| 450 | 273 | } | |
| 451 | |||
| 452 | // pretty string | ||
| 453 |
1/1✓ Branch 1 taken 11601 times.
|
11601 | std::string stdStr = standardString(false); |
| 454 | |||
| 455 | 11601 | std::string prettyStringFromFactory; | |
| 456 |
2/2✓ Branch 0 taken 2801 times.
✓ Branch 1 taken 8800 times.
|
11601 | if (okToCallFactory) { |
| 457 |
2/2✓ Branch 1 taken 2801 times.
✓ Branch 4 taken 2801 times.
|
2801 | prettyStringFromFactory = UnitFactory::instance().lookupPrettyString(stdStr); |
| 458 | } | ||
| 459 |
2/2✓ Branch 1 taken 685 times.
✓ Branch 2 taken 10916 times.
|
11601 | if (!prettyStringFromFactory.empty()) { |
| 460 |
1/1✓ Branch 1 taken 685 times.
|
685 | setPrettyString(prettyStringFromFactory); |
| 461 | } else { | ||
| 462 | // otherwise handle | ||
| 463 |
3/3✓ Branch 1 taken 10916 times.
✓ Branch 5 taken 319 times.
✓ Branch 6 taken 10597 times.
|
10916 | if (!prettyString(false).empty()) { |
| 464 |
2/2✓ Branch 1 taken 319 times.
✓ Branch 4 taken 319 times.
|
319 | Unit wThisPretty = parseUnitString(prettyString(false)); |
| 465 |
1/1✓ Branch 1 taken 319 times.
|
319 | wThisPretty.pow(expNum, expDenom, okToCallFactory); |
| 466 |
2/2✓ Branch 1 taken 319 times.
✓ Branch 4 taken 319 times.
|
319 | setPrettyString(wThisPretty.standardString(false)); |
| 467 | 319 | } | |
| 468 | } // if | ||
| 469 | 11601 | } | |
| 470 | |||
| 471 | 289838 | std::vector<Unit_Impl::UnitElement>::iterator Unit_Impl::findBaseUnit(const std::string& baseUnit) { | |
| 472 |
1/1✓ Branch 3 taken 289838 times.
|
289838 | auto result = std::find_if(m_units.begin(), m_units.end(), |
| 473 | 909516 | [&baseUnit](const auto& unitElement) { return firstOfPairEqual<std::string, int>(unitElement, baseUnit); }); | |
| 474 | 289838 | return result; | |
| 475 | } | ||
| 476 | |||
| 477 | 714070 | std::vector<Unit_Impl::UnitElement>::const_iterator Unit_Impl::findBaseUnit(const std::string& baseUnit) const { | |
| 478 |
1/1✓ Branch 3 taken 714070 times.
|
714070 | auto result = std::find_if(m_units.cbegin(), m_units.cend(), |
| 479 | 2914853 | [&baseUnit](const auto& unitElement) { return firstOfPairEqual<std::string, int>(unitElement, baseUnit); }); | |
| 480 | 714070 | return result; | |
| 481 | } | ||
| 482 | |||
| 483 | } // namespace detail | ||
| 484 | |||
| 485 |
3/4✓ Branch 2 taken 142870 times.
✓ Branch 5 taken 142870 times.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
|
142871 | Unit::Unit(int scaleExponent, const std::string& prettyString) : m_impl(new detail::Unit_Impl(scaleExponent, prettyString)) {} |
| 486 | |||
| 487 |
3/4✓ Branch 2 taken 5 times.
✓ Branch 5 taken 5 times.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
|
6 | Unit::Unit(const std::string& scaleAbbreviation, const std::string& prettyString) : m_impl(new detail::Unit_Impl(scaleAbbreviation, prettyString)) {} |
| 488 | |||
| 489 | 2510491 | Unit Unit::clone() const { | |
| 490 |
1/1✓ Branch 3 taken 2510491 times.
|
2510491 | return getImpl<detail::Unit_Impl>()->clone(); |
| 491 | } | ||
| 492 | |||
| 493 | 301 | Unit Unit::cloneToMixed() const { | |
| 494 |
1/1✓ Branch 3 taken 301 times.
|
301 | return getImpl<detail::Unit_Impl>()->cloneToMixed(); |
| 495 | } | ||
| 496 | |||
| 497 | 127385 | std::vector<std::string> Unit::baseUnits() const { | |
| 498 |
1/1✓ Branch 3 taken 127385 times.
|
127385 | return getImpl<detail::Unit_Impl>()->baseUnits(); |
| 499 | } | ||
| 500 | |||
| 501 | 144294 | bool Unit::isBaseUnit(const std::string& baseUnit) const { | |
| 502 |
1/1✓ Branch 3 taken 144294 times.
|
144294 | return getImpl<detail::Unit_Impl>()->isBaseUnit(baseUnit); |
| 503 | } | ||
| 504 | |||
| 505 | 482051 | int Unit::baseUnitExponent(const std::string& baseUnit) const { | |
| 506 |
1/1✓ Branch 3 taken 482051 times.
|
482051 | return getImpl<detail::Unit_Impl>()->baseUnitExponent(baseUnit); |
| 507 | } | ||
| 508 | |||
| 509 | 283428 | void Unit::setBaseUnitExponent(const std::string& baseUnit, int exponent) { | |
| 510 |
1/1✓ Branch 3 taken 283422 times.
|
283428 | getImpl<detail::Unit_Impl>()->setBaseUnitExponent(baseUnit, exponent); |
| 511 | 283422 | } | |
| 512 | |||
| 513 | 22466 | Scale Unit::scale() const { | |
| 514 |
1/1✓ Branch 3 taken 22466 times.
|
22466 | return getImpl<detail::Unit_Impl>()->scale(); |
| 515 | } | ||
| 516 | |||
| 517 | 274 | bool Unit::setScale(int scaleExponent) { | |
| 518 |
1/1✓ Branch 3 taken 274 times.
|
274 | return getImpl<detail::Unit_Impl>()->setScale(scaleExponent); |
| 519 | } | ||
| 520 | |||
| 521 | 92 | bool Unit::setScale(const std::string& scaleAbbreviation) { | |
| 522 |
1/1✓ Branch 3 taken 92 times.
|
92 | return getImpl<detail::Unit_Impl>()->setScale(scaleAbbreviation); |
| 523 | } | ||
| 524 | |||
| 525 | 35171 | std::string Unit::standardString(bool withScale) const { | |
| 526 |
1/1✓ Branch 3 taken 35171 times.
|
35171 | return getImpl<detail::Unit_Impl>()->standardString(withScale); |
| 527 | } | ||
| 528 | |||
| 529 | 16545 | std::string Unit::prettyString(bool withScale) const { | |
| 530 |
1/1✓ Branch 3 taken 16545 times.
|
16545 | return getImpl<detail::Unit_Impl>()->prettyString(withScale); |
| 531 | } | ||
| 532 | |||
| 533 | 4890 | void Unit::setPrettyString(const std::string& str) { | |
| 534 |
1/1✓ Branch 3 taken 4890 times.
|
4890 | getImpl<detail::Unit_Impl>()->setPrettyString(str); |
| 535 | 4890 | } | |
| 536 | |||
| 537 | 95 | std::string Unit::print(bool withScale) const { | |
| 538 |
1/1✓ Branch 3 taken 95 times.
|
95 | return getImpl<detail::Unit_Impl>()->print(withScale); |
| 539 | } | ||
| 540 | |||
| 541 | 40112 | UnitSystem Unit::system() const { | |
| 542 |
1/1✓ Branch 3 taken 40112 times.
|
40112 | return getImpl<detail::Unit_Impl>()->system(); |
| 543 | } | ||
| 544 | |||
| 545 | 8621 | bool Unit::operator==(const Unit& rUnit) const { | |
| 546 |
1/1✓ Branch 3 taken 8621 times.
|
8621 | return getImpl<detail::Unit_Impl>()->operator==(rUnit); |
| 547 | } | ||
| 548 | |||
| 549 | 451 | Unit& Unit::operator*=(const Unit& rUnit) { | |
| 550 |
1/1✓ Branch 3 taken 451 times.
|
451 | getImpl<detail::Unit_Impl>()->operator*=(rUnit); |
| 551 | 451 | return *this; | |
| 552 | } | ||
| 553 | |||
| 554 | 673 | Unit& Unit::operator/=(const Unit& rUnit) { | |
| 555 |
1/1✓ Branch 3 taken 673 times.
|
673 | getImpl<detail::Unit_Impl>()->operator/=(rUnit); |
| 556 | 673 | return *this; | |
| 557 | } | ||
| 558 | |||
| 559 | 10255 | Unit& Unit::pow(int expNum, int expDenom, bool okToCallFactory) { | |
| 560 |
1/1✓ Branch 3 taken 10254 times.
|
10255 | getImpl<detail::Unit_Impl>()->pow(expNum, expDenom, okToCallFactory); |
| 561 | 10254 | return *this; | |
| 562 | } | ||
| 563 | |||
| 564 | /// @cond | ||
| 565 | 5065230 | Unit::Unit(std::shared_ptr<detail::Unit_Impl> impl) : m_impl(impl) {} | |
| 566 | /// @endcond | ||
| 567 | |||
| 568 | 95 | std::ostream& operator<<(std::ostream& os, const Unit& u) { | |
| 569 |
2/2✓ Branch 1 taken 95 times.
✓ Branch 4 taken 95 times.
|
95 | os << u.print(); |
| 570 | 95 | return os; | |
| 571 | } | ||
| 572 | |||
| 573 | 123080 | Unit parseUnitString(const std::string& unitString) { | |
| 574 | |||
| 575 |
2/3✓ Branch 1 taken 123080 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 123080 times.
|
123080 | if (!isUnit(unitString)) { |
| 576 | ✗ | LOG_FREE_AND_THROW("openstudio.Unit", unitString << " is not a UnitString, per openstudio::isUnit."); | |
| 577 | } | ||
| 578 | |||
| 579 |
1/1✓ Branch 1 taken 123080 times.
|
123080 | std::string compoundUnitString(unitString); |
| 580 | 123080 | int scaleExponent = 0; | |
| 581 |
2/3✓ Branch 1 taken 123080 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 123080 times.
|
123080 | if (isScaledUnit(unitString)) { |
| 582 | ✗ | std::pair<std::string, std::string> scaledUnitParseResult = decomposeScaledUnitString(unitString); | |
| 583 | ✗ | ScaleConstant scale = ScaleFactory::instance().createScale(scaledUnitParseResult.first); | |
| 584 | ✗ | if (scale().value != 0.0) { | |
| 585 | ✗ | scaleExponent = scale().exponent; | |
| 586 | } | ||
| 587 | ✗ | compoundUnitString = scaledUnitParseResult.second; | |
| 588 | ✗ | } | |
| 589 | |||
| 590 |
2/2✓ Branch 1 taken 123080 times.
✓ Branch 4 taken 123080 times.
|
123080 | Unit result(scaleExponent); |
| 591 | |||
| 592 |
1/1✓ Branch 1 taken 123080 times.
|
123080 | std::pair<std::vector<std::string>, std::vector<std::string>> compoundUnitParseResult = decomposeCompoundUnitString(compoundUnitString); |
| 593 | |||
| 594 | 123080 | std::vector<std::string>::const_iterator i; | |
| 595 | 123080 | auto theEnd = compoundUnitParseResult.first.end(); | |
| 596 | |||
| 597 | // loop over numerator | ||
| 598 |
2/2✓ Branch 4 taken 132692 times.
✓ Branch 5 taken 123080 times.
|
255772 | for (i = compoundUnitParseResult.first.begin(); i != theEnd; ++i) { |
| 599 |
1/1✓ Branch 2 taken 132692 times.
|
132692 | std::pair<std::string, int> atom = decomposeAtomicUnitString(*i); |
| 600 |
2/3✓ Branch 1 taken 132692 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 132692 times.
|
132692 | if (result.isBaseUnit(atom.first)) { |
| 601 | ✗ | result.setBaseUnitExponent(atom.first, result.baseUnitExponent(atom.first) + atom.second); | |
| 602 | } else { | ||
| 603 |
1/1✓ Branch 1 taken 132692 times.
|
132692 | result.setBaseUnitExponent(atom.first, atom.second); |
| 604 | } | ||
| 605 | 132692 | } | |
| 606 | |||
| 607 | // loop over denominator | ||
| 608 | 123080 | theEnd = compoundUnitParseResult.second.end(); | |
| 609 |
2/2✓ Branch 4 taken 11589 times.
✓ Branch 5 taken 123080 times.
|
134669 | for (i = compoundUnitParseResult.second.begin(); i != theEnd; ++i) { |
| 610 |
1/1✓ Branch 2 taken 11589 times.
|
11589 | std::pair<std::string, int> atom = decomposeAtomicUnitString(*i); |
| 611 |
3/3✓ Branch 1 taken 11589 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 11580 times.
|
11589 | if (result.isBaseUnit(atom.first)) { |
| 612 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
|
9 | result.setBaseUnitExponent(atom.first, result.baseUnitExponent(atom.first) - atom.second); |
| 613 | } else { | ||
| 614 |
1/1✓ Branch 1 taken 11580 times.
|
11580 | result.setBaseUnitExponent(atom.first, -atom.second); |
| 615 | } | ||
| 616 | 11589 | } | |
| 617 | |||
| 618 | 246160 | return result; | |
| 619 | 123080 | } | |
| 620 | |||
| 621 | 57 | bool operator!=(const Unit& lUnit, const Unit& rUnit) { | |
| 622 | 57 | return !(lUnit == rUnit); | |
| 623 | } | ||
| 624 | |||
| 625 | 143 | Unit operator*(const Unit& lUnit, const Unit& rUnit) { | |
| 626 | 143 | Unit result = lUnit.clone(); | |
| 627 |
10/10✓ Branch 1 taken 143 times.
✓ Branch 4 taken 143 times.
✓ Branch 7 taken 108 times.
✓ Branch 8 taken 35 times.
✓ Branch 10 taken 108 times.
✓ Branch 13 taken 108 times.
✓ Branch 16 taken 87 times.
✓ Branch 17 taken 21 times.
✓ Branch 18 taken 87 times.
✓ Branch 19 taken 56 times.
|
143 | if ((lUnit.system() != rUnit.system()) && (lUnit.system() != UnitSystem::Mixed)) { |
| 628 |
1/1✓ Branch 1 taken 87 times.
|
87 | result = lUnit.cloneToMixed(); |
| 629 | } | ||
| 630 |
1/1✓ Branch 1 taken 143 times.
|
143 | result *= rUnit; |
| 631 | 143 | return result; | |
| 632 | ✗ | } | |
| 633 | |||
| 634 | 656 | Unit operator/(const Unit& lUnit, const Unit& rUnit) { | |
| 635 | 656 | Unit result = lUnit.clone(); | |
| 636 |
10/10✓ Branch 1 taken 656 times.
✓ Branch 4 taken 656 times.
✓ Branch 7 taken 369 times.
✓ Branch 8 taken 287 times.
✓ Branch 10 taken 369 times.
✓ Branch 13 taken 369 times.
✓ Branch 16 taken 201 times.
✓ Branch 17 taken 168 times.
✓ Branch 18 taken 201 times.
✓ Branch 19 taken 455 times.
|
656 | if ((lUnit.system() != rUnit.system()) && (lUnit.system() != UnitSystem::Mixed)) { |
| 637 |
1/1✓ Branch 1 taken 201 times.
|
201 | result = lUnit.cloneToMixed(); |
| 638 | } | ||
| 639 |
1/1✓ Branch 1 taken 656 times.
|
656 | result /= rUnit; |
| 640 | 656 | return result; | |
| 641 | ✗ | } | |
| 642 | |||
| 643 | 7 | Unit pow(const Unit& rUnit, int expNum, int expDenom) { | |
| 644 |
1/1✓ Branch 1 taken 7 times.
|
7 | Unit result = rUnit.clone(); |
| 645 |
1/1✓ Branch 1 taken 7 times.
|
14 | return result.pow(expNum, expDenom); |
| 646 | 7 | } | |
| 647 | |||
| 648 | } // namespace openstudio | ||
| 649 |