experimental segmented Fusion support

[SVN r35493]
This commit is contained in:
Eric Niebler
2006-10-05 01:25:32 +00:00
parent 9591dc6dd1
commit 114556bbba
12 changed files with 2022 additions and 0 deletions

View File

@ -0,0 +1,92 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
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)
==============================================================================*/
#if !defined(FUSION_FOR_EACH_S_05022006_1027)
#define FUSION_FOR_EACH_S_05022006_1027
#include <boost/mpl/assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/sequence/intrinsic/ext_/segments.hpp>
#include <boost/fusion/support/ext_/is_segmented.hpp>
// fwd declarations
namespace boost { namespace fusion
{
template <typename Sequence, typename F>
void
for_each_s(Sequence& seq, F const& f);
template <typename Sequence, typename F>
void
for_each_s(Sequence const& seq, F const& f);
}}
namespace boost { namespace fusion { namespace detail
{
template<typename F>
struct for_each_s_bind
{
explicit for_each_s_bind(F const &f)
: f_(f)
{}
template<typename Sequence>
void operator ()(Sequence &seq) const
{
fusion::for_each_s(seq, this->f_);
}
template<typename Sequence>
void operator ()(Sequence const &seq) const
{
fusion::for_each_s(seq, this->f_);
}
private:
F const &f_;
};
template<typename Sequence, typename F>
void for_each_s(Sequence &seq, F const &f, mpl::true_)
{
fusion::for_each_s(fusion::segments(seq), for_each_s_bind<F>(f));
}
template<typename Sequence, typename F>
void for_each_s(Sequence &seq, F const &f, mpl::false_)
{
fusion::for_each(seq, f);
}
}}}
namespace boost { namespace fusion
{
namespace result_of
{
template <typename Sequence, typename F>
struct for_each_s
{
typedef void type;
};
}
template <typename Sequence, typename F>
inline void
for_each_s(Sequence& seq, F const& f)
{
detail::for_each_s(seq, f, traits::is_segmented<Sequence>());
}
template <typename Sequence, typename F>
inline void
for_each_s(Sequence const& seq, F const& f)
{
detail::for_each_s(seq, f, traits::is_segmented<Sequence>());
}
}}
#endif

View File

@ -0,0 +1,223 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
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)
==============================================================================*/
#if !defined(FIND_IF_S_05152006_1027)
#define FIND_IF_S_05152006_1027
#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/fusion/algorithm/query/find_if.hpp>
#include <boost/fusion/sequence/container/list/cons.hpp>
#include <boost/fusion/sequence/intrinsic/ext_/segments.hpp>
#include <boost/fusion/sequence/view/ext_/segmented_iterator.hpp>
#include <boost/fusion/sequence/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;
}
}}
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
{
typedef cons<
SegmentedRange
, cons<segmented_range<Sequence, Where, false> >
> type;
static type call(SegmentedRange const &range, Where const &where)
{
return fusion::make_cons(
range
, fusion::make_cons(
segmented_range<Sequence, Where, false>(*fusion::begin(range), where)
)
);
}
};
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
{
namespace result_of
{
template <typename Sequence, typename Pred>
struct find_if_s
{
typedef typename
detail::static_find_if_s<
Sequence
, Pred
>::type
type;
};
}
template <typename Pred, typename Sequence>
typename lazy_disable_if<
is_const<Sequence>
, result_of::find_if_s<Sequence, Pred>
>::type
find_if_s(Sequence& seq)
{
return detail::static_find_if_s<Sequence, Pred>::call(seq);
}
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);
}
}}
#endif

View File

@ -0,0 +1,130 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
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)
==============================================================================*/
#ifndef FUSION_BINARY_TREE_EAN_05032006_1027
#define FUSION_BINARY_TREE_EAN_05032006_1027
#include <boost/mpl/if.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/add_const.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/fusion/support/is_sequence.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/sequence/view/single_view.hpp>
#include <boost/fusion/sequence/container/list/cons.hpp> // for nil
#include <boost/fusion/sequence/container/vector/vector10.hpp>
#include <boost/fusion/support/sequence_base.hpp>
#include <boost/fusion/sequence/intrinsic/ext_/segments.hpp>
#include <boost/fusion/support/ext_/is_segmented.hpp>
#include <boost/fusion/sequence/view/ext_/segmented_iterator.hpp>
namespace boost { namespace fusion
{
struct tree_tag;
namespace detail
{
template<typename T, bool IsConst>
struct reference : add_reference<T> {};
template<typename T>
struct reference<T, true> : reference<typename add_const<T>::type, false> {};
template<typename T>
struct reference<T &, true> : reference<T, false> {};
}
template<typename Data, typename Left = nil, typename Right = nil>
struct tree
: sequence_base<tree<Data, Left, Right> >
{
typedef Data data_type;
typedef Left left_type;
typedef Right right_type;
typedef tree_tag fusion_tag;
typedef forward_traversal_tag category;
typedef mpl::false_ is_view;
typedef typename mpl::if_<
traits::is_sequence<Data>
, Data
, single_view<Data>
>::type data_view;
explicit tree(
typename fusion::detail::call_param<Data>::type data_
, typename fusion::detail::call_param<Left>::type left_ = Left()
, typename fusion::detail::call_param<Right>::type right_ = Right()
)
: segments(left_, data_view(data_), right_)
{}
typedef vector3<Left, data_view, Right> segments_type;
segments_type segments;
};
template<typename Data>
tree<Data> make_tree(Data const &data)
{
return tree<Data>(data);
}
template<typename Data, typename Left, typename Right>
tree<Data, Left, Right> make_tree(Data const &data, Left const &left, Right const &right)
{
return tree<Data, Left, Right>(data, left, right);
}
namespace extension
{
template<>
struct is_segmented_impl<tree_tag>
{
template<typename Sequence>
struct apply : mpl::true_ {};
};
template<>
struct segments_impl<tree_tag>
{
template<typename Sequence>
struct apply
{
typedef typename mpl::if_<
is_const<Sequence>
, typename Sequence::segments_type const &
, typename Sequence::segments_type &
>::type type;
static type call(Sequence &seq)
{
return seq.segments;
}
};
};
template<>
struct begin_impl<tree_tag>
{
template<typename Sequence>
struct apply
: segmented_begin<Sequence>
{};
};
template<>
struct end_impl<tree_tag>
{
template<typename Sequence>
struct apply
: segmented_end<Sequence>
{};
};
}
}}
#endif

View File

@ -0,0 +1,57 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
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)
==============================================================================*/
#if !defined(FUSION_SEGMENTS_04052005_1141)
#define FUSION_SEGMENTS_04052005_1141
#include <boost/fusion/support/tag_of.hpp>
namespace boost { namespace fusion
{
// segments: returns a sequence of sequences
namespace extension
{
template <typename Tag>
struct segments_impl
{
template <typename Sequence>
struct apply {};
};
}
namespace result_of
{
template <typename Sequence>
struct segments
{
typedef typename
extension::segments_impl<typename traits::tag_of<Sequence>::type>::
template apply<Sequence>::type
type;
};
}
template <typename Sequence>
typename result_of::segments<Sequence>::type
segments(Sequence & seq)
{
return
extension::segments_impl<typename traits::tag_of<Sequence>::type>::
template apply<Sequence>::call(seq);
}
template <typename Sequence>
typename result_of::segments<Sequence const>::type
segments(Sequence const& seq)
{
return
extension::segments_impl<typename traits::tag_of<Sequence>::type>::
template apply<Sequence const>::call(seq);
}
}}
#endif

View File

@ -0,0 +1,179 @@
/*=============================================================================
Copyright (c) 2001-2006 Eric Niebler
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)
==============================================================================*/
#ifndef FUSION_MULTIPLE_VIEW_05052005_0335
#define FUSION_MULTIPLE_VIEW_05052005_0335
#include <boost/mpl/int.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/next.hpp>
#include <boost/fusion/support/detail/access.hpp>
#include <boost/fusion/support/sequence_base.hpp>
#include <boost/fusion/support/iterator_base.hpp>
#include <boost/fusion/support/detail/as_fusion_element.hpp>
namespace boost { namespace fusion
{
struct multiple_view_tag;
struct forward_traversal_tag;
struct fusion_sequence_tag;
template<typename Size, typename T>
struct multiple_view
: sequence_base<multiple_view<Size, T> >
{
typedef multiple_view_tag fusion_tag;
typedef fusion_sequence_tag tag; // this gets picked up by MPL
typedef forward_traversal_tag category;
typedef mpl::true_ is_view;
typedef mpl::int_<Size::value> size;
typedef T value_type;
multiple_view()
: val()
{}
explicit multiple_view(typename detail::call_param<T>::type val)
: val(val)
{}
value_type val;
};
template<typename Size, typename T>
inline multiple_view<Size, typename detail::as_fusion_element<T>::type>
make_multiple_view(T const& v)
{
return multiple_view<Size, typename detail::as_fusion_element<T>::type>(v);
}
struct multiple_view_iterator_tag;
struct forward_traversal_tag;
template<typename Index, typename MultipleView>
struct multiple_view_iterator
: iterator_base<multiple_view_iterator<Index, MultipleView> >
{
typedef multiple_view_iterator_tag fusion_tag;
typedef forward_traversal_tag category;
typedef typename MultipleView::value_type value_type;
typedef MultipleView multiple_view_type;
typedef Index index;
explicit multiple_view_iterator(multiple_view_type const &view_)
: view(view_)
{}
multiple_view_type view;
};
namespace extension
{
template <typename Tag>
struct next_impl;
template <>
struct next_impl<multiple_view_iterator_tag>
{
template <typename Iterator>
struct apply
{
typedef multiple_view_iterator<
typename mpl::next<typename Iterator::index>::type
, typename Iterator::multiple_view_type
> type;
static type
call(Iterator const &where)
{
return type(where.view);
}
};
};
template <typename Tag>
struct end_impl;
template <>
struct end_impl<multiple_view_tag>
{
template <typename Sequence>
struct apply
{
typedef multiple_view_iterator<
typename Sequence::size
, Sequence
> type;
static type
call(Sequence &seq)
{
return type(seq);
}
};
};
template <typename Tag>
struct deref_impl;
template <>
struct deref_impl<multiple_view_iterator_tag>
{
template <typename Iterator>
struct apply
{
typedef typename Iterator::value_type type;
static type
call(Iterator const& i)
{
return i.view.val;
}
};
};
template <typename Tag>
struct begin_impl;
template <>
struct begin_impl<multiple_view_tag>
{
template <typename Sequence>
struct apply
{
typedef multiple_view_iterator<
mpl::int_<0>
, Sequence
> type;
static type
call(Sequence &seq)
{
return type(seq);
}
};
};
template <typename Tag>
struct value_of_impl;
template <>
struct value_of_impl<multiple_view_iterator_tag>
{
template <typename Iterator>
struct apply
{
typedef typename Iterator::multiple_view_type multiple_view_type;
typedef typename multiple_view_type::value_type type;
};
};
}
}}
#endif

View File

@ -0,0 +1,416 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
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)
==============================================================================*/
#ifndef FUSION_SEGMENTED_ITERATOR_EAN_05032006_1027
#define FUSION_SEGMENTED_ITERATOR_EAN_05032006_1027
#include <boost/mpl/if.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/fusion/support/tag_of.hpp>
#include <boost/fusion/support/is_sequence.hpp>
#include <boost/fusion/sequence/view/filter_view.hpp>
#include <boost/fusion/sequence/container/list/cons.hpp> // for nil
#include <boost/fusion/sequence/generation/make_cons.hpp>
#include <boost/fusion/iterator/distance.hpp>
#include <boost/fusion/sequence/intrinsic/ext_/segments.hpp>
#include <boost/fusion/support/ext_/is_segmented.hpp>
namespace boost { namespace fusion
{
struct fusion_sequence_tag;
namespace detail
{
using mpl::_;
using mpl::not_;
////////////////////////////////////////////////////////////////////////////
template<typename Sequence>
struct is_empty
: result_of::equal_to<
typename result_of::begin<Sequence>::type
, typename result_of::end<Sequence>::type
>
{};
struct segmented_range_tag;
////////////////////////////////////////////////////////////////////////////
template<typename Sequence, typename Iterator, bool IsSegmented>
struct segmented_range
: sequence_base<segmented_range<Sequence, Iterator, IsSegmented> >
{
typedef mpl::bool_<IsSegmented> is_segmented;
typedef segmented_range_tag fusion_tag;
typedef fusion_sequence_tag tag; // this gets picked up by MPL
typedef mpl::true_ is_view;
typedef Iterator iterator_type;
// If this is a range of segments, skip over the empty ones
typedef typename mpl::if_<
is_segmented
, filter_view<Sequence, not_<is_empty<_> > >
, Sequence
>::type sequence_non_ref_type;
typedef typename mpl::if_<
traits::is_view<sequence_non_ref_type>
, sequence_non_ref_type
, sequence_non_ref_type &
>::type sequence_type;
typedef typename traits::category_of<sequence_non_ref_type>::type category;
explicit segmented_range(Sequence &sequence_)
: sequence(sequence_type(sequence_))
, where(fusion::begin(sequence))
{}
segmented_range(sequence_type sequence_, iterator_type const &where_)
: sequence(sequence_)
, where(where_)
{}
sequence_type sequence;
iterator_type where;
};
}
namespace extension
{
template<>
struct is_segmented_impl<detail::segmented_range_tag>
{
template<typename Sequence>
struct apply
: Sequence::is_segmented
{};
};
template<>
struct size_impl<detail::segmented_range_tag>
{
template<typename Sequence>
struct apply
: mpl::int_<
result_of::distance<
typename Sequence::iterator_type
, typename result_of::end<typename Sequence::sequence_non_ref_type>::type
>::value
>
{};
};
template<>
struct segments_impl<detail::segmented_range_tag>
{
template<typename Sequence>
struct apply
{
typedef Sequence &type;
static type call(Sequence &seq)
{
return seq;
}
};
};
template<>
struct begin_impl<detail::segmented_range_tag>
{
template<typename Sequence>
struct apply
{
typedef typename Sequence::iterator_type type;
static type call(Sequence &seq)
{
return seq.where;
}
};
};
template<>
struct end_impl<detail::segmented_range_tag>
{
template<typename Sequence>
struct apply
{
typedef typename Sequence::sequence_non_ref_type sequence;
typedef typename result_of::end<sequence>::type type;
static type call(Sequence &seq)
{
return fusion::end(seq.sequence);
}
};
};
}
namespace detail
{
///////////////////////////////////////////////////////////////////////
template<typename Range>
struct range_next;
template<typename Sequence, typename Iterator, bool IsSegmented>
struct range_next<segmented_range<Sequence, Iterator, IsSegmented> >
{
typedef typename result_of::next<Iterator>::type iterator_type;
typedef segmented_range<Sequence, iterator_type, IsSegmented> type;
static type call(segmented_range<Sequence, Iterator, IsSegmented> const &rng)
{
return type(rng.sequence, fusion::next(rng.where));
}
};
///////////////////////////////////////////////////////////////////////
template<typename Cons>
struct is_range_next_empty
: is_empty<typename range_next<typename Cons::car_type>::type>
{};
template<>
struct is_range_next_empty<nil>
: mpl::true_
{};
///////////////////////////////////////////////////////////////////////
template<typename Sequence, bool IsSegmented = traits::is_segmented<Sequence>::value>
struct as_segmented_range
{
typedef typename result_of::segments<Sequence>::type segments;
typedef typename remove_reference<segments>::type sequence;
typedef typename result_of::begin<filter_view<sequence, not_<is_empty<_> > > >::type begin;
typedef segmented_range<sequence, begin, true> type;
static type call(Sequence &seq)
{
return type(fusion::segments(seq));
}
};
template<typename Sequence>
struct as_segmented_range<Sequence, false>
{
typedef typename remove_reference<Sequence>::type sequence;
typedef typename result_of::begin<sequence>::type begin;
typedef segmented_range<sequence, begin, false> type;
static type call(Sequence &seq)
{
return type(seq);
}
};
template<typename Sequence, typename Iterator, bool IsSegmented>
struct as_segmented_range<segmented_range<Sequence, Iterator, IsSegmented>, IsSegmented>
{
typedef segmented_range<Sequence, Iterator, IsSegmented> type;
static type &call(type &seq)
{
return seq;
}
};
///////////////////////////////////////////////////////////////////////
template<
typename Sequence
, typename State = nil
, bool IsSegmented = traits::is_segmented<Sequence>::value
>
struct push_segments
{
typedef typename as_segmented_range<Sequence>::type range;
typedef typename result_of::begin<range>::type begin;
typedef typename result_of::deref<begin>::type next_ref;
typedef typename remove_reference<next_ref>::type next;
typedef push_segments<next, cons<range, State> > push;
typedef typename push::type type;
static type call(Sequence &seq, State const &state)
{
range rng(as_segmented_range<Sequence>::call(seq));
return push::call(*fusion::begin(rng), fusion::make_cons(rng, state));
}
};
template<typename Sequence, typename State>
struct push_segments<Sequence, State, false>
{
typedef typename as_segmented_range<Sequence>::type range;
typedef cons<range, State> type;
static type call(Sequence &seq, State const &state)
{
range rng(as_segmented_range<Sequence>::call(seq));
return fusion::make_cons(rng, state);
}
};
///////////////////////////////////////////////////////////////////////
template<typename State, bool IsEmpty = is_range_next_empty<State>::value>
struct pop_segments
{
typedef range_next<typename State::car_type> next;
typedef push_segments<typename next::type, typename State::cdr_type> push;
typedef typename push::type type;
static type call(State const &state)
{
typename next::type rng(next::call(state.car));
return push::call(rng, state.cdr);
}
};
template<typename State>
struct pop_segments<State, true>
{
typedef pop_segments<typename State::cdr_type> pop;
typedef typename pop::type type;
static type call(State const &state)
{
return pop::call(state.cdr);
}
};
template<>
struct pop_segments<nil, true>
{
typedef nil type;
static type call(nil const &)
{
return nil();
}
};
} // namespace detail
struct segmented_iterator_tag;
////////////////////////////////////////////////////////////////////////////
template<typename Cons>
struct segmented_iterator
: fusion::iterator_base<segmented_iterator<Cons> >
{
typedef segmented_iterator_tag fusion_tag;
typedef fusion::forward_traversal_tag category;
typedef Cons cons_type;
typedef typename Cons::car_type car_type;
typedef typename Cons::cdr_type cdr_type;
explicit segmented_iterator(Cons const &cons)
: cons_(cons)
{}
cons_type const &cons() const { return this->cons_; };
car_type const &car() const { return this->cons_.car; };
cdr_type const &cdr() const { return this->cons_.cdr; };
private:
Cons cons_;
};
///////////////////////////////////////////////////////////////////////////
template<typename Sequence>
struct segmented_begin
{
typedef typename detail::push_segments<Sequence> push;
typedef segmented_iterator<typename push::type> type;
static type call(Sequence &seq)
{
return type(push::call(seq, nil()));
}
};
///////////////////////////////////////////////////////////////////////////
template<typename Sequence>
struct segmented_end
{
typedef segmented_iterator<nil> type;
static type call(Sequence &)
{
return type(nil());
}
};
namespace extension
{
template<>
struct value_of_impl<segmented_iterator_tag>
{
template<typename Iterator>
struct apply
{
typedef typename result_of::begin<typename Iterator::car_type>::type begin;
typedef typename result_of::value_of<begin>::type type;
};
};
template<>
struct deref_impl<segmented_iterator_tag>
{
template<typename Iterator>
struct apply
{
typedef typename result_of::begin<typename Iterator::car_type>::type begin;
typedef typename result_of::deref<begin>::type type;
static type call(Iterator const &it)
{
return *fusion::begin(it.car());
}
};
};
// discards the old head, expands the right child of the new head
// and pushes the result to the head of the list.
template<>
struct next_impl<segmented_iterator_tag>
{
template<
typename Iterator
, bool IsSegmentDone = detail::is_range_next_empty<Iterator>::value
>
struct apply
{
typedef typename Iterator::cdr_type cdr_type;
typedef detail::range_next<typename Iterator::car_type> next;
typedef segmented_iterator<cons<typename next::type, cdr_type> > type;
static type call(Iterator const &it)
{
return type(fusion::make_cons(next::call(it.car()), it.cdr()));
}
};
template<typename Iterator>
struct apply<Iterator, true> // segment done, move to next segment
{
typedef typename Iterator::cdr_type cdr_type;
typedef typename detail::pop_segments<cdr_type> pop;
typedef segmented_iterator<typename pop::type> type;
static type call(Iterator const &it)
{
return type(pop::call(it.cdr()));
}
};
};
}
}} // namespace boost::fusion
#endif

View File

@ -0,0 +1,522 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
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)
==============================================================================*/
#ifndef FUSION_SEGMENTED_ITERATOR_RANGE_EAN_05032006_1027
#define FUSION_SEGMENTED_ITERATOR_RANGE_EAN_05032006_1027
#include <boost/mpl/bool.hpp>
#include <boost/mpl/minus.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/and.hpp>
#include <boost/fusion/sequence/container/list/cons.hpp>
#include <boost/fusion/sequence/view/joint_view.hpp>
#include <boost/fusion/sequence/view/single_view.hpp>
#include <boost/fusion/sequence/view/transform_view.hpp>
#include <boost/fusion/sequence/view/iterator_range.hpp>
#include <boost/fusion/sequence/view/ext_/multiple_view.hpp>
#include <boost/fusion/sequence/view/ext_/segmented_iterator.hpp>
namespace boost { namespace fusion
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////
template<typename Cons, typename State = nil>
struct reverse_cons;
template<typename Car, typename Cdr, typename State>
struct reverse_cons<cons<Car, Cdr>, State>
{
typedef reverse_cons<Cdr, cons<Car, State> > reverse;
typedef typename reverse::type type;
static type call(cons<Car, Cdr> const &cons, State const &state = State())
{
return reverse::call(cons.cdr, fusion::make_cons(cons.car, state));
}
};
template<typename State>
struct reverse_cons<nil, State>
{
typedef State type;
static State const &call(nil const &, State const &state = State())
{
return state;
}
};
////////////////////////////////////////////////////////////////////////////
// tags
struct full_view {};
struct left_view {};
struct right_view {};
struct center_view {};
template<typename Tag>
struct segmented_view_tag;
////////////////////////////////////////////////////////////////////////////
// a segmented view of that includes all elements either to the
// right or the left of a segmented iterator.
template<typename Tag, typename Cons1, typename Cons2 = void_>
struct segmented_view
: sequence_base<segmented_view<Tag, Cons1, Cons2> >
{
typedef segmented_view_tag<Tag> fusion_tag;
typedef fusion_sequence_tag tag; // this gets picked up by MPL
typedef mpl::true_ is_view;
typedef forward_traversal_tag category;
explicit segmented_view(Cons1 const &cons)
: cons(cons)
{}
typedef Cons1 cons_type;
cons_type const &cons;
};
// a segmented view that contains all the elements in between
// two segmented iterators
template<typename Cons1, typename Cons2>
struct segmented_view<center_view, Cons1, Cons2>
: sequence_base<segmented_view<center_view, Cons1, Cons2> >
{
typedef segmented_view_tag<center_view> fusion_tag;
typedef fusion_sequence_tag tag; // this gets picked up by MPL
typedef mpl::true_ is_view;
typedef forward_traversal_tag category;
segmented_view(Cons1 const &lcons, Cons2 const &rcons)
: left_cons(lcons)
, right_cons(rcons)
{}
typedef Cons1 left_cons_type;
typedef Cons2 right_cons_type;
left_cons_type const &left_cons;
right_cons_type const &right_cons;
};
////////////////////////////////////////////////////////////////////////////
// Used to transform a sequence of segments. The first segment is
// bounded by RightCons, and the last segment is bounded by LeftCons
// and all the others are passed through unchanged.
template<typename RightCons, typename LeftCons = RightCons>
struct segments_transform
{
explicit segments_transform(RightCons const &cons_)
: right_cons(cons_)
, left_cons(cons_)
{}
segments_transform(RightCons const &right_cons_, LeftCons const &left_cons_)
: right_cons(right_cons_)
, left_cons(left_cons_)
{}
template<typename First, typename Second>
struct result;
template<typename Second>
struct result<right_view, Second>
{
typedef segmented_view<right_view, RightCons> type;
};
template<typename Second>
struct result<left_view, Second>
{
typedef segmented_view<left_view, LeftCons> type;
};
template<typename Second>
struct result<full_view, Second>
{
typedef Second type;
};
template<typename Second>
segmented_view<right_view, RightCons> operator ()(right_view, Second &second) const
{
return segmented_view<right_view, RightCons>(this->right_cons);
}
template<typename Second>
segmented_view<left_view, LeftCons> operator ()(left_view, Second &second) const
{
return segmented_view<left_view, LeftCons>(this->left_cons);
}
template<typename Second>
Second &operator ()(full_view, Second &second) const
{
return second;
}
private:
RightCons const &right_cons;
LeftCons const &left_cons;
};
} // namespace detail
namespace extension
{
////////////////////////////////////////////////////////////////////////////
template<typename Tag>
struct is_segmented_impl<detail::segmented_view_tag<Tag> >
{
template<typename Sequence>
struct apply
: mpl::true_
{};
};
////////////////////////////////////////////////////////////////////////////
template<>
struct segments_impl<detail::segmented_view_tag<detail::right_view> >
{
template<
typename Sequence
, typename Cdr = typename Sequence::cons_type::cdr_type
>
struct apply
{
typedef typename Sequence::cons_type::car_type segmented_range;
typedef typename result_of::size<segmented_range>::type size;
typedef typename mpl::prior<size>::type size_minus_1;
typedef detail::segments_transform<Cdr> tfx;
typedef joint_view<
single_view<detail::right_view> const
, multiple_view<size_minus_1, detail::full_view> const
> mask;
typedef transform_view<mask const, segmented_range const, tfx> type;
static type call(Sequence &seq)
{
return type(
mask(
make_single_view(detail::right_view())
, make_multiple_view<size_minus_1>(detail::full_view())
)
, seq.cons.car
, tfx(seq.cons.cdr)
);
}
};
template<typename Sequence>
struct apply<Sequence, nil>
{
typedef typename Sequence::cons_type::car_type segmented_range;
typedef typename segmented_range::iterator_type begin;
typedef typename segmented_range::sequence_non_ref_type sequence_type;
typedef typename result_of::end<sequence_type>::type end;
typedef iterator_range<begin, end> range;
typedef single_view<range> type;
static type call(Sequence &seq)
{
return type(range(seq.cons.car.where, fusion::end(seq.cons.car.sequence)));
}
};
};
////////////////////////////////////////////////////////////////////////////
template<>
struct segments_impl<detail::segmented_view_tag<detail::left_view> >
{
template<
typename Sequence
, typename Cdr = typename Sequence::cons_type::cdr_type
>
struct apply
{
typedef typename Sequence::cons_type::car_type right_segmented_range;
typedef typename right_segmented_range::sequence_type sequence_type;
typedef typename right_segmented_range::iterator_type iterator_type;
typedef iterator_range<
typename result_of::begin<sequence_type>::type
, typename result_of::next<iterator_type>::type
> segmented_range;
typedef detail::segments_transform<Cdr> tfx;
typedef typename result_of::size<segmented_range>::type size;
typedef typename mpl::prior<size>::type size_minus_1;
typedef joint_view<
multiple_view<size_minus_1, detail::full_view> const
, single_view<detail::left_view> const
> mask;
typedef transform_view<mask const, segmented_range const, tfx> type;
static type call(Sequence &seq)
{
return type(
mask(
make_multiple_view<size_minus_1>(detail::full_view())
, make_single_view(detail::left_view())
)
, segmented_range(fusion::begin(seq.cons.car.sequence), fusion::next(seq.cons.car.where))
, tfx(seq.cons.cdr)
);
}
};
template<typename Sequence>
struct apply<Sequence, nil>
{
typedef typename Sequence::cons_type::car_type segmented_range;
typedef typename segmented_range::sequence_non_ref_type sequence_type;
typedef typename result_of::begin<sequence_type>::type begin;
typedef typename segmented_range::iterator_type end;
typedef iterator_range<begin, end> range;
typedef single_view<range> type;
static type call(Sequence &seq)
{
return type(range(fusion::begin(seq.cons.car.sequence), seq.cons.car.where));
}
};
};
////////////////////////////////////////////////////////////////////////////
template<>
struct segments_impl<detail::segmented_view_tag<detail::center_view> >
{
template<typename Sequence>
struct apply
{
typedef typename Sequence::right_cons_type right_cons_type;
typedef typename Sequence::left_cons_type left_cons_type;
typedef typename right_cons_type::car_type right_segmented_range;
typedef typename left_cons_type::car_type left_segmented_range;
typedef iterator_range<
typename result_of::begin<left_segmented_range>::type
, typename result_of::next<typename result_of::begin<right_segmented_range>::type>::type
> segmented_range;
typedef typename mpl::minus<
typename result_of::size<segmented_range>::type
, mpl::int_<2>
>::type size_minus_2;
BOOST_MPL_ASSERT_RELATION(0, <=, size_minus_2::value);
typedef detail::segments_transform<
typename left_cons_type::cdr_type
, typename right_cons_type::cdr_type
> tfx;
typedef joint_view<
multiple_view<size_minus_2, detail::full_view> const
, single_view<detail::left_view> const
> left_mask;
typedef joint_view<
single_view<detail::right_view> const
, left_mask const
> mask;
typedef transform_view<mask const, segmented_range const, tfx> type;
static type call(Sequence &seq)
{
left_mask lmask(
make_multiple_view<size_minus_2>(detail::full_view())
, make_single_view(detail::left_view())
);
return type(
mask(make_single_view(detail::right_view()), lmask)
, segmented_range(fusion::begin(seq.left_cons.car), fusion::next(fusion::begin(seq.right_cons.car)))
, tfx(seq.left_cons.cdr, seq.right_cons.cdr)
);
}
};
};
}
// specialize iterator_range for use with segmented iterators, so that
// it presents a segmented view of the range.
template<typename First, typename Last>
struct iterator_range;
template<typename First, typename Last>
struct iterator_range<segmented_iterator<First>, segmented_iterator<Last> >
: sequence_base<iterator_range<segmented_iterator<First>, segmented_iterator<Last> > >
{
typedef typename convert_iterator<segmented_iterator<First> >::type begin_type;
typedef typename convert_iterator<segmented_iterator<Last> >::type end_type;
typedef typename detail::reverse_cons<First>::type begin_cons_type;
typedef typename detail::reverse_cons<Last>::type end_cons_type;
typedef iterator_range_tag fusion_tag;
typedef fusion_sequence_tag tag; // this gets picked up by MPL
typedef typename traits::category_of<begin_type>::type category;
typedef typename result_of::distance<begin_type, end_type>::type size;
typedef mpl::true_ is_view;
iterator_range(segmented_iterator<First> const& first_, segmented_iterator<Last> const& last_)
: first(convert_iterator<segmented_iterator<First> >::call(first_))
, last(convert_iterator<segmented_iterator<Last> >::call(last_))
, first_cons(detail::reverse_cons<First>::call(first_.cons()))
, last_cons(detail::reverse_cons<Last>::call(last_.cons()))
{}
begin_type first;
end_type last;
begin_cons_type first_cons;
end_cons_type last_cons;
};
namespace detail
{
template<typename Cons1, typename Cons2>
struct same_segment
: mpl::false_
{};
template<typename Car1, typename Cdr1, typename Car2, typename Cdr2>
struct same_segment<cons<Car1, Cdr1>, cons<Car2, Cdr2> >
: mpl::and_<
traits::is_segmented<Car1>
, is_same<Car1, Car2>
>
{};
////////////////////////////////////////////////////////////////////////////
template<typename Cons1, typename Cons2>
struct segments_gen;
////////////////////////////////////////////////////////////////////////////
template<typename Cons1, typename Cons2, bool SameSegment>
struct segments_gen2
{
typedef segments_gen<typename Cons1::cdr_type, typename Cons2::cdr_type> gen;
typedef typename gen::type type;
static type call(Cons1 const &cons1, Cons2 const &cons2)
{
return gen::call(cons1.cdr, cons2.cdr);
}
};
template<typename Cons1, typename Cons2>
struct segments_gen2<Cons1, Cons2, false>
{
typedef segmented_view<center_view, Cons1, Cons2> view;
typedef typename result_of::segments<view>::type type;
static type call(Cons1 const &cons1, Cons2 const &cons2)
{
view v(cons1, cons2);
return fusion::segments(v);
}
};
template<typename Car1, typename Car2>
struct segments_gen2<cons<Car1>, cons<Car2>, false>
{
typedef iterator_range<
typename Car1::iterator_type
, typename Car2::iterator_type
> range;
typedef single_view<range> type;
static type call(cons<Car1> const &cons1, cons<Car2> const &cons2)
{
return type(range(cons1.car.where, cons2.car.where));
}
};
////////////////////////////////////////////////////////////////////////////
template<typename Cons1, typename Cons2>
struct segments_gen
: segments_gen2<Cons1, Cons2, same_segment<Cons1, Cons2>::value>
{};
template<typename Car, typename Cdr>
struct segments_gen<cons<Car, Cdr>, nil>
{
typedef segmented_view<right_view, cons<Car, Cdr> > view;
typedef typename result_of::segments<view>::type type;
static type call(cons<Car, Cdr> const &cons, nil const &)
{
view v(cons);
return fusion::segments(v);
}
};
template<>
struct segments_gen<nil, nil>
{
typedef nil type;
static type call(nil const &, nil const &)
{
return nil();
}
};
} // namespace detail
namespace extension
{
template<typename Tag>
struct is_segmented_impl;
// An iterator_range of segmented_iterators is segmented
template<>
struct is_segmented_impl<iterator_range_tag>
{
template<typename Iterator>
struct is_segmented_iterator : mpl::false_ {};
template<typename Cons>
struct is_segmented_iterator<segmented_iterator<Cons> > : mpl::true_ {};
template<typename Sequence>
struct apply
: mpl::and_<
is_segmented_iterator<typename Sequence::begin_type>
, is_segmented_iterator<typename Sequence::end_type>
>
{};
};
template<typename Sequence>
struct segments_impl;
template<>
struct segments_impl<iterator_range_tag>
{
template<typename Sequence>
struct apply
{
typedef typename Sequence::begin_cons_type begin_cons;
typedef typename Sequence::end_cons_type end_cons;
typedef detail::segments_gen<begin_cons, end_cons> gen;
typedef typename gen::type type;
static type call(Sequence &sequence)
{
return gen::call(sequence.first_cons, sequence.last_cons);
}
};
};
}
}}
#endif

View File

@ -0,0 +1,49 @@
/*=============================================================================
Copyright (c) 2006 Eric Niebler
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)
==============================================================================*/
#if !defined(FUSION_IS_SEGMENTED_03202006_0015)
#define FUSION_IS_SEGMENTED_03202006_0015
#include <boost/fusion/support/tag_of.hpp>
namespace boost { namespace fusion
{
// Special tags:
struct sequence_facade_tag;
struct boost_tuple_tag; // boost::tuples::tuple tag
struct array_tag; // boost::array tag
struct mpl_sequence_tag; // mpl sequence tag
struct std_pair_tag; // std::pair tag
struct iterator_range_tag;
namespace extension
{
template<typename Tag>
struct is_segmented_impl
{
template<typename Sequence>
struct apply
: mpl::false_
{};
};
template<>
struct is_segmented_impl<iterator_range_tag>;
}
namespace traits
{
template <typename Sequence>
struct is_segmented
: extension::is_segmented_impl<typename traits::tag_of<Sequence>::type>::
template apply<Sequence>
{
};
}
}}
#endif

View File

@ -95,3 +95,17 @@ import testing ;
; ;
} }
{
# Text for extension features, must be explicitly specified on the command line to be run
# TODO these are not in a test-suite because currently test-suites cannot be marked "explicit"
run algorithm/ext_/for_each_s.cpp ;
explicit for_each_s ;
run algorithm/ext_/find_if_s.cpp ;
explicit find_if_s ;
run sequence/ext_/iterator_range_s.cpp ;
explicit iterator_range_s ;
}

110
test/algorithm/ext_/find_if_s.cpp Executable file
View File

@ -0,0 +1,110 @@
/*=============================================================================
Copyright (c) 2001-2006 Joel de Guzman
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)
==============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/sequence/container/vector/vector.hpp>
#include <boost/fusion/sequence/adapted/mpl.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/algorithm/query/ext_/find_if_s.hpp>
#include <boost/fusion/sequence/container/ext_/tree.hpp>
#include <boost/fusion/sequence/generation/make_vector.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/less.hpp>
#include <boost/type_traits/is_same.hpp>
struct X
{
operator int() const
{
return 12345;
}
};
template<typename Tree>
void
process_tree(Tree const &tree)
{
using namespace boost;
using mpl::_;
typedef typename fusion::result_of::find_if_s<Tree const, is_same<_,short> >::type short_iter;
typedef typename fusion::result_of::find_if_s<Tree const, is_same<_,float> >::type float_iter;
// find_if_s of a segmented data structure returns generic
// segmented iterators
short_iter si = fusion::find_if_s<is_same<_,short> >(tree);
float_iter fi = fusion::find_if_s<is_same<_,float> >(tree);
// they behave like ordinary Fusion iterators ...
BOOST_TEST((*si == short('d')));
BOOST_TEST((*fi == float(1)));
}
int
main()
{
using namespace boost::fusion;
{
using boost::is_same;
using boost::mpl::_;
typedef vector<int, char, int, double> vector_type;
vector_type v(12345, 'x', 678910, 3.36);
std::cout << *find_if_s<is_same<_, char> >(v) << std::endl;
BOOST_TEST((*find_if_s<is_same<_, char> >(v) == 'x'));
std::cout << *find_if_s<is_same<_, int> >(v) << std::endl;
BOOST_TEST((*find_if_s<is_same<_, int> >(v) == 12345));
std::cout << *find_if_s<is_same<_, double> >(v) << std::endl;
BOOST_TEST((*find_if_s<is_same<_, double> >(v) == 3.36));
}
{
using boost::mpl::vector;
using boost::is_same;
using boost::mpl::_;
typedef vector<int, char, X, double> mpl_vec;
BOOST_TEST((*find_if_s<is_same<_, X> >(mpl_vec()) == 12345));
}
{
using boost::mpl::vector_c;
using boost::mpl::less;
using boost::mpl::int_;
using boost::is_same;
using boost::mpl::_;
typedef vector_c<int, 1, 2, 3, 4> mpl_vec;
BOOST_TEST((*find_if_s<less<_, int_<3> > >(mpl_vec()) == 1));
}
{
process_tree(
make_tree(
make_vector(double(0),'B')
, make_tree(
make_vector(1,2,long(3))
, make_tree(make_vector('a','b','c'))
, make_tree(make_vector(short('d'),'e','f'))
)
, make_tree(
make_vector(4,5,6)
, make_tree(make_vector(float(1),'h','i'))
, make_tree(make_vector('j','k','l'))
)
)
);
}
return boost::report_errors();
}

View File

@ -0,0 +1,83 @@
/*=============================================================================
Copyright (c) 2001-2006 Joel de Guzman, Eric Niebler
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)
==============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/sequence/container/vector/vector.hpp>
#include <boost/fusion/sequence/adapted/mpl.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/algorithm/iteration/ext_/for_each_s.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/fusion/sequence/generation/make_vector.hpp>
#include <boost/fusion/sequence/container/ext_/tree.hpp>
struct print
{
template <typename T>
void operator()(T const& v) const
{
std::cout << "[ " << v << " ] ";
}
};
struct increment
{
template <typename T>
void operator()(T& v) const
{
++v;
}
};
int
main()
{
using namespace boost::fusion;
using boost::mpl::vector_c;
namespace fusion = boost::fusion;
{
typedef vector<int, char, double, char const*> vector_type;
vector_type v(1, 'x', 3.3, "Ruby");
for_each_s(v, print());
std::cout << std::endl;
}
{
typedef vector<int, char, double, char const*> vector_type;
vector_type v(1, 'x', 3.3, "Ruby");
for_each_s(v, increment());
std::cout << v << std::endl;
}
{
typedef vector_c<int, 2, 3, 4, 5, 6> mpl_vec;
fusion::for_each_s(mpl_vec(), print());
std::cout << std::endl;
}
{
fusion::for_each_s(
make_tree(
make_vector(double(0),'B')
, make_tree(
make_vector(1,2,long(3))
, make_tree(make_vector('a','b','c'))
, make_tree(make_vector(short('d'),'e','f'))
)
, make_tree(
make_vector(4,5,6)
, make_tree(make_vector(float(1),'h','i'))
, make_tree(make_vector('j','k','l'))
)
)
, print()
);
}
return boost::report_errors();
}

View File

@ -0,0 +1,147 @@
/*=============================================================================
Copyright (c) 2001-2006 Joel de Guzman
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)
==============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/algorithm/iteration/ext_/for_each_s.hpp>
#include <boost/fusion/algorithm/query/ext_/find_if_s.hpp>
#include <boost/fusion/sequence/container/vector/vector.hpp>
#include <boost/fusion/sequence/container/ext_/tree.hpp>
#include <boost/fusion/sequence/generation/make_vector.hpp>
#include <boost/fusion/sequence/view/iterator_range/iterator_range.hpp>
#include <boost/fusion/sequence/view/ext_/segmented_iterator_range.hpp>
#include <boost/fusion/sequence/comparison/equal_to.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/begin.hpp>
#include <boost/mpl/next.hpp>
#include <boost/static_assert.hpp>
#include <sstream>
struct ostream_fun
{
ostream_fun(std::ostream &sout)
: sout_(sout)
{}
template<typename T>
void operator ()(T const &t) const
{
sout_ << t << ' ';
}
private:
std::ostream & sout_;
};
template<typename Tree>
void
process_tree(Tree const &tree)
{
using namespace boost;
using namespace fusion;
using mpl::_;
typedef typename result_of::find_if_s<Tree const, is_same<_,short> >::type short_iter;
typedef typename result_of::find_if_s<Tree const, is_same<_,float> >::type float_iter;
typedef iterator_range<short_iter, float_iter> slice_t;
BOOST_STATIC_ASSERT(traits::is_segmented<slice_t>::value);
// find_if_s of a segmented data structure returns generic
// segmented iterators
short_iter si = find_if_s<is_same<_,short> >(tree);
float_iter fi = find_if_s<is_same<_,float> >(tree);
// If you put them in an iterator range, the range
// is automatically a segmented data structure.
slice_t slice(si, fi);
std::stringstream sout;
fusion::for_each_s(slice, ostream_fun(sout));
BOOST_TEST((sout.str() == "100 e f 0 B "));
}
int
main()
{
using namespace boost::fusion;
std::cout << tuple_open('[');
std::cout << tuple_close(']');
std::cout << tuple_delimiter(", ");
{
char const* s = "Ruby";
typedef vector<int, char, double, char const*> vector_type;
vector_type vec(1, 'x', 3.3, s);
{
typedef vector_iterator<vector_type, 1> i1t;
typedef vector_iterator<vector_type, 3> i3t;
i1t i1(vec);
i3t i3(vec);
typedef iterator_range<i1t, i3t> slice_t;
slice_t slice(i1, i3);
std::cout << slice << std::endl;
BOOST_TEST((slice == make_vector('x', 3.3)));
BOOST_STATIC_ASSERT(result_of::size<slice_t>::value == 2);
}
{
typedef vector_iterator<vector_type, 0> i1t;
typedef vector_iterator<vector_type, 0> i3t;
i1t i1(vec);
i3t i3(vec);
typedef iterator_range<i1t, i3t> slice_t;
slice_t slice(i1, i3);
std::cout << slice << std::endl;
BOOST_TEST(slice == make_vector());
BOOST_STATIC_ASSERT(result_of::size<slice_t>::value == 0);
}
}
{
typedef boost::mpl::vector_c<int, 2, 3, 4, 5, 6> mpl_vec;
typedef boost::mpl::begin<mpl_vec>::type it0;
typedef boost::mpl::next<it0>::type it1;
typedef boost::mpl::next<it1>::type it2;
typedef boost::mpl::next<it2>::type it3;
it1 f;
it3 l;
typedef iterator_range<it1, it3> slice_t;
slice_t slice(f, l);
std::cout << slice << std::endl;
BOOST_TEST((slice == make_vector(3, 4)));
BOOST_STATIC_ASSERT(result_of::size<slice_t>::value == 2);
}
{
process_tree(
make_tree(
make_vector(double(0),'B')
, make_tree(
make_vector(1,2,long(3))
, make_tree(make_vector('a','b','c'))
, make_tree(make_vector(short('d'),'e','f'))
)
, make_tree(
make_vector(4,5,6)
, make_tree(make_vector(float(1),'h','i'))
, make_tree(make_vector('j','k','l'))
)
)
);
}
return boost::report_errors();
}