Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:58

0001 /*!
0002 @file
0003 Adapts `std::vector` for use with Hana.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Copyright Gonzalo Brito Gadeschi 2015
0007 Distributed under the Boost Software License, Version 1.0.
0008 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0009  */
0010 
0011 #ifndef BOOST_HANA_EXT_STD_VECTOR_HPP
0012 #define BOOST_HANA_EXT_STD_VECTOR_HPP
0013 
0014 #include <boost/hana/config.hpp>
0015 #include <boost/hana/equal.hpp>
0016 #include <boost/hana/fwd/core/tag_of.hpp>
0017 #include <boost/hana/less.hpp>
0018 
0019 #include <algorithm>
0020 #include <iterator>
0021 #include <memory>
0022 #include <type_traits>
0023 #include <utility>
0024 #include <vector>
0025 
0026 
0027 namespace boost { namespace hana {
0028     namespace ext { namespace std { struct vector_tag; }}
0029 
0030     template <typename T, typename Allocator>
0031     struct tag_of<std::vector<T, Allocator>> {
0032         using type = ext::std::vector_tag;
0033     };
0034 
0035     //////////////////////////////////////////////////////////////////////////
0036     // Comparable
0037     //////////////////////////////////////////////////////////////////////////
0038     template <>
0039     struct equal_impl<ext::std::vector_tag, ext::std::vector_tag> {
0040         template <typename T1, typename A1, typename T2, typename A2>
0041         static bool apply(std::vector<T1, A1> const& v1,
0042                           std::vector<T2, A2> const& v2)
0043         {
0044             return std::equal(begin(v1), end(v1),
0045                               begin(v2), end(v2),
0046                               hana::equal);
0047         }
0048     };
0049 
0050     //////////////////////////////////////////////////////////////////////////
0051     // Orderable
0052     //////////////////////////////////////////////////////////////////////////
0053     template <>
0054     struct less_impl<ext::std::vector_tag, ext::std::vector_tag> {
0055         template <typename T1, typename A1, typename T2, typename A2>
0056         static bool apply(std::vector<T1, A1> const& v1,
0057                           std::vector<T2, A2> const& v2)
0058         {
0059             return std::lexicographical_compare(begin(v1), end(v1),
0060                                                 begin(v2), end(v2),
0061                                                 hana::less);
0062         }
0063     };
0064 
0065 #if 0
0066     //////////////////////////////////////////////////////////////////////////
0067     // Functor
0068     //////////////////////////////////////////////////////////////////////////
0069     template <>
0070     struct transform_impl<ext::std::vector_tag> {
0071         template <typename V, typename F>
0072         static auto apply(V&& v, F&& f) {
0073             using U = std::remove_cv_t<std::remove_reference_t<
0074                 decltype(f(*v.begin()))
0075             >>;
0076             using Alloc = typename std::remove_reference_t<V>::allocator_type;
0077             using NewAlloc = typename std::allocator_traits<Alloc>::
0078                              template rebind_alloc<U>;
0079             std::vector<U, NewAlloc> result; result.reserve(v.size());
0080 
0081             std::transform(begin(v), end(v),
0082                            std::back_inserter(result), std::forward<F>(f));
0083             return result;
0084         }
0085 
0086         template <typename T, typename Alloc, typename F>
0087         static auto apply(std::vector<T, Alloc>&& v, F&& f)
0088             -> std::enable_if_t<
0089                 std::is_same<
0090                     T,
0091                     std::remove_cv_t<std::remove_reference_t<
0092                         decltype(f(*v.begin()))
0093                     >>
0094                 >{}
0095                 , std::vector<T, Alloc>
0096             >
0097         {
0098             // If we receive a rvalue and the function returns elements of
0099             // the same type, we modify the vector in-place instead of
0100             // returning a new one.
0101             std::transform(std::make_move_iterator(begin(v)),
0102                            std::make_move_iterator(end(v)),
0103                            begin(v), std::forward<F>(f));
0104             return std::move(v);
0105         }
0106     };
0107 #endif
0108 }} // end namespace boost::hana
0109 
0110 #endif // !BOOST_HANA_EXT_STD_VECTOR_HPP