vector.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*!
  2. @file
  3. Adapts `std::vector` for use with Hana.
  4. @copyright Louis Dionne 2013-2017
  5. @copyright Gonzalo Brito Gadeschi 2015
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  8. */
  9. #ifndef BOOST_HANA_EXT_STD_VECTOR_HPP
  10. #define BOOST_HANA_EXT_STD_VECTOR_HPP
  11. #include <boost/hana/config.hpp>
  12. #include <boost/hana/equal.hpp>
  13. #include <boost/hana/fwd/core/tag_of.hpp>
  14. #include <boost/hana/less.hpp>
  15. #include <algorithm>
  16. #include <iterator>
  17. #include <memory>
  18. #include <type_traits>
  19. #include <utility>
  20. #include <vector>
  21. BOOST_HANA_NAMESPACE_BEGIN
  22. namespace ext { namespace std { struct vector_tag; }}
  23. template <typename T, typename Allocator>
  24. struct tag_of<std::vector<T, Allocator>> {
  25. using type = ext::std::vector_tag;
  26. };
  27. //////////////////////////////////////////////////////////////////////////
  28. // Comparable
  29. //////////////////////////////////////////////////////////////////////////
  30. template <>
  31. struct equal_impl<ext::std::vector_tag, ext::std::vector_tag> {
  32. template <typename T1, typename A1, typename T2, typename A2>
  33. static bool apply(std::vector<T1, A1> const& v1,
  34. std::vector<T2, A2> const& v2)
  35. {
  36. return std::equal(begin(v1), end(v1),
  37. begin(v2), end(v2),
  38. hana::equal);
  39. }
  40. };
  41. //////////////////////////////////////////////////////////////////////////
  42. // Orderable
  43. //////////////////////////////////////////////////////////////////////////
  44. template <>
  45. struct less_impl<ext::std::vector_tag, ext::std::vector_tag> {
  46. template <typename T1, typename A1, typename T2, typename A2>
  47. static bool apply(std::vector<T1, A1> const& v1,
  48. std::vector<T2, A2> const& v2)
  49. {
  50. return std::lexicographical_compare(begin(v1), end(v1),
  51. begin(v2), end(v2),
  52. hana::less);
  53. }
  54. };
  55. #if 0
  56. //////////////////////////////////////////////////////////////////////////
  57. // Functor
  58. //////////////////////////////////////////////////////////////////////////
  59. template <>
  60. struct transform_impl<ext::std::vector_tag> {
  61. template <typename V, typename F>
  62. static auto apply(V&& v, F&& f) {
  63. using U = std::remove_cv_t<std::remove_reference_t<
  64. decltype(f(*v.begin()))
  65. >>;
  66. using Alloc = typename std::remove_reference_t<V>::allocator_type;
  67. using NewAlloc = typename std::allocator_traits<Alloc>::
  68. template rebind_alloc<U>;
  69. std::vector<U, NewAlloc> result; result.reserve(v.size());
  70. std::transform(begin(v), end(v),
  71. std::back_inserter(result), std::forward<F>(f));
  72. return result;
  73. }
  74. template <typename T, typename Alloc, typename F>
  75. static auto apply(std::vector<T, Alloc>&& v, F&& f)
  76. -> std::enable_if_t<
  77. std::is_same<
  78. T,
  79. std::remove_cv_t<std::remove_reference_t<
  80. decltype(f(*v.begin()))
  81. >>
  82. >{}
  83. , std::vector<T, Alloc>
  84. >
  85. {
  86. // If we receive a rvalue and the function returns elements of
  87. // the same type, we modify the vector in-place instead of
  88. // returning a new one.
  89. std::transform(std::make_move_iterator(begin(v)),
  90. std::make_move_iterator(end(v)),
  91. begin(v), std::forward<F>(f));
  92. return std::move(v);
  93. }
  94. };
  95. #endif
  96. BOOST_HANA_NAMESPACE_END
  97. #endif // !BOOST_HANA_EXT_STD_VECTOR_HPP