[libcxx-commits] [PATCH] D58735: Add slice_array operator= valarray overload

Zoe Carver via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 23 13:14:29 PST 2020


zoecarver updated this revision to Diff 307182.
zoecarver retitled this revision from "Add slice_array operator= valarray overload " to "Add slice_array operator= valarray overload".
zoecarver added a comment.
Herald added a project: libc++.

- Rebase off master
- Address review comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58735/new/

https://reviews.llvm.org/D58735

Files:
  libcxx/include/valarray
  libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp


Index: libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp
===================================================================
--- libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp
+++ libcxx/test/std/numerics/numarray/template.slice.array/slice.arr.assign/valarray.pass.cpp
@@ -10,6 +10,7 @@
 
 // template <class T> class slice_array
 
+// void operator=(const T& value) const;
 // void operator=(const valarray<value_type>& v) const;
 
 #include <valarray>
@@ -23,7 +24,8 @@
     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 +44,23 @@
     assert(v1[14] == 14);
     assert(v1[15] == 15);
 
-  return 0;
+    ASSERT_SAME_TYPE(decltype(s1 = v2), void);
+
+    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);
+
+    s2 = 42;
+    assert(m[0] == 42);
+    assert(m[1] == 42);
+    assert(m[2] == 42);
+
+    ASSERT_SAME_TYPE(decltype(s2 = 42), void);
+
+    return 0;
 }
Index: libcxx/include/valarray
===================================================================
--- libcxx/include/valarray
+++ libcxx/include/valarray
@@ -136,6 +136,7 @@
     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 @@
     _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 @@
         *__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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58735.307182.patch
Type: text/x-patch
Size: 2485 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201123/cc128c60/attachment.bin>


More information about the libcxx-commits mailing list