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

Zoe Carver via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 27 12:57:11 PST 2019


zoecarver created this revision.
zoecarver added reviewers: howard.hinnant, EricWF, ldionne, mclow.lists.
Herald added a subscriber: libcxx-commits.

Add the `slice_array::operator=(const std::valarray<T>& val_arr)` overload. Fix this issue <https://bugs.llvm.org/show_bug.cgi?id=40792>.


Repository:
  rCXX libc++

https://reviews.llvm.org/D58735

Files:
  include/valarray
  test/std/numerics/numarray/template.indirect.array/indirect.array.assign/slice_assignment.pass.cpp


Index: test/std/numerics/numarray/template.indirect.array/indirect.array.assign/slice_assignment.pass.cpp
===================================================================
--- /dev/null
+++ test/std/numerics/numarray/template.indirect.array/indirect.array.assign/slice_assignment.pass.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 valarray<value_type>& v) const;
+
+#include <valarray>
+#include <cassert>
+
+int main(int, char**)
+{
+    typedef std::valarray<double> matrix;
+
+    matrix m = { 0, 0, 0 };
+
+    m[std::slice(0, 3, 1)] = {1, 2, 3};
+
+    assert(m[0] == 1);
+    assert(m[1] == 2);
+    assert(m[2] == 3);
+
+    return 0;
+}
Index: include/valarray
===================================================================
--- include/valarray
+++ include/valarray
@@ -1263,6 +1263,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)
@@ -1302,6 +1305,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.188606.patch
Type: text/x-patch
Size: 1875 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190227/34834c5b/attachment.bin>


More information about the libcxx-commits mailing list