1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

add extension index_iterator

This commit is contained in:
bolero-MURAKAMI 2014-07-13 01:41:33 +09:00
parent 46b2b96013
commit 28e9a4e5d9
2 changed files with 7 additions and 5 deletions

View file

@ -23,12 +23,13 @@
#include <sprout/type_traits/integral_constant.hpp> #include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/identity.hpp> #include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/enabler_if.hpp> #include <sprout/type_traits/enabler_if.hpp>
#include <sprout/functional/subscript.hpp>
namespace sprout { namespace sprout {
// //
// index_iterator // index_iterator
// //
template<typename Container, bool ConvertibleToPointer = false> template<typename Container, bool ConvertibleToPointer = false, typename Subscript = sprout::subscript<> >
class index_iterator class index_iterator
: public std::iterator< : public std::iterator<
std::random_access_iterator_tag, std::random_access_iterator_tag,
@ -64,6 +65,7 @@ namespace sprout {
typename std::remove_reference<container_type>::type const typename std::remove_reference<container_type>::type const
>::type const_container_type; >::type const_container_type;
private: private:
typedef Subscript subscript_type;
typedef std::iterator< typedef std::iterator<
std::random_access_iterator_tag, std::random_access_iterator_tag,
typename traits_type::value_type, typename traits_type::value_type,
@ -127,10 +129,10 @@ namespace sprout {
sprout::swap(index_, other.index_); sprout::swap(index_, other.index_);
} }
SPROUT_CONSTEXPR reference operator*() const { SPROUT_CONSTEXPR reference operator*() const {
return holder_.get()[index_]; return subscript_type()(holder_.get(), index_);
} }
SPROUT_CONSTEXPR pointer operator->() const { SPROUT_CONSTEXPR pointer operator->() const {
return &holder_.get()[index_]; return &subscript_type()(holder_.get(), index_);
} }
SPROUT_CXX14_CONSTEXPR index_iterator& operator++() { SPROUT_CXX14_CONSTEXPR index_iterator& operator++() {
index_iterator temp(next()); index_iterator temp(next());
@ -169,7 +171,7 @@ namespace sprout {
return *this; return *this;
} }
SPROUT_CONSTEXPR reference operator[](difference_type n) const { SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return holder_.get()[index_ + n]; return subscript_type()(holder_.get(), index_ + n);
} }
}; };

View file

@ -45,7 +45,7 @@ namespace testspr {
SPROUT_CONSTEXPR reduct_iterator() SPROUT_CONSTEXPR reduct_iterator()
: current() : current()
{} {}
SPROUT_CONSTEXPR reduct_iterator(reduct_iterator const& other) = default; reduct_iterator(reduct_iterator const& other) = default;
explicit SPROUT_CONSTEXPR reduct_iterator(iterator_type it) explicit SPROUT_CONSTEXPR reduct_iterator(iterator_type it)
: current(it) : current(it)
{} {}