zip_with.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*!
  2. @file
  3. Defines `boost::hana::zip_with`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_ZIP_WITH_HPP
  9. #define BOOST_HANA_ZIP_WITH_HPP
  10. #include <boost/hana/fwd/zip_with.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/sequence.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/core/make.hpp>
  16. #include <boost/hana/detail/fast_and.hpp>
  17. #include <boost/hana/length.hpp>
  18. #include <cstddef>
  19. #include <utility>
  20. BOOST_HANA_NAMESPACE_BEGIN
  21. //! @cond
  22. template <typename F, typename Xs, typename ...Ys>
  23. constexpr auto zip_with_t::operator()(F&& f, Xs&& xs, Ys&& ...ys) const {
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(detail::fast_and<
  26. hana::Sequence<Xs>::value, hana::Sequence<Ys>::value...
  27. >::value,
  28. "hana::zip_with(f, xs, ys...) requires 'xs' and 'ys...' to be Sequences");
  29. #endif
  30. return zip_with_impl<typename hana::tag_of<Xs>::type>::apply(
  31. static_cast<F&&>(f),
  32. static_cast<Xs&&>(xs),
  33. static_cast<Ys&&>(ys)...
  34. );
  35. }
  36. //! @endcond
  37. template <typename S>
  38. struct zip_with_impl<S, when<Sequence<S>::value>> {
  39. template <std::size_t N, typename F, typename ...Xs>
  40. static constexpr decltype(auto) transverse(F&& f, Xs&& ...xs) {
  41. return static_cast<F&&>(f)(hana::at_c<N>(static_cast<Xs&&>(xs))...);
  42. }
  43. template <std::size_t ...N, typename F, typename ...Xs>
  44. static constexpr auto
  45. zip_helper(std::index_sequence<N...>, F&& f, Xs&& ...xs) {
  46. return hana::make<S>(transverse<N>(f, xs...)...);
  47. }
  48. template <typename F, typename X, typename ...Xs>
  49. static constexpr auto
  50. apply(F&& f, X&& x, Xs&& ...xs) {
  51. constexpr std::size_t N = decltype(hana::length(x))::value;
  52. return zip_helper(std::make_index_sequence<N>{},
  53. static_cast<F&&>(f),
  54. static_cast<X&&>(x), static_cast<Xs&&>(xs)...);
  55. }
  56. };
  57. BOOST_HANA_NAMESPACE_END
  58. #endif // !BOOST_HANA_ZIP_WITH_HPP