sliced.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <boost/next_prior.hpp>
  17. namespace boost
  18. {
  19. namespace adaptors
  20. {
  21. struct sliced
  22. {
  23. sliced(std::size_t t_, std::size_t u_)
  24. : t(t_), u(u_) {}
  25. std::size_t t;
  26. std::size_t u;
  27. };
  28. template< class RandomAccessRange >
  29. class sliced_range : public boost::iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type >
  30. {
  31. typedef boost::iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type > base_t;
  32. public:
  33. template<typename Rng, typename T, typename U>
  34. sliced_range(Rng& rng, T t, U u)
  35. : base_t(boost::next(boost::begin(rng), t),
  36. boost::next(boost::begin(rng), u))
  37. {
  38. }
  39. };
  40. template< class RandomAccessRange >
  41. inline sliced_range<RandomAccessRange>
  42. slice( RandomAccessRange& rng, std::size_t t, std::size_t u )
  43. {
  44. BOOST_RANGE_CONCEPT_ASSERT((
  45. RandomAccessRangeConcept<RandomAccessRange>));
  46. BOOST_ASSERT( t <= u && "error in slice indices" );
  47. BOOST_ASSERT( static_cast<std::size_t>(boost::size(rng)) >= u &&
  48. "second slice index out of bounds" );
  49. return sliced_range<RandomAccessRange>(rng, t, u);
  50. }
  51. template< class RandomAccessRange >
  52. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type >
  53. slice( const RandomAccessRange& rng, std::size_t t, std::size_t u )
  54. {
  55. BOOST_RANGE_CONCEPT_ASSERT((
  56. RandomAccessRangeConcept<const RandomAccessRange>));
  57. BOOST_ASSERT( t <= u && "error in slice indices" );
  58. BOOST_ASSERT( static_cast<std::size_t>(boost::size(rng)) >= u &&
  59. "second slice index out of bounds" );
  60. return sliced_range<const RandomAccessRange>(rng, t, u);
  61. }
  62. template< class RandomAccessRange >
  63. inline sliced_range<RandomAccessRange>
  64. operator|( RandomAccessRange& r, const sliced& f )
  65. {
  66. BOOST_RANGE_CONCEPT_ASSERT((
  67. RandomAccessRangeConcept<RandomAccessRange>));
  68. return sliced_range<RandomAccessRange>( r, f.t, f.u );
  69. }
  70. template< class RandomAccessRange >
  71. inline sliced_range<const RandomAccessRange>
  72. operator|( const RandomAccessRange& r, const sliced& f )
  73. {
  74. BOOST_RANGE_CONCEPT_ASSERT((
  75. RandomAccessRangeConcept<const RandomAccessRange>));
  76. return sliced_range<const RandomAccessRange>( r, f.t, f.u );
  77. }
  78. } // namespace adaptors
  79. using adaptors::sliced_range;
  80. } // namespace boost
  81. #endif