From 071d7dd49b4da5a4eb3dd1b4a60352eb4390a772 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 1 Oct 2011 00:04:03 +0900 Subject: [PATCH] =?UTF-8?q?iterator.hpp=20=E8=BF=BD=E5=8A=A0=20utility.hpp?= =?UTF-8?q?=20=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sprout/array.hpp | 6 +- sprout/detail/iterator.hpp | 132 ------------------- sprout/iterator.hpp | 9 ++ sprout/iterator/next.hpp | 64 ++++++++++ sprout/iterator/prev.hpp | 65 ++++++++++ sprout/iterator/reverse_iterator.hpp | 184 +++++++++++++++++++++++++++ sprout/string.hpp | 6 +- sprout/user_config.hpp | 4 + sprout/utility.hpp | 7 + sprout/utility/forward.hpp | 20 +++ 10 files changed, 359 insertions(+), 138 deletions(-) create mode 100644 sprout/iterator.hpp create mode 100644 sprout/iterator/next.hpp create mode 100644 sprout/iterator/prev.hpp create mode 100644 sprout/iterator/reverse_iterator.hpp create mode 100644 sprout/user_config.hpp create mode 100644 sprout/utility.hpp create mode 100644 sprout/utility/forward.hpp diff --git a/sprout/array.hpp b/sprout/array.hpp index fb9d11ef..5bfd1b1d 100644 --- a/sprout/array.hpp +++ b/sprout/array.hpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #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 reverse_iterator; - typedef sprout::detail::reverse_iterator const_reverse_iterator; + typedef sprout::reverse_iterator reverse_iterator; + typedef sprout::reverse_iterator const_reverse_iterator; public: SPROUT_STATIC_CONSTEXPR size_type static_size = N; SPROUT_STATIC_CONSTEXPR size_type fixed_size = static_size; diff --git a/sprout/detail/iterator.hpp b/sprout/detail/iterator.hpp index 4ce3e1a8..4330f43f 100644 --- a/sprout/detail/iterator.hpp +++ b/sprout/detail/iterator.hpp @@ -3,7 +3,6 @@ #include #include -#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 - class reverse_iterator - : public std::iterator< - typename std::iterator_traits::iterator_category, - typename std::iterator_traits::value_type, - typename std::iterator_traits::difference_type, - typename std::iterator_traits::pointer, - typename std::iterator_traits::reference - > - { - public: - typedef Iterator iterator_type; - typedef typename std::iterator_traits::iterator_category iterator_category; - typedef typename std::iterator_traits::value_type value_type; - typedef typename std::iterator_traits::difference_type difference_type; - typedef typename std::iterator_traits::pointer pointer; - typedef typename std::iterator_traits::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 - SPROUT_CONSTEXPR reverse_iterator(reverse_iterator const& it) - : current(it) - , deref_tmp(it - 1) - {} - template - reverse_iterator& operator=(reverse_iterator 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 - SPROUT_CONSTEXPR bool operator==( - sprout::detail::reverse_iterator const& lhs, - sprout::detail::reverse_iterator const& rhs - ) - { - return lhs.base() == rhs.base(); - } - template - SPROUT_CONSTEXPR bool operator!=( - sprout::detail::reverse_iterator const& lhs, - sprout::detail::reverse_iterator const& rhs - ) - { - return !(lhs == rhs); - } - template - SPROUT_CONSTEXPR sprout::detail::reverse_iterator operator+( - typename sprout::detail::reverse_iterator::difference_type n, - sprout::detail::reverse_iterator const& it - ) - { - return it + n; - } - - // - // swap - // - template - void swap(sprout::detail::reverse_iterator& lhs, sprout::detail::reverse_iterator& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) { - lhs.swap(rhs); - } } // namespace detail } // namespace sprout diff --git a/sprout/iterator.hpp b/sprout/iterator.hpp new file mode 100644 index 00000000..d79f97f6 --- /dev/null +++ b/sprout/iterator.hpp @@ -0,0 +1,9 @@ +#ifndef SPROUT_ITERATOR_HPP +#define SPROUT_ITERATOR_HPP + +#include +#include +#include +#include + +#endif // #ifndef SPROUT_ITERATOR_HPP diff --git a/sprout/iterator/next.hpp b/sprout/iterator/next.hpp new file mode 100644 index 00000000..a595d01e --- /dev/null +++ b/sprout/iterator/next.hpp @@ -0,0 +1,64 @@ +#ifndef SPROUT_ITERATOR_NEXT_HPP +#define SPROUT_ITERATOR_NEXT_HPP +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + SPROUT_CONSTEXPR typename std::enable_if< + std::is_literal_type::type>::value, + typename std::decay::type + >::type next_impl( + Iterator&& it, + std::random_access_iterator_tag* + ) + { + return sprout::forward(it) + 1; + } + template + Iterator next_impl( + Iterator&& it, + void* + ) + { + return std::next(sprout::forward(it)); + } + + template + SPROUT_CONSTEXPR typename std::enable_if< + std::is_literal_type::type>::value, + typename std::decay::type + >::type next_impl( + Iterator&& it, + typename std::iterator_traits::type>::difference_type n, + std::random_access_iterator_tag* + ) + { + return sprout::forward(it) + n; + } + template + Iterator next_impl( + Iterator it, + typename std::iterator_traits::type>::difference_type n, + void* + ) + { + return std::next(sprout::forward(it), n); + } + } // namespace detail + // + // next + // + template + SPROUT_CONSTEXPR typename std::decay::type next(Iterator&& it) { + return sprout::detail::next_impl( + sprout::forward(it), + static_cast::type>::iterator_category*>(nullptr) + ); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ITERATOR_NEXT_HPP diff --git a/sprout/iterator/prev.hpp b/sprout/iterator/prev.hpp new file mode 100644 index 00000000..d1b2faa4 --- /dev/null +++ b/sprout/iterator/prev.hpp @@ -0,0 +1,65 @@ +#ifndef SPROUT_ITERATOR_PREV_HPP +#define SPROUT_ITERATOR_PREV_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + SPROUT_CONSTEXPR typename std::enable_if< + std::is_literal_type::type>::value, + typename std::decay::type + >::type prev_impl( + Iterator&& it, + std::random_access_iterator_tag* + ) + { + return sprout::forward(it) - 1; + } + template + Iterator prev_impl( + Iterator&& it, + void* + ) + { + return std::prev(sprout::forward(it)); + } + + template + SPROUT_CONSTEXPR typename std::enable_if< + std::is_literal_type::type>::value, + typename std::decay::type + >::type prev_impl( + Iterator&& it, + typename std::iterator_traits::type>::difference_type n, + std::random_access_iterator_tag* + ) + { + return sprout::forward(it) - n; + } + template + Iterator prev_impl( + Iterator it, + typename std::iterator_traits::type>::difference_type n, + void* + ) + { + return std::prev(sprout::forward(it), n); + } + } // namespace detail + // + // prev + // + template + SPROUT_CONSTEXPR typename std::decay::type prev(Iterator&& it) { + return sprout::detail::prev_impl( + sprout::forward(it), + static_cast::type>::iterator_category*>(nullptr) + ); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ITERATOR_PREV_HPP diff --git a/sprout/iterator/reverse_iterator.hpp b/sprout/iterator/reverse_iterator.hpp new file mode 100644 index 00000000..1f750596 --- /dev/null +++ b/sprout/iterator/reverse_iterator.hpp @@ -0,0 +1,184 @@ +#ifndef SPROUT_ITERATOR_REVERSE_ITERATOR_HPP +#define SPROUT_ITERATOR_REVERSE_ITERATOR_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + // + // reverse_iterator + // + template + class reverse_iterator + : public std::iterator< + typename std::iterator_traits::iterator_category, + typename std::iterator_traits::value_type, + typename std::iterator_traits::difference_type, + typename std::iterator_traits::pointer, + typename std::iterator_traits::reference + > + { + public: + typedef Iterator iterator_type; + typedef typename std::iterator_traits::iterator_category iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef typename std::iterator_traits::pointer pointer; + typedef typename std::iterator_traits::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 + SPROUT_CONSTEXPR reverse_iterator(reverse_iterator const& it) + : current(it) + , deref_tmp(sprout::prev(it)) + {} + template + reverse_iterator& operator=(reverse_iterator 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 + SPROUT_CONSTEXPR bool operator==( + sprout::reverse_iterator const& lhs, + sprout::reverse_iterator const& rhs + ) + { + return lhs.base() == rhs.base(); + } + template + SPROUT_CONSTEXPR bool operator!=( + sprout::reverse_iterator const& lhs, + sprout::reverse_iterator const& rhs + ) + { + return !(lhs == rhs); + } + template + bool operator<( + sprout::reverse_iterator const& lhs, + sprout::reverse_iterator const& rhs + ) + { + return lhs.base() < rhs.base(); + } + template + bool operator>( + sprout::reverse_iterator const& lhs, + sprout::reverse_iterator const& rhs + ) + { + return rhs < lhs; + } + template + bool operator<=( + sprout::reverse_iterator const& lhs, + sprout::reverse_iterator const& rhs + ) + { + return !(rhs < lhs); + } + template + bool operator>=( + sprout::reverse_iterator const& lhs, + sprout::reverse_iterator const& rhs + ) + { + return !(lhs < rhs); + } + template + auto operator-( + sprout::reverse_iterator const& lhs, + sprout::reverse_iterator const& rhs + ) -> decltype(lhs.current - rhs.current) + { + return lhs.current - rhs.current; + } + template + SPROUT_CONSTEXPR sprout::reverse_iterator operator+( + typename sprout::reverse_iterator::difference_type n, + sprout::reverse_iterator const& it + ) + { + return it + n; + } + + // + // swap + // + template + void swap(sprout::reverse_iterator& lhs, sprout::reverse_iterator& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) { + lhs.swap(rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ITERATOR_REVERSE_ITERATOR_HPP + diff --git a/sprout/string.hpp b/sprout/string.hpp index d68191b0..67345f6d 100644 --- a/sprout/string.hpp +++ b/sprout/string.hpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #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 reverse_iterator; - typedef sprout::detail::reverse_iterator const_reverse_iterator; + typedef sprout::reverse_iterator reverse_iterator; + typedef sprout::reverse_iterator const_reverse_iterator; typedef Traits traits_type; public: SPROUT_STATIC_CONSTEXPR size_type npos = -1; diff --git a/sprout/user_config.hpp b/sprout/user_config.hpp new file mode 100644 index 00000000..c0a86f4e --- /dev/null +++ b/sprout/user_config.hpp @@ -0,0 +1,4 @@ + +//#define SPROUT_CONFIG_DISABLE_CONSTEXPR +//#define SPROUT_CONFIG_DISABLE_NOEXCEPT +//#define SPROUT_CONFIG_USE_SSCRISK_CEL diff --git a/sprout/utility.hpp b/sprout/utility.hpp new file mode 100644 index 00000000..7de2faff --- /dev/null +++ b/sprout/utility.hpp @@ -0,0 +1,7 @@ +#ifndef SPROUT_UTILITY_HPP +#define SPROUT_UTILITY_HPP + +#include +#include + +#endif // #ifndef SPROUT_UTILITY_HPP diff --git a/sprout/utility/forward.hpp b/sprout/utility/forward.hpp new file mode 100644 index 00000000..6ab573b1 --- /dev/null +++ b/sprout/utility/forward.hpp @@ -0,0 +1,20 @@ +#ifndef SPROUT_UTILITY_FORWARD_HPP +#define SPROUT_UTILITY_FORWARD_HPP + +#include +#include + +namespace sprout { + // + // forward + // + template + SPROUT_CONSTEXPR T&& forward(typename std::remove_reference::type& t) SPROUT_NOEXCEPT { + return static_cast(t); + } + template + SPROUT_CONSTEXPR T&& forward(typename std::remove_reference::type&& t) SPROUT_NOEXCEPT = delete; +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_FORWARD_HPP +