Add util::map(array) for transforming arrays elementwise
This commit is contained in:
parent
8ece2e1884
commit
f193ddf41d
1 changed files with 22 additions and 0 deletions
|
|
@ -31,6 +31,11 @@ namespace psemek::util
|
|||
|
||||
array & operator = (array const &) = delete;
|
||||
|
||||
dims_type const & dims() const
|
||||
{
|
||||
return dims_;
|
||||
}
|
||||
|
||||
std::size_t dim(std::size_t i) const
|
||||
{
|
||||
assert(i < N);
|
||||
|
|
@ -298,4 +303,21 @@ namespace psemek::util
|
|||
data_ = std::move(data);
|
||||
}
|
||||
|
||||
template <typename F, typename T, std::size_t N>
|
||||
auto map(F && f, array<T, N> const & a)
|
||||
{
|
||||
using R = std::decay_t<decltype(f(T{}))>;
|
||||
|
||||
array<R, N> r(a.dims());
|
||||
|
||||
auto begin = a.begin();
|
||||
auto end = a.end();
|
||||
auto out = r.begin();
|
||||
|
||||
for (; begin != end;)
|
||||
*out++ = f(*begin++);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue