// Copyright 2005 Daniel Wallin. // Copyright 2005 Joel de Guzman. // Copyright 2005 Dan Marsden. // Copyright 2008 Hartmut Kaiser. // // Use, modification and distribution is subject to the Boost Software // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // Modeled after range_ex, Copyright 2004 Eric Niebler #ifndef PHOENIX_ALGORITHM_QUERYING_HPP #define PHOENIX_ALGORITHM_QUERYING_HPP #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace phoenix { namespace impl { struct find { template struct result : range_result_iterator {}; template typename result::type execute(R& r, T const& x, mpl::true_) const { return r.find(x); } template typename result::type execute(R& r, T const& x, mpl::false_) const { return std::find(detail::begin_(r), detail::end_(r), x); } template typename result::type operator()(R& r, T const& x) const { return execute(r, x, has_find()); } }; struct find_if { template struct result : range_result_iterator {}; template typename result::type operator()(R& r, P p) const { return std::find_if(detail::begin_(r), detail::end_(r), p); } }; struct find_end { template struct result : range_result_iterator {}; template typename result::type operator()(R& r, R2& r2) const { return std::find_end( detail::begin_(r) , detail::end_(r) , detail::begin_(r2) , detail::end_(r2) ); } template typename result::type operator()(R& r, R2& r2, P p) const { return std::find_end( detail::begin_(r) , detail::end_(r) , detail::begin_(r2) , detail::end_(r2) , p ); } }; struct find_first_of { template struct result : range_result_iterator {}; template typename result::type operator()(R& r, R2& r2) const { return std::find_first_of( detail::begin_(r) , detail::end_(r) , detail::begin_(r2) , detail::end_(r2) ); } template typename result::type operator()(R& r, R2& r2, P p) const { return std::find_first_of( detail::begin_(r) , detail::end_(r) , detail::begin_(r2) , detail::end_(r2) , p ); } }; struct adjacent_find { template struct result : range_result_iterator {}; template typename result::type operator()(R& r) const { return std::adjacent_find(detail::begin_(r), detail::end_(r)); } template typename result::type operator()(R& r, P p) const { return std::adjacent_find(detail::begin_(r), detail::end_(r), p); } }; struct count { template struct result : range_difference {}; template typename result::type operator()(R& r, T const& x) const { return std::count(detail::begin_(r), detail::end_(r), x); } }; struct count_if { template struct result : range_difference {}; template typename result::type operator()(R& r, P p) const { return std::count_if(detail::begin_(r), detail::end_(r), p); } }; struct distance { template struct result : range_difference {}; template typename result::type operator()(R& r) const { return std::distance(detail::begin_(r), detail::end_(r)); } }; struct equal { template struct result { typedef bool type; }; template bool operator()(R& r, I i) const { return std::equal(detail::begin_(r), detail::end_(r), i); } template bool operator()(R& r, I i, P p) const { return std::equal(detail::begin_(r), detail::end_(r), i, p); } }; struct search { template struct result : range_result_iterator {}; template typename result::type operator()(R& r, R2& r2) const { return std::search( detail::begin_(r) , detail::end_(r) , detail::begin_(r2) , detail::end_(r2) ); } template typename result::type operator()(R& r, R2& r2, P p) const { return std::search( detail::begin_(r) , detail::end_(r) , detail::begin_(r2) , detail::end_(r2) , p ); } }; struct lower_bound { template struct result : range_result_iterator {}; template typename result::type execute(R& r, T const& val, mpl::true_) const { return r.lower_bound(val); } template typename result::type execute(R& r, T const& val, mpl::false_) const { return std::lower_bound(detail::begin_(r), detail::end_(r), val); } template typename result::type operator()(R& r, T const& val) const { return execute(r, val, has_lower_bound()); } template typename result::type operator()(R& r, T const& val, C c) const { return std::lower_bound(detail::begin_(r), detail::end_(r), val, c); } }; struct upper_bound { template struct result : range_result_iterator {}; template typename result::type execute(R& r, T const& val, mpl::true_) const { return r.upper_bound(val); } template typename result::type execute(R& r, T const& val, mpl::false_) const { return std::upper_bound(detail::begin_(r), detail::end_(r), val); } template typename result::type operator()(R& r, T const& val) const { return execute(r, val, has_upper_bound()); } template typename result::type operator()(R& r, T const& val, C c) const { return std::upper_bound(detail::begin_(r), detail::end_(r), val, c); } }; struct equal_range { template struct result { typedef std::pair< typename range_result_iterator::type , typename range_result_iterator::type > type; }; template typename result::type execute(R& r, T const& val, mpl::true_) const { return r.equal_range(val); } template typename result::type execute(R& r, T const& val, mpl::false_) const { return std::equal_range(detail::begin_(r), detail::end_(r), val); } template typename result::type operator()(R& r, T const& val) const { return execute(r, val, has_equal_range()); } template typename result::type operator()(R& r, T const& val, C c) const { return std::equal_range(detail::begin_(r), detail::end_(r), val, c); } }; struct mismatch { template struct result { typedef std::pair< typename range_result_iterator::type , typename detail::decay_array::type > type; }; template typename result::type operator()(R& r, I i) const { return std::mismatch(detail::begin_(r), detail::end_(r), i); } template typename result::type operator()(R& r, I i, P p) const { return std::mismatch(detail::begin_(r), detail::end_(r), i, p); } }; struct binary_search { template struct result { typedef bool type; }; template bool operator()(R& r, T const& val) const { return std::binary_search(detail::begin_(r), detail::end_(r), val); } template bool operator()(R& r, T const& val, C c) const { return std::binary_search(detail::begin_(r), detail::end_(r), val, c); } }; struct includes { template struct result { typedef bool type; }; template bool operator()(R1& r1, R2& r2) const { return std::includes( detail::begin_(r1), detail::end_(r1) , detail::begin_(r2), detail::end_(r2) ); } template bool operator()(R1& r1, R2& r2, C c) const { return std::includes( detail::begin_(r1), detail::end_(r1) , detail::begin_(r2), detail::end_(r2) , c ); } }; struct min_element { template struct result : range_result_iterator {}; template typename result::type operator()(R& r) const { return std::min_element(detail::begin_(r), detail::end_(r)); } template typename result::type operator()(R& r, P p) const { return std::min_element(detail::begin_(r), detail::end_(r), p); } }; struct max_element { template struct result : range_result_iterator {}; template typename result::type operator()(R& r) const { return std::max_element(detail::begin_(r), detail::end_(r)); } template typename result::type operator()(R& r, P p) const { return std::max_element(detail::begin_(r), detail::end_(r), p); } }; struct lexicographical_compare { template struct result { typedef bool type; }; template typename result::type operator()(R1& r1, R2& r2) const { return std::lexicographical_compare( detail::begin_(r1), detail::end_(r1) , detail::begin_(r2), detail::end_(r2) ); } template typename result::type operator()(R1& r1, R2& r2, P p) const { return std::lexicographical_compare( detail::begin_(r1), detail::end_(r1) , detail::begin_(r2), detail::end_(r2) , p ); } }; } function const find = impl::find(); function const find_if = impl::find_if(); function const find_end = impl::find_end(); function const find_first_of = impl::find_first_of(); function const adjacent_find = impl::adjacent_find(); function const count = impl::count(); function const count_if = impl::count_if(); function const distance = impl::distance(); function const equal = impl::equal(); function const search = impl::search(); function const lower_bound = impl::lower_bound(); function const upper_bound = impl::upper_bound(); function const equal_range = impl::equal_range(); function const mismatch = impl::mismatch(); function const binary_search = impl::binary_search(); function const includes = impl::includes(); function const min_element = impl::min_element(); function const max_element = impl::max_element(); function const lexicographical_compare = impl::lexicographical_compare(); }} #endif