add adaptor steps

This commit is contained in:
bolero-MURAKAMI 2012-11-24 22:46:23 +09:00
parent 05f75bef27
commit 597d05563b
9 changed files with 455 additions and 15 deletions

View file

@ -5,6 +5,7 @@
#include <sprout/iterator/reverse_iterator.hpp>
#include <sprout/iterator/transform_iterator.hpp>
#include <sprout/iterator/filter_iterator.hpp>
#include <sprout/iterator/step_iterator.hpp>
#include <sprout/iterator/indexed_iterator.hpp>
#include <sprout/iterator/valued_iterator.hpp>
#include <sprout/iterator/joint_iterator.hpp>

View file

@ -0,0 +1,283 @@
#ifndef SPROUT_ITERATOR_STEP_ITERATOR_HPP
#define SPROUT_ITERATOR_STEP_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
#include <sprout/iterator/distance.hpp>
#include <sprout/utility/swap.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
namespace sprout {
//
// step_iterator
//
template<typename Iterator>
class step_iterator
: public std::iterator<
typename std::iterator_traits<Iterator>::iterator_category,
typename std::iterator_traits<Iterator>::value_type,
typename std::iterator_traits<Iterator>::difference_type,
typename std::iterator_traits<Iterator>::pointer,
typename std::iterator_traits<Iterator>::reference
>
{
public:
typedef Iterator iterator_type;
typedef typename std::iterator_traits<iterator_type>::iterator_category iterator_category;
typedef typename std::iterator_traits<iterator_type>::value_type value_type;
typedef typename std::iterator_traits<iterator_type>::difference_type difference_type;
typedef typename std::iterator_traits<iterator_type>::pointer pointer;
typedef typename std::iterator_traits<iterator_type>::reference reference;
private:
static SPROUT_CONSTEXPR iterator_type get_back(iterator_type it, difference_type wid, iterator_type last) {
return sprout::next(it, (NS_SSCRISK_CEL_OR_SPROUT::distance(it, last) - 1) / wid * wid);
}
static SPROUT_CONSTEXPR difference_type next_distance(step_iterator const& it, difference_type n) {
return n >= 0 ? NS_SSCRISK_CEL_OR_SPROUT::min(NS_SSCRISK_CEL_OR_SPROUT::distance(it.current, it.back), n * it.wid)
: it.is_last ? 0
: NS_SSCRISK_CEL_OR_SPROUT::max(-NS_SSCRISK_CEL_OR_SPROUT::distance(it.current, it.back), n * it.wid)
;
}
protected:
iterator_type current;
iterator_type last;
difference_type wid;
private:
iterator_type back;
bool is_last;
private:
SPROUT_CONSTEXPR step_iterator(step_iterator const& other, difference_type n)
: current(sprout::next(other.current, next_distance(other, n))), last(other.last), wid(other.wid)
, back(other.back), is_last(next_distance(other, n) == 0)
{}
public:
SPROUT_CONSTEXPR step_iterator()
: current(), last(), wid(1)
, back(), is_last()
{}
SPROUT_CONSTEXPR step_iterator(step_iterator const& other)
: current(other.current), last(other.last), wid(other.wid)
, back(other.back), is_last(other.is_last)
{}
explicit SPROUT_CONSTEXPR step_iterator(iterator_type it, difference_type wid = 1, iterator_type last = iterator_type())
: current(it), last(last), wid(wid)
, back(get_back(it, wid, last)), is_last(it == last)
{}
template<typename U>
SPROUT_CONSTEXPR step_iterator(step_iterator<U> const& it)
: current(it.base()), last(it.end()), wid(it.width())
, back(get_back(it.base(), it.width(), it.end())), is_last(it.is_end())
{}
template<typename U>
step_iterator& operator=(step_iterator<U> const& it) {
step_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR difference_type width() const {
return wid;
}
SPROUT_CONSTEXPR iterator_type end() const {
return last;
}
SPROUT_CONSTEXPR bool is_end() const {
return is_last;
}
SPROUT_CONSTEXPR reference operator*() const {
return *current;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &*current;
}
step_iterator& operator++() {
if (current == back) {
is_last = true;
}
current += next_distance(*this, 1);
return *this;
}
step_iterator operator++(int) {
step_iterator result(*this);
if (current == back) {
is_last = true;
}
current += next_distance(*this, 1);
return result;
}
step_iterator& operator--() {
current += next_distance(*this, -1);
if (current != back) {
is_last = false;
}
return *this;
}
step_iterator operator--(int) {
step_iterator temp(*this);
current += next_distance(*this, -1);
if (current != back) {
is_last = false;
}
return temp;
}
SPROUT_CONSTEXPR step_iterator operator+(difference_type n) const {
return step_iterator(*this, n);
}
SPROUT_CONSTEXPR step_iterator operator-(difference_type n) const {
return step_iterator(*this, -n);
}
step_iterator& operator+=(difference_type n) {
step_iterator temp(*this, n);
temp.swap(*this);
return *this;
}
step_iterator& operator-=(difference_type n) {
step_iterator temp(*this, -n);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return *(current + n * wid);
}
SPROUT_CONSTEXPR step_iterator next() const {
return step_iterator(*this, 1);
}
SPROUT_CONSTEXPR step_iterator prev() const {
return step_iterator(*this, -1);
}
void swap(step_iterator& other)
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(swap(current, other.current))
&& SPROUT_NOEXCEPT_EXPR(swap(last, other.last))
&& SPROUT_NOEXCEPT_EXPR(swap(wid, other.wid))
&& SPROUT_NOEXCEPT_EXPR(swap(back, other.back))
&& SPROUT_NOEXCEPT_EXPR(swap(wid, other.wid))
&& SPROUT_NOEXCEPT_EXPR(swap(is_last, other.is_last))
)
{
swap(current, other.current);
swap(last, other.last);
swap(wid, other.wid);
swap(back, other.back);
swap(is_last, other.is_last);
}
};
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator==(sprout::step_iterator<Iterator1> const& lhs, sprout::step_iterator<Iterator2> const& rhs) {
return lhs.base() == rhs.base() && lhs.is_end() == rhs.is_end();
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::step_iterator<Iterator1> const& lhs, sprout::step_iterator<Iterator2> const& rhs) {
return !(lhs == rhs);
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator<(sprout::step_iterator<Iterator1> const& lhs, sprout::step_iterator<Iterator2> const& rhs) {
return lhs.base() < rhs.base() || (lhs.base() == rhs.base() && !lhs.is_end() && rhs.is_end());
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator>(sprout::step_iterator<Iterator1> const& lhs, sprout::step_iterator<Iterator2> const& rhs) {
return rhs < lhs;
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator<=(sprout::step_iterator<Iterator1> const& lhs, sprout::step_iterator<Iterator2> const& rhs) {
return !(rhs < lhs);
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator>=(sprout::step_iterator<Iterator1> const& lhs, sprout::step_iterator<Iterator2> const& rhs) {
return !(lhs < rhs);
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR decltype(std::declval<Iterator1>() - std::declval<Iterator2>())
operator-(sprout::step_iterator<Iterator1> const& lhs, sprout::step_iterator<Iterator2> const& rhs) {
return (lhs.base() - rhs.base()) / lhs.width() + (lhs.is_end() ? 1 : 0) - (rhs.is_end() ? 1 : 0);
}
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::step_iterator<Iterator>
operator+(
typename sprout::step_iterator<Iterator>::difference_type n,
sprout::step_iterator<Iterator> const& it
)
{
return it + n;
}
//
// make_step_iterator
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::step_iterator<Iterator>
make_step_iterator(Iterator it, typename sprout::step_iterator<Iterator>::difference_type wid = 1) {
return sprout::step_iterator<Iterator>(it, wid);
}
//
// swap
//
template<typename Iterator>
inline void
swap(sprout::step_iterator<Iterator>& lhs, sprout::step_iterator<Iterator>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
//
// iterator_distance
//
template<typename Iterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<sprout::step_iterator<Iterator> >::difference_type
iterator_distance(sprout::step_iterator<Iterator> first, sprout::step_iterator<Iterator> last) {
return last - first;
}
//
// iterator_next
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::step_iterator<Iterator>
iterator_next(sprout::step_iterator<Iterator> const& it) {
return it.next();
}
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::step_iterator<Iterator>
iterator_next(
sprout::step_iterator<Iterator> const& it,
typename sprout::step_iterator<Iterator>::difference_type n
)
{
return it + n;
}
//
// iterator_prev
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::step_iterator<Iterator>
iterator_prev(sprout::step_iterator<Iterator> const& it) {
return it.prev();
}
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::step_iterator<Iterator>
iterator_prev(
sprout::step_iterator<Iterator> const& it,
typename sprout::step_iterator<Iterator>::difference_type n
)
{
return it - n;
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_STEP_ITERATOR_HPP

View file

@ -23,6 +23,7 @@
#include <sprout/range/adaptor/adapted_dropped_end.hpp>
#include <sprout/range/adaptor/adapted_window.hpp>
#include <sprout/range/adaptor/adapted_offset.hpp>
#include <sprout/range/adaptor/steps.hpp>
#include <sprout/range/adaptor/jointed.hpp>
#include <sprout/range/adaptor/deep_copied.hpp>
#include <sprout/range/adaptor/indexed.hpp>

View file

@ -58,9 +58,9 @@ namespace sprout {
};
//
// adapt_offset_forwarder
// adapted_offset_forwarder
//
class adapt_offset_forwarder {
class adapted_offset_forwarder {
public:
template<typename Adaptor, typename Difference1, typename Difference2>
SPROUT_CONSTEXPR sprout::adaptors::adapt_offset_holder<Adaptor, Difference1, Difference2>
@ -75,10 +75,10 @@ namespace sprout {
};
//
// adapt_offset
// adapted_offset
//
namespace {
SPROUT_STATIC_CONSTEXPR sprout::adaptors::adapt_offset_forwarder adapt_offset{};
SPROUT_STATIC_CONSTEXPR sprout::adaptors::adapted_offset_forwarder adapted_offset{};
} // anonymous-namespace
//

View file

@ -58,9 +58,9 @@ namespace sprout {
};
//
// adapt_window_forwarder
// adapted_window_forwarder
//
class adapt_window_forwarder {
class adapted_window_forwarder {
public:
template<typename Adaptor, typename Difference1, typename Difference2>
SPROUT_CONSTEXPR sprout::adaptors::adapt_window_holder<Adaptor, Difference1, Difference2>
@ -75,10 +75,10 @@ namespace sprout {
};
//
// adapt_window
// adapted_window
//
namespace {
SPROUT_STATIC_CONSTEXPR sprout::adaptors::adapt_window_forwarder adapt_window{};
SPROUT_STATIC_CONSTEXPR sprout::adaptors::adapted_window_forwarder adapted_window{};
} // anonymous-namespace
//

View file

@ -63,10 +63,10 @@ namespace sprout {
public:
indexed_holder() = default;
indexed_holder(indexed_holder const&) = default;
SPROUT_CONSTEXPR indexed_holder(index_type index)
explicit SPROUT_CONSTEXPR indexed_holder(index_type index)
: index_(index)
{}
SPROUT_CONSTEXPR index_type index() const {
SPROUT_CONSTEXPR index_type const& index() const {
return index_;
}
};

View file

@ -0,0 +1,154 @@
#ifndef SPROUT_RANGE_ADAPTOR_STEPS_HPP
#define SPROUT_RANGE_ADAPTOR_STEPS_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/pit.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/iterator/step_iterator.hpp>
#include <sprout/range/range_container.hpp>
#include <sprout/range/algorithm/copy.hpp>
#include <sprout/type_traits/lvalue_reference.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/lvalue_forward.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace adaptors {
//
// steps_range
//
template<typename Range>
class steps_range
: public sprout::range::range_container<
sprout::step_iterator<typename sprout::container_traits<Range>::iterator>
>
, public sprout::detail::container_nosy_static_size<Range>
, public sprout::detail::container_nosy_fixed_size<Range>
{
public:
typedef Range range_type;
typedef sprout::range::range_container<
sprout::step_iterator<typename sprout::container_traits<Range>::iterator>
> base_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::value_type value_type;
typedef typename base_type::difference_type difference_type;
public:
steps_range() = default;
steps_range(steps_range const&) = default;
explicit SPROUT_CONSTEXPR steps_range(range_type& range)
: base_type(
iterator(sprout::begin(range), 1, sprout::end(range)),
iterator(sprout::end(range), 1, sprout::end(range))
)
{}
SPROUT_CONSTEXPR steps_range(difference_type width, range_type& range)
: base_type(
iterator(sprout::begin(range), width, sprout::end(range)),
iterator(sprout::end(range), width, sprout::end(range))
)
{}
SPROUT_CONSTEXPR steps_range(difference_type width, difference_type init, range_type& range)
: base_type(
iterator(sprout::next(sprout::begin(range), NS_SSCRISK_CEL_OR_SPROUT::min(init, sprout::size(range))), width, sprout::end(range)),
iterator(sprout::end(range), width, sprout::end(range))
)
{}
};
//
// step_holder
//
template<typename Difference>
class step_holder {
public:
typedef Difference difference_type;
private:
difference_type width_;
difference_type init_;
public:
step_holder() = default;
step_holder(step_holder const&) = default;
explicit SPROUT_CONSTEXPR step_holder(difference_type width, difference_type init = 0)
: width_(width), init_(init)
{}
SPROUT_CONSTEXPR difference_type const& width() const {
return width_;
}
SPROUT_CONSTEXPR difference_type const& init() const {
return init_;
}
};
//
// steps_forwarder
//
class steps_forwarder {
public:
template<typename Difference>
SPROUT_CONSTEXPR sprout::adaptors::step_holder<Difference>
operator()(Difference width) {
return sprout::adaptors::step_holder<Difference>(width);
}
template<typename Difference>
SPROUT_CONSTEXPR sprout::adaptors::step_holder<Difference>
operator()(Difference width, typename std::common_type<Difference>::type init) {
return sprout::adaptors::step_holder<Difference>(width, init);
}
};
//
// steps
//
namespace {
SPROUT_STATIC_CONSTEXPR sprout::adaptors::steps_forwarder steps{};
} // anonymous-namespace
//
// operator|
//
template<typename Range, typename Difference>
inline SPROUT_CONSTEXPR sprout::adaptors::steps_range<
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>
operator|(Range&& lhs, sprout::adaptors::step_holder<Difference> const& rhs) {
return sprout::adaptors::steps_range<
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>(
rhs.width(), rhs.init(),
sprout::lvalue_forward<Range>(lhs)
);
}
} // namespace adaptors
//
// container_construct_traits
//
template<typename Range>
struct container_construct_traits<sprout::adaptors::steps_range<Range> > {
public:
typedef typename sprout::container_construct_traits<Range>::copied_type copied_type;
public:
template<typename Cont>
static SPROUT_CONSTEXPR copied_type deep_copy(Cont&& cont) {
return sprout::range::fixed::copy(sprout::forward<Cont>(cont), sprout::pit<copied_type>());
}
template<typename... Args>
static SPROUT_CONSTEXPR copied_type make(Args&&... args) {
return sprout::make<copied_type>(sprout::forward<Args>(args)...);
}
template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type remake(
Cont&& cont,
typename sprout::container_traits<sprout::adaptors::steps_range<Range> >::difference_type size,
Args&&... args
)
{
return sprout::remake<copied_type>(sprout::forward<Cont>(cont), size, sprout::forward<Args>(args)...);
}
};
} // namespace sprout
#endif // #ifndef SPROUT_RANGE_ADAPTOR_STEPS_HPP

View file

@ -60,7 +60,7 @@ namespace sprout {
public:
valued_holder() = default;
valued_holder(valued_holder const&) = default;
SPROUT_CONSTEXPR valued_holder(param_type param)
explicit SPROUT_CONSTEXPR valued_holder(param_type param)
: holder_(param)
{}
SPROUT_CONSTEXPR get_type get() const {

View file

@ -33,10 +33,11 @@ namespace sprout {
iterator last_;
public:
// construct/copy/destroy:
range_container() = default;
SPROUT_CONSTEXPR range_container(iterator first, iterator last)
: first_(first)
, last_(last)
SPROUT_CONSTEXPR range_container()
: first_(), last_()
{}
SPROUT_CONSTEXPR range_container(iterator const& first, iterator const& last)
: first_(first), last_(last)
{}
template<typename Iterator2>