2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
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)
|
|
|
|
=============================================================================*/
|
2011-10-08 02:30:39 +00:00
|
|
|
#ifndef SPROUT_ITERATOR_INDEX_ITERATOR_HPP
|
|
|
|
#define SPROUT_ITERATOR_INDEX_ITERATOR_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <utility>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2012-03-31 07:24:13 +00:00
|
|
|
#include <sprout/container/traits.hpp>
|
2011-10-08 02:30:39 +00:00
|
|
|
#include <sprout/iterator/next.hpp>
|
|
|
|
#include <sprout/iterator/prev.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include <sprout/iterator/distance.hpp>
|
2014-01-05 13:20:27 +00:00
|
|
|
#include <sprout/iterator/const_iterator_cast.hpp>
|
2013-02-18 17:49:10 +00:00
|
|
|
#include <sprout/iterator/detail/iterator_to_pointer.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/value_holder/value_holder.hpp>
|
2012-10-05 15:58:56 +00:00
|
|
|
#include <sprout/utility/swap.hpp>
|
2013-11-22 12:11:08 +00:00
|
|
|
#include <sprout/type_traits/integral_constant.hpp>
|
2014-01-05 13:20:27 +00:00
|
|
|
#include <sprout/type_traits/identity.hpp>
|
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2014-07-12 16:41:33 +00:00
|
|
|
#include <sprout/functional/subscript.hpp>
|
2011-10-08 02:30:39 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// index_iterator
|
|
|
|
//
|
2014-07-12 16:41:33 +00:00
|
|
|
template<typename Container, bool ConvertibleToPointer = false, typename Subscript = sprout::subscript<> >
|
2011-10-08 02:30:39 +00:00
|
|
|
class index_iterator
|
|
|
|
: public std::iterator<
|
|
|
|
std::random_access_iterator_tag,
|
2012-12-21 13:35:48 +00:00
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::value_type,
|
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::difference_type,
|
2011-12-22 14:28:14 +00:00
|
|
|
typename std::conditional<
|
2011-10-08 02:30:39 +00:00
|
|
|
std::is_const<typename std::remove_reference<Container>::type>::value,
|
2012-12-21 13:35:48 +00:00
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::const_pointer,
|
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::pointer
|
2011-10-08 02:30:39 +00:00
|
|
|
>::type,
|
2011-12-22 14:28:14 +00:00
|
|
|
typename std::conditional<
|
2011-10-08 02:30:39 +00:00
|
|
|
std::is_const<typename std::remove_reference<Container>::type>::value,
|
2012-12-21 13:35:48 +00:00
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::const_reference,
|
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::reference
|
2011-10-08 02:30:39 +00:00
|
|
|
>::type
|
|
|
|
>
|
2013-02-18 17:49:10 +00:00
|
|
|
, public sprout::detail::iterator_to_pointer_base<
|
|
|
|
sprout::index_iterator<Container, ConvertibleToPointer>,
|
|
|
|
typename std::conditional<
|
|
|
|
std::is_const<typename std::remove_reference<Container>::type>::value,
|
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::const_pointer,
|
|
|
|
typename sprout::container_traits<typename std::remove_reference<Container>::type>::pointer
|
|
|
|
>::type,
|
|
|
|
ConvertibleToPointer
|
|
|
|
>
|
2011-10-08 02:30:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Container container_type;
|
2012-12-21 13:35:48 +00:00
|
|
|
typedef typename sprout::container_traits<typename std::remove_reference<container_type>::type> traits_type;
|
2011-12-22 14:28:14 +00:00
|
|
|
typedef typename std::conditional<
|
2011-10-08 02:30:39 +00:00
|
|
|
std::is_reference<container_type>::value,
|
2012-12-21 13:35:48 +00:00
|
|
|
typename std::remove_reference<container_type>::type const&,
|
|
|
|
typename std::remove_reference<container_type>::type const
|
2011-10-08 02:30:39 +00:00
|
|
|
>::type const_container_type;
|
|
|
|
private:
|
2014-07-12 16:41:33 +00:00
|
|
|
typedef Subscript subscript_type;
|
2011-10-08 02:30:39 +00:00
|
|
|
typedef std::iterator<
|
|
|
|
std::random_access_iterator_tag,
|
|
|
|
typename traits_type::value_type,
|
|
|
|
typename traits_type::difference_type,
|
2011-12-22 14:28:14 +00:00
|
|
|
typename std::conditional<
|
2011-10-08 02:30:39 +00:00
|
|
|
std::is_const<typename std::remove_reference<container_type>::type>::value,
|
|
|
|
typename traits_type::const_pointer,
|
|
|
|
typename traits_type::pointer
|
|
|
|
>::type,
|
2011-12-22 14:28:14 +00:00
|
|
|
typename std::conditional<
|
2011-10-08 02:30:39 +00:00
|
|
|
std::is_const<typename std::remove_reference<container_type>::type>::value,
|
|
|
|
typename traits_type::const_reference,
|
|
|
|
typename traits_type::reference
|
|
|
|
>::type
|
|
|
|
> base_type;
|
2012-12-21 13:35:48 +00:00
|
|
|
typedef sprout::value_holder<container_type> holder_type;
|
2011-10-08 02:30:39 +00:00
|
|
|
public:
|
|
|
|
typedef typename base_type::iterator_category iterator_category;
|
|
|
|
typedef typename base_type::value_type value_type;
|
|
|
|
typedef typename base_type::difference_type difference_type;
|
|
|
|
typedef typename base_type::pointer pointer;
|
|
|
|
typedef typename base_type::reference reference;
|
|
|
|
typedef typename traits_type::size_type size_type;
|
|
|
|
private:
|
2012-12-21 13:35:48 +00:00
|
|
|
holder_type holder_;
|
2013-02-23 06:21:27 +00:00
|
|
|
difference_type index_;
|
2011-10-08 02:30:39 +00:00
|
|
|
private:
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR index_iterator(holder_type const& r, difference_type index)
|
|
|
|
: holder_(r), index_(index)
|
2011-10-08 02:30:39 +00:00
|
|
|
{}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR index_iterator()
|
2013-02-23 06:21:27 +00:00
|
|
|
: holder_(), index_()
|
2011-10-08 02:30:39 +00:00
|
|
|
{}
|
|
|
|
index_iterator(index_iterator const&) = default;
|
2012-12-21 13:35:48 +00:00
|
|
|
explicit SPROUT_CONSTEXPR index_iterator(typename holder_type::param_type p)
|
2013-02-23 06:21:27 +00:00
|
|
|
: holder_(p), index_(0)
|
2012-12-21 13:35:48 +00:00
|
|
|
{}
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR index_iterator(typename holder_type::param_type p, difference_type index)
|
|
|
|
: holder_(p), index_(index)
|
2011-10-08 02:30:39 +00:00
|
|
|
{}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CONSTEXPR operator index_iterator<const_container_type>() const {
|
2011-10-08 02:30:39 +00:00
|
|
|
return index_iterator<const_container_type>(holder_.get(), index_);
|
|
|
|
}
|
2012-12-21 13:35:48 +00:00
|
|
|
SPROUT_CONSTEXPR typename holder_type::mutable_or_const_reference base() const {
|
|
|
|
return holder_.get();
|
|
|
|
}
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR difference_type index() const {
|
2012-12-21 13:35:48 +00:00
|
|
|
return index_;
|
|
|
|
}
|
2011-10-08 02:30:39 +00:00
|
|
|
SPROUT_CONSTEXPR index_iterator next() const {
|
|
|
|
return index_iterator(holder_, index_ + 1);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR index_iterator prev() const {
|
|
|
|
return index_iterator(holder_, index_ - 1);
|
|
|
|
}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void swap(index_iterator& other)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(sprout::swap(holder_, other.holder_))
|
2012-10-05 15:58:56 +00:00
|
|
|
{
|
|
|
|
sprout::swap(holder_, other.holder_);
|
|
|
|
sprout::swap(index_, other.index_);
|
2011-10-08 02:30:39 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator*() const {
|
2014-07-12 16:41:33 +00:00
|
|
|
return subscript_type()(holder_.get(), index_);
|
2011-10-08 02:30:39 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR pointer operator->() const {
|
2014-07-12 16:41:33 +00:00
|
|
|
return &subscript_type()(holder_.get(), index_);
|
2011-10-08 02:30:39 +00:00
|
|
|
}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR index_iterator& operator++() {
|
2011-10-08 02:30:39 +00:00
|
|
|
index_iterator temp(next());
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR index_iterator operator++(int) {
|
2011-10-08 02:30:39 +00:00
|
|
|
index_iterator result(*this);
|
|
|
|
++*this;
|
|
|
|
return result;
|
|
|
|
}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR index_iterator& operator--() {
|
2011-10-08 02:30:39 +00:00
|
|
|
index_iterator temp(prev());
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR index_iterator operator--(int) {
|
2011-10-08 02:30:39 +00:00
|
|
|
index_iterator result(*this);
|
|
|
|
--*this;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR index_iterator operator+(difference_type n) const {
|
|
|
|
return index_iterator(holder_, index_ + n);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR index_iterator operator-(difference_type n) const {
|
|
|
|
return index_iterator(holder_, index_ - n);
|
|
|
|
}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR index_iterator& operator+=(difference_type n) {
|
2011-10-08 02:30:39 +00:00
|
|
|
index_iterator temp(holder_, index_ + n);
|
2012-03-23 14:31:47 +00:00
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
2011-10-08 02:30:39 +00:00
|
|
|
}
|
2013-10-08 16:03:36 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR index_iterator& operator-=(difference_type n) {
|
2011-10-08 02:30:39 +00:00
|
|
|
index_iterator temp(holder_, index_ - n);
|
2012-03-23 14:31:47 +00:00
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
2011-10-08 02:30:39 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
|
2014-07-12 16:41:33 +00:00
|
|
|
return subscript_type()(holder_.get(), index_ + n);
|
2011-10-08 02:30:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-23 06:21:27 +00:00
|
|
|
template<typename Container1, typename Container2, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_same<typename std::decay<Container1>::type, typename std::decay<Container2>::type>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
operator==(sprout::index_iterator<Container1, C> const& lhs, sprout::index_iterator<Container2, C> const& rhs) {
|
|
|
|
return lhs.index() == rhs.index();
|
|
|
|
}
|
|
|
|
template<typename Container1, typename Container2, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_same<typename std::decay<Container1>::type, typename std::decay<Container2>::type>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
operator!=(sprout::index_iterator<Container1, C> const& lhs, sprout::index_iterator<Container2, C> const& rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
template<typename Container1, typename Container2, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_same<typename std::decay<Container1>::type, typename std::decay<Container2>::type>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
operator<(sprout::index_iterator<Container1, C> const& lhs, sprout::index_iterator<Container2, C> const& rhs) {
|
|
|
|
return lhs.index() < rhs.index();
|
|
|
|
}
|
|
|
|
template<typename Container1, typename Container2, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_same<typename std::decay<Container1>::type, typename std::decay<Container2>::type>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
operator>(sprout::index_iterator<Container1, C> const& lhs, sprout::index_iterator<Container2, C> const& rhs) {
|
|
|
|
return rhs < lhs;
|
|
|
|
}
|
|
|
|
template<typename Container1, typename Container2, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_same<typename std::decay<Container1>::type, typename std::decay<Container2>::type>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
operator<=(sprout::index_iterator<Container1, C> const& lhs, sprout::index_iterator<Container2, C> const& rhs) {
|
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
|
|
|
template<typename Container1, typename Container2, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_same<typename std::decay<Container1>::type, typename std::decay<Container2>::type>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
operator>=(sprout::index_iterator<Container1, C> const& lhs, sprout::index_iterator<Container2, C> const& rhs) {
|
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
|
|
|
template<typename Container1, typename Container2, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_same<typename std::decay<Container1>::type, typename std::decay<Container2>::type>::value,
|
|
|
|
decltype(
|
|
|
|
std::declval<typename std::iterator_traits<sprout::index_iterator<Container1, C> >::difference_type>()
|
|
|
|
- std::declval<typename std::iterator_traits<sprout::index_iterator<Container2, C> >::difference_type>()
|
|
|
|
)
|
|
|
|
>::type
|
|
|
|
operator-(sprout::index_iterator<Container1, C> const& lhs, sprout::index_iterator<Container2, C> const& rhs) {
|
|
|
|
return lhs.index() - rhs.index();
|
|
|
|
}
|
|
|
|
template<typename Container, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::index_iterator<Container, C>
|
|
|
|
operator+(typename sprout::index_iterator<Container, C>::difference_type n, sprout::index_iterator<Container, C> const& it) {
|
|
|
|
return it + n;
|
|
|
|
}
|
|
|
|
|
2011-10-08 02:30:39 +00:00
|
|
|
//
|
|
|
|
// swap
|
|
|
|
//
|
2013-02-18 17:49:10 +00:00
|
|
|
template<typename Container, bool C>
|
2013-10-08 16:03:36 +00:00
|
|
|
inline SPROUT_CXX14_CONSTEXPR void
|
2013-02-18 17:49:10 +00:00
|
|
|
swap(sprout::index_iterator<Container, C>& lhs, sprout::index_iterator<Container, C>& rhs)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
|
2012-10-05 15:58:56 +00:00
|
|
|
{
|
2011-10-08 02:30:39 +00:00
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:23:49 +00:00
|
|
|
//
|
|
|
|
// is_index_iterator
|
|
|
|
//
|
|
|
|
template<typename T>
|
|
|
|
struct is_index_iterator
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::false_type
|
2012-04-13 12:23:49 +00:00
|
|
|
{};
|
|
|
|
template<typename T>
|
|
|
|
struct is_index_iterator<T const>
|
|
|
|
: public sprout::is_index_iterator<T>
|
|
|
|
{};
|
|
|
|
template<typename T>
|
|
|
|
struct is_index_iterator<T const volatile>
|
|
|
|
: public sprout::is_index_iterator<T>
|
|
|
|
{};
|
2013-02-18 17:49:10 +00:00
|
|
|
template<typename Container, bool C>
|
|
|
|
struct is_index_iterator<sprout::index_iterator<Container, C> >
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::true_type
|
2012-04-13 12:23:49 +00:00
|
|
|
{};
|
|
|
|
|
2012-09-26 07:31:11 +00:00
|
|
|
//
|
|
|
|
// iterator_next
|
2011-10-08 02:30:39 +00:00
|
|
|
//
|
2013-02-18 17:49:10 +00:00
|
|
|
template<typename Container, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::index_iterator<Container, C>
|
|
|
|
iterator_next(sprout::index_iterator<Container, C> const& it) {
|
2011-10-08 02:30:39 +00:00
|
|
|
return it.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2012-09-26 07:31:11 +00:00
|
|
|
// iterator_prev
|
2011-10-08 02:30:39 +00:00
|
|
|
//
|
2013-02-18 17:49:10 +00:00
|
|
|
template<typename Container, bool C>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::index_iterator<Container, C>
|
|
|
|
iterator_prev(sprout::index_iterator<Container, C> const& it) {
|
2011-10-08 02:30:39 +00:00
|
|
|
return it.prev();
|
|
|
|
}
|
2014-01-05 13:20:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// is_const_iterator_cast_convertible
|
|
|
|
//
|
|
|
|
template<typename FromContainer, typename ToContainer, bool C>
|
|
|
|
struct is_const_iterator_cast_convertible<sprout::index_iterator<FromContainer, C>, sprout::index_iterator<ToContainer, C> >
|
|
|
|
: public std::is_same<typename std::decay<FromContainer>::type, typename std::decay<ToContainer>::type>
|
|
|
|
{};
|
|
|
|
//
|
|
|
|
// const_iterator_conversion
|
|
|
|
//
|
|
|
|
template<
|
|
|
|
typename T,
|
|
|
|
typename Container, bool C,
|
|
|
|
typename sprout::enabler_if<sprout::is_const_iterator_cast_convertible<sprout::index_iterator<Container, C>, T>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
const_iterator_conversion(sprout::index_iterator<Container, C> const& it) {
|
|
|
|
return T(const_cast<typename T::container_type>(it.base()), it.index());
|
|
|
|
}
|
2012-02-25 02:51:23 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
2011-10-08 02:30:39 +00:00
|
|
|
#endif // #ifndef SPROUT_ITERATOR_INDEX_ITERATOR_HPP
|