mirror of
https://github.com/aicodix/code.git
synced 2026-04-27 22:35:44 +00:00
added exclusive reduce algorithm
This commit is contained in:
parent
80784388b6
commit
eabdff7209
2 changed files with 49 additions and 0 deletions
32
exclusive_reduce.hh
Normal file
32
exclusive_reduce.hh
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
Reduce N times while excluding ith input element
|
||||
|
||||
Copyright 2018 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#ifndef EXCLUSIVE_REDUCE_HH
|
||||
#define EXCLUSIVE_REDUCE_HH
|
||||
|
||||
namespace CODE {
|
||||
|
||||
template <typename TYPE, typename OPERATOR>
|
||||
void exclusive_reduce(const TYPE *in, TYPE *out, int N, OPERATOR op)
|
||||
{
|
||||
TYPE pre = in[0];
|
||||
for (int i = 1; i < N-1; ++i) {
|
||||
out[i] = pre;
|
||||
pre = op(pre, in[i]);
|
||||
}
|
||||
out[N-1] = pre;
|
||||
TYPE suf = in[N-1];
|
||||
for (int i = N-2; i > 0; --i) {
|
||||
out[i] = op(out[i], suf);
|
||||
suf = op(suf, in[i]);
|
||||
}
|
||||
out[0] = suf;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue