3 Adapts `std::vector` for use with Hana.
5 @copyright Louis Dionne 2013-2016
6 @copyright Gonzalo Brito Gadeschi 2015
7 Distributed under the Boost Software License, Version 1.0.
8 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_HANA_EXT_STD_VECTOR_HPP
12 #define BOOST_HANA_EXT_STD_VECTOR_HPP
14 #include <boost/hana/config.hpp>
15 #include <boost/hana/equal.hpp>
16 #include <boost/hana/fwd/core/tag_of.hpp>
17 #include <boost/hana/less.hpp>
22 #include <type_traits>
27 BOOST_HANA_NAMESPACE_BEGIN
28 namespace ext { namespace std { struct vector_tag; }}
30 template <typename T, typename Allocator>
31 struct tag_of<std::vector<T, Allocator>> {
32 using type = ext::std::vector_tag;
35 //////////////////////////////////////////////////////////////////////////
37 //////////////////////////////////////////////////////////////////////////
39 struct equal_impl<ext::std::vector_tag, ext::std::vector_tag> {
40 template <typename T1, typename A1, typename T2, typename A2>
41 static bool apply(std::vector<T1, A1> const& v1,
42 std::vector<T2, A2> const& v2)
44 return std::equal(begin(v1), end(v1),
50 //////////////////////////////////////////////////////////////////////////
52 //////////////////////////////////////////////////////////////////////////
54 struct less_impl<ext::std::vector_tag, ext::std::vector_tag> {
55 template <typename T1, typename A1, typename T2, typename A2>
56 static bool apply(std::vector<T1, A1> const& v1,
57 std::vector<T2, A2> const& v2)
59 return std::lexicographical_compare(begin(v1), end(v1),
66 //////////////////////////////////////////////////////////////////////////
68 //////////////////////////////////////////////////////////////////////////
70 struct transform_impl<ext::std::vector_tag> {
71 template <typename V, typename F>
72 static auto apply(V&& v, F&& f) {
73 using U = std::remove_cv_t<std::remove_reference_t<
74 decltype(f(*v.begin()))
76 using Alloc = typename std::remove_reference_t<V>::allocator_type;
77 using NewAlloc = typename std::allocator_traits<Alloc>::
78 template rebind_alloc<U>;
79 std::vector<U, NewAlloc> result; result.reserve(v.size());
81 std::transform(begin(v), end(v),
82 std::back_inserter(result), std::forward<F>(f));
86 template <typename T, typename Alloc, typename F>
87 static auto apply(std::vector<T, Alloc>&& v, F&& f)
91 std::remove_cv_t<std::remove_reference_t<
92 decltype(f(*v.begin()))
95 , std::vector<T, Alloc>
98 // If we receive a rvalue and the function returns elements of
99 // the same type, we modify the vector in-place instead of
100 // returning a new one.
101 std::transform(std::make_move_iterator(begin(v)),
102 std::make_move_iterator(end(v)),
103 begin(v), std::forward<F>(f));
108 BOOST_HANA_NAMESPACE_END
110 #endif // !BOOST_HANA_EXT_STD_VECTOR_HPP