Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pugs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
code
pugs
Commits
3cd6a765
Commit
3cd6a765
authored
Sep 30, 2021
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Add min/max/sum functions to SmallArray
parent
0f646a89
No related branches found
No related tags found
1 merge request
!124
Add files for high order integration with quadratures
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/utils/SmallArray.hpp
+57
-0
57 additions, 0 deletions
src/utils/SmallArray.hpp
tests/test_SmallArray.cpp
+68
-0
68 additions, 0 deletions
tests/test_SmallArray.cpp
with
125 additions
and
0 deletions
src/utils/SmallArray.hpp
+
57
−
0
View file @
3cd6a765
...
...
@@ -6,6 +6,7 @@
#include
<utils/PugsAssert.hpp>
#include
<utils/PugsMacros.hpp>
#include
<utils/PugsUtils.hpp>
#include
<utils/Types.hpp>
#include
<algorithm>
...
...
@@ -148,4 +149,60 @@ convert_to_small_array(const Container& given_container)
return
array
;
}
template
<
typename
DataType
>
[[
nodiscard
]]
std
::
remove_const_t
<
DataType
>
min
(
const
SmallArray
<
DataType
>&
array
)
{
using
data_type
=
std
::
remove_const_t
<
DataType
>
;
using
index_type
=
typename
SmallArray
<
DataType
>::
index_type
;
data_type
min_value
=
std
::
numeric_limits
<
data_type
>::
max
();
for
(
index_type
i
=
0
;
i
<
array
.
size
();
++
i
)
{
if
(
array
[
i
]
<
min_value
)
{
min_value
=
array
[
i
];
}
}
return
min_value
;
}
template
<
typename
DataType
>
[[
nodiscard
]]
std
::
remove_const_t
<
DataType
>
max
(
const
SmallArray
<
DataType
>&
array
)
{
using
data_type
=
std
::
remove_const_t
<
DataType
>
;
using
index_type
=
typename
SmallArray
<
DataType
>::
index_type
;
data_type
max_value
=
-
std
::
numeric_limits
<
data_type
>::
max
();
for
(
index_type
i
=
0
;
i
<
array
.
size
();
++
i
)
{
if
(
array
[
i
]
>
max_value
)
{
max_value
=
array
[
i
];
}
}
return
max_value
;
}
template
<
typename
DataType
>
[[
nodiscard
]]
std
::
remove_const_t
<
DataType
>
sum
(
const
SmallArray
<
DataType
>&
array
)
{
using
data_type
=
std
::
remove_const_t
<
DataType
>
;
using
index_type
=
typename
SmallArray
<
DataType
>::
index_type
;
data_type
sum_value
=
[]
{
if
constexpr
(
std
::
is_arithmetic_v
<
DataType
>
)
{
return
0
;
}
else
if
constexpr
(
is_tiny_vector_v
<
DataType
>
or
is_tiny_matrix_v
<
DataType
>
)
{
return
zero
;
}
else
{
static_assert
(
std
::
is_arithmetic_v
<
DataType
>
,
"invalid data type"
);
}
}();
for
(
index_type
i
=
0
;
i
<
array
.
size
();
++
i
)
{
sum_value
+=
array
[
i
];
}
return
sum_value
;
}
#endif // SMALL_ARRAY_HPP
This diff is collapsed.
Click to expand it.
tests/test_SmallArray.cpp
+
68
−
0
View file @
3cd6a765
#include
<catch2/catch_test_macros.hpp>
#include
<catch2/matchers/catch_matchers_all.hpp>
#include
<algebra/TinyMatrix.hpp>
#include
<algebra/TinyVector.hpp>
#include
<utils/PugsAssert.hpp>
#include
<utils/SmallArray.hpp>
#include
<utils/Types.hpp>
...
...
@@ -281,5 +283,71 @@ TEST_CASE("SmallArray", "[utils]")
REQUIRE
(
array
[
i
]
==
std
::
numeric_limits
<
int
>::
max
()
/
2
);
}
}
SECTION
(
"checking for SmallArray reductions"
)
{
SmallArray
<
int
>
a
(
10
);
a
[
0
]
=
13
;
a
[
1
]
=
1
;
a
[
2
]
=
8
;
a
[
3
]
=
-
3
;
a
[
4
]
=
23
;
a
[
5
]
=
-
1
;
a
[
6
]
=
13
;
a
[
7
]
=
0
;
a
[
8
]
=
12
;
a
[
9
]
=
9
;
SECTION
(
"Min"
)
{
REQUIRE
(
min
(
a
)
==
-
3
);
}
SECTION
(
"Max"
)
{
REQUIRE
(
max
(
a
)
==
23
);
}
SECTION
(
"Sum"
)
{
REQUIRE
((
sum
(
a
)
==
75
));
}
SECTION
(
"TinyVector Sum"
)
{
using
N2
=
TinyVector
<
2
,
int
>
;
SmallArray
<
N2
>
b
(
10
);
b
[
0
]
=
{
13
,
2
};
b
[
1
]
=
{
1
,
3
};
b
[
2
]
=
{
8
,
-
2
};
b
[
3
]
=
{
-
3
,
2
};
b
[
4
]
=
{
23
,
4
};
b
[
5
]
=
{
-
1
,
-
3
};
b
[
6
]
=
{
13
,
17
};
b
[
7
]
=
{
0
,
9
};
b
[
8
]
=
{
12
,
13
};
b
[
9
]
=
{
9
,
-
17
};
REQUIRE
((
sum
(
b
)
==
N2
{
75
,
28
}));
}
SECTION
(
"TinyMatrix Sum"
)
{
using
N22
=
TinyMatrix
<
2
,
2
,
int
>
;
SmallArray
<
N22
>
b
(
10
);
b
[
0
]
=
{
13
,
2
,
0
,
1
};
b
[
1
]
=
{
1
,
3
,
6
,
3
};
b
[
2
]
=
{
8
,
-
2
,
-
1
,
21
};
b
[
3
]
=
{
-
3
,
2
,
5
,
12
};
b
[
4
]
=
{
23
,
4
,
7
,
1
};
b
[
5
]
=
{
-
1
,
-
3
,
33
,
11
};
b
[
6
]
=
{
13
,
17
,
12
,
13
};
b
[
7
]
=
{
0
,
9
,
1
,
14
};
b
[
8
]
=
{
12
,
13
,
-
3
,
-
71
};
b
[
9
]
=
{
9
,
-
17
,
0
,
16
};
REQUIRE
((
sum
(
b
)
==
N22
{
75
,
28
,
60
,
21
}));
}
}
#endif // NDEBUG
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment