1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-12-03 21:15:42 +00:00
Sprout/sprout/iterator/generator_iterator.hpp

162 lines
5.5 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)
=============================================================================*/
#ifndef SPROUT_ITERATOR_GENERATOR_ITERATOR_HPP
#define SPROUT_ITERATOR_GENERATOR_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
#include <sprout/generator/functions.hpp>
2012-10-05 15:58:56 +00:00
#include <sprout/utility/swap.hpp>
2015-12-09 10:54:35 +00:00
#include <sprout/type_traits/identity.hpp>
#include <sprout/iterator/iterator.hpp>
namespace sprout {
//
// generator_iterator
//
template<typename Generator>
class generator_iterator
: public sprout::iterator<
std::forward_iterator_tag,
typename std::remove_reference<decltype(sprout::generators::generated_value(std::declval<Generator const>()))>::type,
std::ptrdiff_t,
typename std::remove_reference<decltype(sprout::generators::generated_value(std::declval<Generator const>()))>::type*,
decltype(sprout::generators::generated_value(std::declval<Generator const>()))
>
{
public:
typedef Generator generator_type;
typedef std::forward_iterator_tag iterator_category;
2015-12-09 10:54:35 +00:00
typedef typename sprout::identity<decltype(sprout::generators::generated_value(std::declval<Generator const>()))>::type reference;
typedef typename std::remove_reference<reference>::type value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type* pointer;
private:
generator_type gen_;
2013-02-23 06:21:27 +00:00
difference_type index_;
public:
SPROUT_CONSTEXPR generator_iterator()
2013-02-23 06:21:27 +00:00
: gen_(), index_()
{}
explicit SPROUT_CONSTEXPR generator_iterator(
generator_type const& gen,
2013-08-06 15:15:09 +00:00
difference_type index = sprout::numeric_limits<difference_type>::max()
)
2013-02-23 06:21:27 +00:00
: gen_(gen), index_(index)
{}
SPROUT_CXX14_CONSTEXPR generator_type& generator() {
return gen_;
}
SPROUT_CONSTEXPR generator_type const& generator() const {
return gen_;
}
2013-02-23 06:21:27 +00:00
SPROUT_CONSTEXPR difference_type index() const {
return index_;
}
SPROUT_CONSTEXPR generator_iterator operator()() const {
2013-02-23 06:21:27 +00:00
return generator_iterator(sprout::generators::next_generator(gen_)(), index_ - 1);
}
SPROUT_CONSTEXPR generator_iterator next() const {
return (*this)();
}
SPROUT_CONSTEXPR reference generated_value() const {
return sprout::generators::generated_value(gen_);
}
SPROUT_CONSTEXPR generator_iterator next_generator() const {
return (*this)();
}
SPROUT_CONSTEXPR operator reference() const {
return generated_value();
}
SPROUT_CXX14_CONSTEXPR void swap(generator_iterator& other)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout::swap(gen_, other.gen_))
2012-10-05 15:58:56 +00:00
{
sprout::swap(gen_, other.gen_);
2013-02-23 06:21:27 +00:00
sprout::swap(index_, other.index_);
}
SPROUT_CONSTEXPR reference operator*() const {
2013-02-23 06:21:27 +00:00
return sprout::generators::generated_value(gen_);
}
SPROUT_CONSTEXPR pointer operator->() const {
return &*(*this);
}
SPROUT_CXX14_CONSTEXPR generator_iterator& operator++() {
generator_iterator temp((*this)());
temp.swap(*this);
return *this;
}
SPROUT_CXX14_CONSTEXPR generator_iterator operator++(int) {
generator_iterator result(*this);
++*this;
return result;
}
};
2013-02-23 06:21:27 +00:00
template<typename Generator>
inline SPROUT_CONSTEXPR bool
operator==(sprout::generator_iterator<Generator> const& lhs, sprout::generator_iterator<Generator> const& rhs) {
return lhs.index() == rhs.index();
}
template<typename Generator>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::generator_iterator<Generator> const& lhs, sprout::generator_iterator<Generator> const& rhs) {
return !(lhs == rhs);
}
template<typename Generator>
inline SPROUT_CONSTEXPR bool
operator<(sprout::generator_iterator<Generator> const& lhs, sprout::generator_iterator<Generator> const& rhs) {
return rhs.index() < lhs.index();
}
template<typename Generator>
inline SPROUT_CONSTEXPR bool
operator>(sprout::generator_iterator<Generator> const& lhs, sprout::generator_iterator<Generator> const& rhs) {
return rhs < lhs;
}
template<typename Generator>
inline SPROUT_CONSTEXPR bool
operator<=(sprout::generator_iterator<Generator> const& lhs, sprout::generator_iterator<Generator> const& rhs) {
return !(rhs < lhs);
}
template<typename Generator>
inline SPROUT_CONSTEXPR bool
operator>=(sprout::generator_iterator<Generator> const& lhs, sprout::generator_iterator<Generator> const& rhs) {
return !(lhs < rhs);
}
template<typename Generator>
inline SPROUT_CONSTEXPR typename sprout::generator_iterator<Generator>::difference_type
operator-(sprout::generator_iterator<Generator> const& lhs, sprout::generator_iterator<Generator> const& rhs) {
return rhs.index() - lhs.index();
}
//
// swap
//
template<typename Generator>
inline SPROUT_CXX14_CONSTEXPR void
2012-10-05 15:58:56 +00:00
swap(sprout::generator_iterator<Generator>& lhs, sprout::generator_iterator<Generator>& rhs)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
{
lhs.swap(rhs);
}
//
// iterator_next
//
template<typename Generator>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::generator_iterator<Generator>
iterator_next(sprout::generator_iterator<Generator> const& it) {
return it.next();
}
} // namespace sprout
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_ITERATOR_GENERATOR_ITERATOR_HPP