13 #ifndef HELIB_SUMREGISTER_H
14 #define HELIB_SUMREGISTER_H
33 std::vector<std::unique_ptr<T>> intermediateResults;
35 unsigned int maxNumOfInputs;
36 unsigned int remainingInputs;
37 bool flushRequiredFlag =
false;
38 bool resultFlag =
false;
47 maxNumOfInputs(_maxNumOfInputs), remainingInputs(_maxNumOfInputs)
50 if (_maxNumOfInputs != 0)
51 this->depth = std::ceil(std::log2(_maxNumOfInputs));
54 if ((_maxNumOfInputs & (_maxNumOfInputs - 1)) || (_maxNumOfInputs == 0)) {
55 this->flushRequiredFlag =
true;
59 this->intermediateResults = std::vector<std::unique_ptr<T>>(depth + 1);
67 void add(std::unique_ptr<T>& t)
69 if (this->remainingInputs == 0) {
72 this->remainingInputs--;
74 if (this->intermediateResults.at(0) !=
nullptr) {
75 *this->intermediateResults.at(0) += *t;
77 for (
size_t i = 1, j = 0; i < this->intermediateResults.size();
79 if (intermediateResults[i] !=
nullptr) {
80 *this->intermediateResults[i] += *this->intermediateResults[j];
81 this->intermediateResults[j].reset();
83 this->intermediateResults[i] =
84 std::move(this->intermediateResults[j]);
90 if (intermediateResults.at(depth) !=
nullptr)
94 intermediateResults.at(0) = std::move(t);
97 if ((this->remainingInputs == 0) && this->flushRequiredFlag) {
109 return std::move(intermediateResults.at(depth));
134 for (
size_t i = 0, j = 1; i < depth; i++, j++) {
135 if (this->intermediateResults[i] ==
nullptr)
138 if (this->intermediateResults[j] ==
nullptr) {
139 this->intermediateResults[j] = std::move(this->intermediateResults[i]);
141 *this->intermediateResults[j] += *this->intermediateResults[i];
142 this->intermediateResults[i].reset();
146 if (intermediateResults.at(depth) !=
nullptr)
155 std::cout <<
"Current values\n";
156 for (
size_t i = 0; i < this->intermediateResults.size(); i++) {
157 std::cout <<
"[" << i <<
"]: "
158 << (this->intermediateResults[i] !=
nullptr
159 ? *this->intermediateResults[i]
161 <<
" (" << this->intermediateResults[i] <<
")" <<
'\n';
171 return intermediateResults;
176 #endif // HELIB_SUMREGISTER_H