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 | |
---|
18 | namespace boost |
---|
19 | { |
---|
20 | namespace adaptors |
---|
21 | { |
---|
22 | struct sliced |
---|
23 | { |
---|
24 | sliced(std::size_t t_, std::size_t u_) |
---|
25 | : t(t_), u(u_) {} |
---|
26 | std::size_t t; |
---|
27 | std::size_t u; |
---|
28 | }; |
---|
29 | |
---|
30 | template< class RandomAccessRange > |
---|
31 | class sliced_range : public boost::iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type > |
---|
32 | { |
---|
33 | typedef boost::iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type > base_t; |
---|
34 | public: |
---|
35 | template<typename Rng, typename T, typename U> |
---|
36 | sliced_range(Rng& rng, T t, U u) |
---|
37 | : base_t(boost::make_iterator_range(rng, t, u - boost::size(rng))) |
---|
38 | { |
---|
39 | } |
---|
40 | }; |
---|
41 | |
---|
42 | template< class RandomAccessRange > |
---|
43 | inline sliced_range<RandomAccessRange> |
---|
44 | slice( RandomAccessRange& rng, std::size_t t, std::size_t u ) |
---|
45 | { |
---|
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 | |
---|
50 | return sliced_range<RandomAccessRange>(rng, t, u); |
---|
51 | } |
---|
52 | |
---|
53 | template< class RandomAccessRange > |
---|
54 | inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type > |
---|
55 | slice( const RandomAccessRange& rng, std::size_t t, std::size_t u ) |
---|
56 | { |
---|
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 | |
---|
61 | return sliced_range<const RandomAccessRange>(rng, t, u); |
---|
62 | } |
---|
63 | |
---|
64 | template< class RandomAccessRange > |
---|
65 | inline sliced_range<RandomAccessRange> |
---|
66 | operator|( RandomAccessRange& r, const sliced& f ) |
---|
67 | { |
---|
68 | return sliced_range<RandomAccessRange>( r, f.t, f.u ); |
---|
69 | } |
---|
70 | |
---|
71 | template< class RandomAccessRange > |
---|
72 | inline sliced_range<const RandomAccessRange> |
---|
73 | operator|( const RandomAccessRange& r, const sliced& f ) |
---|
74 | { |
---|
75 | return sliced_range<const RandomAccessRange>( r, f.t, f.u ); |
---|
76 | } |
---|
77 | |
---|
78 | } // namespace adaptors |
---|
79 | } // namespace boost |
---|
80 | |
---|
81 | #endif |
---|