src/utilities/core/Compare.hpp
| 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 | #ifndef UTILITIES_CORE_COMPARE_HPP | ||
| 7 | #define UTILITIES_CORE_COMPARE_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include <utility> // for std::pair | ||
| 12 | #include <iostream> | ||
| 13 | #include <vector> | ||
| 14 | #include <string> | ||
| 15 | |||
| 16 | #include <boost/optional.hpp> | ||
| 17 | #include <boost/algorithm/string.hpp> | ||
| 18 | |||
| 19 | // In the GNU C Library, "minor" is defined | ||
| 20 | // by <sys/sysmacros.h>. For historical compatibility, it is | ||
| 21 | // currently defined by <sys/types.h> as well, but we plan to | ||
| 22 | // remove this soon. To use "minor", include <sys/sysmacros.h> | ||
| 23 | // directly. If you did not intend to use a system-defined macro | ||
| 24 | // "minor", you should undefine it after including <sys/types.h>. | ||
| 25 | // int minor() const; | ||
| 26 | #ifdef major | ||
| 27 | # undef major | ||
| 28 | #endif | ||
| 29 | #ifdef minor | ||
| 30 | # undef minor | ||
| 31 | #endif | ||
| 32 | |||
| 33 | namespace openstudio { | ||
| 34 | |||
| 35 | class BCLComponent; | ||
| 36 | class WorkspaceObject; | ||
| 37 | |||
| 38 | /** Small functor object for case insensitive char compare. | ||
| 39 | * Used in istringEqual. */ | ||
| 40 | struct UTILITIES_API IcharCompare | ||
| 41 | { | ||
| 42 | 79244993 | bool operator()(char cA, char cB) const { | |
| 43 | 79244993 | return toupper(cA) == toupper(cB); | |
| 44 | }; | ||
| 45 | }; | ||
| 46 | |||
| 47 | /** Test equality between two strings without regard to case. */ | ||
| 48 | 201609281 | inline UTILITIES_API bool istringEqual(const std::string& x, const std::string& y) { | |
| 49 |
4/4✓ Branch 2 taken 12723788 times.
✓ Branch 3 taken 188885493 times.
✓ Branch 8 taken 4469411 times.
✓ Branch 9 taken 8254377 times.
|
201609281 | return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin(), IcharCompare()); |
| 50 | }; | ||
| 51 | |||
| 52 | /** Small functor object for case insensitive std::string equality. */ | ||
| 53 | struct UTILITIES_API IstringEqual | ||
| 54 | { | ||
| 55 | 29529 | bool operator()(const std::string& x, const std::string& y) const { | |
| 56 |
1/1✓ Branch 2 taken 29529 times.
|
29529 | return boost::iequals(x, y); |
| 57 | ; | ||
| 58 | }; | ||
| 59 | }; | ||
| 60 | |||
| 61 | /** Small functor object for case insensitive std::string equality. */ | ||
| 62 | struct UTILITIES_API IstringFind | ||
| 63 | { | ||
| 64 | IstringFind(); | ||
| 65 | IstringFind(const std::string& target); | ||
| 66 | void addTarget(const std::string& target); | ||
| 67 | bool operator()(const std::string& other) const; | ||
| 68 | |||
| 69 | private: | ||
| 70 | std::vector<std::string> m_targets; | ||
| 71 | }; | ||
| 72 | |||
| 73 | /** Compare two strings without regard to case. */ | ||
| 74 | 648646 | inline UTILITIES_API bool istringLess(const std::string& x, const std::string& y) { | |
| 75 |
1/1✓ Branch 3 taken 648646 times.
|
648646 | return boost::lexicographical_compare(x, y, boost::is_iless()); |
| 76 | }; | ||
| 77 | |||
| 78 | /** Small functor object for case insensitive std::string compare. | ||
| 79 | * Used in maps of name to objects. */ | ||
| 80 | struct UTILITIES_API IstringCompare | ||
| 81 | { | ||
| 82 | 6966763 | bool operator()(const std::string& x, const std::string& y) const { | |
| 83 |
1/1✓ Branch 3 taken 6966763 times.
|
6966763 | return boost::lexicographical_compare(x, y, boost::is_iless()); |
| 84 | }; | ||
| 85 | }; | ||
| 86 | |||
| 87 | /** Small functor object for case insensitive std::string compare in pairs of std::string, | ||
| 88 | * tests first strings then seconds. Used in maps of type and name to objects. */ | ||
| 89 | struct UTILITIES_API IstringPairCompare | ||
| 90 | { | ||
| 91 | bool operator()(const std::pair<std::string, std::string>& x, const std::pair<std::string, std::string>& y) const; | ||
| 92 | }; | ||
| 93 | |||
| 94 | class UTILITIES_API VersionString | ||
| 95 | { | ||
| 96 | public: | ||
| 97 | explicit VersionString(const std::string& version); | ||
| 98 | |||
| 99 | // Default constructor, assumes the current OpenStudio::openStudioLongVersion is passed | ||
| 100 | // For use in std::pair<VersionString, std::string> otherwise C# complains | ||
| 101 | VersionString(); | ||
| 102 | |||
| 103 | VersionString(int major, int minor); | ||
| 104 | |||
| 105 | VersionString(int major, int minor, int patch); | ||
| 106 | |||
| 107 | VersionString(int major, int minor, int patch, const std::string& patchString); | ||
| 108 | |||
| 109 | VersionString(int major, int minor, int patch, const std::string& patchString, const std::string& buildString); | ||
| 110 | |||
| 111 | std::string str() const; | ||
| 112 | |||
| 113 | /// major verion must be a positve integer, e.g. returns 3 for '3.1.0-rc1' | ||
| 114 | int major() const; | ||
| 115 | |||
| 116 | /// minor version must be a positive integer, e.g. returns 1 for '3.1.0-rc1' | ||
| 117 | int minor() const; | ||
| 118 | |||
| 119 | /// returns the integer portion of patch version, e.g. returns 0 for '3.1.0-rc1' | ||
| 120 | boost::optional<int> patch() const; | ||
| 121 | |||
| 122 | /// returns the string portion of patch version, e.g. returns 'rc1' for '3.1.0-rc1' | ||
| 123 | std::string patchString() const; | ||
| 124 | |||
| 125 | /// build version can be any string, e.g. returns 'build2' for '3.1.0-rc1+build2' | ||
| 126 | std::string buildString() const; | ||
| 127 | |||
| 128 | /// deprecated will always return empty | ||
| 129 | //boost::optional<int> build() const; | ||
| 130 | |||
| 131 | bool operator<(const VersionString& other) const; | ||
| 132 | |||
| 133 | bool operator>(const VersionString& other) const; | ||
| 134 | |||
| 135 | bool operator==(const VersionString& other) const; | ||
| 136 | |||
| 137 | bool operator!=(const VersionString& other) const; | ||
| 138 | |||
| 139 | bool operator<=(const VersionString& other) const; | ||
| 140 | |||
| 141 | bool operator>=(const VersionString& other) const; | ||
| 142 | |||
| 143 | //bool fidelityEqual(const VersionString& other) const; | ||
| 144 | |||
| 145 | /** Returns true if it is plausible for nextVersionCandidate to be the | ||
| 146 | * next version after this one. */ | ||
| 147 | bool isNextVersion(const VersionString& nextVersionCandidate) const; | ||
| 148 | |||
| 149 | private: | ||
| 150 | bool parseVersionString(const std::string& version); | ||
| 151 | std::string m_str; | ||
| 152 | int m_major; | ||
| 153 | int m_minor; | ||
| 154 | boost::optional<int> m_patch; | ||
| 155 | std::string m_patchString; | ||
| 156 | std::string m_buildString; | ||
| 157 | }; | ||
| 158 | |||
| 159 | UTILITIES_API std::ostream& operator<<(std::ostream& os, const VersionString& version); | ||
| 160 | |||
| 161 | using OptionalVersionString = boost::optional<VersionString>; | ||
| 162 | |||
| 163 | // sorts WorkspaceObjects by name | ||
| 164 | struct UTILITIES_API WorkspaceObjectNameLess | ||
| 165 | { | ||
| 166 | bool operator()(const WorkspaceObject& a, const WorkspaceObject& b) const; | ||
| 167 | }; | ||
| 168 | |||
| 169 | // sorts WorkspaceObjects by name | ||
| 170 | struct UTILITIES_API WorkspaceObjectNameGreater | ||
| 171 | { | ||
| 172 | bool operator()(const WorkspaceObject& a, const WorkspaceObject& b) const; | ||
| 173 | }; | ||
| 174 | |||
| 175 | // sorts BCLComponents by name | ||
| 176 | struct UTILITIES_API BCLComponentNameLess | ||
| 177 | { | ||
| 178 | bool operator()(const BCLComponent& a, const BCLComponent& b) const; | ||
| 179 | }; | ||
| 180 | |||
| 181 | // sorts BCLComponents by name | ||
| 182 | struct UTILITIES_API BCLComponentNameGreater | ||
| 183 | { | ||
| 184 | bool operator()(const BCLComponent& a, const BCLComponent& b) const; | ||
| 185 | }; | ||
| 186 | |||
| 187 | /** Test equality between objects in two vectors of pointers */ | ||
| 188 | template <class T> | ||
| 189 | 21 | bool checkPtrVecEqual(const std::vector<std::shared_ptr<T>>& a, const std::vector<std::shared_ptr<T>>& b) { | |
| 190 | 21 | bool result((a.size()) == (b.size())); | |
| 191 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 time.
|
21 | if (result) { |
| 192 | 20 | typename std::vector<std::shared_ptr<T>>::const_iterator aIt; | |
| 193 | 20 | typename std::vector<std::shared_ptr<T>>::const_iterator bIt; | |
| 194 | 20 | typename std::vector<std::shared_ptr<T>>::const_iterator aItend; | |
| 195 |
2/2✓ Branch 7 taken 52 times.
✓ Branch 8 taken 17 times.
|
69 | for (aIt = a.begin(), bIt = b.begin(), aItend = a.end(); aIt < aItend; ++aIt, ++bIt) { |
| 196 |
6/8✓ Branch 2 taken 49 times.
✓ Branch 3 taken 3 times.
✓ Branch 6 taken 49 times.
✗ Branch 7 not taken.
✓ Branch 12 taken 49 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 3 times.
✓ Branch 15 taken 49 times.
|
52 | if ((*aIt) && (*bIt) && (**aIt == **bIt)) { |
| 197 | } else { | ||
| 198 | 3 | result = false; | |
| 199 | 3 | break; | |
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | 21 | return result; | |
| 204 | } | ||
| 205 | |||
| 206 | /** Test equality of provided value with first element in a pair. */ | ||
| 207 | template <class T, class U> | ||
| 208 | 3821701 | bool firstOfPairEqual(const std::pair<T, U>& pair, const T& value) { | |
| 209 | 3821701 | return pair.first == value; | |
| 210 | } | ||
| 211 | |||
| 212 | /** Test equality of provided value with second element in a pair. */ | ||
| 213 | template <class T, class U> | ||
| 214 | 3 | bool secondOfPairEqual(const std::pair<T, U>& pair, const U& value) { | |
| 215 | 3 | return pair.second == value; | |
| 216 | } | ||
| 217 | |||
| 218 | /** Test istringEqual of provided string with first element in a pair. */ | ||
| 219 | template <class T> | ||
| 220 | bool firstOfPairIStringEqual(const std::pair<std::string, T>& pair, const std::string& value) { | ||
| 221 | return istringEqual(pair.first, value); | ||
| 222 | } | ||
| 223 | |||
| 224 | /** Test equality of provided value with second element in a pair. */ | ||
| 225 | template <class T> | ||
| 226 | bool secondOfPairIStringEqual(const std::pair<T, std::string>& pair, const std::string& value) { | ||
| 227 | return istringEqual(pair.second, value); | ||
| 228 | } | ||
| 229 | |||
| 230 | template <class T> | ||
| 231 | struct FirstOfPairLess | ||
| 232 | { | ||
| 233 | bool operator()(const T& left, const T& right) const { | ||
| 234 | return (left.first < right.first); | ||
| 235 | } | ||
| 236 | }; | ||
| 237 | |||
| 238 | template <class T> | ||
| 239 | struct SecondOfPairLess | ||
| 240 | { | ||
| 241 | ✗ | bool operator()(const T& left, const T& right) const { | |
| 242 | ✗ | return (left.second < right.second); | |
| 243 | } | ||
| 244 | }; | ||
| 245 | |||
| 246 | template <class T, class U> | ||
| 247 | struct GetFirstOfPair | ||
| 248 | { | ||
| 249 | T operator()(const std::pair<T, U>& pair) const { | ||
| 250 | return pair.first; | ||
| 251 | } | ||
| 252 | }; | ||
| 253 | |||
| 254 | template <class T, class U> | ||
| 255 | struct GetSecondOfPair | ||
| 256 | { | ||
| 257 | U operator()(const std::pair<T, U>& pair) const { | ||
| 258 | return pair.second; | ||
| 259 | } | ||
| 260 | }; | ||
| 261 | |||
| 262 | template <class T> | ||
| 263 | struct IndexLess | ||
| 264 | { | ||
| 265 | bool operator()(const T& left, const T& right) const { | ||
| 266 | return (left.index < right.index); | ||
| 267 | } | ||
| 268 | }; | ||
| 269 | |||
| 270 | template <class T, class U> | ||
| 271 | bool uuidsEqual(const T& left, const U& right) { | ||
| 272 | return (!left.uuid().isNull()) && (left.uuid() == right.uuid()); | ||
| 273 | } | ||
| 274 | |||
| 275 | template <class T, class U> | ||
| 276 | bool uuidsAndVersionsEqual(const T& left, const U& right) { | ||
| 277 | return (!left.uuid().isNull()) && (left.uuid() == right.uuid()) && (!left.versionUUID().isNull()) && (left.versionUUID() == right.versionUUID()); | ||
| 278 | } | ||
| 279 | |||
| 280 | template <class T, class U> | ||
| 281 | bool uuidEquals(const T& object, const U& uuid) { | ||
| 282 | return !uuid.isNull() && (object.uuid() == uuid); | ||
| 283 | } | ||
| 284 | |||
| 285 | template <class T, class U> | ||
| 286 | 7212 | bool handleEquals(const T& object, const U& handle) { | |
| 287 |
9/12bool openstudio::handleEquals<openstudio::IdfObject, openstudio::UUID>(openstudio::IdfObject const&, openstudio::UUID const&):
✓ Branch 1 taken 3348 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3348 times.
✓ Branch 7 taken 3348 times.
✓ Branch 9 taken 3348 times.
✗ Branch 10 not taken.
bool openstudio::handleEquals<openstudio::model::ModelObject, openstudio::UUID>(openstudio::model::ModelObject const&, openstudio::UUID const&):
✓ Branch 1 taken 3864 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3864 times.
✓ Branch 7 taken 3864 times.
✓ Branch 9 taken 896 times.
✓ Branch 10 taken 2968 times.
|
7212 | return !handle.isNull() && (object.handle() == handle); |
| 288 | } | ||
| 289 | |||
| 290 | }; // namespace openstudio | ||
| 291 | |||
| 292 | #endif // UTILITIES_CORE_COMPARE_HPP | ||
| 293 |