Boost logo

Boost :

From: Hamish Mackenzie (hamish_at_[hidden])
Date: 2002-04-14 21:57:57


On Sun, 2002-04-14 at 19:43, Andrei Alexandrescu wrote:
> "Hamish Mackenzie" <hamish_at_[hidden]> wrote in message
> news:1018803083.2507.1250.camel_at_lightening.firestream.co.uk...
> > I think a lot of confusion has resulted from people, including
> myself,
> > thinking that mpl::type_list and loki::type_list are (or should be)
> the
> > same thing. Both have advantages and disadvantages.
> > If mpl and loki are to be integrated it is loki::type_list and
> > mpl::list_node that need to be merged.
>
>
> Thanks Hamish for a refreshingly constructive and useful motion.
>
> > loki::type_list could be renamed to type_list_node.
> >
> > Then mpl list_node could be split into type_list_node and
> value_list_node
> > (reducing the number of template args from 4 to 2).
> >
> > This would leave us with
> > mpl::type_list == loki::TYPE_LIST_NN
> > mpl::type_list_node == loki::type_list_node
>
> I believe this is a nice approach that could make everybody happy.
> Then, Loki's dependency of mpl would be precisely one little header.
>

Something else that occurs to me is that I can see no reason not have
both implementations of the algorithms in the same namespace. The loki
style implementation being specialisation for type_list_node and
type_list_nil. This may be desirable as I expect sometimes recursive
approach may use fewer compiler resources.

That would make count_if (code borrowed from Aleksey's and Andrei's
posts)

// ____ IMPLEMENTATION 1 ____

template<
  typename Sequence
, typename Predicate
>
struct count_if
{
  typedef typename fold<
    Sequence
  , integral_c<unsigned long, 0>
  , select_if< apply<Predicate,_2>, next<_1>, _1 >
>::type type;

  typedef typename type::value_type value_type;
  BOOST_STATIC_CONSTANT(value_type, value = type::value)
};

// ____IMPLEMENTATION 2____
// for type lists only

#ifdef HAS_PARTIAL_SPEC // Sorry don't know what the name is
#ifdef USE_RECURSIVE_ALGORITHMS

template <class Predicate>
struct count_if<type_list_nil, Predicate>
{
  typedef unsigned long value_type;

  BOOST_STATIC_CONSTANT( value_type, value = 0);

  typedef integer_c< value_type, value > type;
};

template <class H, class T, class Predicate>
struct count_if<type_list_node<H, T>, Predicate>
{
  typedef unsigned long value_type;

  BOOST_STATIC_CONSTANT(
    value_type,
    value = (Predicate::apply<H>::value ? 1 : 0) +
      count_if<T, Predicate>::value);

  typedef integer_c< value_type, value > type;
};

#endif // USE_RECURSIVE_ALGORITHMS
#endif // HAS_PARTIAL_SPEC

I have made them a bit longer in order to make them compatible.

It would be nice if both lived in the same file so if I don't understand
one I can look at the other. Also a well written test library could
check the results are the same using both algorithms.

Hamish


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk