1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-08-03 12:49:50 +00:00

destructive change sprout::generate -> sprout::unfold

add range adapter reversed, replaced, filtered
This commit is contained in:
bolero-MURAKAMI 2012-09-21 15:43:30 +09:00
parent 6b3f7ad894
commit 73ead93fe5
60 changed files with 3840 additions and 318 deletions

View file

@ -0,0 +1,11 @@
#ifndef SPROUT_ITERATOR_ADAPTOR_HPP
#define SPROUT_ITERATOR_ADAPTOR_HPP
#include <sprout/config.hpp>
#include <sprout/iterator/reverse_iterator.hpp>
#include <sprout/iterator/transform_iterator.hpp>
#include <sprout/iterator/filter_iterator.hpp>
#include <sprout/iterator/counting_iterator.hpp>
#include <sprout/iterator/size_enum_iterator.hpp>
#endif // #ifndef SPROUT_ITERATOR_ADAPTOR_HPP

View file

@ -0,0 +1,202 @@
#ifndef SPROUT_ITERATOR_FILTER_ITERATOR_HPP
#define SPROUT_ITERATOR_FILTER_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
#include <sprout/algorithm/find_if.hpp>
namespace sprout {
//
// filter_iterator
//
template<typename Predicate, typename Iterator>
class filter_iterator
: public std::iterator<
typename std::conditional<
std::is_convertible<typename std::iterator_traits<Iterator>::iterator_category, std::random_access_iterator_tag>::value,
std::bidirectional_iterator_tag,
typename std::iterator_traits<Iterator>::iterator_category
>::type,
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 Predicate predicate_type;
typedef Iterator iterator_type;
typedef typename std::conditional<
std::is_convertible<typename std::iterator_traits<Iterator>::iterator_category, std::random_access_iterator_tag>::value,
std::bidirectional_iterator_tag,
typename std::iterator_traits<Iterator>::iterator_category
>::type iterator_category;
typedef typename std::iterator_traits<Iterator>::value_type value_type;
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
typedef typename std::iterator_traits<Iterator>::pointer pointer;
typedef typename std::iterator_traits<Iterator>::reference reference;
private:
struct private_constructor_tag {};
private:
static SPROUT_CONSTEXPR iterator_type find_next(iterator_type first, iterator_type last, predicate_type pred) {
return sprout::find_if(first, last, pred);
}
static SPROUT_CONSTEXPR iterator_type find_prev(iterator_type first, predicate_type pred) {
return pred(*first) != false ? first
: find_prev(sprout::prev(first), pred)
;
}
protected:
iterator_type current;
iterator_type last;
predicate_type pred;
private:
iterator_type satisfy_predicate() {
while (current != last && !pred(current)) {
++current;
}
}
iterator_type satisfy_predicate_backward() {
while (!pred(current)) {
--current;
}
}
SPROUT_CONSTEXPR filter_iterator(predicate_type pred, iterator_type it, iterator_type last, private_constructor_tag)
: current(it)
, last(last)
, pred(pred)
{}
public:
filter_iterator() = default;
filter_iterator(filter_iterator const&) = default;
SPROUT_CONSTEXPR filter_iterator(predicate_type pred, iterator_type it, iterator_type last = iterator_type())
: current(find_next(it, last, pred))
, last(last)
, pred(pred)
{}
template<typename U>
SPROUT_CONSTEXPR filter_iterator(filter_iterator<Predicate, U> const& it)
: current(it.current)
, last(it.last)
, pred(it.pred)
{}
template<typename U>
filter_iterator& operator=(filter_iterator<Predicate, U> const& it) {
filter_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR iterator_type end() const {
return last;
}
SPROUT_CONSTEXPR predicate_type predicate() const {
return pred;
}
SPROUT_CONSTEXPR reference operator*() const {
return *current;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &*current;
}
filter_iterator& operator++() {
++current;
satisfy_predicate();
return *this;
}
filter_iterator operator++(int) {
filter_iterator result(*this);
++current;
satisfy_predicate();
return result;
}
filter_iterator& operator--() {
--current;
satisfy_predicate_backward();
return *this;
}
filter_iterator operator--(int) {
filter_iterator temp(*this);
--current;
satisfy_predicate_backward();
return temp;
}
SPROUT_CONSTEXPR filter_iterator next() const {
return filter_iterator(pred, find_next(sprout::next(current)), last, private_constructor_tag());
}
SPROUT_CONSTEXPR filter_iterator prev() const {
return filter_iterator(pred, find_prev(sprout::prev(current)), last, private_constructor_tag());
}
void swap(filter_iterator& other) {
using std::swap;
swap(current, other.current);
swap(last, other.last);
swap(pred, other.pred);
}
};
template<typename Predicate, typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool operator==(
sprout::filter_iterator<Predicate, Iterator1> const& lhs,
sprout::filter_iterator<Predicate, Iterator2> const& rhs
)
{
return lhs.base() == rhs.base();
}
template<typename Predicate, typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool operator!=(
sprout::filter_iterator<Predicate, Iterator1> const& lhs,
sprout::filter_iterator<Predicate, Iterator2> const& rhs
)
{
return !(lhs == rhs);
}
//
// make_filter_iterator
//
template<typename Predicate, typename Iterator>
inline SPROUT_CONSTEXPR sprout::filter_iterator<Predicate, Iterator>
make_filter_iterator(Predicate pred, Iterator it, Iterator last = Iterator()) {
return sprout::filter_iterator<Predicate, Iterator>(pred, it, last);
}
//
// swap
//
template<typename Predicate, typename Iterator>
inline void swap(sprout::filter_iterator<Predicate, Iterator>& lhs, sprout::filter_iterator<Predicate, Iterator>& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) {
lhs.swap(rhs);
}
//
// next
//
template<typename Predicate, typename Iterator>
inline SPROUT_CONSTEXPR sprout::filter_iterator<Predicate, Iterator> next(
sprout::filter_iterator<Predicate, Iterator> const& it
)
{
return it.next();
}
//
// prev
//
template<typename Predicate, typename Iterator>
inline SPROUT_CONSTEXPR sprout::filter_iterator<Predicate, Iterator> prev(
sprout::filter_iterator<Predicate, Iterator> const& it
)
{
return it.prev();
}
} // namespace sprout
#endif // SPROUT_ITERATOR_FILTER_ITERATOR_HPP

View file

@ -25,8 +25,7 @@ namespace sprout {
void*
)
{
using std::next;
return next(sprout::forward<ForwardIterator>(it));
return std::next(sprout::forward<ForwardIterator>(it));
}
template<typename RandomAccessIterator>
@ -48,8 +47,7 @@ namespace sprout {
void*
)
{
using std::next;
return next(sprout::forward<ForwardIterator>(it), n);
return std::next(sprout::forward<ForwardIterator>(it), n);
}
} // namespace detail
//

View file

@ -25,8 +25,7 @@ namespace sprout {
void*
)
{
using std::prev;
return prev(sprout::forward<BidirectionalIterator>(it));
return std::prev(sprout::forward<BidirectionalIterator>(it));
}
template<typename RandomAccessIterator>
@ -48,8 +47,7 @@ namespace sprout {
void*
)
{
using std::prev;
return prev(sprout::forward<BidirectionalIterator>(it), n);
return std::prev(sprout::forward<BidirectionalIterator>(it), n);
}
} // namespace detail
//

View file

@ -182,6 +182,15 @@ namespace sprout {
return it + n;
}
//
// make_reverse_iterator
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::reverse_iterator<Iterator>
make_reverse_iterator(Iterator it) {
return sprout::reverse_iterator<Iterator>(it);
}
//
// swap
//

10
sprout/iterator/wave.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef SPROUT_ITERATOR_WAVE_HPP
#define SPROUT_ITERATOR_WAVE_HPP
#include <sprout/config.hpp>
#include <sprout/iterator/sinusoid_iterator.hpp>
#include <sprout/iterator/sawtooth_iterator.hpp>
#include <sprout/iterator/triangle_iterator.hpp>
#include <sprout/iterator/square_iterator.hpp>
#endif // #ifndef SPROUT_ITERATOR_WAVE_HPP