From 1b8c051008d64925a0d384175970e246d891e200 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sun, 3 Apr 2016 13:12:14 +0900 Subject: [PATCH] add valarray --- sprout/detail/algorithm/mask_index.hpp | 2 +- sprout/functional/base.hpp | 1 + sprout/functional/shift_right.hpp | 2 +- sprout/functional/transparent.hpp | 24 + sprout/numeric/fixed/partial_sum.hpp | 2 +- sprout/string/container.hpp | 2 +- sprout/string/string.hpp | 2 +- sprout/valarray.hpp | 31 ++ sprout/valarray/arithmetic.hpp | 116 +++++ sprout/valarray/bitwise.hpp | 116 +++++ sprout/valarray/comparison.hpp | 131 +++++ sprout/valarray/container.hpp | 63 +++ sprout/valarray/exponential.hpp | 260 ++++++++++ sprout/valarray/gslice_array.hpp | 261 ++++++++++ sprout/valarray/hash.hpp | 45 ++ sprout/valarray/hyperbolic.hpp | 202 ++++++++ sprout/valarray/indirect_array.hpp | 199 ++++++++ sprout/valarray/logical.hpp | 59 +++ sprout/valarray/make_valarray.hpp | 73 +++ sprout/valarray/mask_array.hpp | 207 ++++++++ sprout/valarray/power.hpp | 234 +++++++++ sprout/valarray/slice_array.hpp | 222 ++++++++ sprout/valarray/trigonometric.hpp | 240 +++++++++ sprout/valarray/tuple.hpp | 70 +++ sprout/valarray/type_traits.hpp | 43 ++ sprout/valarray/valarray.hpp | 672 +++++++++++++++++++++++++ sprout/valarray/valarray_fwd.hpp | 56 +++ 27 files changed, 3330 insertions(+), 5 deletions(-) create mode 100644 sprout/functional/transparent.hpp create mode 100644 sprout/valarray.hpp create mode 100644 sprout/valarray/arithmetic.hpp create mode 100644 sprout/valarray/bitwise.hpp create mode 100644 sprout/valarray/comparison.hpp create mode 100644 sprout/valarray/container.hpp create mode 100644 sprout/valarray/exponential.hpp create mode 100644 sprout/valarray/gslice_array.hpp create mode 100644 sprout/valarray/hash.hpp create mode 100644 sprout/valarray/hyperbolic.hpp create mode 100644 sprout/valarray/indirect_array.hpp create mode 100644 sprout/valarray/logical.hpp create mode 100644 sprout/valarray/make_valarray.hpp create mode 100644 sprout/valarray/mask_array.hpp create mode 100644 sprout/valarray/power.hpp create mode 100644 sprout/valarray/slice_array.hpp create mode 100644 sprout/valarray/trigonometric.hpp create mode 100644 sprout/valarray/tuple.hpp create mode 100644 sprout/valarray/type_traits.hpp create mode 100644 sprout/valarray/valarray.hpp create mode 100644 sprout/valarray/valarray_fwd.hpp diff --git a/sprout/detail/algorithm/mask_index.hpp b/sprout/detail/algorithm/mask_index.hpp index a02a207d..5d175d2f 100644 --- a/sprout/detail/algorithm/mask_index.hpp +++ b/sprout/detail/algorithm/mask_index.hpp @@ -27,7 +27,7 @@ namespace sprout { >::type mask_index_impl(InputIterator const&, InputIterator const&, Result const& result, typename sprout::container_traits::size_type, - typename sprout::container_traits::difference_type n, + typename sprout::container_traits::difference_type, Args const&... args ) { diff --git a/sprout/functional/base.hpp b/sprout/functional/base.hpp index dae2cf6a..208cc4d6 100644 --- a/sprout/functional/base.hpp +++ b/sprout/functional/base.hpp @@ -11,5 +11,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_FUNCTIONAL_BASE_HPP diff --git a/sprout/functional/shift_right.hpp b/sprout/functional/shift_right.hpp index 2cc21ffc..36a017ec 100644 --- a/sprout/functional/shift_right.hpp +++ b/sprout/functional/shift_right.hpp @@ -26,7 +26,7 @@ namespace sprout { template SPROUT_CONSTEXPR decltype(std::declval() >> std::declval()) operator()(T&& x, U&& y) - const SPROUT_NOEXCEPT_IF_EXPR(std::declval() << std::declval()) + const SPROUT_NOEXCEPT_IF_EXPR(std::declval() >> std::declval()) { return SPROUT_FORWARD(T, x) >> SPROUT_FORWARD(U, y); } diff --git a/sprout/functional/transparent.hpp b/sprout/functional/transparent.hpp new file mode 100644 index 00000000..7ead7cb8 --- /dev/null +++ b/sprout/functional/transparent.hpp @@ -0,0 +1,24 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_FUNCTIONAL_TRANSPARENT_HPP +#define SPROUT_FUNCTIONAL_TRANSPARENT_HPP + +#include + +namespace sprout { + // + // transparent + // + template + struct transparent { + public: + typedef T is_transparent; + }; +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_TRANSPARENT_HPP diff --git a/sprout/numeric/fixed/partial_sum.hpp b/sprout/numeric/fixed/partial_sum.hpp index c499b34a..a8c79782 100644 --- a/sprout/numeric/fixed/partial_sum.hpp +++ b/sprout/numeric/fixed/partial_sum.hpp @@ -123,7 +123,7 @@ namespace sprout { ) { return first != last && sizeof...(Args) + 1 < size - ? partial_sum_impl_1(sprout::next(first), last, result, size, binary_op(value, *first), args..., value) + ? partial_sum_impl_1(sprout::next(first), last, result, binary_op, size, binary_op(value, *first), args..., value) : sprout::detail::container_complate(result, args..., value) ; } diff --git a/sprout/string/container.hpp b/sprout/string/container.hpp index e2f142c8..57a0d40e 100644 --- a/sprout/string/container.hpp +++ b/sprout/string/container.hpp @@ -37,7 +37,7 @@ namespace sprout { static SPROUT_CONSTEXPR copied_type remake(Cont&&, typename sprout::container_traits >::difference_type size, Args&&... args) { typedef sprout::detail::make_construct_impl impl_type; - return impl_type::make(static_cast(size), SPROUT_FORWARD(Args, args)...); + return impl_type::sized_make(static_cast(size), SPROUT_FORWARD(Args, args)...); } }; diff --git a/sprout/string/string.hpp b/sprout/string/string.hpp index 2d0add47..807b0fa9 100644 --- a/sprout/string/string.hpp +++ b/sprout/string/string.hpp @@ -1230,7 +1230,7 @@ namespace sprout { } template static SPROUT_CONSTEXPR copied_type - make(typename copied_type::size_type size, Args&&... args) { + sized_make(typename copied_type::size_type size, Args&&... args) { typedef sprout::detail::string_construct_access access_type; return access_type::raw_construct(size, SPROUT_FORWARD(Args, args)...); } diff --git a/sprout/valarray.hpp b/sprout/valarray.hpp new file mode 100644 index 00000000..08f66bd2 --- /dev/null +++ b/sprout/valarray.hpp @@ -0,0 +1,31 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_HPP +#define SPROUT_VALARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif // #ifndef SPROUT_VALARRAY_HPP diff --git a/sprout/valarray/arithmetic.hpp b/sprout/valarray/arithmetic.hpp new file mode 100644 index 00000000..1d292175 --- /dev/null +++ b/sprout/valarray/arithmetic.hpp @@ -0,0 +1,116 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_ARITHMETIC_HPP +#define SPROUT_VALARRAY_ARITHMETIC_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // operator+ + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator+(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::plus<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator+(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::plus<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator+(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::plus<>(), lhs)); + } + // + // operator- + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator-(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::minus<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator-(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::minus<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator-(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::minus<>(), lhs)); + } + // + // operator* + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator*(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::multiplies<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator*(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::multiplies<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator*(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::multiplies<>(), lhs)); + } + // + // operator/ + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator/(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::divides<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator/(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::divides<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator/(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::divides<>(), lhs)); + } + // + // operator% + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator%(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::modulus<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator%(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::modulus<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator%(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::modulus<>(), lhs)); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_ARITHMETIC_HPP diff --git a/sprout/valarray/bitwise.hpp b/sprout/valarray/bitwise.hpp new file mode 100644 index 00000000..6c88dcdc --- /dev/null +++ b/sprout/valarray/bitwise.hpp @@ -0,0 +1,116 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_BITWISE_HPP +#define SPROUT_VALARRAY_BITWISE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // operator& + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator&(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::bit_and<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator&(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::bit_and<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator&(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::bit_and<>(), lhs)); + } + // + // operator| + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator|(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::bit_or<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator|(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::bit_or<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator|(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::bit_or<>(), lhs)); + } + // + // operator^ + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator^(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::bit_xor<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator^(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::bit_xor<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator^(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::bit_xor<>(), lhs)); + } + // + // operator<< + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<<(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::shift_left<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<<(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::shift_left<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<<(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::shift_left<>(), lhs)); + } + // + // operator>> + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>>(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs, sprout::shift_right<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>>(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), lhs, sprout::bind2nd(sprout::shift_right<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>>(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), rhs, sprout::bind1st(sprout::shift_right<>(), lhs)); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_BITWISE_HPP diff --git a/sprout/valarray/comparison.hpp b/sprout/valarray/comparison.hpp new file mode 100644 index 00000000..21f80f83 --- /dev/null +++ b/sprout/valarray/comparison.hpp @@ -0,0 +1,131 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_COMPARISON_HPP +#define SPROUT_VALARRAY_COMPARISON_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // operator== + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator==(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), sprout::valarray(lhs.size()), sprout::equal_to<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator==(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), sprout::valarray(lhs.size()), sprout::bind2nd(sprout::equal_to<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator==(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), sprout::valarray(rhs.size()), sprout::bind1st(sprout::equal_to<>(), lhs)); + } + // + // operator!= + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator!=(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return !(lhs == rhs); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator!=(sprout::valarray const& lhs, T const& rhs) { + return !(lhs == rhs); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator!=(T const& lhs, sprout::valarray const& rhs) { + return !(lhs == rhs); + } + // + // operator< + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), sprout::valarray(lhs.size()), sprout::less<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), sprout::valarray(lhs.size()), sprout::bind2nd(sprout::less<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), sprout::valarray(rhs.size()), sprout::bind1st(sprout::less<>(), lhs)); + } + // + // operator> + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return rhs < lhs; + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>(sprout::valarray const& lhs, T const& rhs) { + return rhs < lhs; + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>(T const& lhs, sprout::valarray const& rhs) { + return rhs < lhs; + } + // + // operator<= + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<=(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return !(rhs < lhs); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<=(sprout::valarray const& lhs, T const& rhs) { + return !(rhs < lhs); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator<=(T const& lhs, sprout::valarray const& rhs) { + return !(rhs < lhs); + } + // + // operator>= + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>=(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return !(lhs < rhs); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>=(sprout::valarray const& lhs, T const& rhs) { + return !(lhs < rhs); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator>=(T const& lhs, sprout::valarray const& rhs) { + return !(lhs < rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_COMPARISON_HPP diff --git a/sprout/valarray/container.hpp b/sprout/valarray/container.hpp new file mode 100644 index 00000000..0f3ba204 --- /dev/null +++ b/sprout/valarray/container.hpp @@ -0,0 +1,63 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_CONTAINER_HPP +#define SPROUT_VALARRAY_CONTAINER_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // container_construct_traits + // + template + struct container_construct_traits > { + public: + typedef sprout::valarray copied_type; + public: + template + static SPROUT_CONSTEXPR copied_type + deep_copy(Cont&& cont) { + return SPROUT_FORWARD(Cont, cont); + } + template + static SPROUT_CONSTEXPR copied_type + make(Args&&... args) { + typedef sprout::detail::make_construct_impl impl_type; + return impl_type::make(SPROUT_FORWARD(Args, args)...); + } + template + static SPROUT_CONSTEXPR copied_type + remake(Cont&&, typename sprout::container_traits >::difference_type size, Args&&... args) { + typedef sprout::detail::make_construct_impl impl_type; + return impl_type::sized_make(static_cast(size), SPROUT_FORWARD(Args, args)...); + } + }; + + // + // container_transform_traits + // + template + struct container_transform_traits > { + public: + template >::size_type Size> + struct rebind_size { + public: + typedef sprout::valarray type; + }; + template + struct rebind_type { + public: + typedef sprout::valarray type; + }; + }; +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_CONTAINER_HPP diff --git a/sprout/valarray/exponential.hpp b/sprout/valarray/exponential.hpp new file mode 100644 index 00000000..f7acfc29 --- /dev/null +++ b/sprout/valarray/exponential.hpp @@ -0,0 +1,260 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_EXPONENTIAL_HPP +#define SPROUT_VALARRAY_EXPONENTIAL_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout_math_detail { + using sprout::math::exp; + using sprout::math::exp10; + using sprout::math::exp2; + using sprout::math::expm1; + using sprout::math::log; + using sprout::math::log10; + using sprout::math::log1p; + using sprout::math::log2; + + template + inline SPROUT_CONSTEXPR decltype(exp(std::declval())) + call_exp(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(exp(std::declval())) + { + return exp(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(exp10(std::declval())) + call_exp10(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(exp10(std::declval())) + { + return exp10(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(exp2(std::declval())) + call_exp2(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(exp2(std::declval())) + { + return exp2(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(expm1(std::declval())) + call_expm1(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(expm1(std::declval())) + { + return expm1(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(log(std::declval())) + call_log(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(log(std::declval())) + { + return log(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(log10(std::declval())) + call_log10(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(log10(std::declval())) + { + return log10(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(log1p(std::declval())) + call_log1p(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(log1p(std::declval())) + { + return log1p(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(log2(std::declval())) + call_log2(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(log2(std::declval())) + { + return log2(SPROUT_FORWARD(T, x)); + } +} // namespace sprout + +namespace sprout { + namespace detail { + struct exp_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_exp(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_exp(std::declval())) + { + return sprout_math_detail::call_exp(SPROUT_FORWARD(T, x)); + } + }; + struct exp10_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_exp10(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_exp10(std::declval())) + { + return sprout_math_detail::call_exp10(SPROUT_FORWARD(T, x)); + } + }; + struct exp2_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_exp2(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_exp2(std::declval())) + { + return sprout_math_detail::call_exp2(SPROUT_FORWARD(T, x)); + } + }; + struct expm1_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_expm1(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_expm1(std::declval())) + { + return sprout_math_detail::call_expm1(SPROUT_FORWARD(T, x)); + } + }; + struct log_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_log(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_log(std::declval())) + { + return sprout_math_detail::call_log(SPROUT_FORWARD(T, x)); + } + }; + struct log10_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_log10(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_log10(std::declval())) + { + return sprout_math_detail::call_log10(SPROUT_FORWARD(T, x)); + } + }; + struct log1p_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_log1p(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_log1p(std::declval())) + { + return sprout_math_detail::call_log1p(SPROUT_FORWARD(T, x)); + } + }; + struct log2_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_log2(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_log2(std::declval())) + { + return sprout_math_detail::call_log2(SPROUT_FORWARD(T, x)); + } + }; + } // namespace detail + + // + // exp + // + template + inline SPROUT_CONSTEXPR sprout::valarray + exp(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::exp_f()); + } + // + // exp10 + // + template + inline SPROUT_CONSTEXPR sprout::valarray + exp10(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::exp10_f()); + } + // + // exp2 + // + template + inline SPROUT_CONSTEXPR sprout::valarray + exp2(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::exp2_f()); + } + // + // expm1 + // + template + inline SPROUT_CONSTEXPR sprout::valarray + expm1(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::expm1_f()); + } + // + // log + // + template + inline SPROUT_CONSTEXPR sprout::valarray + log(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::log_f()); + } + // + // log10 + // + template + inline SPROUT_CONSTEXPR sprout::valarray + log10(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::log10_f()); + } + // + // log1p + // + template + inline SPROUT_CONSTEXPR sprout::valarray + log1p(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::log1p_f()); + } + // + // log2 + // + template + inline SPROUT_CONSTEXPR sprout::valarray + log2(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::log2_f()); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_EXPONENTIAL_HPP diff --git a/sprout/valarray/gslice_array.hpp b/sprout/valarray/gslice_array.hpp new file mode 100644 index 00000000..2ba1f267 --- /dev/null +++ b/sprout/valarray/gslice_array.hpp @@ -0,0 +1,261 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_GSLICE_ARRAY_HPP +#define SPROUT_VALARRAY_GSLICE_ARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // gslice + // + template + class gslice { + private: + std::size_t start_; + sprout::valarray size_; + sprout::valarray stride_; + public: + SPROUT_CONSTEXPR gslice() + : start_(), size_(), 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 { + return start_; + } + SPROUT_CONSTEXPR sprout::valarray size() const { + return size_; + } + SPROUT_CONSTEXPR sprout::valarray stride() const { + return stride_; + } + }; + + // + // gslice_array + // + template + class gslice_array { + friend class sprout::valarray; + private: + typedef sprout::valarray valarray_type; + public: + typedef typename valarray_type::value_type value_type; + typedef typename valarray_type::reference reference; + typedef typename valarray_type::size_type size_type; + typedef typename valarray_type::difference_type difference_type; + typedef typename valarray_type::pointer pointer; + typedef sprout::index_iterator iterator; + private: + typedef sprout::gslice gslice_type; + typedef sprout::valarray indexes_type; + public: + SPROUT_STATIC_CONSTEXPR size_type static_size = valarray_type::static_size; + private: + sprout::value_holder arr_; + std::size_t start_; + sprout::valarray size_; + sprout::valarray stride_; + private: + gslice_array() = delete; + SPROUT_CONSTEXPR gslice_array(sprout::valarray& arr, gslice_type const& gslice) + : arr_(arr) + , start_(gslice.start()) + , size_( + sprout::range::fixed::partial_sum(gslice.size() | sprout::adaptors::reversed, gslice.size(), sprout::multiplies<>()) + | sprout::adaptors::reversed | sprout::adaptors::copied(gslice.size()) + ) + , stride_(gslice.stride()) + {} + template + SPROUT_CONSTEXPR size_type index_impl( + size_type i, + InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2 + ) const + { + return first1 != last1 ? *first2 * (i / *first1) + index_impl( + i % *first1, + sprout::next(first1), last1, + sprout::next(first2) + ) + : *first2 * i + ; + } + SPROUT_CONSTEXPR size_type index(size_type i) const { + return i < size() ? start_ + index_impl(i, sprout::next(size_.begin()), size_.end(), stride_.begin()) + : arr_->max_size() + ; + } + public: + SPROUT_CONSTEXPR gslice_array(gslice_array const& other) + : arr_(other.arr_), start_(other.start_), size_(other.size_), stride_(other.stride_) + {} + template + SPROUT_CXX14_CONSTEXPR gslice_array const& + operator=(gslice_array const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + return *this; + } + template + SPROUT_CXX14_CONSTEXPR void + operator=(sprout::valarray const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + } + SPROUT_CXX14_CONSTEXPR void + operator=(T const& value) const { + sprout::fill(begin(), end(), value); + } + template + SPROUT_CXX14_CONSTEXPR void + operator+=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 += *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator-=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 -= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator*=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 *= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator/=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 /= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator%=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 %= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator&=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 &= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator|=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 |= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator^=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 ^= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator<<=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 <<= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator>>=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 >>= *it; + } + } + + SPROUT_CXX14_CONSTEXPR void + swap(gslice_array& other) SPROUT_NOEXCEPT { + sprout::swap(arr_, other.arr_); + sprout::swap(start_, other.start_); + sprout::swap(size_, other.size_); + sprout::swap(stride_, other.stride_); + } + + // iterators: + SPROUT_CONSTEXPR iterator + begin() const { + return iterator(*this, 0); + } + SPROUT_CONSTEXPR iterator + end() const { + return iterator(*this, size()); + } + // capacity: + SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + return size_.front(); + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return size() == 0; + } + // element access: + SPROUT_CONSTEXPR reference operator[](size_type i) const { + return (*arr_)[index(i)]; + } + SPROUT_CONSTEXPR reference at(size_type i) const { + return arr_->at(index(i)); + } + SPROUT_CONSTEXPR reference front() const { + return (*this)[0]; + } + SPROUT_CONSTEXPR reference back() const { + return (*this)[size() - 1]; + } + }; + template + SPROUT_CONSTEXPR_OR_CONST typename sprout::gslice_array::size_type sprout::gslice_array::static_size; + + // + // swap + // + template + inline SPROUT_CXX14_CONSTEXPR void + swap(sprout::gslice_array& lhs, sprout::gslice_array& rhs) + SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) + { + lhs.swap(rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_GSLICE_ARRAY_HPP diff --git a/sprout/valarray/hash.hpp b/sprout/valarray/hash.hpp new file mode 100644 index 00000000..2ea9ff25 --- /dev/null +++ b/sprout/valarray/hash.hpp @@ -0,0 +1,45 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_HASH_HPP +#define SPROUT_VALARRAY_HASH_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + // + // hash_value + // + template + inline SPROUT_CONSTEXPR std::size_t + hash_value(sprout::valarray const& v) { + return sprout::hash_range(v); + } +} // namespace sprout + +namespace std { +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wmismatched-tags" +#endif + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +} // namespace std + +#endif // #ifndef SPROUT_VALARRAY_HASH_HPP diff --git a/sprout/valarray/hyperbolic.hpp b/sprout/valarray/hyperbolic.hpp new file mode 100644 index 00000000..518fdec5 --- /dev/null +++ b/sprout/valarray/hyperbolic.hpp @@ -0,0 +1,202 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_HYPERBOLIC_HPP +#define SPROUT_VALARRAY_HYPERBOLIC_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout_math_detail { + using sprout::math::acosh; + using sprout::math::asinh; + using sprout::math::atanh; + using sprout::math::cosh; + using sprout::math::sinh; + using sprout::math::tanh; + + template + inline SPROUT_CONSTEXPR decltype(acosh(std::declval())) + call_acosh(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(acosh(std::declval())) + { + return acosh(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(asinh(std::declval())) + call_asinh(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(asinh(std::declval())) + { + return asinh(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(atanh(std::declval())) + call_atanh(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(atanh(std::declval())) + { + return atanh(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(cosh(std::declval())) + call_cosh(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(cosh(std::declval())) + { + return cosh(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(sinh(std::declval())) + call_sinh(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sinh(std::declval())) + { + return sinh(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(tanh(std::declval())) + call_tanh(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(tanh(std::declval())) + { + return tanh(SPROUT_FORWARD(T, x)); + } +} // namespace sprout + +namespace sprout { + namespace detail { + struct acosh_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_acosh(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_acosh(std::declval())) + { + return sprout_math_detail::call_acosh(SPROUT_FORWARD(T, x)); + } + }; + struct asinh_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_asinh(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_asinh(std::declval())) + { + return sprout_math_detail::call_asinh(SPROUT_FORWARD(T, x)); + } + }; + struct atanh_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_atanh(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_atanh(std::declval())) + { + return sprout_math_detail::call_atanh(SPROUT_FORWARD(T, x)); + } + }; + struct cosh_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_cosh(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_cosh(std::declval())) + { + return sprout_math_detail::call_cosh(SPROUT_FORWARD(T, x)); + } + }; + struct sinh_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_sinh(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_sinh(std::declval())) + { + return sprout_math_detail::call_sinh(SPROUT_FORWARD(T, x)); + } + }; + struct tanh_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_tanh(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_tanh(std::declval())) + { + return sprout_math_detail::call_tanh(SPROUT_FORWARD(T, x)); + } + }; + } // namespace detail + + // + // acosh + // + template + inline SPROUT_CONSTEXPR sprout::valarray + acosh(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::acosh_f()); + } + // + // asinh + // + template + inline SPROUT_CONSTEXPR sprout::valarray + asinh(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::asinh_f()); + } + // + // atanh + // + template + inline SPROUT_CONSTEXPR sprout::valarray + atanh(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::atanh_f()); + } + // + // cosh + // + template + inline SPROUT_CONSTEXPR sprout::valarray + cosh(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::cosh_f()); + } + // + // sinh + // + template + inline SPROUT_CONSTEXPR sprout::valarray + sinh(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::sinh_f()); + } + // + // tanh + // + template + inline SPROUT_CONSTEXPR sprout::valarray + tanh(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::tanh_f()); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_HYPERBOLIC_HPP diff --git a/sprout/valarray/indirect_array.hpp b/sprout/valarray/indirect_array.hpp new file mode 100644 index 00000000..b72208ec --- /dev/null +++ b/sprout/valarray/indirect_array.hpp @@ -0,0 +1,199 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_INDIRECT_ARRAY_HPP +#define SPROUT_VALARRAY_INDIRECT_ARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // indirect_array + // + template + class indirect_array { + friend class sprout::valarray; + private: + typedef sprout::valarray valarray_type; + public: + typedef typename valarray_type::value_type value_type; + typedef typename valarray_type::reference reference; + typedef typename valarray_type::size_type size_type; + typedef typename valarray_type::difference_type difference_type; + typedef typename valarray_type::pointer pointer; + typedef sprout::index_iterator iterator; + private: + typedef sprout::valarray indexes_type; + public: + SPROUT_STATIC_CONSTEXPR size_type static_size = valarray_type::static_size; + private: + sprout::value_holder arr_; + indexes_type indexes_; + private: + indirect_array() = delete; + SPROUT_CONSTEXPR indirect_array(sprout::valarray& arr, indexes_type const& indexes) + : arr_(arr) + , indexes_(indexes) + {} + public: + SPROUT_CONSTEXPR indirect_array(indirect_array const& other) + : arr_(other.arr_), indexes_(other.indexes_) + {} + template + SPROUT_CXX14_CONSTEXPR indirect_array const& + operator=(indirect_array const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + return *this; + } + template + SPROUT_CXX14_CONSTEXPR void + operator=(sprout::valarray const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + } + SPROUT_CXX14_CONSTEXPR void + operator=(T const& value) const { + sprout::fill(begin(), end(), value); + } + template + SPROUT_CXX14_CONSTEXPR void + operator+=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 += *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator-=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 -= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator*=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 *= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator/=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 /= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator%=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 %= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator&=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 &= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator|=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 |= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator^=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 ^= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator<<=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 <<= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator>>=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 >>= *it; + } + } + + SPROUT_CXX14_CONSTEXPR void + swap(indirect_array& other) SPROUT_NOEXCEPT { + sprout::swap(arr_, other.arr_); + sprout::swap(indexes_, other.indexes_); + } + + // iterators: + SPROUT_CONSTEXPR iterator + begin() const { + return iterator(*this, 0); + } + SPROUT_CONSTEXPR iterator + end() const { + return iterator(*this, size()); + } + // capacity: + SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + return indexes_.size(); + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return size() == 0; + } + // element access: + SPROUT_CONSTEXPR reference operator[](size_type i) const { + return (*arr_)[indexes_[i]]; + } + SPROUT_CONSTEXPR reference at(size_type i) const { + return arr_->at(indexes_.at(i)); + } + SPROUT_CONSTEXPR reference front() const { + return (*this)[0]; + } + SPROUT_CONSTEXPR reference back() const { + return (*this)[size() - 1]; + } + }; + template + SPROUT_CONSTEXPR_OR_CONST typename sprout::indirect_array::size_type sprout::indirect_array::static_size; + + // + // swap + // + template + inline SPROUT_CXX14_CONSTEXPR void + swap(sprout::indirect_array& lhs, sprout::indirect_array& rhs) + SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) + { + lhs.swap(rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_INDIRECT_ARRAY_HPP diff --git a/sprout/valarray/logical.hpp b/sprout/valarray/logical.hpp new file mode 100644 index 00000000..ddc3ba7e --- /dev/null +++ b/sprout/valarray/logical.hpp @@ -0,0 +1,59 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_LOGICAL_HPP +#define SPROUT_VALARRAY_LOGICAL_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // operator&& + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator&&(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), sprout::valarray(lhs.size()), sprout::logical_and<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator&&(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), sprout::valarray(lhs.size()), sprout::bind2nd(sprout::logical_and<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator&&(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), sprout::valarray(rhs.size()), sprout::bind1st(sprout::logical_and<>(), lhs)); + } + // + // operator|| + // + template + inline SPROUT_CONSTEXPR sprout::valarray + operator||(sprout::valarray const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), rhs.begin(), sprout::valarray(lhs.size()), sprout::logical_or<>()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator||(sprout::valarray const& lhs, T const& rhs) { + return sprout::fixed::transform(lhs.begin(), lhs.end(), sprout::valarray(lhs.size()), sprout::bind2nd(sprout::logical_or<>(), rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + operator||(T const& lhs, sprout::valarray const& rhs) { + return sprout::fixed::transform(rhs.begin(), rhs.end(), sprout::valarray(rhs.size()), sprout::bind1st(sprout::logical_or<>(), lhs)); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_LOGICAL_HPP diff --git a/sprout/valarray/make_valarray.hpp b/sprout/valarray/make_valarray.hpp new file mode 100644 index 00000000..91f283d3 --- /dev/null +++ b/sprout/valarray/make_valarray.hpp @@ -0,0 +1,73 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_MAKE_VALARRAY_HPP +#define SPROUT_VALARRAY_MAKE_VALARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + enum make_valarray_t { + make_valarray_in_common_elements + }; + } // namespace detail + // + // make_valarray + // + template + inline SPROUT_CONSTEXPR sprout::valarray::type, sizeof...(Types)> + make_valarray(Types&&... args) { + typedef sprout::valarray::type, sizeof...(Types)> type; + typedef sprout::detail::make_construct_impl construct_type; + return construct_type::make(T(SPROUT_FORWARD(Types, args))...); + } + template + inline SPROUT_CONSTEXPR sprout::valarray::type, sizeof...(Types)> + make_valarray(Types&&... args) { + typedef typename sprout::common_decay::type value_type; + typedef sprout::valarray type; + typedef sprout::detail::make_construct_impl construct_type; + return construct_type::make(value_type(SPROUT_FORWARD(Types, args))...); + } + + // + // make_valarray_without_narrowing + // + template + inline SPROUT_CONSTEXPR sprout::valarray::type, sizeof...(Types)> + make_valarray_without_narrowing(Types&&... args) { + typedef sprout::valarray::type, sizeof...(Types)> type; + typedef sprout::detail::make_construct_impl construct_type; + return construct_type::make(SPROUT_FORWARD(Types, args)...); + } + template + inline SPROUT_CONSTEXPR sprout::valarray::type, sizeof...(Types)> + make_valarray_without_narrowing(Types&&... args) { + typedef typename sprout::common_decay::type value_type; + typedef sprout::valarray type; + typedef sprout::detail::make_construct_impl construct_type; + return construct_type::make(SPROUT_FORWARD(Types, args)...); + } + + // + // make_common_valarray + // + template + inline SPROUT_CONSTEXPR sprout::valarray::type, sizeof...(Types)> + make_common_valarray(Types&&... args) { + return sprout::make_valarray(SPROUT_FORWARD(Types, args)...); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_MAKE_VALARRAY_HPP diff --git a/sprout/valarray/mask_array.hpp b/sprout/valarray/mask_array.hpp new file mode 100644 index 00000000..241839d2 --- /dev/null +++ b/sprout/valarray/mask_array.hpp @@ -0,0 +1,207 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_MASKE_ARRAY_HPP +#define SPROUT_VALARRAY_MASKE_ARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // mask_array + // + template + class mask_array { + friend class sprout::valarray; + private: + typedef sprout::valarray valarray_type; + public: + typedef typename valarray_type::value_type value_type; + typedef typename valarray_type::reference reference; + typedef typename valarray_type::size_type size_type; + typedef typename valarray_type::difference_type difference_type; + typedef typename valarray_type::pointer pointer; + typedef sprout::index_iterator iterator; + private: + typedef sprout::valarray mask_type; + typedef sprout::valarray indexes_type; + public: + SPROUT_STATIC_CONSTEXPR size_type static_size = valarray_type::static_size; + private: + sprout::value_holder arr_; + indexes_type indexes_; + private: + mask_array() = delete; + SPROUT_CONSTEXPR mask_array(sprout::valarray& arr, mask_type const& mask) + : arr_(arr) + , indexes_( + sprout::detail::mask_index( + mask.begin(), mask.end(), + indexes_type(sprout::count(mask.begin(), mask.end(), true)) + ) + ) + {} + public: + SPROUT_CONSTEXPR mask_array(mask_array const& other) + : arr_(other.arr_), indexes_(other.indexes_) + {} + template + SPROUT_CXX14_CONSTEXPR mask_array const& + operator=(mask_array const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + return *this; + } + template + SPROUT_CXX14_CONSTEXPR void + operator=(sprout::valarray const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + } + SPROUT_CXX14_CONSTEXPR void + operator=(T const& value) const { + sprout::fill(begin(), end(), value); + } + template + SPROUT_CXX14_CONSTEXPR void + operator+=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 += *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator-=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 -= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator*=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 *= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator/=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 /= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator%=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 %= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator&=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 &= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator|=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 |= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator^=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 ^= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator<<=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 <<= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator>>=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 >>= *it; + } + } + + SPROUT_CXX14_CONSTEXPR void + swap(mask_array& other) SPROUT_NOEXCEPT { + sprout::swap(arr_, other.arr_); + sprout::swap(indexes_, other.indexes_); + } + + // iterators: + SPROUT_CONSTEXPR iterator + begin() const { + return iterator(*this, 0); + } + SPROUT_CONSTEXPR iterator + end() const { + return iterator(*this, size()); + } + // capacity: + SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + return indexes_.size(); + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return size() == 0; + } + // element access: + SPROUT_CONSTEXPR reference operator[](size_type i) const { + return (*arr_)[indexes_[i]]; + } + SPROUT_CONSTEXPR reference at(size_type i) const { + return arr_->at(indexes_.at(i)); + } + SPROUT_CONSTEXPR reference front() const { + return (*this)[0]; + } + SPROUT_CONSTEXPR reference back() const { + return (*this)[size() - 1]; + } + }; + template + SPROUT_CONSTEXPR_OR_CONST typename sprout::mask_array::size_type sprout::mask_array::static_size; + + // + // swap + // + template + inline SPROUT_CXX14_CONSTEXPR void + swap(sprout::mask_array& lhs, sprout::mask_array& rhs) + SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) + { + lhs.swap(rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_MASKE_ARRAY_HPP diff --git a/sprout/valarray/power.hpp b/sprout/valarray/power.hpp new file mode 100644 index 00000000..8c42243c --- /dev/null +++ b/sprout/valarray/power.hpp @@ -0,0 +1,234 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_POWER_HPP +#define SPROUT_VALARRAY_POWER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout_math_detail { + using sprout::math::cbrt; + using sprout::math::abs; + using sprout::math::hypot; + using sprout::math::pow; + using sprout::math::sqrt; + using sprout::math::log_a; + using sprout::abs; + + template + inline SPROUT_CONSTEXPR decltype(cbrt(std::declval())) + call_cbrt(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(cbrt(std::declval())) + { + return cbrt(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(abs(std::declval())) + call_abs(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(abs(std::declval())) + { + return abs(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(hypot(std::declval(), std::declval())) + call_hypot(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(hypot(std::declval(), std::declval())) + { + return hypot(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, y)); + } + template + inline SPROUT_CONSTEXPR decltype(pow(std::declval(), std::declval())) + call_pow(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(pow(std::declval(), std::declval())) + { + return pow(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, y)); + } + template + inline SPROUT_CONSTEXPR decltype(sqrt(std::declval())) + call_sqrt(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sqrt(std::declval())) + { + return sqrt(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(log_a(std::declval(), std::declval())) + call_log_a(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(log_a(std::declval(), std::declval())) + { + return log_a(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, y)); + } +} // namespace sprout + +namespace sprout { + namespace detail { + struct cbrt_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_cbrt(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_cbrt(std::declval())) + { + return sprout_math_detail::call_cbrt(SPROUT_FORWARD(T, x)); + } + }; + struct abs_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_abs(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_abs(std::declval())) + { + return sprout_math_detail::call_abs(SPROUT_FORWARD(T, x)); + } + }; + struct hypot_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_hypot(std::declval(), std::declval())) + operator()(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_hypot(std::declval(), std::declval())) + { + return sprout_math_detail::call_hypot(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, y)); + } + }; + struct pow_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_pow(std::declval(), std::declval())) + operator()(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_pow(std::declval(), std::declval())) + { + return sprout_math_detail::call_pow(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, y)); + } + }; + struct sqrt_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_sqrt(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_sqrt(std::declval())) + { + return sprout_math_detail::call_sqrt(SPROUT_FORWARD(T, x)); + } + }; + struct log_a_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_log_a(std::declval(), std::declval())) + operator()(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_log_a(std::declval(), std::declval())) + { + return sprout_math_detail::call_log_a(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, y)); + } + }; + } // namespace detail + + // + // cbrt + // + template + inline SPROUT_CONSTEXPR sprout::valarray + cbrt(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::cbrt_f()); + } + // + // abs + // + template + inline SPROUT_CONSTEXPR sprout::valarray + abs(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::abs_f()); + } + // + // hypot + // + template + inline SPROUT_CONSTEXPR sprout::valarray + hypot(sprout::valarray const& x, sprout::valarray const& y) { + return sprout::fixed::transform(x.begin(), x.end(), y.end(), x, sprout::detail::hypot_f()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + hypot(sprout::valarray const& x, T const& y) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::bind2nd(sprout::detail::hypot_f(), y)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + hypot(T const& x, sprout::valarray const& y) { + return sprout::fixed::transform(y.begin(), y.end(), y, sprout::bind1st(sprout::detail::hypot_f(), x)); + } + // + // pow + // + template + inline SPROUT_CONSTEXPR sprout::valarray + pow(sprout::valarray const& x, sprout::valarray const& y) { + return sprout::fixed::transform(x.begin(), x.end(), y.end(), x, sprout::detail::pow_f()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + pow(sprout::valarray const& x, T const& y) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::bind2nd(sprout::detail::pow_f(), y)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + pow(T const& x, sprout::valarray const& y) { + return sprout::fixed::transform(y.begin(), y.end(), y, sprout::bind1st(sprout::detail::pow_f(), x)); + } + // + // sqrt + // + template + inline SPROUT_CONSTEXPR sprout::valarray + sqrt(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::sqrt_f()); + } + // + // log_a + // + template + inline SPROUT_CONSTEXPR sprout::valarray + log_a(sprout::valarray const& x, sprout::valarray const& y) { + return sprout::fixed::transform(x.begin(), x.end(), y.end(), x, sprout::detail::log_a_f()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + log_a(sprout::valarray const& x, T const& y) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::bind2nd(sprout::detail::log_a_f(), y)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + log_a(T const& x, sprout::valarray const& y) { + return sprout::fixed::transform(y.begin(), y.end(), y, sprout::bind1st(sprout::detail::log_a_f(), x)); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_POWER_HPP diff --git a/sprout/valarray/slice_array.hpp b/sprout/valarray/slice_array.hpp new file mode 100644 index 00000000..9b65d804 --- /dev/null +++ b/sprout/valarray/slice_array.hpp @@ -0,0 +1,222 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_SLICE_ARRAY_HPP +#define SPROUT_VALARRAY_SLICE_ARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // slice + // + class slice { + private: + std::size_t start_; + std::size_t size_; + std::size_t stride_; + public: + SPROUT_CONSTEXPR slice() + : start_(), size_(), stride_() + {} + SPROUT_CONSTEXPR slice(std::size_t start, std::size_t size, std::size_t stride) + : start_(start), size_(size), stride_(stride) + {} + SPROUT_CONSTEXPR std::size_t start() const { + return start_; + } + SPROUT_CONSTEXPR std::size_t size() const { + return size_; + } + SPROUT_CONSTEXPR std::size_t stride() const { + return stride_; + } + }; + + // + // slice_array + // + template + class slice_array { + friend class sprout::valarray; + private: + typedef sprout::valarray valarray_type; + public: + typedef typename valarray_type::value_type value_type; + typedef typename valarray_type::reference reference; + typedef typename valarray_type::size_type size_type; + typedef typename valarray_type::difference_type difference_type; + typedef typename valarray_type::pointer pointer; + typedef sprout::index_iterator iterator; + public: + SPROUT_STATIC_CONSTEXPR size_type static_size = valarray_type::static_size; + private: + sprout::value_holder arr_; + sprout::slice slice_; + private: + slice_array() = delete; + SPROUT_CONSTEXPR slice_array(sprout::valarray& arr, sprout::slice const& slice) + : arr_(arr), slice_(slice) + {} + public: + SPROUT_CONSTEXPR slice_array(slice_array const& other) + : arr_(other.arr_), slice_(other.slice_) + {} + template + SPROUT_CXX14_CONSTEXPR slice_array const& + operator=(slice_array const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + return *this; + } + template + SPROUT_CXX14_CONSTEXPR void + operator=(sprout::valarray const& ar) const { + sprout::copy(ar.begin(), ar.end(), begin()); + } + SPROUT_CXX14_CONSTEXPR void + operator=(T const& value) const { + sprout::fill(begin(), end(), value); + } + template + SPROUT_CXX14_CONSTEXPR void + operator+=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 += *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator-=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 -= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator*=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 *= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator/=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 /= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator%=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 %= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator&=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 &= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator|=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 |= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator^=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 ^= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator<<=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 <<= *it; + } + } + template + SPROUT_CXX14_CONSTEXPR void + operator>>=(sprout::valarray const& xs) const { + iterator it2 = begin(); + for (typename sprout::valarray::const_iterator it = xs.begin(), last = xs.end(); it != last; ++it, ++it2) { + *it2 >>= *it; + } + } + + SPROUT_CXX14_CONSTEXPR void + swap(slice_array& other) SPROUT_NOEXCEPT { + sprout::swap(arr_, other.arr_); + sprout::swap(slice_, other.slice_); + } + + // iterators: + SPROUT_CONSTEXPR iterator + begin() const { + return iterator(*this, 0); + } + SPROUT_CONSTEXPR iterator + end() const { + return iterator(*this, size()); + } + // capacity: + SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + return slice_.size(); + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return size() == 0; + } + // element access: + SPROUT_CONSTEXPR reference operator[](size_type i) const { + return (*arr_)[slice_.start() + slice_.stride() * i]; + } + SPROUT_CONSTEXPR reference at(size_type i) const { + return arr_->at(slice_.start() + slice_.stride() * i); + } + SPROUT_CONSTEXPR reference front() const { + return (*this)[0]; + } + SPROUT_CONSTEXPR reference back() const { + return (*this)[size() - 1]; + } + }; + template + SPROUT_CONSTEXPR_OR_CONST typename sprout::slice_array::size_type sprout::slice_array::static_size; + + // + // swap + // + template + inline SPROUT_CXX14_CONSTEXPR void + swap(sprout::slice_array& lhs, sprout::slice_array& rhs) + SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) + { + lhs.swap(rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_SLICE_ARRAY_HPP diff --git a/sprout/valarray/trigonometric.hpp b/sprout/valarray/trigonometric.hpp new file mode 100644 index 00000000..d4ed8f63 --- /dev/null +++ b/sprout/valarray/trigonometric.hpp @@ -0,0 +1,240 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_BITWISE_HPP +#define SPROUT_VALARRAY_BITWISE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout_math_detail { + using sprout::math::acos; + using sprout::math::asin; + using sprout::math::atan; + using sprout::math::atan2; + using sprout::math::cos; + using sprout::math::sin; + using sprout::math::tan; + + template + inline SPROUT_CONSTEXPR decltype(acos(std::declval())) + call_acos(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(acos(std::declval())) + { + return acos(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(asin(std::declval())) + call_asin(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(asin(std::declval())) + { + return asin(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(atan(std::declval())) + call_atan(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(atan(std::declval())) + { + return atan(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(atan2(std::declval(), std::declval())) + call_atan2(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(atan2(std::declval(), std::declval())) + { + return atan2(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, u)); + } + template + inline SPROUT_CONSTEXPR decltype(cos(std::declval())) + call_cos(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(cos(std::declval())) + { + return cos(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(sin(std::declval())) + call_sin(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sin(std::declval())) + { + return sin(SPROUT_FORWARD(T, x)); + } + template + inline SPROUT_CONSTEXPR decltype(tan(std::declval())) + call_tan(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(tan(std::declval())) + { + return tan(SPROUT_FORWARD(T, x)); + } +} // namespace sprout + +namespace sprout { + namespace detail { + struct acos_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_acos(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_acos(std::declval())) + { + return sprout_math_detail::call_acos(SPROUT_FORWARD(T, x)); + } + }; + struct asin_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_asin(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_asin(std::declval())) + { + return sprout_math_detail::call_asin(SPROUT_FORWARD(T, x)); + } + }; + struct atan_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_atan(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_atan(std::declval())) + { + return sprout_math_detail::call_atan(SPROUT_FORWARD(T, x)); + } + }; + struct atan2_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_atan2(std::declval(), std::declval())) + operator()(T&& x, U&& y) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_atan2(std::declval(), std::declval())) + { + return sprout_math_detail::call_atan2(SPROUT_FORWARD(T, x), SPROUT_FORWARD(U, y)); + } + }; + struct cos_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_cos(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_cos(std::declval())) + { + return sprout_math_detail::call_cos(SPROUT_FORWARD(T, x)); + } + }; + struct sin_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_sin(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_sin(std::declval())) + { + return sprout_math_detail::call_sin(SPROUT_FORWARD(T, x)); + } + }; + struct tan_f + : public sprout::transparent<> + { + public: + template + SPROUT_CONSTEXPR decltype(sprout_math_detail::call_tan(std::declval())) + operator()(T&& x) + SPROUT_NOEXCEPT_IF_EXPR(sprout_math_detail::call_tan(std::declval())) + { + return sprout_math_detail::call_tan(SPROUT_FORWARD(T, x)); + } + }; + } // namespace detail + + // + // acos + // + template + inline SPROUT_CONSTEXPR sprout::valarray + acos(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::acos_f()); + } + // + // asin + // + template + inline SPROUT_CONSTEXPR sprout::valarray + asin(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::asin_f()); + } + // + // atan + // + template + inline SPROUT_CONSTEXPR sprout::valarray + atan(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::atan_f()); + } + // + // atan2 + // + template + inline SPROUT_CONSTEXPR sprout::valarray + atan2(sprout::valarray const& x, sprout::valarray const& y) { + return sprout::fixed::transform(x.begin(), x.end(), y.end(), x, sprout::detail::atan2_f()); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + atan2(sprout::valarray const& lhs, T const& y) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::bind2nd(sprout::detail::atan2_f()()), y)); + } + template + inline SPROUT_CONSTEXPR sprout::valarray + atan2(T const& x, sprout::valarray const& y) { + return sprout::fixed::transform(y.begin(), y.end(), y, sprout::bind1st(sprout::detail::atan2_f()()), x)); + } + // + // cos + // + template + inline SPROUT_CONSTEXPR sprout::valarray + cos(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::cos_f()); + } + // + // sin + // + template + inline SPROUT_CONSTEXPR sprout::valarray + sin(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::sin_f()); + } + // + // tan + // + template + inline SPROUT_CONSTEXPR sprout::valarray + tan(sprout::valarray const& x) { + return sprout::fixed::transform(x.begin(), x.end(), x, sprout::detail::tan_f()); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_BITWISE_HPP diff --git a/sprout/valarray/tuple.hpp b/sprout/valarray/tuple.hpp new file mode 100644 index 00000000..a11d5df7 --- /dev/null +++ b/sprout/valarray/tuple.hpp @@ -0,0 +1,70 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_TUPLE_HPP +#define SPROUT_VALARRAY_TUPLE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + // + // tuple_get + // + template + inline SPROUT_CONSTEXPR T& + tuple_get(sprout::valarray& t) SPROUT_NOEXCEPT { + static_assert(I < N, "tuple_get: index out of range"); + return t[I]; + } + template + inline SPROUT_CONSTEXPR T const& + tuple_get(sprout::valarray const& t) SPROUT_NOEXCEPT { + static_assert(I < N, "tuple_get: index out of range"); + return t[I]; + } + template + inline SPROUT_CONSTEXPR T&& + tuple_get(sprout::valarray&& t) SPROUT_NOEXCEPT { + return sprout::move(sprout::tuples::get(t)); + } +} // namespace sprout + +namespace std { +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wmismatched-tags" +#endif + // + // tuple_size + // + template + struct tuple_size > + : public sprout::integral_constant + {}; + + // + // tuple_element + // + template + struct tuple_element > + : public std::conditional<(I < N), sprout::identity, sprout::detail::nil_base>::type + {}; +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +} // namespace std + +#endif // #ifndef SPROUT_VALARRAY_TUPLE_HPP diff --git a/sprout/valarray/type_traits.hpp b/sprout/valarray/type_traits.hpp new file mode 100644 index 00000000..516e6e48 --- /dev/null +++ b/sprout/valarray/type_traits.hpp @@ -0,0 +1,43 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_TYPE_TRAITS_HPP +#define SPROUT_VALARRAY_TYPE_TRAITS_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // is_valarray + // + template + struct is_valarray + : public sprout::false_type + {}; + template + struct is_valarray + : public sprout::is_valarray + {}; + template + struct is_valarray + : public sprout::is_valarray + {}; + template + struct is_valarray > + : public sprout::true_type + {}; + +#if SPROUT_USE_VARIABLE_TEMPLATES + template + SPROUT_STATIC_CONSTEXPR bool is_valarray_v = sprout::is_valarray::value; +#endif // #if SPROUT_USE_VARIABLE_TEMPLATES +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_TYPE_TRAITS_HPP diff --git a/sprout/valarray/valarray.hpp b/sprout/valarray/valarray.hpp new file mode 100644 index 00000000..22a7b19f --- /dev/null +++ b/sprout/valarray/valarray.hpp @@ -0,0 +1,672 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_VALARRAY_HPP +#define SPROUT_VALARRAY_VALARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + struct valarray_raw_construct_t {}; + + template + class valarray_construct_access; + + template + class valarray_impl { + public: + typedef T value_type; + typedef sprout::array array_type; + public: + static SPROUT_CONSTEXPR std::size_t + checked_size(std::size_t n) { + return N >= n ? n + : throw std::length_error("valarray<>: length exceeded") + ; + } + public: + array_type array_; + std::size_t size_; + public: + SPROUT_CONSTEXPR valarray_impl() + : array_{{}}, size_(0) + {} + explicit SPROUT_CONSTEXPR valarray_impl(std::size_t n) + : array_{{}}, size_(checked_size(n)) + {} + template + SPROUT_CONSTEXPR valarray_impl( + sprout::index_tuple, + value_type const& value, std::size_t n + ) + : array_{{ + (sprout::math::less(Indexes, n) ? value + : value_type() + )... + }} + , size_(checked_size(n)) + {} + template + SPROUT_CONSTEXPR valarray_impl( + sprout::index_tuple, + RandomAccessIterator first, std::size_t n + ) + : array_{{ + (sprout::math::less(Indexes, n) ? first[Indexes] + : value_type() + )... + }} + , size_(checked_size(n)) + {} + valarray_impl(valarray_impl const& v) = default; + valarray_impl(valarray_impl&& v) = default; + template + SPROUT_CONSTEXPR valarray_impl( + sprout::index_tuple, + sprout::detail::valarray_raw_construct_t, std::size_t n, Args&&... args + ) + : array_{{ + (sprout::math::less(Indexes, n) ? static_cast(SPROUT_FORWARD(Args, args)) + : value_type() + )... + }} + , size_(n) + {} + }; + } // namespace detail + + // + // valarray + // + template + class valarray { + friend class sprout::detail::valarray_construct_access; + private: + typedef sprout::detail::valarray_impl impl_type; + typedef typename impl_type::array_type array_type; + public: + typedef typename array_type::value_type value_type; + typedef typename array_type::iterator iterator; + typedef typename array_type::const_iterator const_iterator; + typedef typename array_type::reference reference; + typedef typename array_type::const_reference const_reference; + typedef typename array_type::size_type size_type; + typedef typename array_type::difference_type difference_type; + typedef typename array_type::pointer pointer; + typedef typename array_type::const_pointer const_pointer; + typedef typename array_type::reverse_iterator reverse_iterator; + typedef typename array_type::const_reverse_iterator const_reverse_iterator; + typedef value_type result_type; + public: + SPROUT_STATIC_CONSTEXPR size_type static_size = N; + private: + impl_type impl_; + private: + template::type> + SPROUT_CONSTEXPR valarray(sprout::detail::valarray_raw_construct_t, size_type n, Args&&... args) + : impl_( + sprout::index_pack::make(), + sprout::detail::valarray_raw_construct_t(), n, SPROUT_FORWARD(Args, args)... + ) + {} + SPROUT_CXX14_CONSTEXPR void + maxcheck(size_type n) const { + if (n > max_size()) { + throw std::out_of_range("valarray<>: index out of range"); + } + } + SPROUT_CXX14_CONSTEXPR void + lengthcheck(size_type n) const { + if (n > max_size()) { + throw std::length_error("valarray<>: length exceeded"); + } + } + public: + // construct/destroy: + SPROUT_CONSTEXPR valarray() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL + explicit SPROUT_CONSTEXPR valarray(std::size_t n) + : impl_(n) + {} + SPROUT_CONSTEXPR valarray(value_type const& value, std::size_t n) + : impl_( + sprout::make_index_tuple::make(), + value, n + ) + {} + SPROUT_CONSTEXPR valarray(value_type const* first, std::size_t n) + : impl_( + sprout::make_index_tuple::make(), + first, n + ) + {} + valarray(valarray const&) = default; + valarray(valarray&&) = default; + SPROUT_INITIALIZER_LIST_CONSTEXPR valarray(std::initializer_list il) + : impl_( + sprout::make_index_tuple::make(), + il.begin(), il.size() + ) + {} + SPROUT_CONSTEXPR valarray(sprout::slice_array const& x) + : impl_( + sprout::make_index_tuple::make(), + x.begin(), x.size() + ) + {} + template + SPROUT_CONSTEXPR valarray(sprout::gslice_array const& x) + : impl_( + sprout::make_index_tuple::make(), + x.begin(), x.size() + ) + {} + template + SPROUT_CONSTEXPR valarray(sprout::mask_array const& x) + : impl_( + sprout::make_index_tuple::make(), + x.begin(), x.size() + ) + {} + template + SPROUT_CONSTEXPR valarray(sprout::indirect_array const& x) + : impl_( + sprout::make_index_tuple::make(), + x.begin(), x.size() + ) + {} + // assignment: + SPROUT_CXX14_CONSTEXPR valarray& + operator=(valarray const& rhs) { + impl_.array_ = rhs.array_; + impl_.size_ = rhs.size_; + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator=(valarray&& rhs) SPROUT_NOEXCEPT { + impl_.array_ = sprout::move(rhs.array_); + impl_.size_ = sprout::move(rhs.size_); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator=(value_type const& rhs) { + impl_.array_.fill(rhs); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator=(std::initializer_list rhs) { + lengthcheck(rhs.size()); + sprout::copy(rhs.begin(), rhs.end(), begin()); + impl_.size_ = rhs.size(); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator=(sprout::slice_array const& rhs) { + lengthcheck(rhs.size()); + sprout::copy(rhs.begin(), rhs.end(), begin()); + impl_.size_ = rhs.size(); + return *this; + } + template + SPROUT_CXX14_CONSTEXPR valarray& + operator=(sprout::gslice_array const& rhs) { + lengthcheck(rhs.size()); + sprout::copy(rhs.begin(), rhs.end(), begin()); + impl_.size_ = rhs.size(); + return *this; + } + template + SPROUT_CXX14_CONSTEXPR valarray& + operator=(sprout::mask_array const& rhs) { + lengthcheck(rhs.size()); + sprout::copy(rhs.begin(), rhs.end(), begin()); + impl_.size_ = rhs.size(); + return *this; + } + template + SPROUT_CXX14_CONSTEXPR valarray& + operator=(sprout::indirect_array const& rhs) { + lengthcheck(rhs.size()); + sprout::copy(rhs.begin(), rhs.end(), begin()); + impl_.size_ = rhs.size(); + return *this; + } + // element access: + SPROUT_CONSTEXPR value_type const& + operator[](std::size_t i) const { + return impl_.array_[i]; + } + SPROUT_CXX14_CONSTEXPR value_type& + operator[](std::size_t i) { + return impl_.array_[i]; + } + // subset operations: + SPROUT_CONSTEXPR valarray + operator[](sprout::slice s) const { + return valarray(sprout::slice_array(const_cast(*this), s)); + } + SPROUT_CXX14_CONSTEXPR sprout::slice_array + operator[](sprout::slice s) { + return sprout::slice_array(*this, s); + } + template + SPROUT_CONSTEXPR valarray + operator[](sprout::gslice const& gs) const { + return valarray(sprout::gslice_array(const_cast(*this), gs)); + } + template + SPROUT_CXX14_CONSTEXPR sprout::gslice_array + operator[](sprout::gslice const& gs) { + return sprout::gslice_array(*this, gs); + } + template + SPROUT_CONSTEXPR valarray + operator[](valarray const& vb) const { + return valarray(sprout::mask_array(const_cast(*this), vb)); + } + template + SPROUT_CXX14_CONSTEXPR sprout::mask_array + operator[](valarray const& vb) { + return sprout::mask_array(*this, vb); + } + template + SPROUT_CONSTEXPR valarray + operator[](valarray const& vs) const { + return valarray(sprout::indirect_array(const_cast(*this), vs)); + } + template + SPROUT_CXX14_CONSTEXPR sprout::indirect_array + operator[](valarray const& vs) { + return sprout::indirect_array(*this, vs); + } + // unary operators: + SPROUT_CONSTEXPR valarray + operator+() const { + return *this; + } + SPROUT_CONSTEXPR valarray + operator-() const { + return sprout::fixed::transform(begin(), end(), *this, sprout::negate<>()); + } + SPROUT_CONSTEXPR valarray + operator~() const { + return sprout::fixed::transform(begin(), end(), *this, sprout::bit_not<>()); + } + SPROUT_CONSTEXPR valarray + operator!() const { + return sprout::fixed::transform >(begin(), end(), sprout::logical_not<>()); + } + // computed assignment: + SPROUT_CXX14_CONSTEXPR valarray& + operator*=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::multiplies_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator/=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::divides_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator%=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::modulus_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator+=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::plus_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator-=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::minus_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator^=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::bit_xor_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator&=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::bit_and_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator|=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::bit_or_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator<<=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::shift_left_assign<>(), x)); + return *this; + } + SPROUT_CXX14_CONSTEXPR valarray& + operator>>=(value_type const& x) { + sprout::for_each(begin(), end(), sprout::bind2nd(sprout::shift_right_assign<>(), x)); + return *this; + } + // member functions: + SPROUT_CXX14_CONSTEXPR void + swap(valarray& other) SPROUT_NOEXCEPT { + sprout::swap(impl_.array_, other.impl_.array_); + sprout::swap(impl_.size_, other.impl_.size_); + } + SPROUT_CONSTEXPR std::size_t + size() const { + return impl_.size_; + } + SPROUT_CONSTEXPR value_type + sum() const { + return sprout::accumulate(begin(), end(), value_type()); + } + SPROUT_CONSTEXPR value_type + min() const { + return *sprout::min_element(begin(), end()); + } + SPROUT_CONSTEXPR value_type + max() const { + return *sprout::max_element(begin(), end()); + } + SPROUT_CONSTEXPR valarray + shift(int i) const { + return i > 0 ? sprout::fixed::copy(begin() + i, end(), *this) + : i < 0 ? sprout::fixed::copy_backward(begin(), end() + i, *this) + : *this + ; + } + SPROUT_CONSTEXPR valarray + cshift(int i) const { + return i > 0 ? sprout::fixed::rotate(*this, begin() + i) + : i < 0 ? sprout::fixed::rotate(*this, end() + i) + : *this + ; + } + SPROUT_CONSTEXPR valarray + apply(value_type func(value_type)) const { + return sprout::fixed::transform(begin(), end(), *this, func); + } + SPROUT_CONSTEXPR valarray + apply(value_type func(value_type const&)) const { + return sprout::fixed::transform(begin(), end(), *this, func); + } + template + SPROUT_CONSTEXPR valarray + apply(F func) const { + return sprout::fixed::transform(begin(), end(), *this, func); + } + SPROUT_CXX14_CONSTEXPR void + resize(std::size_t n, value_type x = value_type()) { + lengthcheck(n); + impl_.size_ = n; + impl_.array_.fill(x); + } + + // modifiers (array): + SPROUT_CXX14_CONSTEXPR void fill(const_reference value) { + impl_.array_.fill(value); + } + SPROUT_CONSTEXPR valarray fill(const_reference value) const { + return valarray(value, size()); + } + SPROUT_CXX14_CONSTEXPR void assign(const_reference value) { + impl_.array_.assign(value); + } + SPROUT_CONSTEXPR valarray assign(const_reference value) const { + return valarray(value, size()); + } + // iterators (array): + SPROUT_CXX14_CONSTEXPR iterator begin() SPROUT_NOEXCEPT { + return impl_.array_.begin(); + } + SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { + return impl_.array_.begin(); + } + SPROUT_CXX14_CONSTEXPR iterator end() SPROUT_NOEXCEPT { + return impl_.array_.begin() + size(); + } + SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { + return impl_.array_.begin() + size(); + } + SPROUT_CXX14_CONSTEXPR reverse_iterator rbegin() SPROUT_NOEXCEPT { + return impl_.array_.rend() - size(); + } + SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT { + return impl_.array_.rend() - size(); + } + SPROUT_CXX14_CONSTEXPR reverse_iterator rend() SPROUT_NOEXCEPT { + return impl_.array_.rend(); + } + SPROUT_CONSTEXPR const_reverse_iterator rend() const SPROUT_NOEXCEPT { + return impl_.array_.rend(); + } + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return impl_.array_.cbegin(); + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return impl_.array_.cbegin() + size(); + } + SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { + return impl_.array_.crend() - size(); + } + SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT { + return impl_.array_.crend(); + } + // capacity (array): + SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { + return impl_.array_.max_size(); + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return size() != 0; + } + // element access (array): + 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 { + return i < size() ? (*this)[i] + : (throw std::out_of_range("valarray<>: index out of range"), (*this)[i]) + ; + } + SPROUT_CXX14_CONSTEXPR reference front() { + return impl_.array_.front(); + } + SPROUT_CONSTEXPR const_reference front() const { + return impl_.array_.front(); + } + SPROUT_CXX14_CONSTEXPR reference back() { + return (*this)[size() - 1]; + } + SPROUT_CONSTEXPR const_reference back() const { + return (*this)[size() - 1]; + } + + SPROUT_CXX14_CONSTEXPR pointer data() SPROUT_NOEXCEPT { + return impl_.array_.data(); + } + SPROUT_CONSTEXPR const_pointer data() const SPROUT_NOEXCEPT { + return impl_.array_.data(); + } + SPROUT_CXX14_CONSTEXPR pointer c_array() SPROUT_NOEXCEPT { + return impl_.array_.c_array(); + } + 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 { + return i >= size() ? throw std::out_of_range("valarray<>: index out of range") + : (void)0 + ; + } + + 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 { + 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 { + return impl_.array_.index_of(p); + } + SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { + return impl_.array_.index_of(p); + } + }; + template + SPROUT_CONSTEXPR_OR_CONST typename sprout::valarray::size_type sprout::valarray::static_size; + + // + // swap + // + template + inline SPROUT_CXX14_CONSTEXPR void + swap(sprout::valarray& lhs, sprout::valarray& rhs) + SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) + { + lhs.swap(rhs); + } + + namespace detail { + template + class valarray_construct_access { + public: + template + static SPROUT_CONSTEXPR sprout::valarray + raw_construct(typename sprout::valarray::size_type n, Args&&... args) { + return sprout::valarray(sprout::detail::valarray_raw_construct_t(), n, SPROUT_FORWARD(Args, args)...); + } + }; + + template + struct make_construct_impl; + + template + struct make_construct_impl > { + private: + typedef sprout::valarray copied_type; + public: + template + static SPROUT_CONSTEXPR copied_type + make(Args&&... args) { + typedef sprout::detail::valarray_construct_access access_type; + return access_type::raw_construct(sizeof...(args), SPROUT_FORWARD(Args, args)...); + } + template + static SPROUT_CONSTEXPR copied_type + sized_make(typename copied_type::size_type size, Args&&... args) { + typedef sprout::detail::valarray_construct_access access_type; + return access_type::raw_construct(size, SPROUT_FORWARD(Args, args)...); + } + }; + } // namespace detail + + // + // to_valarray + // + template + inline SPROUT_CONSTEXPR sprout::valarray + to_valarray(sprout::valarray const& arr) + SPROUT_NOEXCEPT_IF(sprout::is_nothrow_copy_constructible::value) + { + return arr; + } + template + inline SPROUT_CONSTEXPR sprout::valarray::type, N> + to_valarray(T (& arr)[N]) + SPROUT_NOEXCEPT_IF(sprout::is_nothrow_copy_constructible::type>::value) + { + return sprout::valarray::type, N>(arr, N); + } + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::valarray + to_valarray_impl(std::array const& arr, sprout::index_tuple) + SPROUT_NOEXCEPT_IF(sprout::is_nothrow_copy_constructible::value) + { + typedef sprout::detail::make_construct_impl > construct_type; + return construct_type::make(arr[Indexes]...); + } + } // namespace detail + template + inline SPROUT_CONSTEXPR sprout::valarray + to_valarray(std::array const& arr) + SPROUT_NOEXCEPT_IF(sprout::is_nothrow_copy_constructible::value) + { + return sprout::detail::to_valarray_impl(arr, sprout::make_index_tuple::make()); + } + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::valarray + to_valarray_impl(sprout::array const& arr, sprout::index_tuple) + SPROUT_NOEXCEPT_IF(sprout::is_nothrow_copy_constructible::value) + { + typedef sprout::detail::make_construct_impl > construct_type; + return construct_type::make(arr[Indexes]...); + } + } // namespace detail + template + inline SPROUT_CONSTEXPR sprout::valarray + to_valarray(sprout::array const& arr) + SPROUT_NOEXCEPT_IF(sprout::is_nothrow_copy_constructible::value) + { + return sprout::detail::to_valarray_impl(arr, sprout::make_index_tuple::make()); + } +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_VALARRAY_HPP diff --git a/sprout/valarray/valarray_fwd.hpp b/sprout/valarray/valarray_fwd.hpp new file mode 100644 index 00000000..4c202f0e --- /dev/null +++ b/sprout/valarray/valarray_fwd.hpp @@ -0,0 +1,56 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_VALARRAY_VALARRAY_FWD_HPP +#define SPROUT_VALARRAY_VALARRAY_FWD_HPP + +#include +#include + +namespace sprout { + // + // valarray + // + template + class valarray; + + // + // slice + // + class slice; + // + // slice_array + // + template + class slice_array; + + // + // gslice + // + template + class gslice; + // + // gslice_array + // + template + class gslice_array; + + // + // mask_array + // + template + class mask_array; + + // + // indirect_array + // + template + class indirect_array; + +} // namespace sprout + +#endif // #ifndef SPROUT_VALARRAY_VALARRAY_FWD_HPP