[llvm-branch-commits] [libcxx] 644f68e - [libc++] Add slice_array operator= valarray overload.
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Dec 2 10:54:19 PST 2020
Author: zoecarver
Date: 2020-12-02T10:49:20-08:00
New Revision: 644f68ed4d741f33d254354c2b7bc9606b77c721
URL: https://github.com/llvm/llvm-project/commit/644f68ed4d741f33d254354c2b7bc9606b77c721
DIFF: https://github.com/llvm/llvm-project/commit/644f68ed4d741f33d254354c2b7bc9606b77c721.diff
LOG: [libc++] Add slice_array operator= valarray overload.
Add the slice_array::operator=(const std::valarray<T>& val_arr) overload.
Fixes https://llvm.org/PR40792.
Differential Revision: https://reviews.llvm.org/D58735
Added:
libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/template.pass.cpp
Modified:
libcxx/include/valarray
libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index aaae6fbd5b3f..9654cd5aa998 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -136,6 +136,7 @@ public:
void operator>>=(const valarray<value_type>& v) const;
void operator=(const value_type& x) const;
+ void operator=(const valarray<T>& val_arr) const;
slice_array() = delete;
};
@@ -1264,6 +1265,9 @@ public:
_LIBCPP_INLINE_VISIBILITY
void operator=(const value_type& __x) const;
+ _LIBCPP_INLINE_VISIBILITY
+ void operator=(const valarray<value_type>& __va) const;
+
private:
_LIBCPP_INLINE_VISIBILITY
slice_array(const slice& __sl, const valarray<value_type>& __v)
@@ -1303,6 +1307,15 @@ slice_array<_Tp>::operator=(const _Expr& __v) const
*__t = __v[__i];
}
+template <class _Tp>
+inline void
+slice_array<_Tp>::operator=(const valarray<value_type>& __va) const
+{
+ value_type* __t = __vp_;
+ for (size_t __i = 0; __i < __va.size(); ++__i, __t += __stride_)
+ *__t = __va[__i];
+}
+
template <class _Tp>
template <class _Expr>
inline
diff --git a/libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/template.pass.cpp b/libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/template.pass.cpp
new file mode 100644
index 000000000000..de2ffce12b90
--- /dev/null
+++ b/libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/template.pass.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// template <class T> class slice_array
+
+// void operator=(const T& value) const;
+
+#include <valarray>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ double a[] = { 0, 0, 0 };
+ std::valarray<double> m(a, sizeof(a)/sizeof(a[0]));
+ std::slice_array<double> s = m[std::slice(0, 3, 1)];
+ s = 42;
+ assert(m[0] == 42);
+ assert(m[1] == 42);
+ assert(m[2] == 42);
+
+ ASSERT_SAME_TYPE(decltype(s = 42), void);
+
+ return 0;
+}
diff --git a/libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp b/libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp
index d1d9fe18bea7..a09ef7971368 100644
--- a/libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp
+++ b/libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp
@@ -23,7 +23,8 @@ int main(int, char**)
int a2[] = {-1, -2, -3, -4, -5};
std::valarray<int> v1(a1, sizeof(a1)/sizeof(a1[0]));
std::valarray<int> v2(a2, sizeof(a2)/sizeof(a2[0]));
- v1[std::slice(1, 5, 3)] = v2;
+ std::slice_array<int> s1 = v1[std::slice(1, 5, 3)];
+ s1 = v2;
assert(v1.size() == 16);
assert(v1[ 0] == 0);
assert(v1[ 1] == -1);
@@ -42,5 +43,19 @@ int main(int, char**)
assert(v1[14] == 14);
assert(v1[15] == 15);
- return 0;
+ ASSERT_SAME_TYPE(decltype(s1 = v2), void);
+
+// The initializer list constructor is disabled in C++03 mode.
+#if TEST_STD_VER > 03
+ std::valarray<double> m = { 0, 0, 0 };
+ std::slice_array<double> s2 = m[std::slice(0, 3, 1)];
+ s2 = { 1, 2, 3 };
+ assert(m[0] == 1);
+ assert(m[1] == 2);
+ assert(m[2] == 3);
+
+ ASSERT_SAME_TYPE(decltype(s2 = {1, 2, 3}), void);
+#endif // TEST_STD_VER > 03
+
+ return 0;
}
More information about the llvm-branch-commits
mailing list