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

261 lines
8.4 KiB
C++
Raw Normal View History

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>
#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>
2011-10-08 02:30:39 +00:00
#include <sprout/utility/value_holder.hpp>
2012-10-05 15:58:56 +00:00
#include <sprout/utility/swap.hpp>
2011-10-08 02:30:39 +00:00
namespace sprout {
//
// index_iterator
//
template<typename Container>
class index_iterator
: public std::iterator<
std::random_access_iterator_tag,
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,
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,
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
>
{
public:
typedef Container container_type;
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,
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:
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;
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:
holder_type holder_;
2011-10-08 02:30:39 +00:00
size_type index_;
private:
SPROUT_CONSTEXPR index_iterator(holder_type const& r, size_type index)
2011-10-08 02:30:39 +00:00
: holder_(r)
, index_(index)
{}
public:
SPROUT_CONSTEXPR index_iterator()
: holder_()
, index_()
{}
index_iterator(index_iterator const&) = default;
explicit SPROUT_CONSTEXPR index_iterator(typename holder_type::param_type p)
: holder_(p)
, index_(0)
{}
SPROUT_CONSTEXPR index_iterator(typename holder_type::param_type p, size_type index)
2011-10-08 02:30:39 +00:00
: holder_(p)
, index_(index)
{}
operator index_iterator<const_container_type>() const {
return index_iterator<const_container_type>(holder_.get(), index_);
}
SPROUT_CONSTEXPR typename holder_type::mutable_or_const_reference base() const {
return holder_.get();
}
SPROUT_CONSTEXPR size_type index() const {
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);
}
2012-10-05 15:58:56 +00:00
void swap(index_iterator& other)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(holder_, other.holder_)))
{
sprout::swap(holder_, other.holder_);
sprout::swap(index_, other.index_);
2011-10-08 02:30:39 +00:00
}
friend SPROUT_CONSTEXPR bool operator==(index_iterator const& lhs, index_iterator const& rhs) {
return lhs.index_ == rhs.index_;
}
friend SPROUT_CONSTEXPR bool operator!=(index_iterator const& lhs, index_iterator const& rhs) {
return !(lhs == rhs);
}
friend SPROUT_CONSTEXPR bool operator<(index_iterator const& lhs, index_iterator const& rhs) {
2011-10-08 02:30:39 +00:00
return lhs.index_ < rhs.index_;
}
friend SPROUT_CONSTEXPR bool operator>(index_iterator const& lhs, index_iterator const& rhs) {
2011-10-08 02:30:39 +00:00
return rhs < lhs;
}
friend SPROUT_CONSTEXPR bool operator<=(index_iterator const& lhs, index_iterator const& rhs) {
2011-10-08 02:30:39 +00:00
return !(rhs < lhs);
}
friend SPROUT_CONSTEXPR bool operator>=(index_iterator const& lhs, index_iterator const& rhs) {
2011-10-08 02:30:39 +00:00
return !(lhs < rhs);
}
SPROUT_CONSTEXPR reference operator*() const {
return holder_.get()[index_];
}
SPROUT_CONSTEXPR pointer operator->() const {
return &holder_.get()[index_];
}
index_iterator& operator++() {
index_iterator temp(next());
temp.swap(*this);
return *this;
}
index_iterator operator++(int) {
index_iterator result(*this);
++*this;
return result;
}
index_iterator& operator--() {
index_iterator temp(prev());
temp.swap(*this);
return *this;
}
index_iterator operator--(int) {
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);
}
index_iterator& operator+=(difference_type n) {
index_iterator temp(holder_, index_ + n);
temp.swap(*this);
return *this;
2011-10-08 02:30:39 +00:00
}
index_iterator& operator-=(difference_type n) {
index_iterator temp(holder_, index_ - n);
temp.swap(*this);
return *this;
2011-10-08 02:30:39 +00:00
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return holder_.get()[index_ + n];
}
friend SPROUT_CONSTEXPR difference_type operator-(index_iterator const& lhs, index_iterator const& rhs) {
return static_cast<difference_type>(lhs.index_) - static_cast<difference_type>(rhs.index_);
2011-10-08 02:30:39 +00:00
}
friend SPROUT_CONSTEXPR index_iterator operator+(difference_type n, index_iterator const& it) {
return it + n;
}
};
//
// swap
//
template<typename Container>
2012-10-05 15:58:56 +00:00
inline void
swap(sprout::index_iterator<Container>& lhs, sprout::index_iterator<Container>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
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
: public std::false_type
{};
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>
{};
template<typename Container>
struct is_index_iterator<sprout::index_iterator<Container> >
: public std::true_type
{};
2011-10-08 02:30:39 +00:00
//
// iterator_distance
//
template<typename Container>
inline SPROUT_CONSTEXPR typename std::iterator_traits<sprout::index_iterator<Container> >::difference_type
2012-10-05 15:58:56 +00:00
iterator_distance(sprout::index_iterator<Container> first, sprout::index_iterator<Container> last) {
return last - first;
}
//
// iterator_next
2011-10-08 02:30:39 +00:00
//
template<typename Container>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::index_iterator<Container>
iterator_next(sprout::index_iterator<Container> const& it) {
2011-10-08 02:30:39 +00:00
return it.next();
}
template<typename Container>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::index_iterator<Container>
iterator_next(
2011-10-08 02:30:39 +00:00
sprout::index_iterator<Container> const& it,
typename sprout::index_iterator<Container>::difference_type n
)
{
return it + n;
}
//
// iterator_prev
2011-10-08 02:30:39 +00:00
//
template<typename Container>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::index_iterator<Container>
iterator_prev(sprout::index_iterator<Container> const& it) {
2011-10-08 02:30:39 +00:00
return it.prev();
}
template<typename Container>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::index_iterator<Container>
iterator_prev(
2011-10-08 02:30:39 +00:00
sprout::index_iterator<Container> const& it,
typename sprout::index_iterator<Container>::difference_type n
)
{
return it - n;
}
} // namespace sprout
2011-10-08 02:30:39 +00:00
#endif // #ifndef SPROUT_ITERATOR_INDEX_ITERATOR_HPP