| 24 | */ |
| 25 | template <typename ForwardIt> |
| 26 | auto unite(ForwardIt first, ForwardIt last) |
| 27 | { |
| 28 | if (first == last) { |
| 29 | return std::remove_cv_t<std::remove_reference_t<decltype(*first)>>{}; |
| 30 | } |
| 31 | const auto maxElement = std::max_element(first, last, [](const auto& a, const auto& b) { |
| 32 | return a.size() < b.size(); |
| 33 | }); |
| 34 | Q_ASSERT(maxElement != last); |
| 35 | |
| 36 | // Start with the largest-size set. None of this set's elements is inserted |
| 37 | // into another set, so picking the largest one achieves optimum performance. |
| 38 | auto result = *maxElement; |
| 39 | for ( ; first != maxElement; ++first) { |
| 40 | result.unite(*first); |
| 41 | } |
| 42 | // skip the already included *maxElement |
| 43 | for (++first; first != last; ++first) { |
| 44 | result.unite(*first); |
| 45 | } |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * This is an overloaded convenience function. |