-
Notifications
You must be signed in to change notification settings - Fork 341
[Type] Array/Vec: small refresh for modern C++ #5907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fredroy
wants to merge
4
commits into
sofa-framework:master
Choose a base branch
from
fredroy:modernize_arrays
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+433
−33
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| #include <cmath> | ||
| #include <array> | ||
| #include <cassert> | ||
| #include <numeric> | ||
|
|
||
| #define EQUALITY_THRESHOLD 1e-6 | ||
|
|
||
|
|
@@ -257,8 +258,7 @@ class Vec | |
| // assign one value to all elements | ||
| constexpr void assign(const ValueType& value) noexcept | ||
| { | ||
| for (size_type i = 0; i < N; i++) | ||
| elems[i] = value; | ||
| std::fill_n(this->elems.data(), N, value); | ||
| } | ||
|
|
||
| /// Sets every element to 0. | ||
|
|
@@ -472,10 +472,7 @@ class Vec | |
| /// Squared norm. | ||
| constexpr ValueType norm2() const noexcept | ||
| { | ||
| ValueType r = this->elems[0]*this->elems[0]; | ||
| for (Size i=1; i<N; i++) | ||
| r += this->elems[i]*this->elems[i]; | ||
| return r; | ||
| return std::inner_product(elems.begin(), elems.end(), elems.begin(), ValueType{}); | ||
| } | ||
|
|
||
| /// Euclidean norm. | ||
|
|
@@ -583,10 +580,7 @@ class Vec | |
| /// sum of all elements of the vector | ||
| constexpr ValueType sum() const noexcept | ||
| { | ||
| ValueType sum = ValueType(0.0); | ||
| for (Size i=0; i<N; i++) | ||
| sum += this->elems[i]; | ||
| return sum; | ||
| return std::accumulate(elems.begin(), elems.end(), ValueType{}); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
|
|
||
|
|
@@ -597,8 +591,8 @@ class Vec | |
| { | ||
| if constexpr (std::is_floating_point_v<ValueType>) | ||
| { | ||
| return std::equal(this->elems.begin(), this->elems.end(), b.elems.begin(), | ||
| [](auto x, auto y) { return std::abs(x - y) < EQUALITY_THRESHOLD; }); | ||
| constexpr auto equalTest = [](auto x, auto y) { return std::abs(x - y) < EQUALITY_THRESHOLD; }; | ||
| return std::equal(this->elems.begin(), this->elems.end(), b.elems.begin(), equalTest ); | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -680,19 +674,19 @@ class Vec | |
| return elems.end(); | ||
| } | ||
|
|
||
| constexpr reference front() | ||
| constexpr reference front() noexcept | ||
| { | ||
| return elems[0]; | ||
| } | ||
| constexpr const_reference front() const | ||
| constexpr const_reference front() const noexcept | ||
| { | ||
| return elems[0]; | ||
| } | ||
| constexpr reference back() | ||
| constexpr reference back() noexcept | ||
| { | ||
| return elems[N - 1]; | ||
| } | ||
| constexpr const_reference back() const | ||
| constexpr const_reference back() const noexcept | ||
| { | ||
| return elems[N - 1]; | ||
| } | ||
|
|
@@ -717,7 +711,7 @@ class VecNoInit : public Vec<N,real> | |
| {} | ||
|
|
||
| constexpr VecNoInit(Vec<N,real>&& v) noexcept | ||
| : Vec<N,real>(v) | ||
| : Vec<N,real>(std::move(v)) | ||
| {} | ||
|
|
||
| using Vec<N,real>::Vec; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can ask the question of the performances...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually people (and IA...) advise to use stl functions because it may help the compiler more (vectorization and stuff). But of course it depends of the implementation of the libstl itself... I will try to do some bench tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://quick-bench.com/q/WGEPcgHll4O2cAl8Ms-VoyRBn7I
At -O3, all config (clang/gcc & libstdc++/libc++) give same result. But will less optimization the loop is faster.
Should I roll back the implementation ?