Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] how to get the tuple of min values of 2 tuples whatever their length?
From: Jeff Flinn (Jeffrey.Flinn_at_[hidden])
Date: 2013-07-17 08:38:29


On 7/17/2013 7:17 AM, Frédéric Bron wrote:
> I have two tuples:
> typedef boost::tuple<double, int, std::string> Key;
> Key k1, k2;
>
> I would like to get the tuple of the min values for each corresponding
> value between k1 and k2:
> Key min=Key(std::min(k1.get<0>(), k2.get<0>()), std::min(k1.get<1>(),
> k2.get<1>()), std::min(k1.get<2>(), k2.get<2>()));
>
> but I do not know the length of the tuple so I am looking for a
> generic algorithm...
> Is this possible with boost::fusion or boost::mpl?
>
> I have tried with boost::fusion::accumulate but I need to now in the
> functor the index of the value that is currently accessed to compare
> to the right one in k1.

Have you tried the binary version of fusion::transform described at:

http://www.boost.org/doc/libs/1_54_0/libs/fusion/doc/html/fusion/algorithm/transformation/functions/transform.html

You'll need a polymorphic function object wrapping std::min(unless
fusion has an adapter), something like(untested):

struct PolyMin
{
    template<typename T> struct result
    {
        typedef typename boost::remove_const<
             typename boost::remove_reference<T>::type>::type type;
    };

    template<typename T> T operator()(const T& lhs, const T& rhs) const
    { return (std::min)(lhs, rhs); }
};

Jeff


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