Skip to content

Commit efb1031

Browse files
committed
v1.5.0 - added initializer list constructors for basic_vector and basic_matrix
1 parent dd2fc9c commit efb1031

File tree

6 files changed

+217
-113
lines changed

6 files changed

+217
-113
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[https://github.com/davidbrowne/dsga](https://github.com/davidbrowne/dsga)
77

88
## Current Version
9-
v1.4.1
9+
v1.5.0
1010

1111
## Minimum Version of Tested Compilers
1212
* Microsoft Visual Studio 2022 v17.x
@@ -172,7 +172,7 @@ constexpr dsga::vec3 right_handed_normal(const dsga::vec3 &v1, const dsga::vec3
172172
// cross product
173173
//
174174
175-
// arguments are of the vector base class type, and this function will be used if any passed argument is of type ```indexed_vector```
175+
// arguments are of the vector base class type, and this function will be used if any passed argument is of type indexed_vector
176176
template <bool W1, dsga::floating_point_scalar T1, typename D1, bool W2, dsga::floating_point_scalar T2, typename D2>
177177
[[nodiscard]] constexpr auto cross(const dsga::vector_base<W1, T1, 3, D1> &a,
178178
const dsga::vector_base<W2, T2, 3, D2> &b) noexcept
@@ -183,7 +183,7 @@ template <bool W1, dsga::floating_point_scalar T1, typename D1, bool W2, dsga::f
183183
(a[0] * b[1]) - (b[0] * a[1]));
184184
}
185185
186-
// arguments are of type ```basic_vector```, and there is a compact swizzled implementation
186+
// arguments are of type basic_vector, and there is a compact swizzled implementation
187187
template <dsga::floating_point_scalar T1, dsga::floating_point_scalar T2>
188188
[[nodiscard]] constexpr auto cross(const dsga::basic_vector<T1, 3> &a,
189189
const dsga::basic_vector<T2, 3> &b) noexcept
@@ -278,7 +278,7 @@ To make the vectors and matrices as useful as possible in a C++ context, various
278278
* vector - pointer to scalars of concept type ```dsga::dimensional_scalar```
279279
* matrix - pointer to column vectors whose scalars are of concept type ```dsga::floating_point_scalar```
280280
* ```size```
281-
* vector only - these allow logical use of ```data```
281+
* vector only - these ordering facilities allow logical use of ```data```
282282
* ```offsets```
283283
* ```sequence```
284284
* Type Conversions
@@ -311,10 +311,10 @@ This is a c++20 library, so that needs to be the minimum standard that you tell
311311
312312
## Status
313313
314-
Current version: `v1.4.1`
314+
Current version: `v1.5.0`
315315
316316
* Everything major has some tests, but code coverage is not 100%.
317-
* [Last Released: v1.4.0](https://github.com/davidbrowne/dsga/releases/tag/v1.4.0)
317+
* [Last Released: v1.5.0](https://github.com/davidbrowne/dsga/releases/tag/v1.5.0)
318318
319319
### The next steps
320320
* Refining API documentation.
@@ -345,8 +345,8 @@ The tests have been most recently run on:
345345
[doctest] doctest version is "2.4.11"
346346
[doctest] run with "--help" for options
347347
===============================================================================
348-
[doctest] test cases: 109 | 109 passed | 0 failed | 0 skipped
349-
[doctest] assertions: 2159 | 2159 passed | 0 failed |
348+
[doctest] test cases: 111 | 111 passed | 0 failed | 0 skipped
349+
[doctest] assertions: 2167 | 2167 passed | 0 failed |
350350
[doctest] Status: SUCCESS!
351351
```
352352
@@ -356,8 +356,8 @@ The tests have been most recently run on:
356356
[doctest] doctest version is "2.4.11"
357357
[doctest] run with "--help" for options
358358
===============================================================================
359-
[doctest] test cases: 109 | 109 passed | 0 failed | 0 skipped
360-
[doctest] assertions: 2159 | 2159 passed | 0 failed |
359+
[doctest] test cases: 111 | 111 passed | 0 failed | 0 skipped
360+
[doctest] assertions: 2167 | 2167 passed | 0 failed |
361361
[doctest] Status: SUCCESS!
362362
```
363363
@@ -369,8 +369,8 @@ Performs all the unit tests except where there is lack of support for ```std::is
369369
[doctest] doctest version is "2.4.11"
370370
[doctest] run with "--help" for options
371371
===============================================================================
372-
[doctest] test cases: 109 | 109 passed | 0 failed | 0 skipped
373-
[doctest] assertions: 2143 | 2143 passed | 0 failed |
372+
[doctest] test cases: 111 | 111 passed | 0 failed | 0 skipped
373+
[doctest] assertions: 2151 | 2151 passed | 0 failed |
374374
[doctest] Status: SUCCESS!
375375
```
376376
@@ -382,8 +382,8 @@ Performs all the unit tests except where there is lack of support for ```std::is
382382
[doctest] doctest version is "2.4.11"
383383
[doctest] run with "--help" for options
384384
===============================================================================
385-
[doctest] test cases: 109 | 109 passed | 0 failed | 0 skipped
386-
[doctest] assertions: 2159 | 2159 passed | 0 failed |
385+
[doctest] test cases: 111 | 111 passed | 0 failed | 0 skipped
386+
[doctest] assertions: 2167 | 2167 passed | 0 failed |
387387
[doctest] Status: SUCCESS!
388388
```
389389
@@ -395,8 +395,8 @@ Performs all the unit tests except where there is lack of support for ```std::is
395395
[doctest] doctest version is "2.4.11"
396396
[doctest] run with "--help" for options
397397
===============================================================================
398-
[doctest] test cases: 109 | 109 passed | 0 failed | 0 skipped
399-
[doctest] assertions: 2143 | 2143 passed | 0 failed |
398+
[doctest] test cases: 111 | 111 passed | 0 failed | 0 skipped
399+
[doctest] assertions: 2151 | 2151 passed | 0 failed |
400400
[doctest] Status: SUCCESS!
401401
```
402402
@@ -408,8 +408,8 @@ Performs all the unit tests except where there is lack of support for ```std::is
408408
[doctest] doctest version is "2.4.11"
409409
[doctest] run with "--help" for options
410410
===============================================================================
411-
[doctest] test cases: 109 | 109 passed | 0 failed | 0 skipped
412-
[doctest] assertions: 2159 | 2159 passed | 0 failed |
411+
[doctest] test cases: 111 | 111 passed | 0 failed | 0 skipped
412+
[doctest] assertions: 2167 | 2167 passed | 0 failed |
413413
[doctest] Status: SUCCESS!
414414
```
415415
@@ -421,8 +421,8 @@ Performs all the unit tests except where there is lack of support for ```std::is
421421
[doctest] doctest version is "2.4.11"
422422
[doctest] run with "--help" for options
423423
===============================================================================
424-
[doctest] test cases: 109 | 109 passed | 0 failed | 0 skipped
425-
[doctest] assertions: 2143 | 2143 passed | 0 failed |
424+
[doctest] test cases: 111 | 111 passed | 0 failed | 0 skipped
425+
[doctest] assertions: 2151 | 2151 passed | 0 failed |
426426
[doctest] Status: SUCCESS!
427427
```
428428

docs/API.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,11 @@ explicit constexpr basic_vector(const U &u, const Args & ...args) noexcept;
841841
```
842842
Variadic constructor. Can take a combination of vectors, scalars, and matrixes as arguments to initialize the ```basic_vector```.
843843

844+
```c++
845+
constexpr basic_vector(const std::initializer_list<T> &init_list) noexcept;
846+
```
847+
An intializer list of values. If too few values for the vector, the rest of the elements will be set to 0. If too many values for the vector, the rest of the initialization list will be ignored.
848+
844849
##### ```basic_vector::base```
845850
```c++
846851
storage_wrapper<T, Size> base;
@@ -1072,6 +1077,11 @@ explicit constexpr basic_matrix(const basic_matrix<U, Cols, Rows> &arg) noexcept
10721077
```
10731078
Constructors that take a matrix as an argument, but not a normal copy constructor (see default constructors).
10741079

1080+
```c++
1081+
constexpr basic_matrix(const std::initializer_list<T> &init_list) noexcept;
1082+
```
1083+
An intializer list of values. If too few values for the matrix, the rest of the elements will be set to 0. If too many values for the matrix, the rest of the initialization list will be ignored.
1084+
10751085
##### ```basic_matrix::columns```
10761086
```c++
10771087
std::array<basic_vector<T, R>, C> columns;

docs/DOCUMENTATION.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ template <typename ... Args>
317317
constexpr basic_vector(const Args & ...args) noexcept;
318318
```
319319
320-
This approach is exactly what ```basic_matrix``` does.
320+
* An intializer list of values. If too few values for the vector, the rest of the elements will be set to 0. If too many values for the vector, the rest of the initialization list will be ignored.
321+
322+
```c++
323+
constexpr basic_vector(const std::initializer_list<T> &init_list) noexcept;
324+
```
321325

322326
### Vector Member Functions
323327

@@ -529,6 +533,12 @@ template <typename ... Args>
529533
constexpr basic_matrix(const Args & ...args) noexcept;
530534
```
531535
536+
* An intializer list of values. If too few values for the matrix, the rest of the elements will be set to 0. If too many values for the matrix, the rest of the initialization list will be ignored.
537+
538+
```c++
539+
constexpr basic_matrix(const std::initializer_list<T> &init_list) noexcept;
540+
```
541+
532542
### Matrix Member Functions
533543

534544
These are the members that are not part of the [iterator interface](#iterators), the [tuple protocol](#tuple-protocol), or the [low-level interface](#low-level-pointer-access).

include/dsga.hxx

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ inline void cxcm_constexpr_assert_failed(Assert &&a) noexcept
9595
// version info
9696

9797
constexpr inline int DSGA_MAJOR_VERSION = 1;
98-
constexpr inline int DSGA_MINOR_VERSION = 4;
99-
constexpr inline int DSGA_PATCH_VERSION = 1;
98+
constexpr inline int DSGA_MINOR_VERSION = 5;
99+
constexpr inline int DSGA_PATCH_VERSION = 0;
100100

101101
namespace dsga
102102
{
@@ -2014,16 +2014,16 @@ namespace dsga
20142014
using reference = const T &;
20152015

20162016
// range of valid values for mapper_index
2017-
constexpr static std::size_t begin_index = 0;
2018-
constexpr static std::size_t end_index = Count;
2017+
constexpr static int begin_index = 0;
2018+
constexpr static int end_index = Count;
20192019

20202020
// the data
20212021
const indexed_vector<T, Size, Count, Is ...> *mapper_ptr;
2022-
std::size_t mapper_index;
2022+
int mapper_index;
20232023

20242024
// index == 0 is begin iterator
20252025
// index == Count is end iterator -- clamp index in [0, Count] range
2026-
constexpr indexed_vector_const_iterator(const indexed_vector<T, Size, Count, Is ...> &mapper, std::size_t index) noexcept
2026+
constexpr indexed_vector_const_iterator(const indexed_vector<T, Size, Count, Is ...> &mapper, int index) noexcept
20272027
: mapper_ptr(std::addressof(mapper)), mapper_index(index)
20282028
{
20292029
dsga_constexpr_assert((mapper_index >= begin_index) && (mapper_index <= end_index), "index not in range");
@@ -2170,7 +2170,7 @@ namespace dsga
21702170

21712171
// index == 0 is begin iterator
21722172
// index == Count is end iterator -- clamp index in [0, Count] range
2173-
constexpr indexed_vector_iterator(indexed_vector<T, Size, Count, Is ...> &mapper, std::size_t index) noexcept
2173+
constexpr indexed_vector_iterator(indexed_vector<T, Size, Count, Is ...> &mapper, int index) noexcept
21742174
: base_iter(mapper, index)
21752175
{
21762176
}
@@ -2718,6 +2718,16 @@ namespace dsga
27182718
}(std::make_index_sequence<Count>{});
27192719
}
27202720

2721+
// initializer list constructor -- it will use the first Count number of elements of the list,
2722+
// and if list size is less than Count, it will initialize the rest of the vector elements with 0
2723+
constexpr basic_vector(const std::initializer_list<T> &init_list) noexcept
2724+
{
2725+
auto copy_size = (Count > init_list.size()) ? init_list.size() : Count;
2726+
auto fill_size = Count - copy_size;
2727+
auto fill_iter = std::copy_n(init_list.begin(), copy_size, this->begin());
2728+
std::fill_n(fill_iter, fill_size, T(0));
2729+
}
2730+
27212731
//
27222732
// implicit assignment operators
27232733
//
@@ -2924,6 +2934,16 @@ namespace dsga
29242934
}(std::make_index_sequence<Count>{});
29252935
}
29262936

2937+
// initializer list constructor -- it will use the first Count number of elements of the list,
2938+
// and if list size is less than Count, it will initialize the rest of the vector elements with 0
2939+
constexpr basic_vector(const std::initializer_list<T> &init_list) noexcept
2940+
{
2941+
auto copy_size = (Count > init_list.size()) ? init_list.size() : Count;
2942+
auto fill_size = Count - copy_size;
2943+
auto fill_iter = std::copy_n(init_list.begin(), copy_size, this->begin());
2944+
std::fill_n(fill_iter, fill_size, T(0));
2945+
}
2946+
29272947
//
29282948
// assignment operator
29292949
//
@@ -3200,6 +3220,16 @@ namespace dsga
32003220
}(std::make_index_sequence<Count>{});
32013221
}
32023222

3223+
// initializer list constructor -- it will use the first Count number of elements of the list,
3224+
// and if list size is less than Count, it will initialize the rest of the vector elements with 0
3225+
constexpr basic_vector(const std::initializer_list<T> &init_list) noexcept
3226+
{
3227+
auto copy_size = (Count > init_list.size()) ? init_list.size() : Count;
3228+
auto fill_size = Count - copy_size;
3229+
auto fill_iter = std::copy_n(init_list.begin(), copy_size, this->begin());
3230+
std::fill_n(fill_iter, fill_size, T(0));
3231+
}
3232+
32033233
//
32043234
// assignment operators
32053235
//
@@ -3699,6 +3729,16 @@ namespace dsga
36993729
}(std::make_index_sequence<Count>{});
37003730
}
37013731

3732+
// initializer list constructor -- it will use the first Count number of elements of the list,
3733+
// and if list size is less than Count, it will initialize the rest of the vector elements with 0
3734+
constexpr basic_vector(const std::initializer_list<T> &init_list) noexcept
3735+
{
3736+
auto copy_size = (Count > init_list.size()) ? init_list.size() : Count;
3737+
auto fill_size = Count - copy_size;
3738+
auto fill_iter = std::copy_n(init_list.begin(), copy_size, this->begin());
3739+
std::fill_n(fill_iter, fill_size, T(0));
3740+
}
3741+
37023742
//
37033743
// assignment operators
37043744
//
@@ -6210,6 +6250,24 @@ namespace dsga
62106250
}
62116251
}
62126252

6253+
// initializer list constructor -- it will use the first ComponentCount number of elements of the list,
6254+
// and if list size is less than ComponentCount, it will initialize the rest of the matrix elements with 0
6255+
constexpr basic_matrix(const std::initializer_list<T> &init_list) noexcept
6256+
{
6257+
auto element_values = std::array<T, ComponentCount>{};
6258+
auto copy_size = (ComponentCount > init_list.size()) ? init_list.size() : ComponentCount;
6259+
std::copy_n(init_list.begin(), copy_size, element_values.begin());
6260+
6261+
[&]<std::size_t ...Is>(std::index_sequence <Is...>) noexcept
6262+
{
6263+
(([&]<std::size_t ...Js>(std::index_sequence <Js...>) noexcept
6264+
{
6265+
constexpr std::size_t Col = Is;
6266+
columns[Col].set(element_values[Col * R + Js] ...);
6267+
}(std::make_index_sequence<R>{})), ...);
6268+
}(std::make_index_sequence<C>{});
6269+
}
6270+
62136271
//
62146272
// assignment operators
62156273
//

tests/constructor_test.cxx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,19 @@ TEST_SUITE("test vector constructors")
261261
auto s1 = dsga::storage_wrapper{1.1, 2.2, 3, true};
262262
CHECK_EQ(s1, dsga::storage_wrapper<double, 4>{1.1, 2.2, 3.0, 1.0});
263263
}
264+
265+
TEST_CASE("initializer list vector constructor")
266+
{
267+
dsga::vec4 initlist_vec1 = {1, 2};
268+
dsga::vec4 initlist_vec2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
269+
dsga::vec4 initlist_vec3{1, 2};
270+
dsga::vec4 initlist_vec4{};
271+
272+
CHECK_EQ(initlist_vec1, vec4(1, 2, 0, 0));
273+
CHECK_EQ(initlist_vec2, vec4(1, 2, 3, 4));
274+
CHECK_EQ(initlist_vec3, vec4(1, 2, 0, 0));
275+
CHECK_EQ(initlist_vec4, vec4(0, 0, 0, 0));
276+
}
264277
}
265278

266279
TEST_SUITE("test matrix constructors")
@@ -553,4 +566,17 @@ TEST_SUITE("test matrix constructors")
553566
CHECK_EQ(tm10, mat4x4(9, 0, 0, 0, 0, 9, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1));
554567
CHECK_EQ(tm11, dmat4x4(1, 2, 0, 0, 3, 4, 0, 0, 5, 6, 1, 0, 7, 8, 0, 1));
555568
}
569+
570+
TEST_CASE("initializer list matrix constructor")
571+
{
572+
dsga::mat4 initlist_mat1 = {1, 2, 3, 4, 5, 6};
573+
dsga::mat4 initlist_mat2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
574+
dsga::mat4 initlist_mat3{1, 2, 3, 4, 5, 6};
575+
dsga::mat4 initlist_mat4{};
576+
577+
CHECK_EQ(initlist_mat1, mat4(1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
578+
CHECK_EQ(initlist_mat2, mat4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
579+
CHECK_EQ(initlist_mat3, mat4(1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
580+
CHECK_EQ(initlist_mat4, mat4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
581+
}
556582
}

0 commit comments

Comments
 (0)