diff --git a/sprout/valarray/comparison.hpp b/sprout/valarray/comparison.hpp index 56b8f180..8eb704b8 100644 --- a/sprout/valarray/comparison.hpp +++ b/sprout/valarray/comparison.hpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include namespace sprout { // diff --git a/sprout/valarray/gslice_array.hpp b/sprout/valarray/gslice_array.hpp index 2ba1f267..e0c19215 100644 --- a/sprout/valarray/gslice_array.hpp +++ b/sprout/valarray/gslice_array.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -35,7 +36,11 @@ namespace sprout { SPROUT_CONSTEXPR gslice() : start_(), size_(), stride_() {} - SPROUT_CONSTEXPR gslice(std::size_t start, sprout::valarray const& size, sprout::valarray const& stride) + SPROUT_CONSTEXPR gslice( + std::size_t start, + sprout::valarray const& size, + sprout::valarray const& stride + ) : start_(start), size_(size), stride_(stride) {} SPROUT_CONSTEXPR std::size_t start() const { @@ -64,6 +69,7 @@ namespace sprout { typedef typename valarray_type::difference_type difference_type; typedef typename valarray_type::pointer pointer; typedef sprout::index_iterator iterator; + typedef sprout::reverse_iterator reverse_iterator; private: typedef sprout::gslice gslice_type; typedef sprout::valarray indexes_type; @@ -222,26 +228,51 @@ namespace sprout { end() const { return iterator(*this, size()); } + SPROUT_CONSTEXPR reverse_iterator + rbegin() const { + return reverse_iterator(end()); + } + SPROUT_CONSTEXPR reverse_iterator + rend() const { + return reverse_iterator(begin()); + } // capacity: - SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR size_type + size() const SPROUT_NOEXCEPT { return size_.front(); } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR bool + empty() const SPROUT_NOEXCEPT { return size() == 0; } // element access: - SPROUT_CONSTEXPR reference operator[](size_type i) const { + SPROUT_CONSTEXPR reference + operator[](size_type i) const { return (*arr_)[index(i)]; } - SPROUT_CONSTEXPR reference at(size_type i) const { + SPROUT_CONSTEXPR reference + at(size_type i) const { return arr_->at(index(i)); } - SPROUT_CONSTEXPR reference front() const { + SPROUT_CONSTEXPR reference + front() const { return (*this)[0]; } - SPROUT_CONSTEXPR reference back() const { + SPROUT_CONSTEXPR reference + back() const { return (*this)[size() - 1]; } + // others: + SPROUT_CONSTEXPR iterator + nth(size_type i) const { + return i < size() ? begin() + i + : (throw std::out_of_range("gslice_array<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR size_type + index_of(iterator p) const SPROUT_NOEXCEPT { + return p - begin(); + } }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::gslice_array::size_type sprout::gslice_array::static_size; diff --git a/sprout/valarray/indirect_array.hpp b/sprout/valarray/indirect_array.hpp index b72208ec..15498f84 100644 --- a/sprout/valarray/indirect_array.hpp +++ b/sprout/valarray/indirect_array.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,7 @@ namespace sprout { typedef typename valarray_type::difference_type difference_type; typedef typename valarray_type::pointer pointer; typedef sprout::index_iterator iterator; + typedef sprout::reverse_iterator reverse_iterator; private: typedef sprout::valarray indexes_type; public: @@ -160,26 +162,51 @@ namespace sprout { end() const { return iterator(*this, size()); } + SPROUT_CONSTEXPR reverse_iterator + rbegin() const { + return reverse_iterator(end()); + } + SPROUT_CONSTEXPR reverse_iterator + rend() const { + return reverse_iterator(begin()); + } // capacity: - SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR size_type + size() const SPROUT_NOEXCEPT { return indexes_.size(); } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR bool + empty() const SPROUT_NOEXCEPT { return size() == 0; } // element access: - SPROUT_CONSTEXPR reference operator[](size_type i) const { + SPROUT_CONSTEXPR reference + operator[](size_type i) const { return (*arr_)[indexes_[i]]; } - SPROUT_CONSTEXPR reference at(size_type i) const { + SPROUT_CONSTEXPR reference + at(size_type i) const { return arr_->at(indexes_.at(i)); } - SPROUT_CONSTEXPR reference front() const { + SPROUT_CONSTEXPR reference + front() const { return (*this)[0]; } - SPROUT_CONSTEXPR reference back() const { + SPROUT_CONSTEXPR reference + back() const { return (*this)[size() - 1]; } + // others: + SPROUT_CONSTEXPR iterator + nth(size_type i) const { + return i < size() ? begin() + i + : (throw std::out_of_range("indirect_array<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR size_type + index_of(iterator p) const SPROUT_NOEXCEPT { + return p - begin(); + } }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::indirect_array::size_type sprout::indirect_array::static_size; diff --git a/sprout/valarray/logical.hpp b/sprout/valarray/logical.hpp index 5a69764a..cfcba55e 100644 --- a/sprout/valarray/logical.hpp +++ b/sprout/valarray/logical.hpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include namespace sprout { // diff --git a/sprout/valarray/mask_array.hpp b/sprout/valarray/mask_array.hpp index 241839d2..b73d95a5 100644 --- a/sprout/valarray/mask_array.hpp +++ b/sprout/valarray/mask_array.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,7 @@ namespace sprout { typedef typename valarray_type::difference_type difference_type; typedef typename valarray_type::pointer pointer; typedef sprout::index_iterator iterator; + typedef sprout::reverse_iterator reverse_iterator; private: typedef sprout::valarray mask_type; typedef sprout::valarray indexes_type; @@ -168,26 +170,51 @@ namespace sprout { end() const { return iterator(*this, size()); } + SPROUT_CONSTEXPR reverse_iterator + rbegin() const { + return reverse_iterator(end()); + } + SPROUT_CONSTEXPR reverse_iterator + rend() const { + return reverse_iterator(begin()); + } // capacity: - SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR size_type + size() const SPROUT_NOEXCEPT { return indexes_.size(); } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR bool + empty() const SPROUT_NOEXCEPT { return size() == 0; } // element access: - SPROUT_CONSTEXPR reference operator[](size_type i) const { + SPROUT_CONSTEXPR reference + operator[](size_type i) const { return (*arr_)[indexes_[i]]; } - SPROUT_CONSTEXPR reference at(size_type i) const { + SPROUT_CONSTEXPR reference + at(size_type i) const { return arr_->at(indexes_.at(i)); } - SPROUT_CONSTEXPR reference front() const { + SPROUT_CONSTEXPR reference + front() const { return (*this)[0]; } - SPROUT_CONSTEXPR reference back() const { + SPROUT_CONSTEXPR reference + back() const { return (*this)[size() - 1]; } + // others: + SPROUT_CONSTEXPR iterator + nth(size_type i) const { + return i < size() ? begin() + i + : (throw std::out_of_range("mask_array<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR size_type + index_of(iterator p) const SPROUT_NOEXCEPT { + return p - begin(); + } }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::mask_array::size_type sprout::mask_array::static_size; diff --git a/sprout/valarray/slice_array.hpp b/sprout/valarray/slice_array.hpp index 9b65d804..a06f5700 100644 --- a/sprout/valarray/slice_array.hpp +++ b/sprout/valarray/slice_array.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -58,6 +59,7 @@ namespace sprout { typedef typename valarray_type::difference_type difference_type; typedef typename valarray_type::pointer pointer; typedef sprout::index_iterator iterator; + typedef sprout::reverse_iterator reverse_iterator; public: SPROUT_STATIC_CONSTEXPR size_type static_size = valarray_type::static_size; private: @@ -183,26 +185,51 @@ namespace sprout { end() const { return iterator(*this, size()); } + SPROUT_CONSTEXPR reverse_iterator + rbegin() const { + return reverse_iterator(end()); + } + SPROUT_CONSTEXPR reverse_iterator + rend() const { + return reverse_iterator(begin()); + } // capacity: - SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR size_type + size() const SPROUT_NOEXCEPT { return slice_.size(); } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR bool + empty() const SPROUT_NOEXCEPT { return size() == 0; } // element access: - SPROUT_CONSTEXPR reference operator[](size_type i) const { + SPROUT_CONSTEXPR reference + operator[](size_type i) const { return (*arr_)[slice_.start() + slice_.stride() * i]; } - SPROUT_CONSTEXPR reference at(size_type i) const { + SPROUT_CONSTEXPR reference + at(size_type i) const { return arr_->at(slice_.start() + slice_.stride() * i); } - SPROUT_CONSTEXPR reference front() const { + SPROUT_CONSTEXPR reference + front() const { return (*this)[0]; } - SPROUT_CONSTEXPR reference back() const { + SPROUT_CONSTEXPR reference + back() const { return (*this)[size() - 1]; } + // others: + SPROUT_CONSTEXPR iterator + nth(size_type i) const { + return i < size() ? begin() + i + : (throw std::out_of_range("slice_array<>: index out of range"), iterator()) + ; + } + SPROUT_CONSTEXPR size_type + index_of(iterator p) const SPROUT_NOEXCEPT { + return p - begin(); + } }; template SPROUT_CONSTEXPR_OR_CONST typename sprout::slice_array::size_type sprout::slice_array::static_size; diff --git a/sprout/valarray/valarray.hpp b/sprout/valarray/valarray.hpp index d70179cb..84a449cc 100644 --- a/sprout/valarray/valarray.hpp +++ b/sprout/valarray/valarray.hpp @@ -53,6 +53,7 @@ #include #include #include +#include namespace sprout { namespace detail { @@ -453,119 +454,152 @@ namespace sprout { } // modifiers (array): - SPROUT_CXX14_CONSTEXPR void fill(const_reference value) { + SPROUT_CXX14_CONSTEXPR void + fill(const_reference value) { impl_.array_.fill(value); } - SPROUT_CONSTEXPR valarray fill(const_reference value) const { + SPROUT_CONSTEXPR valarray + fill(const_reference value) const { return valarray(value, size()); } - SPROUT_CXX14_CONSTEXPR void assign(const_reference value) { + SPROUT_CXX14_CONSTEXPR void + assign(const_reference value) { impl_.array_.assign(value); } - SPROUT_CONSTEXPR valarray assign(const_reference value) const { + SPROUT_CONSTEXPR valarray + assign(const_reference value) const { return valarray(value, size()); } // iterators (array): - SPROUT_CXX14_CONSTEXPR iterator begin() SPROUT_NOEXCEPT { + SPROUT_CXX14_CONSTEXPR iterator + begin() SPROUT_NOEXCEPT { return impl_.array_.begin(); } - SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_iterator + begin() const SPROUT_NOEXCEPT { return impl_.array_.begin(); } - SPROUT_CXX14_CONSTEXPR iterator end() SPROUT_NOEXCEPT { + SPROUT_CXX14_CONSTEXPR iterator + end() SPROUT_NOEXCEPT { return impl_.array_.begin() + size(); } - SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_iterator + end() const SPROUT_NOEXCEPT { return impl_.array_.begin() + size(); } - SPROUT_CXX14_CONSTEXPR reverse_iterator rbegin() SPROUT_NOEXCEPT { + SPROUT_CXX14_CONSTEXPR reverse_iterator + rbegin() SPROUT_NOEXCEPT { return impl_.array_.rend() - size(); } - SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_reverse_iterator + rbegin() const SPROUT_NOEXCEPT { return impl_.array_.rend() - size(); } - SPROUT_CXX14_CONSTEXPR reverse_iterator rend() SPROUT_NOEXCEPT { + SPROUT_CXX14_CONSTEXPR reverse_iterator + rend() SPROUT_NOEXCEPT { return impl_.array_.rend(); } - SPROUT_CONSTEXPR const_reverse_iterator rend() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_reverse_iterator + rend() const SPROUT_NOEXCEPT { return impl_.array_.rend(); } - SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_iterator + cbegin() const SPROUT_NOEXCEPT { return impl_.array_.cbegin(); } - SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_iterator + cend() const SPROUT_NOEXCEPT { return impl_.array_.cbegin() + size(); } - SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_reverse_iterator + crbegin() const SPROUT_NOEXCEPT { return impl_.array_.crend() - size(); } - SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_reverse_iterator + crend() const SPROUT_NOEXCEPT { return impl_.array_.crend(); } // capacity (array): - SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR size_type + max_size() const SPROUT_NOEXCEPT { return impl_.array_.max_size(); } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR bool + empty() const SPROUT_NOEXCEPT { return size() != 0; } // element access (array): - SPROUT_CXX14_CONSTEXPR reference at(size_type i) { + SPROUT_CXX14_CONSTEXPR reference + at(size_type i) { return i < size() ? (*this)[i] : (throw std::out_of_range("valarray<>: index out of range"), (*this)[i]) ; } - SPROUT_CONSTEXPR const_reference at(size_type i) const { + SPROUT_CONSTEXPR const_reference + at(size_type i) const { return i < size() ? (*this)[i] : (throw std::out_of_range("valarray<>: index out of range"), (*this)[i]) ; } - SPROUT_CXX14_CONSTEXPR reference front() { + SPROUT_CXX14_CONSTEXPR reference + front() { return impl_.array_.front(); } - SPROUT_CONSTEXPR const_reference front() const { + SPROUT_CONSTEXPR const_reference + front() const { return impl_.array_.front(); } - SPROUT_CXX14_CONSTEXPR reference back() { + SPROUT_CXX14_CONSTEXPR reference + back() { return (*this)[size() - 1]; } - SPROUT_CONSTEXPR const_reference back() const { + SPROUT_CONSTEXPR const_reference + back() const { return (*this)[size() - 1]; } - SPROUT_CXX14_CONSTEXPR pointer data() SPROUT_NOEXCEPT { + SPROUT_CXX14_CONSTEXPR pointer + data() SPROUT_NOEXCEPT { return impl_.array_.data(); } - SPROUT_CONSTEXPR const_pointer data() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_pointer + data() const SPROUT_NOEXCEPT { return impl_.array_.data(); } - SPROUT_CXX14_CONSTEXPR pointer c_array() SPROUT_NOEXCEPT { + SPROUT_CXX14_CONSTEXPR pointer + c_array() SPROUT_NOEXCEPT { return impl_.array_.c_array(); } - SPROUT_CONSTEXPR const_pointer c_array() const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR const_pointer + c_array() const SPROUT_NOEXCEPT { return impl_.array_.c_array(); } // others (array): - SPROUT_CXX14_CONSTEXPR void rangecheck(size_type i) const { + SPROUT_CXX14_CONSTEXPR void + rangecheck(size_type i) const { return i >= size() ? throw std::out_of_range("valarray<>: index out of range") : (void)0 ; } - SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { + SPROUT_CXX14_CONSTEXPR iterator + nth(size_type i) { return i < size() ? begin() + i : (throw std::out_of_range("valarray<>: index out of range"), iterator()) ; } - SPROUT_CONSTEXPR const_iterator nth(size_type i) const { + SPROUT_CONSTEXPR const_iterator + nth(size_type i) const { return i < size() ? begin() + i : (throw std::out_of_range("valarray<>: index out of range"), const_iterator()) ; } - SPROUT_CXX14_CONSTEXPR size_type index_of(iterator p) SPROUT_NOEXCEPT { + SPROUT_CXX14_CONSTEXPR size_type + index_of(iterator p) SPROUT_NOEXCEPT { return impl_.array_.index_of(p); } - SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { + SPROUT_CONSTEXPR size_type + index_of(const_iterator p) const SPROUT_NOEXCEPT { return impl_.array_.index_of(p); } };