Skip to content

Commit ff50071

Browse files
committed
v0.10.0 - conversion tests/example with std::valarray
1 parent b73563e commit ff50071

File tree

5 files changed

+109
-15
lines changed

5 files changed

+109
-15
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ Remember, this is a c++20 library, so that needs to be the minimum standard that
186186
187187
## Status
188188
189-
Current version: `v0.9.21`
189+
Current version: `v0.10.0`
190190
191191
* **All the intended vector and matrix functionality is implemented.**
192192
* First pass at test coverage. Everything major has some tests, but code coverage is not 100%.
193-
* [Released v0.9.0](https://github.com/davidbrowne/dsga/releases/tag/v0.9.0)
193+
* [Released v0.10.0](https://github.com/davidbrowne/dsga/releases/tag/v0.10.0)
194194
195195
### The next steps
196196
* Example projects: need small, medium, and large examples. The quick peek at the top of this page is a start, as is a [more detailed generic version of the example](docs/DETAILS.md#detailed-generic-example).
@@ -220,7 +220,7 @@ The tests have been most recently run on:
220220
[doctest] run with "--help" for options
221221
===============================================================================
222222
[doctest] test cases: 100 | 100 passed | 0 failed | 0 skipped
223-
[doctest] assertions: 2391 | 2391 passed | 0 failed |
223+
[doctest] assertions: 2394 | 2394 passed | 0 failed |
224224
[doctest] Status: SUCCESS!
225225
```
226226

@@ -231,7 +231,7 @@ The tests have been most recently run on:
231231
[doctest] run with "--help" for options
232232
===============================================================================
233233
[doctest] test cases: 100 | 100 passed | 0 failed | 0 skipped
234-
[doctest] assertions: 2391 | 2391 passed | 0 failed |
234+
[doctest] assertions: 2394 | 2394 passed | 0 failed |
235235
[doctest] Status: SUCCESS!
236236
```
237237

@@ -244,7 +244,7 @@ Performs all the unit tests except where there is lack of support for ```std::is
244244
[doctest] run with "--help" for options
245245
===============================================================================
246246
[doctest] test cases: 100 | 100 passed | 0 failed | 0 skipped
247-
[doctest] assertions: 2375 | 2375 passed | 0 failed |
247+
[doctest] assertions: 2378 | 2378 passed | 0 failed |
248248
[doctest] Status: SUCCESS!
249249
```
250250

@@ -257,7 +257,7 @@ Performs all the unit tests except where there is lack of support for ```std::is
257257
[doctest] run with "--help" for options
258258
===============================================================================
259259
[doctest] test cases: 100 | 100 passed | 0 failed | 0 skipped
260-
[doctest] assertions: 2391 | 2391 passed | 0 failed |
260+
[doctest] assertions: 2394 | 2394 passed | 0 failed |
261261
[doctest] Status: SUCCESS!
262262
```
263263

@@ -270,7 +270,7 @@ Performs all the unit tests except where there is lack of support for ```std::is
270270
[doctest] run with "--help" for options
271271
===============================================================================
272272
[doctest] test cases: 100 | 100 passed | 0 failed | 0 skipped
273-
[doctest] assertions: 2375 | 2375 passed | 0 failed |
273+
[doctest] assertions: 2378 | 2378 passed | 0 failed |
274274
[doctest] Status: SUCCESS!
275275
```
276276

examples/span_convert.hxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// fill vectors from spans
1111

1212
template <dsga::dimensional_scalar T, std::size_t S, typename U, std::size_t E>
13-
requires ((E != 0) && (E != std::dynamic_extent)) && dsga::non_bool_scalar<U> &&std::convertible_to<U, T>
13+
requires ((E != 0) && (E != std::dynamic_extent)) && dsga::non_bool_scalar<U> && std::convertible_to<U, T>
1414
constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<U, E> rhs)
1515
{
1616
constexpr std::size_t count = std::min(S, E);
@@ -19,7 +19,7 @@ constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<U, E> rhs
1919
}
2020

2121
template <dsga::dimensional_scalar T, std::size_t S, typename U, std::size_t E>
22-
requires ((E != 0) && (E != std::dynamic_extent)) && dsga::non_bool_scalar<U> &&std::convertible_to<U, T>
22+
requires ((E != 0) && (E != std::dynamic_extent)) && dsga::non_bool_scalar<U> && std::convertible_to<U, T>
2323
constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<const U, E> rhs)
2424
{
2525
constexpr std::size_t count = std::min(S, E);
@@ -28,7 +28,7 @@ constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<const U,
2828
}
2929

3030
template <dsga::dimensional_scalar T, std::size_t S, typename U, std::size_t E>
31-
requires ((E == 0) || (E == std::dynamic_extent)) && dsga::non_bool_scalar<U> &&std::convertible_to<U, T>
31+
requires ((E == 0) || (E == std::dynamic_extent)) && dsga::non_bool_scalar<U> && std::convertible_to<U, T>
3232
constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<U, E> rhs)
3333
{
3434
const std::size_t count = std::min(S, rhs.size());
@@ -37,7 +37,7 @@ constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<U, E> rhs
3737
}
3838

3939
template <dsga::dimensional_scalar T, std::size_t S, typename U, std::size_t E>
40-
requires ((E == 0) || (E == std::dynamic_extent)) && dsga::non_bool_scalar<U> &&std::convertible_to<U, T>
40+
requires ((E == 0) || (E == std::dynamic_extent)) && dsga::non_bool_scalar<U> && std::convertible_to<U, T>
4141
constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<const U, E> rhs)
4242
{
4343
const std::size_t count = std::min(S, rhs.size());

examples/valarray_convert.hxx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
// Copyright David Browne 2020-2023.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// https://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include "dsga.hxx"
8+
#include <valarray>
9+
10+
// fill vector from valarray
11+
12+
template <dsga::dimensional_scalar T, std::size_t S, typename U>
13+
requires std::convertible_to<U, T>
14+
void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::valarray<U> rhs)
15+
{
16+
const std::size_t count = std::min(S, rhs.size());
17+
for (std::size_t i = 0; i < count; ++i)
18+
lhs[i] = static_cast<T>(rhs[i]);
19+
}
20+
21+
// fill valarray from vector
22+
23+
template <dsga::dimensional_scalar T, std::size_t S, typename U>
24+
requires std::convertible_to<T, U>
25+
void copy_from_vector(std::valarray<U> &lhs, const dsga::basic_vector<T, S> &rhs)
26+
{
27+
const std::size_t count = std::min(S, lhs.size());
28+
for (std::size_t i = 0; i < count; ++i)
29+
lhs[i] = static_cast<U>(rhs[i]);
30+
}
31+
32+
// create a valarray from a vector
33+
34+
template <dsga::dimensional_scalar T, std::size_t S>
35+
auto to_valarray(const dsga::basic_vector<T, S> &v)
36+
{
37+
return[&]<std::size_t ...Is>(std::index_sequence<Is...>)
38+
{
39+
return std::valarray<T>{v[Is] ...};
40+
}(std::make_index_sequence<S>());
41+
}

include/dsga.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ inline void dsga_constexpr_assert_failed(Assert &&a) noexcept
5757
// version info
5858

5959
constexpr inline int DSGA_MAJOR_VERSION = 0;
60-
constexpr inline int DSGA_MINOR_VERSION = 9;
61-
constexpr inline int DSGA_PATCH_VERSION = 21;
60+
constexpr inline int DSGA_MINOR_VERSION = 10;
61+
constexpr inline int DSGA_PATCH_VERSION = 0;
6262

6363
namespace dsga
6464
{

tests/conversion_test.cxx

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using namespace dsga;
99

1010
#include <span>
11+
#include <valarray>
1112

1213
#if defined(__clang__)
1314
// clang 10.0 does not like colors on windows (link problems with isatty and fileno)
@@ -30,7 +31,7 @@ constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<U, E> rhs
3031
}
3132

3233
template <dsga::dimensional_scalar T, std::size_t S, typename U, std::size_t E>
33-
requires ((E != 0) && (E != std::dynamic_extent)) && dsga::non_bool_scalar<U> &&std::convertible_to<U, T>
34+
requires ((E != 0) && (E != std::dynamic_extent)) && dsga::non_bool_scalar<U> && std::convertible_to<U, T>
3435
constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<const U, E> rhs)
3536
{
3637
constexpr std::size_t count = std::min(S, E);
@@ -48,7 +49,7 @@ constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<U, E> rhs
4849
}
4950

5051
template <dsga::dimensional_scalar T, std::size_t S, typename U, std::size_t E>
51-
requires ((E == 0) || (E == std::dynamic_extent)) && dsga::non_bool_scalar<U> &&std::convertible_to<U, T>
52+
requires ((E == 0) || (E == std::dynamic_extent)) && dsga::non_bool_scalar<U> && std::convertible_to<U, T>
5253
constexpr void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::span<const U, E> rhs)
5354
{
5455
const std::size_t count = std::min(S, rhs.size());
@@ -88,6 +89,38 @@ constexpr std::array<T, C> from_vector_by_data_sequence(const dsga::vector_base<
8889
}(vec.sequence());
8990
}
9091

92+
// fill vector from valarray
93+
94+
template <dsga::dimensional_scalar T, std::size_t S, typename U>
95+
requires std::convertible_to<U, T>
96+
void copy_to_vector(dsga::basic_vector<T, S> &lhs, std::valarray<U> rhs)
97+
{
98+
const std::size_t count = std::min(S, rhs.size());
99+
for (std::size_t i = 0; i < count; ++i)
100+
lhs[i] = static_cast<T>(rhs[i]);
101+
}
102+
103+
// fill valarray from vector
104+
105+
template <dsga::dimensional_scalar T, std::size_t S, typename U>
106+
requires std::convertible_to<T, U>
107+
void copy_from_vector(std::valarray<U> &lhs, const dsga::basic_vector<T, S> &rhs)
108+
{
109+
const std::size_t count = std::min(S, lhs.size());
110+
for (std::size_t i = 0; i < count; ++i)
111+
lhs[i] = static_cast<U>(rhs[i]);
112+
}
113+
114+
// create a valarray from a vector
115+
116+
template <dsga::dimensional_scalar T, std::size_t S>
117+
auto to_valarray(const dsga::basic_vector<T, S> &v)
118+
{
119+
return[&]<std::size_t ...Is>(std::index_sequence<Is...>)
120+
{
121+
return std::valarray<T>{v[Is] ...};
122+
}(std::make_index_sequence<S>());
123+
}
91124

92125
TEST_SUITE("test conversions")
93126
{
@@ -154,6 +187,26 @@ TEST_SUITE("test conversions")
154187
CHECK_EQ(val3, ivec4(-10, -9, -8, -7));
155188
}
156189

190+
SUBCASE("valarray")
191+
{
192+
auto test_vec = dsga::bvec4(true, true, false, false);
193+
auto test_varr = std::valarray<double>(4u);
194+
copy_from_vector(test_varr, test_vec);
195+
196+
auto double_vec = dsga::dvec4(0);
197+
copy_to_vector(double_vec, test_varr);
198+
CHECK_EQ(double_vec, dsga::dvec4{1, 1, 0, 0});
199+
200+
auto cshift_varr = test_varr.cshift(2);
201+
copy_to_vector(test_vec, cshift_varr);
202+
CHECK_EQ(test_vec, dsga::bvec4(false, false, true, true));
203+
204+
auto new_varr = to_valarray(test_vec);
205+
new_varr = new_varr.shift(-1);
206+
copy_to_vector(test_vec, new_varr);
207+
CHECK_EQ(test_vec, dsga::bvec4(false, false, false, true));
208+
}
209+
157210
SUBCASE("ad hoc")
158211
{
159212
// roll your own:

0 commit comments

Comments
 (0)