algorithm 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // -*-c++-*-
  2. // vim: set ft=cpp:
  3. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. file Copyright.txt or https://cmake.org/licensing for details. */
  5. #ifndef cmext_algorithm
  6. #define cmext_algorithm
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <memory>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/type_traits>
  13. #include <cmext/iterator>
  14. namespace cm {
  15. template <typename T>
  16. void append(std::vector<std::unique_ptr<T>>& v,
  17. std::vector<std::unique_ptr<T>>&& r)
  18. {
  19. std::transform(r.begin(), r.end(), std::back_inserter(v),
  20. [](std::unique_ptr<T>& item) { return std::move(item); });
  21. r.clear();
  22. }
  23. template <typename T>
  24. void append(std::vector<T*>& v, std::vector<std::unique_ptr<T>> const& r)
  25. {
  26. std::transform(r.begin(), r.end(), std::back_inserter(v),
  27. [](const std::unique_ptr<T>& item) { return item.get(); });
  28. }
  29. template <typename T, typename InputIt,
  30. cm::enable_if_t<cm::is_input_iterator<InputIt>::value, int> = 0>
  31. void append(std::vector<T>& v, InputIt first, InputIt last)
  32. {
  33. v.insert(v.end(), first, last);
  34. }
  35. template <typename T, typename Range,
  36. cm::enable_if_t<cm::is_input_range<Range>::value, int> = 0>
  37. void append(std::vector<T>& v, Range const& r)
  38. {
  39. v.insert(v.end(), r.begin(), r.end());
  40. }
  41. } // namespace cm
  42. #endif