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

iterator.hpp 追加

utility.hpp 追加
This commit is contained in:
bolero-MURAKAMI 2011-10-01 00:04:03 +09:00
parent 8b357a75da
commit 071d7dd49b
10 changed files with 359 additions and 138 deletions

View file

@ -10,7 +10,7 @@
#include <sprout/index_tuple.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/detail/iterator.hpp>
#include <sprout/iterator.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
@ -29,8 +29,8 @@ namespace sprout {
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef sprout::detail::reverse_iterator<iterator> reverse_iterator;
typedef sprout::detail::reverse_iterator<const_iterator> const_reverse_iterator;
typedef sprout::reverse_iterator<iterator> reverse_iterator;
typedef sprout::reverse_iterator<const_iterator> const_reverse_iterator;
public:
SPROUT_STATIC_CONSTEXPR size_type static_size = N;
SPROUT_STATIC_CONSTEXPR size_type fixed_size = static_size;

View file

@ -3,7 +3,6 @@
#include <iterator>
#include <sprout/config.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
namespace detail {
@ -45,137 +44,6 @@ namespace sprout {
{
return first == last ? 0 : sprout::detail::bidirectional_distance_impl(first + 1, first - 1, last);
}
//
// reverse_iterator
//
template<typename Iterator>
class reverse_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;
protected:
iterator_type current;
private:
iterator_type deref_tmp;
public:
reverse_iterator() = default;
reverse_iterator(reverse_iterator const&) = default;
SPROUT_CONSTEXPR explicit reverse_iterator(iterator_type it)
: current(it)
, deref_tmp(it - 1)
{}
template<typename U>
SPROUT_CONSTEXPR reverse_iterator(reverse_iterator<U> const& it)
: current(it)
, deref_tmp(it - 1)
{}
template<typename U>
reverse_iterator& operator=(reverse_iterator<U> const& it) {
reverse_iterator temp(it);
temp.swap(this);
return this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR reference operator*() const {
return *deref_tmp;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &*deref_tmp;
}
reverse_iterator& operator++() {
--current;
--deref_tmp;
return this;
}
reverse_iterator operator++(int) {
reverse_iterator result(this);
--current;
--deref_tmp;
return result;
}
reverse_iterator& operator--() {
++current;
++deref_tmp;
return this;
}
reverse_iterator operator--(int) {
reverse_iterator temp(this);
++current;
++deref_tmp;
return temp;
}
SPROUT_CONSTEXPR reverse_iterator operator+(difference_type n) const {
return reverse_iterator(current - n);
}
SPROUT_CONSTEXPR reverse_iterator operator-(difference_type n) const {
return reverse_iterator(current + n);
}
reverse_iterator& operator+=(difference_type n) {
reverse_iterator temp(current - n);
temp.swap(this);
return this;
}
reverse_iterator& operator-=(difference_type n) {
reverse_iterator temp(current + n);
temp.swap(this);
return this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return *(deref_tmp - n);
}
void swap(reverse_iterator& other) {
using std::swap;
swap(current, other.current);
swap(deref_tmp, other.deref_tmp);
}
};
template <typename Iterator1, typename Iterator2>
SPROUT_CONSTEXPR bool operator==(
sprout::detail::reverse_iterator<Iterator1> const& lhs,
sprout::detail::reverse_iterator<Iterator2> const& rhs
)
{
return lhs.base() == rhs.base();
}
template <typename Iterator1, typename Iterator2>
SPROUT_CONSTEXPR bool operator!=(
sprout::detail::reverse_iterator<Iterator1> const& lhs,
sprout::detail::reverse_iterator<Iterator2> const& rhs
)
{
return !(lhs == rhs);
}
template <typename Iterator>
SPROUT_CONSTEXPR sprout::detail::reverse_iterator<Iterator> operator+(
typename sprout::detail::reverse_iterator<Iterator>::difference_type n,
sprout::detail::reverse_iterator<Iterator> const& it
)
{
return it + n;
}
//
// swap
//
template<typename Iterator>
void swap(sprout::detail::reverse_iterator<Iterator>& lhs, sprout::detail::reverse_iterator<Iterator>& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) {
lhs.swap(rhs);
}
} // namespace detail
} // namespace sprout

9
sprout/iterator.hpp Normal file
View file

@ -0,0 +1,9 @@
#ifndef SPROUT_ITERATOR_HPP
#define SPROUT_ITERATOR_HPP
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
#include <sprout/iterator/reverse_iterator.hpp>
#endif // #ifndef SPROUT_ITERATOR_HPP

64
sprout/iterator/next.hpp Normal file
View file

@ -0,0 +1,64 @@
#ifndef SPROUT_ITERATOR_NEXT_HPP
#define SPROUT_ITERATOR_NEXT_HPP
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
namespace sprout {
namespace detail {
template<typename Iterator>
SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<typename std::decay<Iterator>::type>::value,
typename std::decay<Iterator>::type
>::type next_impl(
Iterator&& it,
std::random_access_iterator_tag*
)
{
return sprout::forward<Iterator>(it) + 1;
}
template<typename Iterator>
Iterator next_impl(
Iterator&& it,
void*
)
{
return std::next(sprout::forward<Iterator>(it));
}
template<typename Iterator>
SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<typename std::decay<Iterator>::type>::value,
typename std::decay<Iterator>::type
>::type next_impl(
Iterator&& it,
typename std::iterator_traits<typename std::decay<Iterator>::type>::difference_type n,
std::random_access_iterator_tag*
)
{
return sprout::forward<Iterator>(it) + n;
}
template<typename Iterator>
Iterator next_impl(
Iterator it,
typename std::iterator_traits<typename std::decay<Iterator>::type>::difference_type n,
void*
)
{
return std::next(sprout::forward<Iterator>(it), n);
}
} // namespace detail
//
// next
//
template<typename Iterator>
SPROUT_CONSTEXPR typename std::decay<Iterator>::type next(Iterator&& it) {
return sprout::detail::next_impl(
sprout::forward<Iterator>(it),
static_cast<typename std::iterator_traits<typename std::decay<Iterator>::type>::iterator_category*>(nullptr)
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_NEXT_HPP

65
sprout/iterator/prev.hpp Normal file
View file

@ -0,0 +1,65 @@
#ifndef SPROUT_ITERATOR_PREV_HPP
#define SPROUT_ITERATOR_PREV_HPP
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
namespace sprout {
namespace detail {
template<typename Iterator>
SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<typename std::decay<Iterator>::type>::value,
typename std::decay<Iterator>::type
>::type prev_impl(
Iterator&& it,
std::random_access_iterator_tag*
)
{
return sprout::forward<Iterator>(it) - 1;
}
template<typename Iterator>
Iterator prev_impl(
Iterator&& it,
void*
)
{
return std::prev(sprout::forward<Iterator>(it));
}
template<typename Iterator>
SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<typename std::decay<Iterator>::type>::value,
typename std::decay<Iterator>::type
>::type prev_impl(
Iterator&& it,
typename std::iterator_traits<typename std::decay<Iterator>::type>::difference_type n,
std::random_access_iterator_tag*
)
{
return sprout::forward<Iterator>(it) - n;
}
template<typename Iterator>
Iterator prev_impl(
Iterator it,
typename std::iterator_traits<typename std::decay<Iterator>::type>::difference_type n,
void*
)
{
return std::prev(sprout::forward<Iterator>(it), n);
}
} // namespace detail
//
// prev
//
template<typename Iterator>
SPROUT_CONSTEXPR typename std::decay<Iterator>::type prev(Iterator&& it) {
return sprout::detail::prev_impl(
sprout::forward<Iterator>(it),
static_cast<typename std::iterator_traits<typename std::decay<Iterator>::type>::iterator_category*>(nullptr)
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_PREV_HPP

View file

@ -0,0 +1,184 @@
#ifndef SPROUT_ITERATOR_REVERSE_ITERATOR_HPP
#define SPROUT_ITERATOR_REVERSE_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
namespace sprout {
//
// reverse_iterator
//
template<typename Iterator>
class reverse_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;
protected:
iterator_type current;
private:
iterator_type deref_tmp;
public:
reverse_iterator() = default;
reverse_iterator(reverse_iterator const&) = default;
SPROUT_CONSTEXPR explicit reverse_iterator(iterator_type it)
: current(it)
, deref_tmp(sprout::prev(it))
{}
template<typename U>
SPROUT_CONSTEXPR reverse_iterator(reverse_iterator<U> const& it)
: current(it)
, deref_tmp(sprout::prev(it))
{}
template<typename U>
reverse_iterator& operator=(reverse_iterator<U> const& it) {
reverse_iterator temp(it);
temp.swap(this);
return this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR reference operator*() const {
return *deref_tmp;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &*deref_tmp;
}
reverse_iterator& operator++() {
--current;
--deref_tmp;
return this;
}
reverse_iterator operator++(int) {
reverse_iterator result(this);
--current;
--deref_tmp;
return result;
}
reverse_iterator& operator--() {
++current;
++deref_tmp;
return this;
}
reverse_iterator operator--(int) {
reverse_iterator temp(this);
++current;
++deref_tmp;
return temp;
}
SPROUT_CONSTEXPR reverse_iterator operator+(difference_type n) const {
return reverse_iterator(current - n);
}
SPROUT_CONSTEXPR reverse_iterator operator-(difference_type n) const {
return reverse_iterator(current + n);
}
reverse_iterator& operator+=(difference_type n) {
reverse_iterator temp(current - n);
temp.swap(this);
return this;
}
reverse_iterator& operator-=(difference_type n) {
reverse_iterator temp(current + n);
temp.swap(this);
return this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return *(deref_tmp - n);
}
void swap(reverse_iterator& other) {
using std::swap;
swap(current, other.current);
swap(deref_tmp, other.deref_tmp);
}
};
template <typename Iterator1, typename Iterator2>
SPROUT_CONSTEXPR bool operator==(
sprout::reverse_iterator<Iterator1> const& lhs,
sprout::reverse_iterator<Iterator2> const& rhs
)
{
return lhs.base() == rhs.base();
}
template <typename Iterator1, typename Iterator2>
SPROUT_CONSTEXPR bool operator!=(
sprout::reverse_iterator<Iterator1> const& lhs,
sprout::reverse_iterator<Iterator2> const& rhs
)
{
return !(lhs == rhs);
}
template <typename Iterator1, typename Iterator2>
bool operator<(
sprout::reverse_iterator<Iterator1> const& lhs,
sprout::reverse_iterator<Iterator2> const& rhs
)
{
return lhs.base() < rhs.base();
}
template <typename Iterator1, typename Iterator2>
bool operator>(
sprout::reverse_iterator<Iterator1> const& lhs,
sprout::reverse_iterator<Iterator2> const& rhs
)
{
return rhs < lhs;
}
template <typename Iterator1, typename Iterator2>
bool operator<=(
sprout::reverse_iterator<Iterator1> const& lhs,
sprout::reverse_iterator<Iterator2> const& rhs
)
{
return !(rhs < lhs);
}
template <typename Iterator1, typename Iterator2>
bool operator>=(
sprout::reverse_iterator<Iterator1> const& lhs,
sprout::reverse_iterator<Iterator2> const& rhs
)
{
return !(lhs < rhs);
}
template <typename Iterator1, typename Iterator2>
auto operator-(
sprout::reverse_iterator<Iterator1> const& lhs,
sprout::reverse_iterator<Iterator2> const& rhs
) -> decltype(lhs.current - rhs.current)
{
return lhs.current - rhs.current;
}
template <typename Iterator>
SPROUT_CONSTEXPR sprout::reverse_iterator<Iterator> operator+(
typename sprout::reverse_iterator<Iterator>::difference_type n,
sprout::reverse_iterator<Iterator> const& it
)
{
return it + n;
}
//
// swap
//
template<typename Iterator>
void swap(sprout::reverse_iterator<Iterator>& lhs, sprout::reverse_iterator<Iterator>& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) {
lhs.swap(rhs);
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_REVERSE_ITERATOR_HPP

View file

@ -15,7 +15,7 @@
#include <sprout/operation/fixed/push_front.hpp>
#include <sprout/operation/fixed/append_back.hpp>
#include <sprout/operation/fixed/append_front.hpp>
#include <sprout/detail/iterator.hpp>
#include <sprout/iterator.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
@ -101,8 +101,8 @@ namespace sprout {
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef sprout::detail::reverse_iterator<iterator> reverse_iterator;
typedef sprout::detail::reverse_iterator<const_iterator> const_reverse_iterator;
typedef sprout::reverse_iterator<iterator> reverse_iterator;
typedef sprout::reverse_iterator<const_iterator> const_reverse_iterator;
typedef Traits traits_type;
public:
SPROUT_STATIC_CONSTEXPR size_type npos = -1;

4
sprout/user_config.hpp Normal file
View file

@ -0,0 +1,4 @@
//#define SPROUT_CONFIG_DISABLE_CONSTEXPR
//#define SPROUT_CONFIG_DISABLE_NOEXCEPT
//#define SPROUT_CONFIG_USE_SSCRISK_CEL

7
sprout/utility.hpp Normal file
View file

@ -0,0 +1,7 @@
#ifndef SPROUT_UTILITY_HPP
#define SPROUT_UTILITY_HPP
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
#endif // #ifndef SPROUT_UTILITY_HPP

View file

@ -0,0 +1,20 @@
#ifndef SPROUT_UTILITY_FORWARD_HPP
#define SPROUT_UTILITY_FORWARD_HPP
#include <type_traits>
#include <sprout/config.hpp>
namespace sprout {
//
// forward
//
template<typename T>
SPROUT_CONSTEXPR T&& forward(typename std::remove_reference<T>::type& t) SPROUT_NOEXCEPT {
return static_cast<T&&>(t);
}
template<typename T>
SPROUT_CONSTEXPR T&& forward(typename std::remove_reference<T>::type&& t) SPROUT_NOEXCEPT = delete;
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_FORWARD_HPP