zip_shortest_with.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*!
  2. @file
  3. Defines `boost::hana::zip_shortest_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_SHORTEST_WITH_HPP
  9. #define BOOST_HANA_ZIP_SHORTEST_WITH_HPP
  10. #include <boost/hana/fwd/zip_shortest_with.hpp>
  11. #include <boost/hana/concept/sequence.hpp>
  12. #include <boost/hana/core/dispatch.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/detail/algorithm.hpp>
  15. #include <boost/hana/detail/fast_and.hpp>
  16. #include <boost/hana/integral_constant.hpp>
  17. #include <boost/hana/length.hpp>
  18. #include <boost/hana/take_front.hpp>
  19. #include <boost/hana/zip_with.hpp>
  20. #include <cstddef>
  21. BOOST_HANA_NAMESPACE_BEGIN
  22. //! @cond
  23. template <typename F, typename Xs, typename ...Ys>
  24. constexpr auto
  25. zip_shortest_with_t::operator()(F&& f, Xs&& xs, Ys&& ...ys) const {
  26. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  27. static_assert(detail::fast_and<
  28. hana::Sequence<Xs>::value, hana::Sequence<Ys>::value...
  29. >::value,
  30. "hana::zip_shortest_with(f, xs, ys...) requires 'xs' and 'ys...' to be Sequences");
  31. #endif
  32. return zip_shortest_with_impl<typename hana::tag_of<Xs>::type>::apply(
  33. static_cast<F&&>(f),
  34. static_cast<Xs&&>(xs),
  35. static_cast<Ys&&>(ys)...
  36. );
  37. }
  38. //! @endcond
  39. template <typename S, bool condition>
  40. struct zip_shortest_with_impl<S, when<condition>> : default_ {
  41. template <typename F, typename ...Xs>
  42. static constexpr decltype(auto) apply(F&& f, Xs&& ...xs) {
  43. constexpr std::size_t lengths[] = {
  44. decltype(hana::length(xs))::value...
  45. };
  46. constexpr std::size_t min_len =
  47. *detail::min_element(lengths, lengths + sizeof...(xs));
  48. return hana::zip_with(static_cast<F&&>(f),
  49. hana::take_front(static_cast<Xs&&>(xs), hana::size_c<min_len>)...
  50. );
  51. }
  52. };
  53. BOOST_HANA_NAMESPACE_END
  54. #endif // !BOOST_HANA_ZIP_SHORTEST_WITH_HPP