From e00b11abad1ebfd86308052bc83507fcacb9cc82 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 18:01:13 +0300 Subject: [PATCH 01/20] #940 : slice_array's copy ctor --- stl/inc/valarray | 16 ++++----- tests/libcxx/expected_results.txt | 4 --- tests/libcxx/skipped_tests.txt | 4 --- tests/std/test.lst | 1 + .../GH_000940_missing_valarray_copy/env.lst | 4 +++ .../GH_000940_missing_valarray_copy/test.cpp | 35 +++++++++++++++++++ 6 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 tests/std/tests/GH_000940_missing_valarray_copy/env.lst create mode 100644 tests/std/tests/GH_000940_missing_valarray_copy/test.cpp diff --git a/stl/inc/valarray b/stl/inc/valarray index d17080150c7..9a362e7451d 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -938,9 +938,9 @@ public: slice_array() = delete; - slice_array(const slice_array&); // not defined + slice_array(const slice_array&) = default; - slice_array& operator=(const slice_array&); // not defined + slice_array& operator=(const slice_array&) = default; private: friend valarray<_Ty>; @@ -1079,9 +1079,9 @@ public: gslice_array() = delete; - gslice_array(const gslice_array&); // not defined + gslice_array(const gslice_array&) = default; - gslice_array& operator=(const gslice_array&); // not defined + gslice_array& operator=(const gslice_array&) = default; private: friend valarray<_Ty>; @@ -1176,9 +1176,9 @@ public: mask_array() = delete; - mask_array(const mask_array&); // not defined + mask_array(const mask_array&) = default; - mask_array& operator=(const mask_array&); // not defined + mask_array& operator=(const mask_array&) = default; private: friend valarray<_Ty>; @@ -1264,9 +1264,9 @@ public: indirect_array() = delete; - indirect_array(const indirect_array&); // not defined + indirect_array(const indirect_array&) = default; - indirect_array& operator=(const indirect_array&); // not defined + indirect_array& operator=(const indirect_array&) = default; private: friend valarray<_Ty>; diff --git a/tests/libcxx/expected_results.txt b/tests/libcxx/expected_results.txt index a0bb54a8454..89519ab8fda 100644 --- a/tests/libcxx/expected_results.txt +++ b/tests/libcxx/expected_results.txt @@ -567,10 +567,6 @@ std/re/re.traits/transform.pass.cpp FAIL # STL bug: Incorrect return types. std/numerics/complex.number/cmplx.over/pow.pass.cpp FAIL -# STL bug: Missing assignment operators. -std/numerics/numarray/template.mask.array/mask.array.assign/mask_array.pass.cpp FAIL -std/numerics/numarray/template.slice.array/slice.arr.assign/slice_array.pass.cpp FAIL - # STL bug: We allow fill() and swap() for array. std/containers/sequences/array/array.fill/fill.fail.cpp FAIL std/containers/sequences/array/array.swap/swap.fail.cpp FAIL diff --git a/tests/libcxx/skipped_tests.txt b/tests/libcxx/skipped_tests.txt index 92764fd5be5..4ba5f0b6b76 100644 --- a/tests/libcxx/skipped_tests.txt +++ b/tests/libcxx/skipped_tests.txt @@ -567,10 +567,6 @@ re\re.traits\transform.pass.cpp # STL bug: Incorrect return types. numerics\complex.number\cmplx.over\pow.pass.cpp -# STL bug: Missing assignment operators. -numerics\numarray\template.mask.array\mask.array.assign\mask_array.pass.cpp -numerics\numarray\template.slice.array\slice.arr.assign\slice_array.pass.cpp - # STL bug: We allow fill() and swap() for array. containers\sequences\array\array.fill\fill.fail.cpp containers\sequences\array\array.swap\swap.fail.cpp diff --git a/tests/std/test.lst b/tests/std/test.lst index 6358d0afa94..48fb3feb538 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -160,6 +160,7 @@ tests\GH_000545_include_compare tests\GH_000685_condition_variable_any tests\GH_000690_overaligned_function tests\GH_000890_pow_template +tests\GH_000940_missing_valarray_copy tests\LWG2597_complex_branch_cut tests\LWG3018_shared_ptr_function tests\P0024R2_parallel_algorithms_adjacent_difference diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/env.lst b/tests/std/tests/GH_000940_missing_valarray_copy/env.lst new file mode 100644 index 00000000000..19f025bd0e6 --- /dev/null +++ b/tests/std/tests/GH_000940_missing_valarray_copy/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_matrix.lst diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp new file mode 100644 index 00000000000..17930438bfb --- /dev/null +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +int main() { + std::valarray v({1, 2, 3, 4, 5}); + + std::slice_array slice_array = v[std::slice(2, 2, 2)]; + std::slice_array slice_array_copy = slice_array; + + std::slice_array other_array_slice = v[std::slice(1, 1, 1)]; + other_array_slice = slice_array; + + std::gslice gslice(1, std::valarray{2, 1}, std::valarray{1, 2}); + std::gslice_array gslice_array = v[gslice]; + std::gslice_array gslice_array_copy = gslice_array; + + std::gslice other_gslice(1, std::valarray{1, 1}, std::valarray{1, 1}); + std::gslice_array other_gslice_array = v[other_gslice]; + other_gslice_array = gslice_array; + + std::valarray mask({true, false, false, true, true}); + std::mask_array mask_array = v[mask]; + std::mask_array mask_array_copy = mask_array; + + std::valarray indices({2, 4}); + std::indirect_array indirect_array = v[indices]; + std::indirect_array indirect_array_copy = indirect_array; + + std::valarray other_indices({2, 4}); + std::indirect_array other_indirect_array = v[other_indices]; + other_indirect_array = indirect_array; + + return 0; +} From 563960d2e52446e62ec81cf7599b99e87028e44f Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 18:02:32 +0300 Subject: [PATCH 02/20] license --- tests/std/tests/GH_000940_missing_valarray_copy/test.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 17930438bfb..66b6fb3034b 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + #include #include #include From a445153e32cbc8f915ef192cff2d429606101e52 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 18:04:27 +0300 Subject: [PATCH 03/20] one missed case --- tests/std/tests/GH_000940_missing_valarray_copy/test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 66b6fb3034b..6c555b611c8 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -26,6 +26,10 @@ int main() { std::mask_array mask_array = v[mask]; std::mask_array mask_array_copy = mask_array; + std::valarray ohter_mask({false, false, true, false, false}); + std::mask_array other_mask_array = v[ohter_mask]; + other_mask_array = mask_array; + std::valarray indices({2, 4}); std::indirect_array indirect_array = v[indices]; std::indirect_array indirect_array_copy = indirect_array; From e10ba6a81cceaf28abd78f8136ea09bcd441e3c7 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 18:34:39 +0300 Subject: [PATCH 04/20] Seems correct implementation of assignments Though sub-optimal --- stl/inc/valarray | 24 +++++++++++++++---- .../GH_000940_missing_valarray_copy/test.cpp | 4 ++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index 9a362e7451d..a1f322e47ee 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -940,7 +940,11 @@ public: slice_array(const slice_array&) = default; - slice_array& operator=(const slice_array&) = default; + slice_array& operator=(const slice_array& _Right) { + valarray<_Ty> _Tmp = _Right; + *this = _Tmp; + return *this; + } private: friend valarray<_Ty>; @@ -1081,7 +1085,11 @@ public: gslice_array(const gslice_array&) = default; - gslice_array& operator=(const gslice_array&) = default; + gslice_array& operator=(const gslice_array& _Right) { + valarray<_Ty> _Tmp = _Right; + *this = _Tmp; + return *this; + } private: friend valarray<_Ty>; @@ -1178,7 +1186,11 @@ public: mask_array(const mask_array&) = default; - mask_array& operator=(const mask_array&) = default; + mask_array& operator=(const mask_array& _Right) { + valarray<_Ty> _Tmp = _Right; + *this = _Tmp; + return *this; + } private: friend valarray<_Ty>; @@ -1266,7 +1278,11 @@ public: indirect_array(const indirect_array&) = default; - indirect_array& operator=(const indirect_array&) = default; + indirect_array& operator=(const indirect_array _Right) { + valarray<_Ty> _Tmp = _Right; + *this = _Tmp; + return *this; + } private: friend valarray<_Ty>; diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 6c555b611c8..8e53873c230 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -26,8 +26,8 @@ int main() { std::mask_array mask_array = v[mask]; std::mask_array mask_array_copy = mask_array; - std::valarray ohter_mask({false, false, true, false, false}); - std::mask_array other_mask_array = v[ohter_mask]; + std::valarray other_mask({false, true, true, true, false}); + std::mask_array other_mask_array = v[other_mask]; other_mask_array = mask_array; std::valarray indices({2, 4}); From 7c1ef11f7768a04ce5e376fa6d764ec2e79e699f Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 18:38:23 +0300 Subject: [PATCH 05/20] missing ref --- stl/inc/valarray | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index a1f322e47ee..c0263e5da3e 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -1278,7 +1278,7 @@ public: indirect_array(const indirect_array&) = default; - indirect_array& operator=(const indirect_array _Right) { + indirect_array& operator=(const indirect_array& _Right) { valarray<_Ty> _Tmp = _Right; *this = _Tmp; return *this; From 8c97417d74258c4b25a44dda1c49627054c04a10 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 19:20:46 +0300 Subject: [PATCH 06/20] Complete test --- .../GH_000940_missing_valarray_copy/test.cpp | 85 +++++++++++++++++-- 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 8e53873c230..1841143e217 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -5,38 +5,105 @@ #include #include -int main() { - std::valarray v({1, 2, 3, 4, 5}); +void test_slice() { + std::valarray v({0, 1, 2, 3, 4}); std::slice_array slice_array = v[std::slice(2, 2, 2)]; std::slice_array slice_array_copy = slice_array; - std::slice_array other_array_slice = v[std::slice(1, 1, 1)]; + assert(v[0] == 0); + assert(v[1] == 1); + assert(v[2] == 2); + assert(v[3] == 3); + assert(v[4] == 4); + + std::slice_array other_array_slice = v[std::slice(1, 2, 1)]; other_array_slice = slice_array; - std::gslice gslice(1, std::valarray{2, 1}, std::valarray{1, 2}); + assert(v[0] == 0); + assert(v[1] == 2); + assert(v[2] == 4); + assert(v[3] == 3); + assert(v[4] == 4); +} + +void test_gslice() { + std::valarray v({0, 1, 2, 3, 4}); + + std::gslice gslice(2, std::valarray({2}), std::valarray({2})); std::gslice_array gslice_array = v[gslice]; std::gslice_array gslice_array_copy = gslice_array; - std::gslice other_gslice(1, std::valarray{1, 1}, std::valarray{1, 1}); + assert(v[0] == 0); + assert(v[1] == 1); + assert(v[2] == 2); + assert(v[3] == 3); + assert(v[4] == 4); + + std::gslice other_gslice(1, std::valarray({2}), std::valarray({1})); std::gslice_array other_gslice_array = v[other_gslice]; other_gslice_array = gslice_array; - std::valarray mask({true, false, false, true, true}); + assert(v[0] == 0); + assert(v[1] == 2); + assert(v[2] == 4); + assert(v[3] == 3); + assert(v[4] == 4); +} + +void test_mask() { + std::valarray v({0, 1, 2, 3, 4}); + + std::valarray mask({true, false, false, false, true}); std::mask_array mask_array = v[mask]; std::mask_array mask_array_copy = mask_array; - std::valarray other_mask({false, true, true, true, false}); + assert(v[0] == 0); + assert(v[1] == 1); + assert(v[2] == 2); + assert(v[3] == 3); + assert(v[4] == 4); + + std::valarray other_mask({false, true, true, false, false}); std::mask_array other_mask_array = v[other_mask]; other_mask_array = mask_array; - std::valarray indices({2, 4}); + assert(v[0] == 0); + assert(v[1] == 0); + assert(v[2] == 4); + assert(v[3] == 3); + assert(v[4] == 4); +} + +void test_indirect() { + std::valarray v({0, 1, 2, 3, 4}); + + std::valarray indices({2, 3}); std::indirect_array indirect_array = v[indices]; std::indirect_array indirect_array_copy = indirect_array; - std::valarray other_indices({2, 4}); + assert(v[0] == 0); + assert(v[1] == 1); + assert(v[2] == 2); + assert(v[3] == 3); + assert(v[4] == 4); + + std::valarray other_indices({4, 0}); std::indirect_array other_indirect_array = v[other_indices]; other_indirect_array = indirect_array; + assert(v[0] == 3); + assert(v[1] == 1); + assert(v[2] == 2); + assert(v[3] == 3); + assert(v[4] == 2); +} + + +int main() { + test_slice(); + test_gslice(); + test_mask(); + test_indirect(); return 0; } From 974ccb21d4a278e3dba2168a75c2ab4b9a6a76ff Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 20:29:31 +0300 Subject: [PATCH 07/20] test warnings fix --- tests/std/tests/GH_000940_missing_valarray_copy/test.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 1841143e217..1864d1158b6 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -10,6 +10,7 @@ void test_slice() { std::slice_array slice_array = v[std::slice(2, 2, 2)]; std::slice_array slice_array_copy = slice_array; + (void) slice_array_copy; assert(v[0] == 0); assert(v[1] == 1); @@ -33,7 +34,8 @@ void test_gslice() { std::gslice gslice(2, std::valarray({2}), std::valarray({2})); std::gslice_array gslice_array = v[gslice]; std::gslice_array gslice_array_copy = gslice_array; - + (void) gslice_array_copy; + assert(v[0] == 0); assert(v[1] == 1); assert(v[2] == 2); @@ -57,6 +59,7 @@ void test_mask() { std::valarray mask({true, false, false, false, true}); std::mask_array mask_array = v[mask]; std::mask_array mask_array_copy = mask_array; + (void) mask_array_copy; assert(v[0] == 0); assert(v[1] == 1); @@ -81,6 +84,7 @@ void test_indirect() { std::valarray indices({2, 3}); std::indirect_array indirect_array = v[indices]; std::indirect_array indirect_array_copy = indirect_array; + (void) indirect_array_copy; assert(v[0] == 0); assert(v[1] == 1); From b776945f9aa105e87ec9a857e79647cc4e784b37 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 20:29:52 +0300 Subject: [PATCH 08/20] optimize --- stl/inc/valarray | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index c0263e5da3e..0889dc9f540 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -941,8 +941,11 @@ public: slice_array(const slice_array&) = default; slice_array& operator=(const slice_array& _Right) { - valarray<_Ty> _Tmp = _Right; - *this = _Tmp; + size_t _Dst_off = _Start; + size_t _Src_off = _Right._Start; + for (size_t _Idx = 0; _Idx < _Len; ++_Idx, _Dst_off += _Stride, _Src_off += _Right._Stride) { + _Myptr[_Dst_off] = _Right._Myptr[_Src_off]; + } return *this; } @@ -1086,8 +1089,12 @@ public: gslice_array(const gslice_array&) = default; gslice_array& operator=(const gslice_array& _Right) { - valarray<_Ty> _Tmp = _Right; - *this = _Tmp; + _Sizarray _Dst_indexarray(size_t{0}, _Nslice()); + _Sizarray _Src_indexarray(size_t{0}, _Right._Nslice()); + size_t _Size = _Totlen(); + for (size_t _Idx = 0; _Idx < _Size; ++_Idx) { + _Myptr[_Off(_Dst_indexarray)] = _Right._Myptr[_Right._Off(_Src_indexarray)]; + } return *this; } @@ -1187,8 +1194,18 @@ public: mask_array(const mask_array&) = default; mask_array& operator=(const mask_array& _Right) { - valarray<_Ty> _Tmp = _Right; - *this = _Tmp; + size_t _Size = _Totlen(); + for (size_t _Idx = 0, _Dst_off = 0, _Src_off = 0; _Idx < _Size; ++_Idx, ++_Src_off, ++_Dst_off) { + while (!_Mask(_Dst_off)) { + ++_Dst_off; + } + + while (!_Right._Mask(_Src_off)) { + ++_Src_off; + } + + _Myptr[_Dst_off] = _Right._Myptr[_Src_off]; + } return *this; } @@ -1279,8 +1296,10 @@ public: indirect_array(const indirect_array&) = default; indirect_array& operator=(const indirect_array& _Right) { - valarray<_Ty> _Tmp = _Right; - *this = _Tmp; + size_t _Size = _Totlen(); + for (size_t _Idx = 0; _Idx < _Size; ++_Idx) { + _Myptr[_Indir(_Idx)] = _Right._Myptr[_Right._Indir(_Idx)]; + } return *this; } From 7d4b0401db3eff0ae433504d6fe3dbe785e692d6 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sat, 4 Jul 2020 20:33:50 +0300 Subject: [PATCH 09/20] whitespace --- tests/std/tests/GH_000940_missing_valarray_copy/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 1864d1158b6..25d6bdab825 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -35,7 +35,7 @@ void test_gslice() { std::gslice_array gslice_array = v[gslice]; std::gslice_array gslice_array_copy = gslice_array; (void) gslice_array_copy; - + assert(v[0] == 0); assert(v[1] == 1); assert(v[2] == 2); From e4afd4b7a44060c994819055e4a1a523f0571012 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 5 Jul 2020 21:40:48 +0300 Subject: [PATCH 10/20] Optimize `mask_array::operator=` Mostly to use `_Start_off` and `_Next_off` in #991 --- stl/inc/valarray | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index 0889dc9f540..9d7ccc8fe09 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -1178,6 +1178,24 @@ public: return _Mybool[_Idx]; } + size_t _Start_off() const { + size_t _Off = 0; + size_t _Size = _Mybool.size(); + while (_Off < _Size && !_Mybool[_Off]) { + ++_Off; + } + return _Off; + } + + size_t _Next_off(size_t _Off) const { + size_t _Size = _Mybool.size(); + do { + ++_Off; + } while (_Off < _Size && !_Mybool[_Off]); + return _Off; + } + + size_t _Totlen() const { size_t _Count = 0; for (size_t _Idx = 0; _Idx < _Mybool.size(); ++_Idx) { @@ -1194,16 +1212,10 @@ public: mask_array(const mask_array&) = default; mask_array& operator=(const mask_array& _Right) { - size_t _Size = _Totlen(); - for (size_t _Idx = 0, _Dst_off = 0, _Src_off = 0; _Idx < _Size; ++_Idx, ++_Src_off, ++_Dst_off) { - while (!_Mask(_Dst_off)) { - ++_Dst_off; - } - - while (!_Right._Mask(_Src_off)) { - ++_Src_off; - } - + size_t _Size = _Mybool.size(); + size_t _Dst_off = _Start_off(); + size_t _Src_off = _Right._Start_off(); + for (; _Dst_off < _Size; _Src_off = _Next_off(_Src_off), _Dst_off = _Next_off(_Dst_off)) { _Myptr[_Dst_off] = _Right._Myptr[_Src_off]; } return *this; From ae46fb4d9e7b41ab376d1a4828583b6fc4f946df Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 5 Jul 2020 21:43:06 +0300 Subject: [PATCH 11/20] clang format --- stl/inc/valarray | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index 9d7ccc8fe09..b8c2d68ec53 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -1179,7 +1179,7 @@ public: } size_t _Start_off() const { - size_t _Off = 0; + size_t _Off = 0; size_t _Size = _Mybool.size(); while (_Off < _Size && !_Mybool[_Off]) { ++_Off; @@ -1195,7 +1195,6 @@ public: return _Off; } - size_t _Totlen() const { size_t _Count = 0; for (size_t _Idx = 0; _Idx < _Mybool.size(); ++_Idx) { From 662fbe999f9d6b69fe5b07abc741b15d6a6d0f7a Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 5 Jul 2020 21:52:58 +0300 Subject: [PATCH 12/20] _Right --- stl/inc/valarray | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index b8c2d68ec53..a8a9be1a333 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -1214,7 +1214,7 @@ public: size_t _Size = _Mybool.size(); size_t _Dst_off = _Start_off(); size_t _Src_off = _Right._Start_off(); - for (; _Dst_off < _Size; _Src_off = _Next_off(_Src_off), _Dst_off = _Next_off(_Dst_off)) { + for (; _Dst_off < _Size; _Src_off = _Right._Next_off(_Src_off), _Dst_off = _Next_off(_Dst_off)) { _Myptr[_Dst_off] = _Right._Myptr[_Src_off]; } return *this; From dd3d5474d062aa7fae28a8f2b835daa8c9f7e690 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Mon, 6 Jul 2020 09:48:49 +0300 Subject: [PATCH 13/20] Update stl/inc/valarray Co-authored-by: S. B. Tam --- stl/inc/valarray | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index a8a9be1a333..68b0e2110ca 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -940,7 +940,7 @@ public: slice_array(const slice_array&) = default; - slice_array& operator=(const slice_array& _Right) { + const slice_array& operator=(const slice_array& _Right) const { size_t _Dst_off = _Start; size_t _Src_off = _Right._Start; for (size_t _Idx = 0; _Idx < _Len; ++_Idx, _Dst_off += _Stride, _Src_off += _Right._Stride) { From 243bb3ad9d56648e64a3838eb03ab2af05d92e87 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Mon, 6 Jul 2020 09:48:56 +0300 Subject: [PATCH 14/20] Update stl/inc/valarray Co-authored-by: S. B. Tam --- stl/inc/valarray | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index 68b0e2110ca..30e2e44efc9 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -1088,7 +1088,7 @@ public: gslice_array(const gslice_array&) = default; - gslice_array& operator=(const gslice_array& _Right) { + const gslice_array& operator=(const gslice_array& _Right) const { _Sizarray _Dst_indexarray(size_t{0}, _Nslice()); _Sizarray _Src_indexarray(size_t{0}, _Right._Nslice()); size_t _Size = _Totlen(); From 5e7a331594395abf6a18e75bf7594463f1c12ffa Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Mon, 6 Jul 2020 09:49:04 +0300 Subject: [PATCH 15/20] Update stl/inc/valarray Co-authored-by: S. B. Tam --- stl/inc/valarray | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index 30e2e44efc9..75ef87dd381 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -1306,7 +1306,7 @@ public: indirect_array(const indirect_array&) = default; - indirect_array& operator=(const indirect_array& _Right) { + const indirect_array& operator=(const indirect_array& _Right) const { size_t _Size = _Totlen(); for (size_t _Idx = 0; _Idx < _Size; ++_Idx) { _Myptr[_Indir(_Idx)] = _Right._Myptr[_Right._Indir(_Idx)]; From 8b847137430872e560b690cfa9486bb0e5123ff1 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Mon, 6 Jul 2020 09:49:11 +0300 Subject: [PATCH 16/20] Update stl/inc/valarray Co-authored-by: S. B. Tam --- stl/inc/valarray | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index 75ef87dd381..e244fff68dd 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -1210,7 +1210,7 @@ public: mask_array(const mask_array&) = default; - mask_array& operator=(const mask_array& _Right) { + const mask_array& operator=(const mask_array& _Right) const { size_t _Size = _Mybool.size(); size_t _Dst_off = _Start_off(); size_t _Src_off = _Right._Start_off(); From 471720cdfd422a144de03870315c60bf3c0be42a Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Tue, 28 Jul 2020 06:18:01 +0300 Subject: [PATCH 17/20] Update tests/std/tests/GH_000940_missing_valarray_copy/test.cpp Co-authored-by: Casey Carter --- tests/std/tests/GH_000940_missing_valarray_copy/test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 25d6bdab825..69e9efd3fac 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -103,7 +103,6 @@ void test_indirect() { assert(v[4] == 2); } - int main() { test_slice(); test_gslice(); From caf1fc019ce6ba6b41dfc00b8526853ec374f16b Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Tue, 28 Jul 2020 06:35:21 +0300 Subject: [PATCH 18/20] review comments --- stl/inc/valarray | 36 ++++----- .../GH_000940_missing_valarray_copy/test.cpp | 77 ++++++------------- 2 files changed, 43 insertions(+), 70 deletions(-) diff --git a/stl/inc/valarray b/stl/inc/valarray index e244fff68dd..4c50bff4795 100644 --- a/stl/inc/valarray +++ b/stl/inc/valarray @@ -999,7 +999,7 @@ public: return _Ans; } - size_t _Totlen() const { + _NODISCARD size_t _Totlen() const { if (_Len.size() == 0) { return 0; } @@ -1080,7 +1080,7 @@ public: _GSLOP(>>= _Right[_Idx]); } - _Ty& _Data(size_t _Idx) const { + _NODISCARD _Ty& _Data(size_t _Idx) const { return _Myptr[_Idx]; } @@ -1091,7 +1091,7 @@ public: const gslice_array& operator=(const gslice_array& _Right) const { _Sizarray _Dst_indexarray(size_t{0}, _Nslice()); _Sizarray _Src_indexarray(size_t{0}, _Right._Nslice()); - size_t _Size = _Totlen(); + const size_t _Size = _Totlen(); for (size_t _Idx = 0; _Idx < _Size; ++_Idx) { _Myptr[_Off(_Dst_indexarray)] = _Right._Myptr[_Right._Off(_Src_indexarray)]; } @@ -1170,32 +1170,32 @@ public: _MOP(>>= _Right[_Idx]); } - _Ty& _Data(size_t _Idx) const { + _NODISCARD _Ty& _Data(size_t _Idx) const { return _Myptr[_Idx]; } - bool _Mask(size_t _Idx) const { + _NODISCARD bool _Mask(size_t _Idx) const { return _Mybool[_Idx]; } - size_t _Start_off() const { - size_t _Off = 0; - size_t _Size = _Mybool.size(); + _NODISCARD size_t _Start_off() const { + size_t _Off = 0; + const size_t _Size = _Mybool.size(); while (_Off < _Size && !_Mybool[_Off]) { ++_Off; } return _Off; } - size_t _Next_off(size_t _Off) const { - size_t _Size = _Mybool.size(); + _NODISCARD size_t _Next_off(size_t _Off) const { + const size_t _Size = _Mybool.size(); do { ++_Off; } while (_Off < _Size && !_Mybool[_Off]); return _Off; } - size_t _Totlen() const { + _NODISCARD size_t _Totlen() const { size_t _Count = 0; for (size_t _Idx = 0; _Idx < _Mybool.size(); ++_Idx) { if (_Mybool[_Idx]) { @@ -1211,9 +1211,9 @@ public: mask_array(const mask_array&) = default; const mask_array& operator=(const mask_array& _Right) const { - size_t _Size = _Mybool.size(); - size_t _Dst_off = _Start_off(); - size_t _Src_off = _Right._Start_off(); + const size_t _Size = _Mybool.size(); + size_t _Dst_off = _Start_off(); + size_t _Src_off = _Right._Start_off(); for (; _Dst_off < _Size; _Src_off = _Right._Next_off(_Src_off), _Dst_off = _Next_off(_Dst_off)) { _Myptr[_Dst_off] = _Right._Myptr[_Src_off]; } @@ -1290,15 +1290,15 @@ public: _IOP(>>= _Right[_Idx]); } - _Ty& _Data(size_t _Idx) const { + _NODISCARD _Ty& _Data(size_t _Idx) const { return _Myptr[_Idx]; } - size_t _Indir(size_t _Idx) const { + _NODISCARD size_t _Indir(size_t _Idx) const { return _Myindarr[_Idx]; } - size_t _Totlen() const { + _NODISCARD size_t _Totlen() const { return _Myindarr.size(); } @@ -1307,7 +1307,7 @@ public: indirect_array(const indirect_array&) = default; const indirect_array& operator=(const indirect_array& _Right) const { - size_t _Size = _Totlen(); + const size_t _Size = _Totlen(); for (size_t _Idx = 0; _Idx < _Size; ++_Idx) { _Myptr[_Indir(_Idx)] = _Right._Myptr[_Right._Indir(_Idx)]; } diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 25d6bdab825..c5f00c5675f 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -5,102 +5,75 @@ #include #include +template +bool eq(const std::valarray& v, std::initializer_list il) { + return std::equal(begin(v), end(v), il.begin(), il.end()); +} + void test_slice() { - std::valarray v({0, 1, 2, 3, 4}); + std::valarray v{0, 1, 2, 3, 4}; std::slice_array slice_array = v[std::slice(2, 2, 2)]; std::slice_array slice_array_copy = slice_array; (void) slice_array_copy; - assert(v[0] == 0); - assert(v[1] == 1); - assert(v[2] == 2); - assert(v[3] == 3); - assert(v[4] == 4); + assert(eq(v, {0, 1, 2, 3, 4})); - std::slice_array other_array_slice = v[std::slice(1, 2, 1)]; - other_array_slice = slice_array; + std::slice_array other_slice_array = v[std::slice(1, 2, 1)]; + other_slice_array = slice_array; - assert(v[0] == 0); - assert(v[1] == 2); - assert(v[2] == 4); - assert(v[3] == 3); - assert(v[4] == 4); + assert(eq(v, {0, 2, 4, 3, 4})); } void test_gslice() { - std::valarray v({0, 1, 2, 3, 4}); + std::valarray v{0, 1, 2, 3, 4}; - std::gslice gslice(2, std::valarray({2}), std::valarray({2})); + std::gslice gslice(2, std::valarray{2}, std::valarray{2}); std::gslice_array gslice_array = v[gslice]; std::gslice_array gslice_array_copy = gslice_array; (void) gslice_array_copy; - assert(v[0] == 0); - assert(v[1] == 1); - assert(v[2] == 2); - assert(v[3] == 3); - assert(v[4] == 4); + assert(eq(v, {0, 1, 2, 3, 4})); - std::gslice other_gslice(1, std::valarray({2}), std::valarray({1})); + std::gslice other_gslice(1, std::valarray{2}, std::valarray{1}); std::gslice_array other_gslice_array = v[other_gslice]; other_gslice_array = gslice_array; - assert(v[0] == 0); - assert(v[1] == 2); - assert(v[2] == 4); - assert(v[3] == 3); - assert(v[4] == 4); + assert(eq(v, {0, 2, 4, 3, 4})); } void test_mask() { - std::valarray v({0, 1, 2, 3, 4}); + std::valarray v{0, 1, 2, 3, 4}; - std::valarray mask({true, false, false, false, true}); + std::valarray mask{true, false, false, false, true}; std::mask_array mask_array = v[mask]; std::mask_array mask_array_copy = mask_array; (void) mask_array_copy; - assert(v[0] == 0); - assert(v[1] == 1); - assert(v[2] == 2); - assert(v[3] == 3); - assert(v[4] == 4); + assert(eq(v, {0, 1, 2, 3, 4})); - std::valarray other_mask({false, true, true, false, false}); + std::valarray other_mask{false, true, true, false, false}; std::mask_array other_mask_array = v[other_mask]; other_mask_array = mask_array; - assert(v[0] == 0); - assert(v[1] == 0); - assert(v[2] == 4); - assert(v[3] == 3); - assert(v[4] == 4); + assert(eq(v, {0, 0, 4, 3, 4})); } void test_indirect() { - std::valarray v({0, 1, 2, 3, 4}); + std::valarray v{0, 1, 2, 3, 4}; - std::valarray indices({2, 3}); + std::valarray indices{2, 3}; std::indirect_array indirect_array = v[indices]; std::indirect_array indirect_array_copy = indirect_array; (void) indirect_array_copy; - assert(v[0] == 0); - assert(v[1] == 1); - assert(v[2] == 2); - assert(v[3] == 3); - assert(v[4] == 4); + assert(eq(v, {0, 1, 2, 3, 4})); - std::valarray other_indices({4, 0}); + std::valarray other_indices{4, 0}; std::indirect_array other_indirect_array = v[other_indices]; other_indirect_array = indirect_array; - assert(v[0] == 3); - assert(v[1] == 1); - assert(v[2] == 2); - assert(v[3] == 3); - assert(v[4] == 2); + assert(eq(v, {3, 1, 2, 3, 2})); } From 454246e73ff7f367d9ee259de86a0dab0272e8c6 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Tue, 28 Jul 2020 19:15:36 +0300 Subject: [PATCH 19/20] move slices to avoid overlap --- tests/std/tests/GH_000940_missing_valarray_copy/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 2990f06fb0a..76675e3cdd3 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -19,10 +19,10 @@ void test_slice() { assert(eq(v, {0, 1, 2, 3, 4})); - std::slice_array other_slice_array = v[std::slice(1, 2, 1)]; + std::slice_array other_slice_array = v[std::slice(0, 2, 1)]; other_slice_array = slice_array; - assert(eq(v, {0, 2, 4, 3, 4})); + assert(eq(v, {2, 4, 2, 3, 4})); } void test_gslice() { @@ -35,11 +35,11 @@ void test_gslice() { assert(eq(v, {0, 1, 2, 3, 4})); - std::gslice other_gslice(1, std::valarray{2}, std::valarray{1}); + std::gslice other_gslice(0, std::valarray{2}, std::valarray{1}); std::gslice_array other_gslice_array = v[other_gslice]; other_gslice_array = gslice_array; - assert(eq(v, {0, 2, 4, 3, 4})); + assert(eq(v, {2, 4, 2, 3, 4})); } void test_mask() { From 11e14129784ad350b8c0f78d5ad98c9778919a92 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 29 Jul 2020 02:28:44 -0700 Subject: [PATCH 20/20] Include algorithm and cstddef. --- tests/std/tests/GH_000940_missing_valarray_copy/test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp index 76675e3cdd3..91106130487 100644 --- a/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp +++ b/tests/std/tests/GH_000940_missing_valarray_copy/test.cpp @@ -1,7 +1,9 @@ // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include #include +#include #include #include