/*============================================================================= Copyright (c) 2011-2014 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout Distributed under 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) =============================================================================*/ #ifndef SPROUT_RANGE_ADAPTOR_TAKEN_HPP #define SPROUT_RANGE_ADAPTOR_TAKEN_HPP #include #include #include #include #include #include #include #include #include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT namespace sprout { namespace adaptors { // // take_holder // template class take_holder { public: typedef Difference difference_type; private: difference_type distance_; public: explicit SPROUT_CONSTEXPR take_holder(difference_type distance) : distance_(distance) {} SPROUT_CONSTEXPR difference_type const& distance() const { return distance_; } }; // // taken_forwarder // class taken_forwarder { public: template SPROUT_CONSTEXPR sprout::adaptors::take_holder operator()(Difference distance) const { return sprout::adaptors::take_holder(distance); } }; // // taken // namespace { SPROUT_STATIC_CONSTEXPR sprout::adaptors::taken_forwarder taken = {}; } // anonymous-namespace // // operator| // template inline SPROUT_CONSTEXPR sprout::adaptors::detail::adapted_range_default< typename std::remove_reference::type>::type > operator|(Range&& lhs, sprout::adaptors::take_holder const& rhs) { typedef typename sprout::container_traits< typename std::remove_reference::type>::type >::difference_type difference_type; return sprout::adaptors::detail::adapted_range_default< typename std::remove_reference::type>::type >( sprout::begin(sprout::lvalue_forward(lhs)), sprout::next( sprout::begin(sprout::lvalue_forward(lhs)), NS_SSCRISK_CEL_OR_SPROUT::min(rhs.distance(), sprout::size(sprout::lvalue_forward(lhs))) ) ); } } // namespace adaptors } // namespace sprout #endif // #ifndef SPROUT_RANGE_ADAPTOR_TAKEN_HPP