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