Skip to content

Commit 644f68e

Browse files
committed
[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
1 parent f80b630 commit 644f68e

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

libcxx/include/valarray

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public:
136136
void operator>>=(const valarray<value_type>& v) const;
137137
138138
void operator=(const value_type& x) const;
139+
void operator=(const valarray<T>& val_arr) const;
139140
140141
slice_array() = delete;
141142
};
@@ -1264,6 +1265,9 @@ public:
12641265
_LIBCPP_INLINE_VISIBILITY
12651266
void operator=(const value_type& __x) const;
12661267

1268+
_LIBCPP_INLINE_VISIBILITY
1269+
void operator=(const valarray<value_type>& __va) const;
1270+
12671271
private:
12681272
_LIBCPP_INLINE_VISIBILITY
12691273
slice_array(const slice& __sl, const valarray<value_type>& __v)
@@ -1303,6 +1307,15 @@ slice_array<_Tp>::operator=(const _Expr& __v) const
13031307
*__t = __v[__i];
13041308
}
13051309

1310+
template <class _Tp>
1311+
inline void
1312+
slice_array<_Tp>::operator=(const valarray<value_type>& __va) const
1313+
{
1314+
value_type* __t = __vp_;
1315+
for (size_t __i = 0; __i < __va.size(); ++__i, __t += __stride_)
1316+
*__t = __va[__i];
1317+
}
1318+
13061319
template <class _Tp>
13071320
template <class _Expr>
13081321
inline
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <valarray>
10+
11+
// template <class T> class slice_array
12+
13+
// void operator=(const T& value) const;
14+
15+
#include <valarray>
16+
#include <cassert>
17+
18+
#include "test_macros.h"
19+
20+
int main(int, char**)
21+
{
22+
double a[] = { 0, 0, 0 };
23+
std::valarray<double> m(a, sizeof(a)/sizeof(a[0]));
24+
std::slice_array<double> s = m[std::slice(0, 3, 1)];
25+
s = 42;
26+
assert(m[0] == 42);
27+
assert(m[1] == 42);
28+
assert(m[2] == 42);
29+
30+
ASSERT_SAME_TYPE(decltype(s = 42), void);
31+
32+
return 0;
33+
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ int main(int, char**)
2323
int a2[] = {-1, -2, -3, -4, -5};
2424
std::valarray<int> v1(a1, sizeof(a1)/sizeof(a1[0]));
2525
std::valarray<int> v2(a2, sizeof(a2)/sizeof(a2[0]));
26-
v1[std::slice(1, 5, 3)] = v2;
26+
std::slice_array<int> s1 = v1[std::slice(1, 5, 3)];
27+
s1 = v2;
2728
assert(v1.size() == 16);
2829
assert(v1[ 0] == 0);
2930
assert(v1[ 1] == -1);
@@ -42,5 +43,19 @@ int main(int, char**)
4243
assert(v1[14] == 14);
4344
assert(v1[15] == 15);
4445

45-
return 0;
46+
ASSERT_SAME_TYPE(decltype(s1 = v2), void);
47+
48+
// The initializer list constructor is disabled in C++03 mode.
49+
#if TEST_STD_VER > 03
50+
std::valarray<double> m = { 0, 0, 0 };
51+
std::slice_array<double> s2 = m[std::slice(0, 3, 1)];
52+
s2 = { 1, 2, 3 };
53+
assert(m[0] == 1);
54+
assert(m[1] == 2);
55+
assert(m[2] == 3);
56+
57+
ASSERT_SAME_TYPE(decltype(s2 = {1, 2, 3}), void);
58+
#endif // TEST_STD_VER > 03
59+
60+
return 0;
4661
}

0 commit comments

Comments
 (0)