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