new cleaner(?) implementation for segmented fusion

[SVN r73644]
This commit is contained in:
Eric Niebler
2011-08-11 04:14:50 +00:00
parent 00b2cfc52e
commit d9c5b32687
28 changed files with 2171 additions and 1496 deletions

227
include/boost/fusion/algorithm/query/ext_/find_if_s.hpp Executable file → Normal file
View File

@ -1,188 +1,58 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
Copyright (c) 2011 Eric Niebler
Distributed under 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)
==============================================================================*/
#if !defined(FIND_IF_S_05152006_1027)
#define FIND_IF_S_05152006_1027
#if !defined(BOOST_FUSION_FIND_IF_S_HPP_INCLUDED)
#define BOOST_FUSION_FIND_IF_S_HPP_INCLUDED
#include <boost/mpl/not.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/fusion/algorithm/query/find_if.hpp>
#include <boost/fusion/container/list/cons.hpp>
#include <boost/fusion/sequence/intrinsic/ext_/segments.hpp>
#include <boost/fusion/view/ext_/segmented_iterator.hpp>
#include <boost/fusion/view/ext_/segmented_iterator_range.hpp>
#include <boost/fusion/support/ext_/is_segmented.hpp>
// fwd declarations
namespace boost { namespace fusion
{
namespace detail
{
template<typename Sequence, typename Pred, bool IsSegmented = traits::is_segmented<Sequence>::value>
struct static_find_if_s_recurse;
}
namespace result_of
{
template <typename Sequence, typename Pred>
struct find_if_s;
}
}}
#include <boost/fusion/view/ext_/segmented_fold_until.hpp>
namespace boost { namespace fusion { namespace detail
{
template<typename Sequence, typename Where, bool IsSegmented = traits::is_segmented<Sequence>::value>
struct is_found
: mpl::not_<result_of::equal_to<Where, typename result_of::end<Sequence>::type> >
{};
template<typename Sequence, typename Cons>
struct is_found<Sequence, Cons, true>
: mpl::not_<is_same<nil, Cons> >
{};
template<
typename SegmentedRange
, typename Where
, typename Sequence = typename remove_reference<
typename result_of::deref<
typename SegmentedRange::iterator_type
>::type
>::type
, bool IsSegmented = traits::is_segmented<Sequence>::value
>
struct as_segmented_cons
template<typename Pred>
struct segmented_find_if_fun
{
typedef cons<
SegmentedRange
, cons<segmented_range<Sequence, Where, false> >
> type;
template<typename Sig>
struct result;
static type call(SegmentedRange const &range, Where const &where)
template<typename This, typename Range, typename State, typename Context>
struct result<This(Range&, State&, Context&)>
{
return fusion::make_cons(
range
, fusion::make_cons(
segmented_range<Sequence, Where, false>(*fusion::begin(range), where)
)
);
typedef
typename result_of::find_if<Range, Pred>::type
iterator_type;
typedef
typename result_of::make_segmented_iterator<
iterator_type,
typename remove_const<Context>::type
>::type
segmented_iterator_type;
typedef
typename mpl::if_<
result_of::equal_to<
iterator_type,
typename result_of::end<Range>::type
>,
fusion::result<segmented_iterator_type, continue_>, // NOT FOUND
fusion::result<segmented_iterator_type, break_> // FOUND
>::type
type;
};
template<typename Range, typename State, typename Context>
typename result<segmented_find_if_fun(Range&, State const&, Context const&)>::type
operator()(Range& rng, State const&, Context const& context) const
{
return fusion::make_segmented_iterator(fusion::find_if<Pred>(rng), context);
}
};
template<
typename SegmentedRange
, typename Where
, typename Sequence
>
struct as_segmented_cons<SegmentedRange, Where, Sequence, true>
{
typedef cons<SegmentedRange, Where> type;
static type call(SegmentedRange const &range, Where const &where)
{
return fusion::make_cons(range, where);
}
};
template<
typename SegmentedRange
, typename Pred
, bool IsEmpty = is_empty<SegmentedRange>::value
>
struct static_find_if_s_seg
{
typedef typename SegmentedRange::iterator_type first;
typedef typename result_of::deref<first>::type segment_ref;
typedef typename remove_reference<segment_ref>::type segment;
typedef static_find_if_s_recurse<segment, Pred> where;
typedef range_next<SegmentedRange> next;
typedef is_found<segment, typename where::type> is_found;
typedef as_segmented_cons<SegmentedRange, typename where::type> found;
typedef static_find_if_s_seg<typename next::type, Pred> not_found;
typedef typename mpl::eval_if<is_found, found, not_found>::type type;
static type call(SegmentedRange const &range)
{
return call_(range, is_found());
}
private:
static type call_(SegmentedRange const &range, mpl::true_)
{
return found::call(range, where::call(*range.where_));
}
static type call_(SegmentedRange const &range, mpl::false_)
{
return not_found::call(next::call(range));
}
};
template<
typename SegmentedRange
, typename Pred
>
struct static_find_if_s_seg<SegmentedRange, Pred, true>
{
typedef nil type;
static type call(SegmentedRange const &)
{
return nil();
}
};
template<typename Sequence, typename Pred>
struct static_find_if_s_recurse<Sequence, Pred, true>
{
typedef typename as_segmented_range<Sequence>::type range;
typedef static_find_if_s_seg<range, Pred> find_if;
typedef typename find_if::type type;
static type call(Sequence &seq)
{
return find_if::call(range(fusion::segments(seq)));
}
};
template<typename Sequence, typename Pred>
struct static_find_if_s_recurse<Sequence, Pred, false>
{
typedef typename result_of::find_if<Sequence, Pred>::type type;
static type call(Sequence &seq)
{
return fusion::find_if<Pred>(seq);
}
};
template<typename Sequence, typename Pred, bool IsSegmented = traits::is_segmented<Sequence>::value>
struct static_find_if_s
: static_find_if_s_recurse<Sequence, Pred, IsSegmented>
{};
template<typename Sequence, typename Pred>
struct static_find_if_s<Sequence, Pred, true>
{
typedef typename as_segmented_range<Sequence>::type range;
typedef static_find_if_s_recurse<Sequence, Pred> find_if;
typedef typename find_if::type found;
typedef segmented_iterator<typename reverse_cons<found>::type> type;
static type call(Sequence &seq)
{
return type(reverse_cons<found>::call(find_if::call(seq)));
}
};
}}}
namespace boost { namespace fusion
@ -191,31 +61,22 @@ namespace boost { namespace fusion
{
template <typename Sequence, typename Pred>
struct find_if_s
{
typedef typename
detail::static_find_if_s<
Sequence
, Pred
>::type
type;
};
: result_of::segmented_fold_until<Sequence, void_, detail::segmented_find_if_fun<Pred> >
{};
}
template <typename Pred, typename Sequence>
typename lazy_disable_if<
is_const<Sequence>
, result_of::find_if_s<Sequence, Pred>
>::type
typename result_of::find_if_s<Sequence, Pred>::type
find_if_s(Sequence& seq)
{
return detail::static_find_if_s<Sequence, Pred>::call(seq);
return fusion::segmented_fold_until(seq, void_(), detail::segmented_find_if_fun<Pred>());
}
template <typename Pred, typename Sequence>
typename result_of::find_if_s<Sequence const, Pred>::type
find_if_s(Sequence const& seq)
{
return detail::static_find_if_s<Sequence const, Pred>::call(seq);
return fusion::segmented_fold_until(seq, void_(), detail::segmented_find_if_fun<Pred>());
}
}}

View File

@ -0,0 +1,83 @@
/*=============================================================================
Copyright (c) 2011 Eric Niebler
Distributed under 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)
==============================================================================*/
#if !defined(BOOST_FUSION_FIND_S_HPP_INCLUDED)
#define BOOST_FUSION_FIND_S_HPP_INCLUDED
#include <boost/type_traits/remove_const.hpp>
#include <boost/fusion/algorithm/query/find.hpp>
#include <boost/fusion/view/ext_/segmented_fold_until.hpp>
namespace boost { namespace fusion { namespace detail
{
template<typename T>
struct segmented_find_fun
{
template<typename Sig>
struct result;
template<typename This, typename Range, typename State, typename Context>
struct result<This(Range&, State&, Context&)>
{
typedef
typename result_of::find<Range, T>::type
iterator_type;
typedef
typename result_of::make_segmented_iterator<
iterator_type,
typename remove_const<Context>::type
>::type
segmented_iterator_type;
typedef
typename mpl::if_<
result_of::equal_to<
iterator_type,
typename result_of::end<Range>::type
>,
fusion::result<segmented_iterator_type, continue_>, // NOT FOUND
fusion::result<segmented_iterator_type, break_> // FOUND
>::type
type;
};
template<typename Range, typename State, typename Context>
typename result<segmented_find_fun(Range&, State const&, Context const&)>::type
operator()(Range& rng, State const&, Context const& context) const
{
return fusion::make_segmented_iterator(fusion::find<T>(rng), context);
}
};
}}}
namespace boost { namespace fusion
{
namespace result_of
{
template <typename Sequence, typename T>
struct find_s
: result_of::segmented_fold_until<Sequence, void_, detail::segmented_find_fun<T> >
{};
}
template <typename T, typename Sequence>
typename result_of::find_s<Sequence, T>::type
find_s(Sequence& seq)
{
return fusion::segmented_fold_until(seq, void_(), detail::segmented_find_fun<T>());
}
template <typename T, typename Sequence>
typename result_of::find_s<Sequence const, T>::type
find_s(Sequence const& seq)
{
return fusion::segmented_fold_until(seq, void_(), detail::segmented_find_fun<T>());
}
}}
#endif