| 1 | /* |
| 2 | * %injeqt copyright begin% |
| 3 | * Copyright 2014 Rafał Malinowski ([email protected]) |
| 4 | * %injeqt copyright end% |
| 5 | * |
| 6 | * type library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * type library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <injeqt/injeqt.h> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <functional> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace injeqt { namespace internal { |
| 30 | |
| 31 | /** |
| 32 | * @addtogroup Misc |
| 33 | * @{ |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * @class sorted_unique_vector |
| 38 | * @short Vector that stored only unique values thata are always sorted. |
| 39 | * @tparam T type of data |
| 40 | * @tparam LessThanComparator comparator used for sorting |
| 41 | * @tparam EqualityComparator comparator used for uniqueness testing |
| 42 | */ |
| 43 | template<typename K, typename V, K (*KeyExtractor)(const V &)> |
| 44 | class sorted_unique_vector |
| 45 | { |
| 46 | |
| 47 | public: |
| 48 | using type = sorted_unique_vector<K, V, KeyExtractor>; |
| 49 | using value_type = V; |
| 50 | using key_type = K; |
| 51 | using storage_type = std::vector<value_type>; |
| 52 | using const_iterator = typename storage_type::const_iterator; |
| 53 | using size_type = typename storage_type::size_type; |
| 54 | |
| 55 | private: |
| 56 | static bool compare_keys(const value_type &v1, const value_type &v2) |
| 57 | { |
| 58 | auto k1 = KeyExtractor(v1); |
| 59 | auto k2 = KeyExtractor(v2); |
| 60 | return k1 < k2; |
| 61 | } |
| 62 | |
| 63 | static bool compare_with_key(const value_type &v, const key_type &k) |
| 64 | { |
| 65 | auto vk = KeyExtractor(v); |
| 66 | return vk < k; |
| 67 | } |
| 68 | |
| 69 | static bool keys_equal(const value_type &v1, const value_type &v2) |
| 70 | { |
| 71 | auto k1 = KeyExtractor(v1); |
| 72 | auto k2 = KeyExtractor(v2); |
| 73 | return k1 == k2; |
| 74 | } |
| 75 | |
| 76 | public: |
| 77 | /** |
| 78 | * @short Create empty sorted_unique_vector. |
| 79 | */ |
| 80 | sorted_unique_vector() {} |
| 81 | |
| 82 | /** |
| 83 | * @short Create sorted_unique_vector from given vector. |
| 84 | * @param storage vector to get data from |
| 85 | * |
| 86 | * Copies content of storage, sorts it and removes duplicates. |
| 87 | */ |
| 88 | explicit sorted_unique_vector(storage_type storage) : |
| 89 | _content{std::move(storage)} |
| 90 | { |
| 91 | std::stable_sort(std::begin(_content), std::end(_content), compare_keys); |
| 92 | ensure_unique(_content); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @short Create sorted_unique_vector from given initialization list. |
| 97 | * @param values vector to get data from |
| 98 | * |
| 99 | * Copies content of storage, sorts it and removes duplicates. |
| 100 | */ |
| 101 | explicit sorted_unique_vector(std::initializer_list<value_type> values) : |
| 102 | _content{std::move(values)} |
| 103 | { |
| 104 | std::stable_sort(std::begin(_content), std::end(_content), compare_keys); |
| 105 | ensure_unique(_content); |
| 106 | } |
| 107 | |
| 108 | const_iterator begin() const |
| 109 | { |
| 110 | return std::begin(_content); |
| 111 | } |
| 112 | |
| 113 | const_iterator end() const |
| 114 | { |
| 115 | return std::end(_content); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @short Add new item to sorted vector. |
| 120 | * @param item new item |
| 121 | * |
| 122 | * Item is added at proper place so vector remains sorted. Item will not be |
| 123 | * added if another one that compares equal (using EqualityComparator) |
| 124 | * already exists. |
| 125 | */ |
| 126 | void add(value_type item) |
| 127 | { |
| 128 | if (_content.empty()) |
| 129 | { |
| 130 | _content.emplace_back(std::move(item)); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | auto upperBound = std::upper_bound(std::begin(_content), std::end(_content), item, compare_keys); |
| 135 | if (upperBound == std::begin(_content) || !keys_equal(*(upperBound - 1), item)) |
| 136 | _content.emplace(upperBound, std::move(item)); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @short Merge with another sorted vector. |
| 141 | * @param sorted_vector vector to merge with |
| 142 | * |
| 143 | * All items from sorted_vector are added at proper places and duplicates are removed. |
| 144 | */ |
| 145 | void merge(const type &sorted_vector) |
| 146 | { |
| 147 | auto result = storage_type{}; |
| 148 | |
| 149 | std::merge(std::begin(_content), std::end(_content), |
| 150 | std::begin(sorted_vector._content), std::end(sorted_vector._content), |
| 151 | std::back_inserter(result), compare_keys); |
| 152 | ensure_unique(result); |
| 153 | |
| 154 | _content = std::move(result); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return Data stored in sorted vector. |
| 159 | */ |
| 160 | const storage_type & content() const |
| 161 | { |
| 162 | return _content; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return true if no data is stored |
| 167 | */ |
| 168 | bool empty() const |
| 169 | { |
| 170 | return _content.empty(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @return true if element with value v is found |
| 175 | */ |
| 176 | bool contains(const value_type &v) const |
| 177 | { |
| 178 | auto lower_bound = std::lower_bound(begin(), end(), v, compare_keys); |
| 179 | if (lower_bound == end()) |
| 180 | return false; |
| 181 | |
| 182 | return *lower_bound == v; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return true if element with key k is found |
| 187 | */ |
| 188 | bool contains_key(const key_type &k) const |
| 189 | { |
| 190 | auto lower_bound = std::lower_bound(begin(), end(), k, compare_with_key); |
| 191 | if (lower_bound == end()) |
| 192 | return false; |
| 193 | |
| 194 | return k == KeyExtractor(*lower_bound); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @return item with key k |
| 199 | */ |
| 200 | const_iterator get(const key_type &k) const |
| 201 | { |
| 202 | auto lower_bound = std::lower_bound(begin(), end(), k, compare_with_key); |
| 203 | if (lower_bound == end()) |
| 204 | return lower_bound; |
| 205 | |
| 206 | auto vk = KeyExtractor(*lower_bound); |
| 207 | if (vk == k) |
| 208 | return lower_bound; |
| 209 | else |
| 210 | return end(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @return number of stored items |
| 215 | */ |
| 216 | typename storage_type::size_type size() const |
| 217 | { |
| 218 | return _content.size(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @short Removes all items from sorted vector. |
| 223 | */ |
| 224 | void clear() |
| 225 | { |
| 226 | _content.clear(); |
| 227 | } |
| 228 | |
| 229 | private: |
| 230 | storage_type _content; |
| 231 | |
| 232 | void ensure_unique(storage_type &storage) |
| 233 | { |
| 234 | storage.erase(std::unique(std::begin(storage), std::end(storage), keys_equal), std::end(storage)); |
| 235 | } |
| 236 | |
| 237 | }; |
| 238 | |
| 239 | /** |
| 240 | * @return begin iterator to content of sorted_unique_vector. |
| 241 | */ |
| 242 | template<typename K, typename V, K (*KeyExtractor)(const V &)> |
| 243 | typename sorted_unique_vector<K, V, KeyExtractor>::const_iterator begin(const sorted_unique_vector<K, V, KeyExtractor> &sorted_vector) |
| 244 | { |
| 245 | return std::begin(sorted_vector.content()); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @return end iterator to content of sorted_unique_vector. |
| 250 | */ |
| 251 | template<typename K, typename V, K (*KeyExtractor)(const V &)> |
| 252 | typename sorted_unique_vector<K, V, KeyExtractor>::const_iterator end(const sorted_unique_vector<K, V, KeyExtractor> &sorted_vector) |
| 253 | { |
| 254 | return std::end(sorted_vector.content()); |
| 255 | } |
| 256 | |
| 257 | template<typename K, typename V, K (*KeyExtractor)(const V &)> |
| 258 | bool operator == (const sorted_unique_vector<K, V, KeyExtractor> &x, const sorted_unique_vector<K, V, KeyExtractor> &y) |
| 259 | { |
| 260 | return x.content() == y.content(); |
| 261 | } |
| 262 | |
| 263 | template<typename K, typename V, K (*KeyExtractor)(const V &)> |
| 264 | bool operator != (const sorted_unique_vector<K, V, KeyExtractor> &x, const sorted_unique_vector<K, V, KeyExtractor> &y) |
| 265 | { |
| 266 | return !(x == y); |
| 267 | } |
| 268 | |
| 269 | template<typename K1, typename K2, typename V1, typename V2, K1 (*KeyExtractor1)(const V1 &), K2 (*KeyExtractor2)(const V2 &)> |
| 270 | struct match_result |
| 271 | { |
| 272 | std::vector<std::pair<V1, V2>> matched; |
| 273 | sorted_unique_vector<K1, V1, KeyExtractor1> unmatched_1; |
| 274 | sorted_unique_vector<K2, V2, KeyExtractor2> unmatched_2; |
| 275 | }; |
| 276 | |
| 277 | enum class match_increment_mode |
| 278 | { |
| 279 | both, |
| 280 | left |
| 281 | }; |
| 282 | |
| 283 | template<typename K, typename K1, typename K2, typename V1, typename V2, K1 (*KeyExtractor1)(const V1 &), K2 (*KeyExtractor2)(const V2 &)> |
| 284 | match_result<K1, K2, V1, V2, KeyExtractor1, KeyExtractor2> |
| 285 | match( |
| 286 | const sorted_unique_vector<K1, V1, KeyExtractor1> &suv_1, |
| 287 | const sorted_unique_vector<K2, V2, KeyExtractor2> &suv_2, |
| 288 | K(*ke1)(const V1 &), |
| 289 | K(*ke2)(const V2 &), |
| 290 | match_increment_mode increment_mode = match_increment_mode::both) |
| 291 | { |
| 292 | auto unmatched_1 = std::vector<V1>{}; |
| 293 | auto unmatched_2 = std::vector<V2>{}; |
| 294 | auto matched = std::vector<std::pair<V1, V2>>{}; |
| 295 | |
| 296 | auto suv_1_it = begin(suv_1); |
| 297 | auto suv_1_end = end(suv_1); |
| 298 | auto suv_2_it = begin(suv_2); |
| 299 | auto suv_2_end = end(suv_2); |
| 300 | |
| 301 | while (suv_1_it != suv_1_end && suv_2_it != suv_2_end) |
| 302 | { |
| 303 | auto suv_1_key = ke1(*suv_1_it); |
| 304 | auto suv_2_key = ke2(*suv_2_it); |
| 305 | if (suv_1_key == suv_2_key) |
| 306 | { |
| 307 | matched.emplace_back(*suv_1_it, *suv_2_it); |
| 308 | switch (increment_mode) |
| 309 | { |
| 310 | case match_increment_mode::both: |
| 311 | ++suv_1_it; |
| 312 | ++suv_2_it; |
| 313 | break; |
| 314 | case match_increment_mode::left: |
| 315 | ++suv_1_it; |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | else if (suv_1_key < suv_2_key) |
| 320 | { |
| 321 | unmatched_1.emplace_back(*suv_1_it); |
| 322 | ++suv_1_it; |
| 323 | } |
| 324 | else if (suv_2_key < suv_1_key) |
| 325 | { |
| 326 | unmatched_2.emplace_back(*suv_2_it); |
| 327 | ++suv_2_it; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | while (suv_1_it != suv_1_end) |
| 332 | { |
| 333 | unmatched_1.emplace_back(*suv_1_it); |
| 334 | suv_1_it++; |
| 335 | } |
| 336 | |
| 337 | while (suv_2_it != suv_2_end) |
| 338 | { |
| 339 | unmatched_2.emplace_back(*suv_2_it); |
| 340 | suv_2_it++; |
| 341 | } |
| 342 | |
| 343 | return |
| 344 | { |
| 345 | matched, |
| 346 | sorted_unique_vector<K1, V1, KeyExtractor1>{unmatched_1}, |
| 347 | sorted_unique_vector<K2, V2, KeyExtractor2>{unmatched_2} |
| 348 | }; |
| 349 | } |
| 350 | |
| 351 | template<typename K, typename V1, typename V2, K (*KeyExtractor1)(const V1 &), K (*KeyExtractor2)(const V2 &)> |
| 352 | match_result<K, K, V1, V2, KeyExtractor1, KeyExtractor2> |
| 353 | match(const sorted_unique_vector<K, V1, KeyExtractor1> &suv_1, const sorted_unique_vector<K, V2, KeyExtractor2> &suv_2) |
| 354 | { |
| 355 | return match(suv_1, suv_2, KeyExtractor1, KeyExtractor2); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * @} |
| 360 | */ |
| 361 | |
| 362 | }} |