diff --git a/sprout/array.hpp b/sprout/array.hpp index 8a6f4b3b..c9f0ccbd 100644 --- a/sprout/array.hpp +++ b/sprout/array.hpp @@ -12,6 +12,9 @@ #include #include #include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION +# include +#endif namespace sprout { // @@ -21,8 +24,13 @@ namespace sprout { class array { public: typedef T value_type; +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION + typedef sprout::index_iterator iterator; + typedef sprout::index_iterator const_iterator; +#else typedef T* iterator; typedef T const* const_iterator; +#endif typedef T& reference; typedef T const& const_reference; typedef std::size_t size_type; @@ -51,6 +59,26 @@ namespace sprout { throw std::out_of_range("array<>: index out of range"); } } +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION + iterator begin() SPROUT_NOEXCEPT { + return iterator(*this, 0); + } + SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { + return const_iterator(*this, 0); + } + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return const_iterator(*this, 0); + } + iterator end() SPROUT_NOEXCEPT { + return iterator(*this, size()); + } + SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { + return const_iterator(*this, size()); + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return const_iterator(*this, size()); + } +#else iterator begin() SPROUT_NOEXCEPT { return &elems[0]; } @@ -69,6 +97,7 @@ namespace sprout { SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { return &elems[0] + size(); } +#endif reverse_iterator rbegin() SPROUT_NOEXCEPT { return reverse_iterator(end()); } diff --git a/sprout/iterator/index_iterator.hpp b/sprout/iterator/index_iterator.hpp new file mode 100644 index 00000000..ccd604c6 --- /dev/null +++ b/sprout/iterator/index_iterator.hpp @@ -0,0 +1,217 @@ +#ifndef SPROUT_ITERATOR_INDEX_ITERATOR_HPP +#define SPROUT_ITERATOR_INDEX_ITERATOR_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // index_iterator + // + template + class index_iterator + : public std::iterator< + std::random_access_iterator_tag, + typename sprout::fixed_container_traits::type>::value_type, + typename sprout::fixed_container_traits::type>::difference_type, + typename sprout::detail::if_c< + std::is_const::type>::value, + typename sprout::fixed_container_traits::type>::const_pointer, + typename sprout::fixed_container_traits::type>::pointer + >::type, + typename sprout::detail::if_c< + std::is_const::type>::value, + typename sprout::fixed_container_traits::type>::const_reference, + typename sprout::fixed_container_traits::type>::reference + >::type + > + { + public: + typedef Container container_type; + typedef typename sprout::fixed_container_traits::type> traits_type; + typedef typename sprout::detail::if_c< + std::is_reference::value, + typename std::decay::type const&, + typename std::decay::type const + >::type const_container_type; + private: + typedef std::iterator< + std::random_access_iterator_tag, + typename traits_type::value_type, + typename traits_type::difference_type, + typename sprout::detail::if_c< + std::is_const::type>::value, + typename traits_type::const_pointer, + typename traits_type::pointer + >::type, + typename sprout::detail::if_c< + std::is_const::type>::value, + typename traits_type::const_reference, + typename traits_type::reference + >::type + > base_type; + public: + typedef typename base_type::iterator_category iterator_category; + typedef typename base_type::value_type value_type; + typedef typename base_type::difference_type difference_type; + typedef typename base_type::pointer pointer; + typedef typename base_type::reference reference; + typedef typename traits_type::size_type size_type; + private: + sprout::value_holder holder_; + size_type index_; + private: + SPROUT_CONSTEXPR index_iterator(sprout::value_holder const& r, size_type index) + : holder_(r) + , index_(index) + {} + public: + SPROUT_CONSTEXPR index_iterator() + : holder_() + , index_() + {} + index_iterator(index_iterator const&) = default; + SPROUT_CONSTEXPR index_iterator(typename sprout::value_holder::param_type p, size_type index) + : holder_(p) + , index_(index) + {} + operator index_iterator() const { + return index_iterator(holder_.get(), index_); + } + SPROUT_CONSTEXPR index_iterator next() const { + return index_iterator(holder_, index_ + 1); + } + SPROUT_CONSTEXPR index_iterator prev() const { + return index_iterator(holder_, index_ - 1); + } + void swap(index_iterator& other) { + using std::swap; + swap(holder_, other.holder_); + swap(index_, other.index_); + } + friend SPROUT_CONSTEXPR bool operator==(index_iterator const& lhs, index_iterator const& rhs) { + return lhs.index_ == rhs.index_; + } + friend SPROUT_CONSTEXPR bool operator!=(index_iterator const& lhs, index_iterator const& rhs) { + return !(lhs == rhs); + } + friend bool operator<(index_iterator const& lhs, index_iterator const& rhs) { + return lhs.index_ < rhs.index_; + } + friend bool operator>(index_iterator const& lhs, index_iterator const& rhs) { + return rhs < lhs; + } + friend bool operator<=(index_iterator const& lhs, index_iterator const& rhs) { + return !(rhs < lhs); + } + friend bool operator>=(index_iterator const& lhs, index_iterator const& rhs) { + return !(lhs < rhs); + } + SPROUT_CONSTEXPR reference operator*() const { + return holder_.get()[index_]; + } + SPROUT_CONSTEXPR pointer operator->() const { + return &holder_.get()[index_]; + } + index_iterator& operator++() { + index_iterator temp(next()); + temp.swap(*this); + return *this; + } + index_iterator operator++(int) { + index_iterator result(*this); + ++*this; + return result; + } + index_iterator& operator--() { + index_iterator temp(prev()); + temp.swap(*this); + return *this; + } + index_iterator operator--(int) { + index_iterator result(*this); + --*this; + return result; + } + SPROUT_CONSTEXPR index_iterator operator+(difference_type n) const { + return index_iterator(holder_, index_ + n); + } + SPROUT_CONSTEXPR index_iterator operator-(difference_type n) const { + return index_iterator(holder_, index_ - n); + } + index_iterator& operator+=(difference_type n) { + index_iterator temp(holder_, index_ + n); + temp.swap(this); + return this; + } + index_iterator& operator-=(difference_type n) { + index_iterator temp(holder_, index_ - n); + temp.swap(this); + return this; + } + SPROUT_CONSTEXPR reference operator[](difference_type n) const { + return holder_.get()[index_ + n]; + } + friend difference_type operator-(index_iterator const& lhs, index_iterator const& rhs) { + return static_cast(rhs.index_) - static_cast(lhs.index_); + } + friend SPROUT_CONSTEXPR index_iterator operator+(difference_type n, index_iterator const& it) { + return it + n; + } + }; + + // + // swap + // + template + void swap(sprout::index_iterator& lhs, sprout::index_iterator& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) { + lhs.swap(rhs); + } + + // + // next + // + template + SPROUT_CONSTEXPR sprout::index_iterator next( + sprout::index_iterator const& it + ) + { + return it.next(); + } + template + SPROUT_CONSTEXPR sprout::index_iterator next( + sprout::index_iterator const& it, + typename sprout::index_iterator::difference_type n + ) + { + return it + n; + } + + // + // prev + // + template + SPROUT_CONSTEXPR sprout::index_iterator prev( + sprout::index_iterator const& it + ) + { + return it.prev(); + } + template + SPROUT_CONSTEXPR sprout::index_iterator prev( + sprout::index_iterator const& it, + typename sprout::index_iterator::difference_type n + ) + { + return it - n; + } +} // namespace sprout + +#endif // #ifndef SPROUT_ITERATOR_INDEX_ITERATOR_HPP diff --git a/sprout/iterator/value_iterator.hpp b/sprout/iterator/value_iterator.hpp index e84e341e..a18b95b2 100644 --- a/sprout/iterator/value_iterator.hpp +++ b/sprout/iterator/value_iterator.hpp @@ -4,10 +4,12 @@ #include #include #include +#include #include #include #include #include +#include namespace sprout { // @@ -25,6 +27,11 @@ namespace sprout { { public: typedef T type; + typedef typename sprout::detail::if_c< + std::is_reference::value, + typename std::decay::type const&, + typename std::decay::type const + >::type const_type; private: typedef std::iterator< std::random_access_iterator_tag, @@ -57,6 +64,9 @@ namespace sprout { : holder_(p) , count_(count) {} + operator value_iterator() const { + return value_iterator(holder_.get(), count_); + } SPROUT_CONSTEXPR value_iterator next() const { return value_iterator(holder_, count_ != 0 ? count_ - 1 : count_); } diff --git a/sprout/string.hpp b/sprout/string.hpp index ba1dc1e1..116d7fcd 100644 --- a/sprout/string.hpp +++ b/sprout/string.hpp @@ -17,6 +17,10 @@ #include #include #include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION +# include +# include +#endif namespace sprout { // @@ -84,6 +88,44 @@ namespace sprout { static SPROUT_CONSTEXPR int_type eof() SPROUT_NOEXCEPT { return impl_type::eof(); } +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION + template + static SPROUT_CONSTEXPR int compare(CharIterator s1, CharIterator s2, std::size_t n) { + return !n ? 0 + : lt(*s1, *s2) ? -1 + : lt(*s2, *s1) ? 1 + : compare(s1 + 1, s2 + 1, n - 1) + ; + } + template + static SPROUT_CONSTEXPR std::size_t length(CharIterator s) { + return !*s ? 0 + : 1 + length(s + 1) + ; + } + template + static SPROUT_CONSTEXPR CharIterator find(CharIterator s, std::size_t n, char_type const& a) { + return !n ? nullptr + : eq(*s, a) ? s + : find(s + 1, n - 1, a) + ; + } + template + static CharIterator1 move(CharIterator1 s1, CharIterator2 s2, std::size_t n) { + std::copy_backward(s2, s2 + n, s1); + return s1; + } + template + static CharIterator1 copy(CharIterator1 s1, CharIterator2 s2, std::size_t n) { + std::copy(s2, s2 + n, s1); + return s1; + } + template + static CharIterator assign(CharIterator s, std::size_t n, char_type a) { + std::fill(s, s + n, a); + return s; + } +#endif }; // @@ -93,8 +135,13 @@ namespace sprout { class basic_string { public: typedef T value_type; +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION + typedef sprout::index_iterator iterator; + typedef sprout::index_iterator const_iterator; +#else typedef T* iterator; typedef T const* const_iterator; +#endif typedef T& reference; typedef T const& const_reference; typedef std::size_t size_type; @@ -132,6 +179,15 @@ namespace sprout { { return sprout::basic_string{{(Indexes < n ? s[Indexes] : T())...}, n}; } +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION + static SPROUT_CONSTEXPR int compare_impl_1(const_iterator dest, size_type pos1, size_type n1, const_iterator s, size_type n2) { + return compare_impl_2( + traits_type::compare(dest + pos1, s, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, n2)), + n1, + n2 + ); + } +#endif public: static SPROUT_CONSTEXPR basic_string from_c_str(value_type const* s, size_type n) { return !(N < n) @@ -168,6 +224,26 @@ namespace sprout { throw std::out_of_range("basic_string<>: index out of range"); } } +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION + iterator begin() SPROUT_NOEXCEPT { + return iterator(*this, 0); + } + SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { + return const_iterator(*this, 0); + } + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return const_iterator(*this, 0); + } + iterator end() SPROUT_NOEXCEPT { + return iterator(*this, size()); + } + SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { + return const_iterator(*this, size()); + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return const_iterator(*this, size()); + } +#else iterator begin() SPROUT_NOEXCEPT { return &elems[0]; } @@ -186,6 +262,7 @@ namespace sprout { SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { return &elems[0] + size(); } +#endif reverse_iterator rbegin() SPROUT_NOEXCEPT { return const_reverse_iterator(end()); } @@ -322,14 +399,14 @@ namespace sprout { SPROUT_CONSTEXPR int compare(basic_string const& str) const { return compare(0, size(), str.c_str(), str.size()); } - SPROUT_CONSTEXPR int compare(T const* s) const { + SPROUT_CONSTEXPR int compare(value_type const* s) const { return compare(0, size(), s, traits_type::length(s)); } template SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, basic_string const& str) const { return compare(pos1, n1, str, 0, npos); } - SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, char const* s) const { + SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, value_type const* s) const { return compare(pos1, n1, s, traits_type::length(s)); } template @@ -339,7 +416,7 @@ namespace sprout { : throw "basic_string<>: index out of range" ; } - SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, char const* s, size_type n2) const { + SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, value_type const* s, size_type n2) const { return !(size() < pos1) ? compare_impl_1(c_str(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2) : throw "basic_string<>: index out of range" @@ -353,6 +430,37 @@ namespace sprout { : throw "basic_string<>: index out of range" ; } +#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION + basic_string& assign(const_iterator s, size_type n) { + maxcheck(n); + for (size_type i = 0; i < n; ++i) { + traits_type::assign(elems[i], s[i]); + } + for (size_type i = n; i < max_size(); ++i) { + traits_type::assign(elems[i], value_type()); + } + len = n; + return *this; + } + basic_string& assign(const_iterator s) { + return assign(s, traits_type::length(s)); + } + basic_string& operator=(const_iterator rhs) { + return assign(rhs); + } + SPROUT_CONSTEXPR int compare(const_iterator s) const { + return compare(0, size(), s, traits_type::length(s)); + } + SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, const_iterator s) const { + return compare(pos1, n1, s, traits_type::length(s)); + } + SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, const_iterator s, size_type n2) const { + return !(size() < pos1) + ? compare_impl_1(c_str(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2) + : throw "basic_string<>: index out of range" + ; + } +#endif }; template SPROUT_CONSTEXPR inline bool operator==(sprout::basic_string const& lhs, sprout::basic_string const& rhs) { @@ -866,3 +974,4 @@ namespace std { } // namespace std #endif // #ifndef SPROUT_STRING_HPP +