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

add nth(), index_of() container member functions

This commit is contained in:
bolero-MURAKAMI 2015-03-19 23:43:13 +09:00
parent ee008e8a77
commit 6a9cda9d4c
3 changed files with 99 additions and 0 deletions

View file

@ -216,6 +216,46 @@ namespace sprout {
throw std::out_of_range("array<>: index out of range"); 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<typename T, std::size_t N> template<typename T, std::size_t N>
SPROUT_CONSTEXPR_OR_CONST typename sprout::array<T, N>::size_type sprout::array<T, N>::static_size; SPROUT_CONSTEXPR_OR_CONST typename sprout::array<T, N>::size_type sprout::array<T, N>::static_size;

View file

@ -933,6 +933,46 @@ namespace sprout {
; ;
} }
#endif #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<typename T, std::size_t N, typename Traits> template<typename T, std::size_t N, typename Traits>
SPROUT_CONSTEXPR_OR_CONST typename sprout::basic_string<T, N, Traits>::size_type sprout::basic_string<T, N, Traits>::npos; SPROUT_CONSTEXPR_OR_CONST typename sprout::basic_string<T, N, Traits>::size_type sprout::basic_string<T, N, Traits>::npos;

View file

@ -415,6 +415,25 @@ namespace sprout {
SPROUT_CONSTEXPR difference_type from_end() const { SPROUT_CONSTEXPR difference_type from_end() const {
return to_last_ - sprout::size(get_array()); 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<typename Container> template<typename Container>
SPROUT_CONSTEXPR_OR_CONST typename sprout::sub_array<Container>::size_type sprout::sub_array<Container>::enumerable_size; SPROUT_CONSTEXPR_OR_CONST typename sprout::sub_array<Container>::size_type sprout::sub_array<Container>::enumerable_size;