segmented_iterator_range.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2011 Eric Niebler
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <sstream>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  10. #include <boost/fusion/algorithm/query/find_if.hpp>
  11. #include <boost/fusion/container/vector/vector.hpp>
  12. #include <boost/fusion/container/generation/make_vector.hpp>
  13. #include <boost/fusion/view/iterator_range/iterator_range.hpp>
  14. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  15. #include <boost/fusion/sequence/io/out.hpp>
  16. #include <boost/fusion/sequence/intrinsic/size.hpp>
  17. #include <boost/mpl/vector_c.hpp>
  18. #include <boost/mpl/begin.hpp>
  19. #include <boost/mpl/next.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include "tree.hpp"
  22. struct ostream_fun
  23. {
  24. ostream_fun(std::ostream &sout)
  25. : sout_(sout)
  26. {}
  27. template<typename T>
  28. void operator ()(T const &t) const
  29. {
  30. sout_ << t << ' ';
  31. }
  32. private:
  33. std::ostream & sout_;
  34. };
  35. template<typename Tree>
  36. void
  37. process_tree(Tree const &tree)
  38. {
  39. using namespace boost;
  40. using namespace fusion;
  41. using mpl::_;
  42. typedef typename boost::fusion::result_of::find_if<Tree const, is_same<_,short> >::type short_iter;
  43. typedef typename boost::fusion::result_of::find_if<Tree const, is_same<_,float> >::type float_iter;
  44. typedef iterator_range<short_iter, float_iter> slice_t;
  45. BOOST_STATIC_ASSERT(traits::is_segmented<slice_t>::value);
  46. // find_if of a segmented data structure returns generic
  47. // segmented iterators
  48. short_iter si = find_if<is_same<_,short> >(tree);
  49. float_iter fi = find_if<is_same<_,float> >(tree);
  50. // If you put them in an iterator range, the range
  51. // is automatically a segmented data structure.
  52. slice_t slice(si, fi);
  53. std::stringstream sout;
  54. fusion::for_each(slice, ostream_fun(sout));
  55. BOOST_TEST((sout.str() == "100 e f 0 B "));
  56. }
  57. int
  58. main()
  59. {
  60. using namespace boost::fusion;
  61. std::cout << tuple_open('[');
  62. std::cout << tuple_close(']');
  63. std::cout << tuple_delimiter(", ");
  64. {
  65. char const* s = "Ruby";
  66. typedef vector<int, char, double, char const*> vector_type;
  67. vector_type vec(1, 'x', 3.3, s);
  68. {
  69. typedef vector_iterator<vector_type, 1> i1t;
  70. typedef vector_iterator<vector_type, 3> i3t;
  71. i1t i1(vec);
  72. i3t i3(vec);
  73. typedef iterator_range<i1t, i3t> slice_t;
  74. slice_t slice(i1, i3);
  75. std::cout << slice << std::endl;
  76. BOOST_TEST((slice == make_vector('x', 3.3)));
  77. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<slice_t>::value == 2);
  78. }
  79. {
  80. typedef vector_iterator<vector_type, 0> i1t;
  81. typedef vector_iterator<vector_type, 0> i3t;
  82. i1t i1(vec);
  83. i3t i3(vec);
  84. typedef iterator_range<i1t, i3t> slice_t;
  85. slice_t slice(i1, i3);
  86. std::cout << slice << std::endl;
  87. BOOST_TEST(slice == make_vector());
  88. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<slice_t>::value == 0);
  89. }
  90. }
  91. {
  92. typedef boost::mpl::vector_c<int, 2, 3, 4, 5, 6> mpl_vec;
  93. typedef boost::mpl::begin<mpl_vec>::type it0;
  94. typedef boost::mpl::next<it0>::type it1;
  95. typedef boost::mpl::next<it1>::type it2;
  96. typedef boost::mpl::next<it2>::type it3;
  97. it1 f;
  98. it3 l;
  99. typedef iterator_range<it1, it3> slice_t;
  100. slice_t slice(f, l);
  101. std::cout << slice << std::endl;
  102. BOOST_TEST((slice == make_vector(3, 4)));
  103. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<slice_t>::value == 2);
  104. }
  105. {
  106. process_tree(
  107. make_tree(
  108. make_vector(double(0),'B')
  109. , make_tree(
  110. make_vector(1,2,long(3))
  111. , make_tree(make_vector('a','b','c'))
  112. , make_tree(make_vector(short('d'),'e','f'))
  113. )
  114. , make_tree(
  115. make_vector(4,5,6)
  116. , make_tree(make_vector(float(1),'h','i'))
  117. , make_tree(make_vector('j','k','l'))
  118. )
  119. )
  120. );
  121. }
  122. return boost::report_errors();
  123. }