sliced.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_ADAPTOR_SLICED_HPP
  11. #define BOOST_RANGE_ADAPTOR_SLICED_HPP
  12. #include <boost/range/adaptor/argument_fwd.hpp>
  13. #include <boost/range/size_type.hpp>
  14. #include <boost/range/iterator_range.hpp>
  15. #include <boost/range/concepts.hpp>
  16. namespace boost
  17. {
  18. namespace adaptors
  19. {
  20. struct sliced
  21. {
  22. sliced(std::size_t t_, std::size_t u_)
  23. : t(t_), u(u_) {}
  24. std::size_t t;
  25. std::size_t u;
  26. };
  27. template< class RandomAccessRange >
  28. class sliced_range : public boost::iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type >
  29. {
  30. typedef boost::iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type > base_t;
  31. public:
  32. template<typename Rng, typename T, typename U>
  33. sliced_range(Rng& rng, T t, U u)
  34. : base_t(boost::next(boost::begin(rng), t),
  35. boost::next(boost::begin(rng), u))
  36. {
  37. }
  38. };
  39. template< class RandomAccessRange >
  40. inline sliced_range<RandomAccessRange>
  41. slice( RandomAccessRange& rng, std::size_t t, std::size_t u )
  42. {
  43. BOOST_RANGE_CONCEPT_ASSERT((
  44. RandomAccessRangeConcept<RandomAccessRange>));
  45. BOOST_ASSERT( t <= u && "error in slice indices" );
  46. BOOST_ASSERT( static_cast<std::size_t>(boost::size(rng)) >= u &&
  47. "second slice index out of bounds" );
  48. return sliced_range<RandomAccessRange>(rng, t, u);
  49. }
  50. template< class RandomAccessRange >
  51. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type >
  52. slice( const RandomAccessRange& rng, std::size_t t, std::size_t u )
  53. {
  54. BOOST_RANGE_CONCEPT_ASSERT((
  55. RandomAccessRangeConcept<const RandomAccessRange>));
  56. BOOST_ASSERT( t <= u && "error in slice indices" );
  57. BOOST_ASSERT( static_cast<std::size_t>(boost::size(rng)) >= u &&
  58. "second slice index out of bounds" );
  59. return sliced_range<const RandomAccessRange>(rng, t, u);
  60. }
  61. template< class RandomAccessRange >
  62. inline sliced_range<RandomAccessRange>
  63. operator|( RandomAccessRange& r, const sliced& f )
  64. {
  65. BOOST_RANGE_CONCEPT_ASSERT((
  66. RandomAccessRangeConcept<RandomAccessRange>));
  67. return sliced_range<RandomAccessRange>( r, f.t, f.u );
  68. }
  69. template< class RandomAccessRange >
  70. inline sliced_range<const RandomAccessRange>
  71. operator|( const RandomAccessRange& r, const sliced& f )
  72. {
  73. BOOST_RANGE_CONCEPT_ASSERT((
  74. RandomAccessRangeConcept<const RandomAccessRange>));
  75. return sliced_range<const RandomAccessRange>( r, f.t, f.u );
  76. }
  77. } // namespace adaptors
  78. using adaptors::sliced_range;
  79. } // namespace boost
  80. #endif