/*! @file Defines `boost::hana::find_if`. @copyright Louis Dionne 2013-2016 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_FIND_IF_HPP #define BOOST_HANA_FIND_IF_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr auto find_if_t::operator()(Xs&& xs, Pred&& pred) const { using S = typename hana::tag_of::type; using FindIf = BOOST_HANA_DISPATCH_IF(find_if_impl, hana::Searchable::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Searchable::value, "hana::find_if(xs, pred) requires 'xs' to be a Searchable"); #endif return FindIf::apply(static_cast(xs), static_cast(pred)); } //! @endcond template struct find_if_impl> : default_ { template static constexpr auto apply(Args&& ...) = delete; }; namespace detail { template struct advance_until; template struct advance_until : advance_until(detail::decay()(hana::at_c(std::declval())) )>::type::value)> { }; template struct advance_until { template static constexpr auto apply(Ys&&) { return hana::nothing; } }; template struct advance_until { template static constexpr auto apply(Ys&& ys) { return hana::just(hana::at_c(static_cast(ys))); } }; } template struct find_if_impl::value>> { template static constexpr auto apply(Xs&& xs, Pred&&) { constexpr std::size_t N = decltype(hana::length(xs))::value; return detail::advance_until::apply( static_cast(xs) ); } }; template struct find_if_impl::value && !Sequence::value>> { template static constexpr auto find_if_helper(Xs&& xs, Pred&& pred, hana::true_) { return hana::just(hana::front( hana::drop_while(static_cast(xs), hana::compose(hana::not_, static_cast(pred))) )); } template static constexpr auto find_if_helper(Xs&&, Pred&&, hana::false_) { return hana::nothing; } template static constexpr auto apply(Xs&& xs, Pred&& pred) { constexpr bool found = !decltype( hana::is_empty(hana::drop_while(static_cast(xs), hana::compose(hana::not_, static_cast(pred)))) )::value; return find_if_impl::find_if_helper(static_cast(xs), static_cast(pred), hana::bool_{}); } }; template struct find_if_impl { template static constexpr auto find_if_helper(Xs&&, hana::false_) { return hana::nothing; } template static constexpr auto find_if_helper(Xs&& xs, hana::true_) { return hana::just(static_cast(xs)[0]); } template static constexpr auto apply(Xs&& xs, Pred&& pred) { return find_if_helper(static_cast(xs), hana::bool_c(pred)(static_cast(xs)[0]) )::value> ); } }; namespace struct_detail { template struct get_member { X x; template constexpr decltype(auto) operator()(Member&& member) && { return hana::second(static_cast(member))( static_cast(x) ); } }; } template struct find_if_impl::value>> { template static constexpr decltype(auto) apply(X&& x, Pred&& pred) { return hana::transform( hana::find_if(hana::accessors(), hana::compose(static_cast(pred), hana::first) ), struct_detail::get_member{static_cast(x)} ); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_FIND_IF_HPP