98 lines
3.4 KiB
C++
98 lines
3.4 KiB
C++
// Boost.Range library
|
|
//
|
|
// Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and
|
|
// distribution is subject to the Boost Software License, Version
|
|
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
//
|
|
// For more information, see http://www.boost.org/libs/range/
|
|
//
|
|
|
|
#ifndef ACTIV_BOOST_RANGE_ADAPTOR_SLICED_HPP
|
|
#define ACTIV_BOOST_RANGE_ADAPTOR_SLICED_HPP
|
|
|
|
#include <ActivBoost/range/adaptor/argument_fwd.hpp>
|
|
#include <ActivBoost/range/size_type.hpp>
|
|
#include <ActivBoost/range/iterator_range.hpp>
|
|
#include <ActivBoost/range/concepts.hpp>
|
|
#include <ActivBoost/next_prior.hpp>
|
|
|
|
namespace Boost
|
|
{
|
|
namespace adaptors
|
|
{
|
|
struct sliced
|
|
{
|
|
sliced(std::size_t t_, std::size_t u_)
|
|
: t(t_), u(u_) {}
|
|
std::size_t t;
|
|
std::size_t u;
|
|
};
|
|
|
|
template< class RandomAccessRange >
|
|
class sliced_range : public Boost::iterator_range< ACTIV_BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type >
|
|
{
|
|
typedef Boost::iterator_range< ACTIV_BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type > base_t;
|
|
public:
|
|
template<typename Rng, typename T, typename U>
|
|
sliced_range(Rng& rng, T t, U u)
|
|
: base_t(Boost::next(Boost::begin(rng), t),
|
|
Boost::next(Boost::begin(rng), u))
|
|
{
|
|
}
|
|
};
|
|
|
|
template< class RandomAccessRange >
|
|
inline sliced_range<RandomAccessRange>
|
|
slice( RandomAccessRange& rng, std::size_t t, std::size_t u )
|
|
{
|
|
ACTIV_BOOST_RANGE_CONCEPT_ASSERT((
|
|
RandomAccessRangeConcept<RandomAccessRange>));
|
|
|
|
ACTIV_BOOST_ASSERT( t <= u && "error in slice indices" );
|
|
ACTIV_BOOST_ASSERT( static_cast<std::size_t>(Boost::size(rng)) >= u &&
|
|
"second slice index out of bounds" );
|
|
|
|
return sliced_range<RandomAccessRange>(rng, t, u);
|
|
}
|
|
|
|
template< class RandomAccessRange >
|
|
inline iterator_range< ACTIV_BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type >
|
|
slice( const RandomAccessRange& rng, std::size_t t, std::size_t u )
|
|
{
|
|
ACTIV_BOOST_RANGE_CONCEPT_ASSERT((
|
|
RandomAccessRangeConcept<const RandomAccessRange>));
|
|
|
|
ACTIV_BOOST_ASSERT( t <= u && "error in slice indices" );
|
|
ACTIV_BOOST_ASSERT( static_cast<std::size_t>(Boost::size(rng)) >= u &&
|
|
"second slice index out of bounds" );
|
|
|
|
return sliced_range<const RandomAccessRange>(rng, t, u);
|
|
}
|
|
|
|
template< class RandomAccessRange >
|
|
inline sliced_range<RandomAccessRange>
|
|
operator|( RandomAccessRange& r, const sliced& f )
|
|
{
|
|
ACTIV_BOOST_RANGE_CONCEPT_ASSERT((
|
|
RandomAccessRangeConcept<RandomAccessRange>));
|
|
|
|
return sliced_range<RandomAccessRange>( r, f.t, f.u );
|
|
}
|
|
|
|
template< class RandomAccessRange >
|
|
inline sliced_range<const RandomAccessRange>
|
|
operator|( const RandomAccessRange& r, const sliced& f )
|
|
{
|
|
ACTIV_BOOST_RANGE_CONCEPT_ASSERT((
|
|
RandomAccessRangeConcept<const RandomAccessRange>));
|
|
|
|
return sliced_range<const RandomAccessRange>( r, f.t, f.u );
|
|
}
|
|
|
|
} // namespace adaptors
|
|
using adaptors::sliced_range;
|
|
} // namespace Boost
|
|
|
|
#endif
|