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

215 lines
7 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2017 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)
=============================================================================*/
2012-05-22 12:16:16 +00:00
#ifndef SPROUT_ITERATOR_COUNTING_ITERATOR_HPP
#define SPROUT_ITERATOR_COUNTING_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
#include <sprout/iterator/iterator.hpp>
2012-05-22 12:16:16 +00:00
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
#include <sprout/iterator/distance.hpp>
2012-10-05 15:58:56 +00:00
#include <sprout/utility/swap.hpp>
2012-11-09 13:33:11 +00:00
#include <sprout/type_traits/enabler_if.hpp>
2012-05-22 12:16:16 +00:00
namespace sprout {
//
// counting_iterator
//
template<typename Incrementable>
class counting_iterator
: public sprout::iterator<
2012-05-22 12:16:16 +00:00
std::random_access_iterator_tag,
Incrementable,
std::ptrdiff_t,
Incrementable*,
Incrementable
>
{
public:
typedef std::random_access_iterator_tag iterator_category;
typedef Incrementable value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type* pointer;
typedef value_type reference;
private:
2012-11-09 13:33:11 +00:00
template<typename T, typename sprout::enabler_if<std::is_integral<T>::value>::type = sprout::enabler>
static SPROUT_CONSTEXPR T
default_value() {
2013-08-06 15:15:09 +00:00
return sprout::numeric_limits<value_type>::max();
2012-11-09 13:33:11 +00:00
}
template<typename T, typename sprout::enabler_if<!std::is_integral<T>::value>::type = sprout::enabler>
static SPROUT_CONSTEXPR T
default_value() {
return T();
}
2012-05-22 12:16:16 +00:00
private:
2012-11-09 13:33:11 +00:00
value_type current_;
2012-05-22 12:16:16 +00:00
public:
2012-05-23 10:33:06 +00:00
SPROUT_CONSTEXPR counting_iterator()
2012-11-09 13:33:11 +00:00
: current_(default_value<value_type>())
2012-05-23 10:33:06 +00:00
{}
2012-05-22 12:16:16 +00:00
counting_iterator(counting_iterator const&) = default;
explicit SPROUT_CONSTEXPR counting_iterator(value_type const& v)
: current_(v)
{}
template<typename U>
SPROUT_CONSTEXPR counting_iterator(counting_iterator<U> const& it)
: current_(it.current_)
{}
template<typename U>
SPROUT_CXX14_CONSTEXPR counting_iterator& operator=(counting_iterator<U> const& it) {
2012-05-22 12:16:16 +00:00
counting_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR reference operator*() const {
return current_;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &current_;
}
SPROUT_CXX14_CONSTEXPR counting_iterator& operator++() {
2012-05-22 12:16:16 +00:00
++current_;
return *this;
}
SPROUT_CXX14_CONSTEXPR counting_iterator operator++(int) {
2012-05-22 12:16:16 +00:00
counting_iterator result(*this);
++current_;
return result;
}
SPROUT_CXX14_CONSTEXPR counting_iterator& operator--() {
2012-05-22 12:16:16 +00:00
--current_;
return *this;
}
SPROUT_CXX14_CONSTEXPR counting_iterator operator--(int) {
2012-05-22 12:16:16 +00:00
counting_iterator temp(*this);
--current_;
return temp;
}
SPROUT_CONSTEXPR counting_iterator operator+(difference_type n) const {
return counting_iterator(current_ + n);
}
SPROUT_CONSTEXPR counting_iterator operator-(difference_type n) const {
return counting_iterator(current_ - n);
}
SPROUT_CXX14_CONSTEXPR counting_iterator& operator+=(difference_type n) {
2012-05-22 12:16:16 +00:00
counting_iterator temp(current_ + n);
temp.swap(*this);
return *this;
}
SPROUT_CXX14_CONSTEXPR counting_iterator& operator-=(difference_type n) {
2012-05-22 12:16:16 +00:00
counting_iterator temp(current_ - n);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return current_ + n;
}
SPROUT_CONSTEXPR counting_iterator next() const {
return counting_iterator(current_ + 1);
}
SPROUT_CONSTEXPR counting_iterator prev() const {
return counting_iterator(current_ - 1);
}
SPROUT_CXX14_CONSTEXPR void swap(counting_iterator& other)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout::swap(current_, other.current_))
2012-10-05 15:58:56 +00:00
{
sprout::swap(current_, other.current_);
2012-05-22 12:16:16 +00:00
}
};
template<typename Incrementable1, typename Incrementable2>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator==(sprout::counting_iterator<Incrementable1> const& lhs, sprout::counting_iterator<Incrementable2> const& rhs) {
2012-05-22 12:16:16 +00:00
return *lhs == *rhs;
}
template<typename Incrementable1, typename Incrementable2>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator!=(sprout::counting_iterator<Incrementable1> const& lhs, sprout::counting_iterator<Incrementable2> const& rhs) {
2012-05-22 12:16:16 +00:00
return !(lhs == rhs);
}
template<typename Incrementable1, typename Incrementable2>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator<(sprout::counting_iterator<Incrementable1> const& lhs, sprout::counting_iterator<Incrementable2> const& rhs) {
2012-05-22 12:16:16 +00:00
return *lhs < *rhs;
}
template<typename Incrementable1, typename Incrementable2>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator>(sprout::counting_iterator<Incrementable1> const& lhs, sprout::counting_iterator<Incrementable2> const& rhs) {
2012-05-22 12:16:16 +00:00
return rhs < lhs;
}
template<typename Incrementable1, typename Incrementable2>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator<=(sprout::counting_iterator<Incrementable1> const& lhs, sprout::counting_iterator<Incrementable2> const& rhs) {
2012-05-22 12:16:16 +00:00
return !(rhs < lhs);
}
template<typename Incrementable1, typename Incrementable2>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator>=(sprout::counting_iterator<Incrementable1> const& lhs, sprout::counting_iterator<Incrementable2> const& rhs) {
2012-05-22 12:16:16 +00:00
return !(lhs < rhs);
}
template<typename Incrementable1, typename Incrementable2>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR typename sprout::counting_iterator<Incrementable1>::difference_type
operator-(sprout::counting_iterator<Incrementable1> const& lhs, sprout::counting_iterator<Incrementable2> const& rhs) {
2012-05-22 12:16:16 +00:00
return static_cast<typename sprout::counting_iterator<Incrementable1>::difference_type>(*lhs - *rhs);
}
template<typename Incrementable>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::counting_iterator<Incrementable>
operator+(
2012-05-22 12:16:16 +00:00
typename sprout::counting_iterator<Incrementable>::difference_type n,
sprout::counting_iterator<Incrementable> const& it
)
{
return it + n;
}
//
// make_counting_iterator
//
template<typename Incrementable>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::counting_iterator<Incrementable>
2012-05-22 12:16:16 +00:00
make_counting_iterator(Incrementable const& v) {
return sprout::counting_iterator<Incrementable>(v);
}
//
// swap
//
template<typename Incrementable>
inline SPROUT_CXX14_CONSTEXPR void
2012-10-05 15:58:56 +00:00
swap(sprout::counting_iterator<Incrementable>& lhs, sprout::counting_iterator<Incrementable>& rhs)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
2012-05-22 12:16:16 +00:00
{
lhs.swap(rhs);
}
//
// iterator_next
2012-05-22 12:16:16 +00:00
//
template<typename Incrementable>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::counting_iterator<Incrementable>
iterator_next(sprout::counting_iterator<Incrementable> const& it) {
2012-05-22 12:16:16 +00:00
return it.next();
}
//
// iterator_prev
2012-05-22 12:16:16 +00:00
//
template<typename Incrementable>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::counting_iterator<Incrementable>
iterator_prev(sprout::counting_iterator<Incrementable> const& it) {
2012-05-22 12:16:16 +00:00
return it.prev();
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_COUNTING_ITERATOR_HPP