diff --git a/sprout/array/array.hpp b/sprout/array/array.hpp index 2a8ecb6e..587895d6 100644 --- a/sprout/array/array.hpp +++ b/sprout/array/array.hpp @@ -216,6 +216,46 @@ namespace sprout { throw std::out_of_range("array<>: index out of range"); } } + +#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION + SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { + return i < size() + ? iterator(*this, i) + : (throw std::out_of_range("array<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR const_iterator nth(size_type i) const { + return i < size() + ? const_iterator(*this, i) + : (throw std::out_of_range("array<>: index out of range"), const_iterator()) + ; + } + SPROUT_CONSTEXPR size_type index_of(iterator p) const SPROUT_NOEXCEPT { + return p.index(); + } + SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { + return p.index(); + } +#else + SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { + return i < size() + ? iterator(elems) + i + : (throw std::out_of_range("array<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR const_iterator nth(size_type i) const { + return i < size() + ? const_iterator(elems) + i + : (throw std::out_of_range("array<>: index out of range"), const_iterator()) + ; + } + SPROUT_CONSTEXPR size_type index_of(iterator p) const SPROUT_NOEXCEPT { + return sprout::distance(begin(), p); + } + SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { + return sprout::distance(begin(), p); + } +#endif }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::array::size_type sprout::array::static_size; diff --git a/sprout/string/string.hpp b/sprout/string/string.hpp index d4195f3f..d9e0f216 100644 --- a/sprout/string/string.hpp +++ b/sprout/string/string.hpp @@ -933,6 +933,46 @@ namespace sprout { ; } #endif + +#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION + SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { + return i < size() + ? iterator(*this, i) + : (throw std::out_of_range("basic_string<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR const_iterator nth(size_type i) const { + return i < size() + ? const_iterator(*this, i) + : (throw std::out_of_range("basic_string<>: index out of range"), const_iterator()) + ; + } + SPROUT_CONSTEXPR size_type index_of(iterator p) const SPROUT_NOEXCEPT { + return p.index(); + } + SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { + return p.index(); + } +#else + SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { + return i < size() + ? data() + i + : (throw std::out_of_range("basic_string<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR const_iterator nth(size_type i) const { + return i < size() + ? data() + i + : (throw std::out_of_range("basic_string<>: index out of range"), const_iterator()) + ; + } + SPROUT_CONSTEXPR size_type index_of(iterator p) const SPROUT_NOEXCEPT { + return sprout::distance(begin(), p); + } + SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { + return sprout::distance(begin(), p); + } +#endif }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::basic_string::size_type sprout::basic_string::npos; diff --git a/sprout/sub_array/sub_array.hpp b/sprout/sub_array/sub_array.hpp index dd9fb73c..316a5e7d 100644 --- a/sprout/sub_array/sub_array.hpp +++ b/sprout/sub_array/sub_array.hpp @@ -415,6 +415,25 @@ namespace sprout { SPROUT_CONSTEXPR difference_type from_end() const { return to_last_ - sprout::size(get_array()); } + + SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { + return i < size() + ? sprout::next(begin(), i) + : (throw std::out_of_range("sub_array<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR const_iterator nth(size_type i) const { + return i < size() + ? sprout::next(begin(), i) + : (throw std::out_of_range("array<>: index out of range"), const_iterator()) + ; + } + SPROUT_CXX14_CONSTEXPR size_type index_of(iterator p) { + return sprout::distance(begin(), p); + } + SPROUT_CONSTEXPR size_type index_of(const_iterator p) const { + return sprout::distance(begin(), p); + } }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::sub_array::size_type sprout::sub_array::enumerable_size;