diff --git a/sprout/container/container_holder.hpp b/sprout/container/container_holder.hpp index 146f85dd..2d7997c5 100644 --- a/sprout/container/container_holder.hpp +++ b/sprout/container/container_holder.hpp @@ -2,6 +2,7 @@ #define SPROUT_CONTAINER_CONTAINER_HOLDER_HPP #include +#include #include #include #include @@ -82,6 +83,35 @@ namespace sprout { SPROUT_CONSTEXPR const_param_type get_internal() const { return *container; } + // element access: + reference operator[](size_type i) { + return *sprout::next(begin(), i); + } + SPROUT_CONSTEXPR const_reference operator[](size_type i) const { + return *sprout::next(begin(), i); + } + reference at(size_type i) { + return i < size() ? (*this)[i] + : (throw std::out_of_range("container_holder<>: index out of range"), (*this)[i]) + ; + } + SPROUT_CONSTEXPR const_reference at(size_type i) const { + return i < size() ? (*this)[i] + : (throw std::out_of_range("container_holder<>: index out of range"), (*this)[i]) + ; + } + reference front() { + return *begin(); + } + SPROUT_CONSTEXPR const_reference front() const { + return *begin(); + } + reference back() { + return *sprout::next(begin(), size() - 1); + } + SPROUT_CONSTEXPR const_reference back() const { + return *sprout::next(begin(), size() - 1); + } }; // diff --git a/sprout/cstring/memchr.hpp b/sprout/cstring/memchr.hpp index c9e5e3b5..81ff7976 100644 --- a/sprout/cstring/memchr.hpp +++ b/sprout/cstring/memchr.hpp @@ -30,9 +30,9 @@ namespace sprout { inline SPROUT_CONSTEXPR void const* memchr(void const* s, int c, std::size_t n) { return sprout::detail::memchr_impl( - sprout::as_iterator_base( + sprout::ptr_unindex( sprout::find( - sprout::as_iterator(static_cast(s)), sprout::as_iterator(static_cast(s), n), + sprout::ptr_index(static_cast(s)), sprout::ptr_index(static_cast(s), n), static_cast(c) ) ), @@ -43,9 +43,9 @@ namespace sprout { inline SPROUT_CONSTEXPR void* memchr(void* s, int c, std::size_t n) { return sprout::detail::memchr_impl( - sprout::as_iterator_base( + sprout::ptr_unindex( sprout::find( - sprout::as_iterator(static_cast(s)), sprout::as_iterator(static_cast(s), n), + sprout::ptr_index(static_cast(s)), sprout::ptr_index(static_cast(s), n), static_cast(c) ) ), diff --git a/sprout/cstring/memcmp.hpp b/sprout/cstring/memcmp.hpp index 9cbb4277..dc515505 100644 --- a/sprout/cstring/memcmp.hpp +++ b/sprout/cstring/memcmp.hpp @@ -15,8 +15,8 @@ namespace sprout { inline SPROUT_CONSTEXPR int memcmp(void const* s1, void const* s2, std::size_t n) { return sprout::tristate_lexicographical_compare( - sprout::as_iterator(static_cast(s1)), sprout::as_iterator(static_cast(s1), n), - sprout::as_iterator(static_cast(s2)), sprout::as_iterator(static_cast(s2), n) + sprout::ptr_index(static_cast(s1)), sprout::ptr_index(static_cast(s1), n), + sprout::ptr_index(static_cast(s2)), sprout::ptr_index(static_cast(s2), n) ); } } // namespace sprout diff --git a/sprout/cstring/strlen.hpp b/sprout/cstring/strlen.hpp index add8841e..68392c46 100644 --- a/sprout/cstring/strlen.hpp +++ b/sprout/cstring/strlen.hpp @@ -70,8 +70,8 @@ namespace sprout { inline SPROUT_CONSTEXPR std::size_t strlen(char const* s, std::size_t n) { return sprout::distance( - sprout::as_iterator(s), - sprout::find(sprout::as_iterator(s), sprout::as_iterator(s, n), '\0') + sprout::ptr_index(s), + sprout::find(sprout::ptr_index(s), sprout::ptr_index(s, n), '\0') ); } @@ -91,8 +91,8 @@ namespace sprout { strlen(Elem* s, std::size_t n) { typedef typename std::decay::type type; return sprout::distance( - sprout::as_iterator(s), - sprout::find(sprout::as_iterator(s), sprout::as_iterator(s, n), type()) + sprout::ptr_index(s), + sprout::find(sprout::ptr_index(s), sprout::ptr_index(s, n), type()) ); } } // namespace sprout diff --git a/sprout/cstring/strncmp.hpp b/sprout/cstring/strncmp.hpp index e1d4dbdf..5528dddd 100644 --- a/sprout/cstring/strncmp.hpp +++ b/sprout/cstring/strncmp.hpp @@ -17,8 +17,8 @@ namespace sprout { inline SPROUT_CONSTEXPR int strncmp(char const* s1, char const* s2, std::size_t n) { return sprout::tristate_lexicographical_compare( - sprout::as_iterator(s1), sprout::as_iterator(s1, n), '\0', - sprout::as_iterator(s2), sprout::as_iterator(s2, n), '\0' + sprout::ptr_index(s1), sprout::ptr_index(s1, n), '\0', + sprout::ptr_index(s2), sprout::ptr_index(s2, n), '\0' ); } @@ -30,8 +30,8 @@ namespace sprout { strncmp(Elem* s1, Elem* s2, std::size_t n) { typedef typename std::decay::type type; return sprout::tristate_lexicographical_compare( - sprout::as_iterator(s1), sprout::as_iterator(s1, n), type(), - sprout::as_iterator(s2), sprout::as_iterator(s2, n), type() + sprout::ptr_index(s1), sprout::ptr_index(s1, n), type(), + sprout::ptr_index(s2), sprout::ptr_index(s2, n), type() ); } } // namespace sprout diff --git a/sprout/cwchar/wmemchr.hpp b/sprout/cwchar/wmemchr.hpp index 8a2ede3e..ab86a60a 100644 --- a/sprout/cwchar/wmemchr.hpp +++ b/sprout/cwchar/wmemchr.hpp @@ -31,9 +31,9 @@ namespace sprout { inline SPROUT_CONSTEXPR wchar_t const* wmemchr(wchar_t const* s, wchar_t c, size_t n) { return sprout::detail::wmemchr_impl( - sprout::as_iterator_base( + sprout::ptr_unindex( sprout::find( - sprout::as_iterator(s), sprout::as_iterator(s, n), + sprout::ptr_index(s), sprout::ptr_index(s, n), c ) ), @@ -44,9 +44,9 @@ namespace sprout { inline SPROUT_CONSTEXPR wchar_t* wmemchr(wchar_t* s, wchar_t c, size_t n) { return sprout::detail::wmemchr_impl( - sprout::as_iterator_base( + sprout::ptr_unindex( sprout::find( - sprout::as_iterator(s), sprout::as_iterator(s, n), + sprout::ptr_index(s), sprout::ptr_index(s, n), c ) ), diff --git a/sprout/cwchar/wmemcmp.hpp b/sprout/cwchar/wmemcmp.hpp index edf59495..63beef5a 100644 --- a/sprout/cwchar/wmemcmp.hpp +++ b/sprout/cwchar/wmemcmp.hpp @@ -16,8 +16,8 @@ namespace sprout { inline SPROUT_CONSTEXPR int wmemcmp(wchar_t const* s1, wchar_t const* s2, std::size_t n) { return sprout::tristate_lexicographical_compare( - sprout::as_iterator(s1), sprout::as_iterator(s1, n), - sprout::as_iterator(s2), sprout::as_iterator(s2, n) + sprout::ptr_index(s1), sprout::ptr_index(s1, n), + sprout::ptr_index(s2), sprout::ptr_index(s2, n) ); } } // namespace sprout diff --git a/sprout/iterator/ptr_index_iterator.hpp b/sprout/iterator/ptr_index_iterator.hpp index d4375dd7..7397ac91 100644 --- a/sprout/iterator/ptr_index_iterator.hpp +++ b/sprout/iterator/ptr_index_iterator.hpp @@ -223,41 +223,41 @@ namespace sprout { } // - // as_iterator + // ptr_index // template inline SPROUT_CONSTEXPR sprout::ptr_index_iterator - as_iterator(T* p) { + ptr_index(T* p) { return sprout::make_ptr_index_iterator(p); } template inline SPROUT_CONSTEXPR sprout::ptr_index_iterator - as_iterator(T* p, typename std::iterator_traits::difference_type n) { + ptr_index(T* p, typename std::iterator_traits::difference_type n) { return sprout::make_ptr_index_iterator(p, n); } template inline SPROUT_CONSTEXPR Iterator - as_iterator(Iterator const& it) { + ptr_index(Iterator const& it) { return it; } template inline SPROUT_CONSTEXPR Iterator - as_iterator(Iterator const& it, typename std::iterator_traits::difference_type n) { + ptr_index(Iterator const& it, typename std::iterator_traits::difference_type n) { return sprout::next(it, n); } // - // as_iterator_base + // ptr_unindex // template inline SPROUT_CONSTEXPR typename sprout::ptr_index_iterator::pointer - as_iterator_base(sprout::ptr_index_iterator const& it) { + ptr_unindex(sprout::ptr_index_iterator const& it) { return it.ptr(); } template inline SPROUT_CONSTEXPR Iterator - as_iterator_base(Iterator const& it) { + ptr_unindex(Iterator const& it) { return it; } diff --git a/sprout/string/char_traits.hpp b/sprout/string/char_traits.hpp index 2c630de3..757ff6e4 100644 --- a/sprout/string/char_traits.hpp +++ b/sprout/string/char_traits.hpp @@ -74,8 +74,8 @@ namespace sprout { } static SPROUT_CONSTEXPR int compare(char_type const* s1, char_type const* s2, std::size_t n) { return sprout::tristate_lexicographical_compare( - sprout::as_iterator(s1), sprout::as_iterator(s1, n), char_type(), - sprout::as_iterator(s2), sprout::as_iterator(s2, n), char_type(), + sprout::ptr_index(s1), sprout::ptr_index(s1, n), char_type(), + sprout::ptr_index(s2), sprout::ptr_index(s2, n), char_type(), sprout::detail::char_traits_lt() ); } @@ -84,9 +84,9 @@ namespace sprout { } static SPROUT_CONSTEXPR char_type const* find(char_type const* s, std::size_t n, char_type const& a) { return find_impl( - sprout::as_iterator_base( + sprout::ptr_unindex( sprout::find_if( - sprout::as_iterator(s), sprout::as_iterator(s, n), + sprout::ptr_index(s), sprout::ptr_index(s, n), sprout::bind2nd(sprout::detail::char_traits_eq(), a) ) ), @@ -121,7 +121,7 @@ namespace sprout { template static SPROUT_CONSTEXPR int compare(char_type const* s1, ConstIterator s2, std::size_t n) { return sprout::tristate_lexicographical_compare( - sprout::as_iterator(s1), sprout::as_iterator(s1, n), char_type(), + sprout::ptr_index(s1), sprout::ptr_index(s1, n), char_type(), s2, s2 + n, char_type(), sprout::detail::char_traits_lt() ); @@ -130,7 +130,7 @@ namespace sprout { static SPROUT_CONSTEXPR int compare(ConstIterator s1, char_type const* s2, std::size_t n) { return sprout::tristate_lexicographical_compare( s1, s1 + n, char_type(), - sprout::as_iterator(s2), sprout::as_iterator(s2, n), char_type(), + sprout::ptr_index(s2), sprout::ptr_index(s2, n), char_type(), sprout::detail::char_traits_lt() ); } @@ -148,7 +148,7 @@ namespace sprout { } template static SPROUT_CONSTEXPR ConstIterator find(ConstIterator s, std::size_t n, char_type const& a) { - return sprout::as_iterator_base( + return sprout::ptr_unindex( sprout::find_if( s, s + n, sprout::bind2nd(sprout::detail::char_traits_eq(), a) @@ -190,9 +190,9 @@ namespace sprout { return sprout::strlen(s, n); } static SPROUT_CONSTEXPR char_type const* find(char_type const* s, std::size_t n, char_type const& a) { - return sprout::as_iterator_base( + return sprout::ptr_unindex( sprout::find_if( - sprout::as_iterator(s), sprout::as_iterator(s, n), + sprout::ptr_index(s), sprout::ptr_index(s, n), sprout::bind2nd(sprout::detail::char_traits_eq(), a) ) ); diff --git a/sprout/sub_array/sub_array.hpp b/sprout/sub_array/sub_array.hpp index 625c9e23..f97b5354 100644 --- a/sprout/sub_array/sub_array.hpp +++ b/sprout/sub_array/sub_array.hpp @@ -284,14 +284,12 @@ namespace sprout { return *sprout::next(sprout::begin(get_array()), to_first_ + i); } reference at(size_type i) { - return i < size() - ? *sprout::next(sprout::begin(get_array()), to_first_ + i) + return i < size() ? *sprout::next(sprout::begin(get_array()), to_first_ + i) : (throw std::out_of_range("sub_array<>: index out of range"), *sprout::next(sprout::begin(get_array()), to_first_ + i)) ; } SPROUT_CONSTEXPR const_reference at(size_type i) const { - return i < size() - ? *sprout::next(sprout::begin(get_array()), to_first_ + i) + return i < size() ? *sprout::next(sprout::begin(get_array()), to_first_ + i) : (throw std::out_of_range("sub_array<>: index out of range"), *sprout::next(sprout::begin(get_array()), to_first_ + i)) ; } @@ -359,6 +357,12 @@ namespace sprout { SPROUT_CONSTEXPR difference_type to_last() const { return to_last_; } + SPROUT_CONSTEXPR difference_type from_begin() const { + return to_first_; + } + SPROUT_CONSTEXPR difference_type from_end() const { + return to_last_ - sprout::size(get_array()); + } }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::sub_array::size_type sprout::sub_array::enumerable_size;