src/utilities/core/Finder.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_FINDER_HPP | ||
| 7 | #define UTILITIES_CORE_FINDER_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | #include "String.hpp" | ||
| 11 | |||
| 12 | #include <boost/algorithm/string.hpp> | ||
| 13 | #include <boost/optional.hpp> | ||
| 14 | |||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | namespace openstudio { | ||
| 18 | |||
| 19 | /** Functor object for finding objects by name. Templated on T where T has method | ||
| 20 | * name(). Applies to T and to shared_ptr<T>. */ | ||
| 21 | template <class T> | ||
| 22 | class NameFinder | ||
| 23 | { | ||
| 24 | public: | ||
| 25 | 1438460 | NameFinder(const std::string& name, bool caseSensitive = false) : m_name(name), m_caseSensitive(caseSensitive) {} | |
| 26 | |||
| 27 | 2699962 | bool operator()(const T& object) const { | |
| 28 |
2/4openstudio::NameFinder<Object>::operator()(Object const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
openstudio::NameFinder<openstudio::IddKey>::operator()(openstudio::IddKey const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 2699938 times.
|
2699962 | if (m_caseSensitive) { |
| 29 | ✗ | return (m_name == object.name()); | |
| 30 | } | ||
| 31 |
3/3openstudio::NameFinder<Object>::operator()(Object const&) const:
✓ Branch 3 taken 24 times.
openstudio::NameFinder<openstudio::IddKey>::operator()(openstudio::IddKey const&) const:
✓ Branch 2 taken 2699938 times.
✓ Branch 5 taken 2699938 times.
|
2699962 | return boost::iequals(m_name, object.name()); |
| 32 | } | ||
| 33 | |||
| 34 | 19 | bool operator()(const std::shared_ptr<T>& object) const { | |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (m_caseSensitive) { |
| 36 | ✗ | return (m_name == object->name()); | |
| 37 | } | ||
| 38 |
1/1✓ Branch 4 taken 19 times.
|
19 | return boost::iequals(m_name, object->name()); |
| 39 | } | ||
| 40 | |||
| 41 | private: | ||
| 42 | std::string m_name; | ||
| 43 | bool m_caseSensitive; | ||
| 44 | }; | ||
| 45 | |||
| 46 | /** Template function for finding objects in a vector T by name | ||
| 47 | */ | ||
| 48 | template <class T> | ||
| 49 | 7 | boost::optional<T> findByName(const std::vector<T>& vec, const std::string& name, bool caseSensitive = false) { | |
| 50 | 7 | boost::optional<T> result; | |
| 51 |
1/1✓ Branch 1 taken 7 times.
|
7 | NameFinder<T> finder(name, caseSensitive); |
| 52 | 7 | typename std::vector<T>::const_iterator it; | |
| 53 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 6 taken 7 times.
|
7 | it = find_if(vec.begin(), vec.end(), finder); |
| 54 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 4 times.
|
7 | if (it != vec.end()) { |
| 55 |
1/1✓ Branch 2 taken 3 times.
|
3 | result = *it; |
| 56 | }; | ||
| 57 | 14 | return result; | |
| 58 | 7 | } | |
| 59 | |||
| 60 | /** Template function for finding objects in a vector of shared_ptr<T> by name | ||
| 61 | */ | ||
| 62 | template <class T> | ||
| 63 | 7 | std::shared_ptr<T> findByName(const std::vector<std::shared_ptr<T>>& vec, const std::string& name, bool caseSensitive = false) { | |
| 64 | 7 | std::shared_ptr<T> result; | |
| 65 |
1/1✓ Branch 1 taken 7 times.
|
7 | NameFinder<T> finder(name, caseSensitive); |
| 66 | 7 | typename std::vector<std::shared_ptr<T>>::const_iterator it; | |
| 67 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 6 taken 7 times.
|
7 | it = find_if(vec.begin(), vec.end(), finder); |
| 68 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 4 times.
|
7 | if (it != vec.end()) { |
| 69 | 3 | result = *it; | |
| 70 | }; | ||
| 71 | 14 | return result; | |
| 72 | 7 | } | |
| 73 | |||
| 74 | /** Template function for finding index of object in a vector<T> by name. */ | ||
| 75 | template <class T> | ||
| 76 | 6 | boost::optional<int> findIndexByName(const std::vector<T>& vec, const std::string& name, bool caseSensitive = false) { | |
| 77 | 6 | boost::optional<int> index; | |
| 78 |
1/1✓ Branch 1 taken 6 times.
|
6 | NameFinder<T> finder(name, caseSensitive); |
| 79 | 6 | typename std::vector<T>::const_iterator it; | |
| 80 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 6 taken 6 times.
|
6 | it = find_if(vec.begin(), vec.end(), finder); |
| 81 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
|
6 | if (it != vec.end()) { |
| 82 |
1/1✓ Branch 3 taken 2 times.
|
2 | index = (int)(it - vec.begin()); |
| 83 | } | ||
| 84 | 6 | return index; | |
| 85 | 6 | } | |
| 86 | |||
| 87 | /** Template function for finding index of object in a vector<T> by name. */ | ||
| 88 | template <class T> | ||
| 89 | typename std::vector<T>::iterator findIteratorByName(std::vector<T>& vec, const std::string& name, bool caseSensitive = false) { | ||
| 90 | boost::optional<int> index; | ||
| 91 | NameFinder<T> finder(name, caseSensitive); | ||
| 92 | typename std::vector<T>::iterator it; | ||
| 93 | it = find_if(vec.begin(), vec.end(), finder); | ||
| 94 | return it; | ||
| 95 | } | ||
| 96 | |||
| 97 | /** Functor object for finding objects by name. Templated on T where T has method | ||
| 98 | * name(). Applies to T and to shared_ptr<T>. */ | ||
| 99 | template <class T> | ||
| 100 | class StructNameFinder | ||
| 101 | { | ||
| 102 | public: | ||
| 103 | StructNameFinder(const std::string& name, bool caseSensitive = false) : m_name(name), m_caseSensitive(caseSensitive) {} | ||
| 104 | |||
| 105 | bool operator()(const T& object) const { | ||
| 106 | if (m_caseSensitive) { | ||
| 107 | return (m_name == object.name); | ||
| 108 | } | ||
| 109 | return boost::iequals(m_name, object.name); | ||
| 110 | } | ||
| 111 | |||
| 112 | bool operator()(const std::shared_ptr<T>& object) const { | ||
| 113 | if (m_caseSensitive) { | ||
| 114 | return (m_name == object->name); | ||
| 115 | } | ||
| 116 | return boost::iequals(m_name, object->name); | ||
| 117 | } | ||
| 118 | |||
| 119 | private: | ||
| 120 | std::string m_name; | ||
| 121 | bool m_caseSensitive; | ||
| 122 | }; | ||
| 123 | |||
| 124 | /** Template function for finding objects in a vector T by name | ||
| 125 | */ | ||
| 126 | template <class T> | ||
| 127 | boost::optional<T> findStructByName(const std::vector<T>& vec, const std::string& name, bool caseSensitive = false) { | ||
| 128 | boost::optional<T> result; | ||
| 129 | StructNameFinder<T> finder(name, caseSensitive); | ||
| 130 | typename std::vector<T>::const_iterator it; | ||
| 131 | it = find_if(vec.begin(), vec.end(), finder); | ||
| 132 | if (it != vec.end()) { | ||
| 133 | result = *it; | ||
| 134 | }; | ||
| 135 | return result; | ||
| 136 | } | ||
| 137 | |||
| 138 | /** Template function for finding objects in a vector of shared_ptr<T> by name | ||
| 139 | */ | ||
| 140 | template <class T> | ||
| 141 | std::shared_ptr<T> findStructByName(const std::vector<std::shared_ptr<T>>& vec, const std::string& name, bool caseSensitive = false) { | ||
| 142 | std::shared_ptr<T> result; | ||
| 143 | StructNameFinder<T> finder(name, caseSensitive); | ||
| 144 | typename std::vector<std::shared_ptr<T>>::const_iterator it; | ||
| 145 | it = find_if(vec.begin(), vec.end(), finder); | ||
| 146 | if (it != vec.end()) { | ||
| 147 | result = *it; | ||
| 148 | }; | ||
| 149 | return result; | ||
| 150 | } | ||
| 151 | |||
| 152 | /** Template function for finding index of object in a vector<T> by name. */ | ||
| 153 | template <class T> | ||
| 154 | boost::optional<int> findStructIndexByName(const std::vector<T>& vec, const std::string& name, bool caseSensitive = false) { | ||
| 155 | boost::optional<int> index; | ||
| 156 | StructNameFinder<T> finder(name, caseSensitive); | ||
| 157 | typename std::vector<T>::const_iterator it; | ||
| 158 | it = find_if(vec.begin(), vec.end(), finder); | ||
| 159 | if (it != vec.end()) { | ||
| 160 | index = it - vec.begin(); | ||
| 161 | } | ||
| 162 | return index; | ||
| 163 | } | ||
| 164 | |||
| 165 | /** Template function for finding index of object in a vector<T> by name. */ | ||
| 166 | template <class T> | ||
| 167 | typename std::vector<T>::iterator findStructIteratorByName(std::vector<T>& vec, const std::string& name, bool caseSensitive = false) { | ||
| 168 | boost::optional<int> index; | ||
| 169 | StructNameFinder<T> finder(name, caseSensitive); | ||
| 170 | typename std::vector<T>::iterator it; | ||
| 171 | it = find_if(vec.begin(), vec.end(), finder); | ||
| 172 | return it; | ||
| 173 | } | ||
| 174 | |||
| 175 | /** Functor object for finding objects by value. Templated on T, which has method U value(). */ | ||
| 176 | template <class T, typename U> | ||
| 177 | class ValueFinder | ||
| 178 | { | ||
| 179 | public: | ||
| 180 | ValueFinder(const U& value) : m_value(value){}; | ||
| 181 | |||
| 182 | bool operator()(const T& object) const { | ||
| 183 | return (m_value == object.value()); | ||
| 184 | } | ||
| 185 | |||
| 186 | bool operator()(const std::shared_ptr<T>& objectPointer) const { | ||
| 187 | return (m_value == objectPointer->value()); | ||
| 188 | } | ||
| 189 | |||
| 190 | private: | ||
| 191 | U m_value; | ||
| 192 | }; | ||
| 193 | |||
| 194 | } // namespace openstudio | ||
| 195 | |||
| 196 | #endif // UTILITIES_CORE_FINDER_HPP | ||
| 197 |