Boost logo

Boost Users :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2007-06-12 06:42:43


Andrej van der Zee <mavdzee_at_[hidden]> writes:
> Than you for the quick reply :) Now I reimplemented
> intersect by converting the type sequences to mpl::set
> and do a mpl::has_key instead of mpl::contains:
>
> template <typename seq1, typename seq2>
> struct intersect
> {
> typedef typename copy<seq2
> , inserter< set<>, insert< _1, _2 > >
> >::type set_seq2;
>
> typedef typename copy_if<seq1
> , has_key <set_seq2, _1>
> , back_inserter < vector<> >
> >::type type;
> };
>
> Now I would like to optimize this and only convert to
> a set if the type sequence does not support
> mpl::has_key. How should I do this?

Ideally, we should have a metafunction that would allow you to query
whether the sequence (or any other library entity) supports a
particular concept (Associative Sequence in your case).

In absence of that, something like the following should work:

    template< typename Sequence, typename Dummy = boost::mpl::false_ >
    struct is_Associative_Sequence
       : boost::mpl::false_
    {
    };

    template< typename S >
    struct is_Associative_Sequence<
              S
            , typename boost::mpl::has_key_impl<
                  typename boost::mpl::sequence_tag<S>::type
>::template apply<S,S>::type
>
       : boost::mpl::true_
    {
    };

    int main(int argc, char *argv[])
    {
         BOOST_MPL_ASSERT_NOT(( is_Associative_Sequence< boost::mpl::vector0<> > ));
         BOOST_MPL_ASSERT(( is_Associative_Sequence< boost::mpl::map0<> > ));
    }

HTH,

-- 
Aleksey Gurtovoy
MetaCommunications Engineering

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net