src/utilities/units/ScaleFactory.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 "../core/Assert.hpp" | ||
| 7 | #include "ScaleFactory.hpp" | ||
| 8 | |||
| 9 | #include "../core/String.hpp" | ||
| 10 | #include "../core/Exception.hpp" | ||
| 11 | |||
| 12 | #include <map> | ||
| 13 | #include <vector> | ||
| 14 | #include <boost/regex.hpp> | ||
| 15 | |||
| 16 | namespace openstudio { | ||
| 17 | |||
| 18 | 47200 | bool ScaleFactory::registerScale(ScaleConstant scale) { | |
| 19 | |||
| 20 |
3/3✓ Branch 1 taken 47200 times.
✓ Branch 4 taken 47200 times.
✓ Branch 7 taken 47200 times.
|
47200 | bool result = m_exponentMap.insert(ExponentLookupMap::value_type(scale().exponent, scale)).second; |
| 21 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47200 times.
|
47200 | if (!result) { |
| 22 | ✗ | LOG(Info, "A scale with exponent " << scale().exponent << " is already registered."); | |
| 23 | ✗ | return result; | |
| 24 | } | ||
| 25 | |||
| 26 |
3/3✓ Branch 1 taken 47200 times.
✓ Branch 4 taken 47200 times.
✓ Branch 7 taken 47200 times.
|
47200 | result = m_abbreviationMap.insert(AbbreviationLookupMap::value_type(scale().abbr, scale)).second; |
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47200 times.
|
47200 | if (!result) { |
| 28 | ✗ | LOG(Info, "A scale with abbreviation " << scale().abbr << " is already registered."); | |
| 29 | } | ||
| 30 | |||
| 31 | 47200 | return result; | |
| 32 | } | ||
| 33 | |||
| 34 | 102 | std::vector<Scale> ScaleFactory::registeredScales() { | |
| 35 | |||
| 36 | // make vector<Scale> of the correct length | ||
| 37 |
1/1✓ Branch 2 taken 102 times.
|
102 | std::vector<Scale> result(m_exponentMap.size()); |
| 38 | // iterate over each element | ||
| 39 | 102 | std::vector<Scale>::iterator resultIter; | |
| 40 | |||
| 41 | 102 | ExponentLookupMap::const_iterator mapIter; | |
| 42 | 102 | auto mapEnd = m_exponentMap.end(); | |
| 43 |
2/2✓ Branch 7 taken 2550 times.
✓ Branch 8 taken 102 times.
|
2652 | for (mapIter = m_exponentMap.begin(), resultIter = result.begin(); mapIter != mapEnd; ++mapIter, ++resultIter) { |
| 44 |
2/2✓ Branch 2 taken 2550 times.
✓ Branch 6 taken 2550 times.
|
2550 | *resultIter = mapIter->second(); // copy registered scale into vector |
| 45 | } | ||
| 46 | |||
| 47 | 204 | return result; | |
| 48 | ✗ | } | |
| 49 | |||
| 50 | 189828 | ScaleConstant ScaleFactory::createScale(int exponent) const { | |
| 51 | |||
| 52 | 189828 | ExponentLookupMap::const_iterator lookupPair; | |
| 53 |
1/1✓ Branch 1 taken 189828 times.
|
189828 | lookupPair = m_exponentMap.find(exponent); |
| 54 | |||
| 55 |
2/2✓ Branch 2 taken 5 times.
✓ Branch 3 taken 189823 times.
|
189828 | if (lookupPair == m_exponentMap.end()) { |
| 56 | // fall back method to return something | ||
| 57 |
1/1✓ Branch 1 taken 5 times.
|
5 | return m_recoverFromFailedCreate(); |
| 58 | } | ||
| 59 | |||
| 60 |
1/1✓ Branch 2 taken 189823 times.
|
189823 | return lookupPair->second; // return function pointer that encloses const Scale& |
| 61 | } | ||
| 62 | |||
| 63 | 103 | ScaleConstant ScaleFactory::createScale(const std::string& abbr) const { | |
| 64 | |||
| 65 | 103 | AbbreviationLookupMap::const_iterator lookupPair; | |
| 66 |
1/1✓ Branch 1 taken 103 times.
|
103 | lookupPair = m_abbreviationMap.find(abbr); |
| 67 | |||
| 68 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 3 taken 101 times.
|
103 | if (lookupPair == m_abbreviationMap.end()) { |
| 69 | // fall back method to return something | ||
| 70 |
1/1✓ Branch 1 taken 2 times.
|
2 | return m_recoverFromFailedCreate(); |
| 71 | } | ||
| 72 | |||
| 73 |
1/1✓ Branch 2 taken 101 times.
|
101 | return lookupPair->second; // return function pointer that encloses const Scale& |
| 74 | } | ||
| 75 | |||
| 76 | 190037 | ScaleFactory& ScaleFactory::instance() { | |
| 77 |
4/7✓ Branch 0 taken 1888 times.
✓ Branch 1 taken 188149 times.
✓ Branch 3 taken 1888 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1888 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
190037 | static ScaleFactory instance; |
| 78 | 190037 | return instance; | |
| 79 | } | ||
| 80 | |||
| 81 | 1888 | ScaleFactory::ScaleFactory() { | |
| 82 | // default ScaleFactory initialization | ||
| 83 | /** \todo Provide functionality for swapping out entire ScaleFactory library or for | ||
| 84 | * complete user customization. */ | ||
| 85 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(yotta); |
| 86 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(zetta); |
| 87 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(exa); |
| 88 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(peta); |
| 89 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(tera); |
| 90 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(giga); |
| 91 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(mega); |
| 92 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(hectokilo); |
| 93 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(myria); |
| 94 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(kilo); |
| 95 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(hecto); |
| 96 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(deka); |
| 97 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(one); |
| 98 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(deci); |
| 99 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(centi); |
| 100 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(milli); |
| 101 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(decimilli); |
| 102 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(centimilli); |
| 103 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(micro); |
| 104 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(nano); |
| 105 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(pico); |
| 106 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(femto); |
| 107 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(atto); |
| 108 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(zepto); |
| 109 |
1/1✓ Branch 2 taken 1888 times.
|
1888 | registerScale(yocto); |
| 110 | 1888 | } | |
| 111 | |||
| 112 | 7 | ScaleConstant ScaleFactory::m_recoverFromFailedCreate() const { | |
| 113 | |||
| 114 | // fatal error if no scales are registered since will never get valid scale. | ||
| 115 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if (m_exponentMap.empty()) { |
| 116 | ✗ | LOG_AND_THROW("No scales are registered with ScaleFactory."); | |
| 117 | } | ||
| 118 | |||
| 119 | // otherwise, alert user that scale asked for does not exist | ||
| 120 | 7 | return notDefined; | |
| 121 | } | ||
| 122 | |||
| 123 | 4 | std::ostream& operator<<(std::ostream& os, const ScaleFactory& factory) { | |
| 124 |
2/2✓ Branch 5 taken 100 times.
✓ Branch 6 taken 4 times.
|
104 | for (const auto& map : factory.m_exponentMap) { |
| 125 |
3/3✓ Branch 1 taken 100 times.
✓ Branch 4 taken 100 times.
✓ Branch 7 taken 100 times.
|
100 | os << map.second() << '\n'; // output scale and go to next line |
| 126 | } | ||
| 127 | |||
| 128 | 4 | return os; | |
| 129 | } | ||
| 130 | |||
| 131 | 3 | std::string printScales() { | |
| 132 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::stringstream outStream; |
| 133 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | outStream << ScaleFactory::instance(); |
| 134 |
1/1✓ Branch 1 taken 3 times.
|
6 | return outStream.str(); |
| 135 | 3 | } | |
| 136 | |||
| 137 | 101 | std::pair<std::string, std::string> extractScaleAbbreviation(const std::string& str) { | |
| 138 | |||
| 139 | 101 | std::pair<std::string, std::string> result; | |
| 140 |
2/2✓ Branch 1 taken 101 times.
✓ Branch 4 taken 101 times.
|
101 | std::vector<Scale> scales = ScaleFactory::instance().registeredScales(); |
| 141 |
1/1✓ Branch 1 taken 101 times.
|
101 | std::string escapeChar("\\"); |
| 142 | |||
| 143 | // loop through scales and look for regex match | ||
| 144 |
2/2✓ Branch 5 taken 1396 times.
✓ Branch 6 taken 12 times.
|
1408 | for (const auto& scale : scales) { |
| 145 |
1/1✓ Branch 1 taken 1396 times.
|
1396 | std::string abbr = scale.abbr; |
| 146 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 1347 times.
|
1396 | if (abbr.empty()) { |
| 147 | 49 | continue; | |
| 148 | } | ||
| 149 |
1/1✓ Branch 1 taken 1347 times.
|
1347 | std::stringstream regexComposer; |
| 150 |
1/1✓ Branch 1 taken 1347 times.
|
1347 | regexComposer << "^("; |
| 151 |
2/2✓ Branch 2 taken 101 times.
✓ Branch 3 taken 1246 times.
|
1347 | if (abbr.starts_with(escapeChar)) { |
| 152 |
1/1✓ Branch 1 taken 101 times.
|
101 | regexComposer << "\\"; |
| 153 | } // add extra escape character for regex | ||
| 154 |
2/2✓ Branch 1 taken 1347 times.
✓ Branch 4 taken 1347 times.
|
1347 | regexComposer << abbr << ")"; |
| 155 | // LOG_FREE(Debug,"openstudio.ScaleFactory","Scale abbreviation regex = " << regexComposer.str()); | ||
| 156 |
2/2✓ Branch 1 taken 1347 times.
✓ Branch 4 taken 1347 times.
|
1347 | boost::regex re(regexComposer.str()); |
| 157 |
1/1✓ Branch 1 taken 1347 times.
|
1347 | boost::match_results<std::string::const_iterator> match; |
| 158 |
3/3✓ Branch 1 taken 1347 times.
✓ Branch 3 taken 89 times.
✓ Branch 4 taken 1258 times.
|
1347 | if (boost::regex_search(str, match, re)) { |
| 159 |
1/1✓ Branch 1 taken 89 times.
|
89 | result.first = scale.abbr; |
| 160 |
2/2✓ Branch 2 taken 89 times.
✓ Branch 5 taken 89 times.
|
89 | result.second = std::string(match[0].second, str.end()); |
| 161 | 89 | break; | |
| 162 | } | ||
| 163 | 1663 | } | |
| 164 | |||
| 165 | 202 | return result; | |
| 166 | 101 | } | |
| 167 | |||
| 168 | 1149 | ScaleOpReturnType operator*(const Scale& firstScale, const Scale& secondScale) { | |
| 169 | 1149 | return scaleOpHelperFunction(firstScale.exponent + secondScale.exponent); | |
| 170 | } | ||
| 171 | |||
| 172 | 14 | ScaleOpReturnType operator/(const Scale& firstScale, const Scale& secondScale) { | |
| 173 | 14 | return scaleOpHelperFunction(firstScale.exponent - secondScale.exponent); | |
| 174 | } | ||
| 175 | |||
| 176 | 285 | ScaleOpReturnType pow(const Scale& scale, int expNum, int expDenom) { | |
| 177 | 285 | return scaleOpHelperFunction((scale.exponent * expNum) / expDenom); | |
| 178 | } | ||
| 179 | |||
| 180 | 1448 | ScaleOpReturnType scaleOpHelperFunction(int desiredExponent) { | |
| 181 | |||
| 182 | 1448 | ScaleOpReturnType result; | |
| 183 | 1448 | result.second = 1.0; | |
| 184 | |||
| 185 | // try to get scale with desired exponent | ||
| 186 |
2/2✓ Branch 1 taken 1448 times.
✓ Branch 4 taken 1448 times.
|
1448 | result.first = ScaleFactory::instance().createScale(desiredExponent); |
| 187 |
3/3✓ Branch 1 taken 1451 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1448 times.
|
1451 | while (result.first().value == 0.0) { |
| 188 | // if fails, move toward 10^0 | ||
| 189 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
|
3 | if (desiredExponent > 0) { |
| 190 | 1 | --desiredExponent; | |
| 191 | 1 | result.second *= 10.0; | |
| 192 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | result.first = ScaleFactory::instance().createScale(desiredExponent); |
| 193 | } else { | ||
| 194 | 2 | ++desiredExponent; | |
| 195 | 2 | result.second /= 10.0; | |
| 196 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | result.first = ScaleFactory::instance().createScale(desiredExponent); |
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | 1448 | return result; | |
| 201 | ✗ | } | |
| 202 | |||
| 203 | } // namespace openstudio | ||
| 204 |