Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Philip Trettner
typed-geometry
Commits
25b20d15
Commit
25b20d15
authored
Jan 25, 2021
by
Julius Nehring-Wirxel
Browse files
Add `max_index` and `min_index`
parent
e76242f6
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/typed-geometry/functions/basic/statistics.hh
View file @
25b20d15
...
@@ -10,6 +10,8 @@
...
@@ -10,6 +10,8 @@
*
*
* - min_element
* - min_element
* - max_element
* - max_element
* - max_index
* - min_index
* - average (same as arithmetic_mean)
* - average (same as arithmetic_mean)
* - mean (same as arithmetic_mean)
* - mean (same as arithmetic_mean)
* - arithmetic_mean
* - arithmetic_mean
...
@@ -39,11 +41,11 @@ template <class T = void, class RangeT, class TransformF, class ReduceF>
...
@@ -39,11 +41,11 @@ template <class T = void, class RangeT, class TransformF, class ReduceF>
using
U
=
std
::
decay_t
<
decltype
(
f
(
t
(
R
(
*
it
)),
t
(
R
(
*
it
))))
>
;
using
U
=
std
::
decay_t
<
decltype
(
f
(
t
(
R
(
*
it
)),
t
(
R
(
*
it
))))
>
;
auto
const
e
=
tg
::
end
(
values
);
auto
const
e
=
tg
::
end
(
values
);
U
r
=
t
(
R
(
*
it
));
U
r
=
t
(
R
(
*
it
));
it
++
;
++
it
;
while
(
it
!=
e
)
while
(
it
!=
e
)
{
{
r
=
f
(
r
,
t
(
R
(
*
it
)));
r
=
f
(
r
,
t
(
R
(
*
it
)));
it
++
;
++
it
;
}
}
return
r
;
return
r
;
}
}
...
@@ -111,6 +113,63 @@ template <class RangeT, class TransformT = identity_fun>
...
@@ -111,6 +113,63 @@ template <class RangeT, class TransformT = identity_fun>
return
detail
::
fold_right
(
values
,
transform
,
[](
auto
&&
a
,
auto
&&
b
)
{
return
max
(
a
,
b
);
});
return
detail
::
fold_right
(
values
,
transform
,
[](
auto
&&
a
,
auto
&&
b
)
{
return
max
(
a
,
b
);
});
}
}
/// returns the index of the max element
template
<
class
RangeT
,
class
TransformT
=
identity_fun
>
[[
nodiscard
]]
constexpr
size_t
max_index
(
RangeT
const
&
values
,
TransformT
&&
transform
=
{})
{
TG_CONTRACT
(
tg
::
begin
(
values
)
!=
tg
::
end
(
values
)
&&
"values must not be empty"
);
size_t
curr_idx
=
0
;
auto
it
=
tg
::
begin
(
values
);
auto
const
end
=
tg
::
end
(
values
);
auto
max_v
=
transform
(
*
it
);
size_t
max_idx
=
curr_idx
;
++
it
;
++
curr_idx
;
while
(
it
!=
end
)
{
auto
v
=
transform
(
*
it
);
if
(
v
>
max_v
)
{
max_v
=
v
;
max_idx
=
curr_idx
;
}
++
it
;
++
curr_idx
;
}
return
max_idx
;
}
/// returns the index of the min element
template
<
class
RangeT
,
class
TransformT
=
identity_fun
>
[[
nodiscard
]]
constexpr
size_t
min_index
(
RangeT
const
&
values
,
TransformT
&&
transform
=
{})
{
TG_CONTRACT
(
tg
::
begin
(
values
)
!=
tg
::
end
(
values
)
&&
"values must not be empty"
);
size_t
curr_idx
=
0
;
auto
it
=
tg
::
begin
(
values
);
auto
const
end
=
tg
::
end
(
values
);
auto
min_v
=
transform
(
*
it
);
size_t
min_idx
=
curr_idx
;
++
it
;
++
curr_idx
;
while
(
it
!=
end
)
{
auto
v
=
transform
(
*
it
);
if
(
v
<
min_v
)
{
min_v
=
v
;
min_idx
=
curr_idx
;
}
++
it
;
++
curr_idx
;
}
return
min_idx
;
}
template
<
class
T
=
void
,
class
RangeT
=
void
,
class
TransformT
=
identity_fun
>
template
<
class
T
=
void
,
class
RangeT
=
void
,
class
TransformT
=
identity_fun
>
[[
nodiscard
]]
constexpr
auto
sum
(
RangeT
const
&
values
,
TransformT
&&
transform
=
{})
[[
nodiscard
]]
constexpr
auto
sum
(
RangeT
const
&
values
,
TransformT
&&
transform
=
{})
{
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment