Skip to content

Instantly share code, notes, and snippets.

@Fuyutsubaki
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fuyutsubaki/a17eec9fb0735baaff92 to your computer and use it in GitHub Desktop.
Save Fuyutsubaki/a17eec9fb0735baaff92 to your computer and use it in GitHub Desktop.
constexpr recurrence_iterator
#include <iterator>
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
#include<sprout/range/range_container.hpp>
template<class Value, class Func>
class recurrence_iterator
: public std::iterator<
std::forward_iterator_tag,
Value,
std::ptrdiff_t,
Value*,
Value
>
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef Value value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type* pointer;
typedef value_type reference;
private:
Value value_;
Func func_;
bool end_flag_;
public:
recurrence_iterator(recurrence_iterator const&) = default;
explicit constexpr recurrence_iterator(const Value&val, Func f,bool end_flag)
:value_(val)
, func_(f)
, end_flag_(end_flag)
{}
SPROUT_CONSTEXPR bool is_end()const
{
return end_flag_;
}
SPROUT_CONSTEXPR reference operator*() const
{
return value_;
}
SPROUT_CONSTEXPR pointer operator->() const
{
return & operator*()();
}
SPROUT_CONSTEXPR recurrence_iterator next() const
{
return recurrence_iterator(func_(value_), func_,end_flag_);
}
};
template<class Func, typename Value1, typename Value2>
inline SPROUT_CONSTEXPR bool
operator==(recurrence_iterator<Value1,Func> const& lhs, recurrence_iterator<Value2,Func> const& rhs) {
return lhs.is_end()==rhs.is_end();
}
template<class Func, typename Value1, typename Value2>
inline SPROUT_CONSTEXPR bool
operator!=(recurrence_iterator<Value1, Func> const& lhs, recurrence_iterator<Value2, Func> const& rhs) {
return !(lhs == rhs);
}
//
// iterator_next
//
template<class Value,class Func>
inline SPROUT_CONSTEXPR recurrence_iterator<Value, Func>
iterator_next(recurrence_iterator<Value, Func> const& it)
{
return it.next();
}
//
// iterator_prev
//
template<class Value, class Func>
inline SPROUT_CONSTEXPR recurrence_iterator<Value, Func>
iterator_prev(recurrence_iterator<Value, Func> const& it)
{
return it.prev();
}
template<class Value,class Func>
SPROUT_CONSTEXPR auto make_recurrence_list(Value const&val,Func func)
->sprout::range::range_container<recurrence_iterator<Value,Func>>
{
using Iter=recurrence_iterator<Value,Func>;
return {Iter{val,func,false},Iter{val,func,true}};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment