forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalancer.cc
More file actions
48 lines (39 loc) · 1.04 KB
/
Copy pathbalancer.cc
File metadata and controls
48 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "parser/balancer.h"
#include "parser/source_factory.h"
namespace google {
namespace api {
namespace expr {
namespace parser {
ExpressionBalancer::ExpressionBalancer(std::shared_ptr<SourceFactory> sf,
std::string function, Expr expr)
: sf_(sf), function_(function), terms_{expr}, ops_{} {}
void ExpressionBalancer::addTerm(int64_t op, Expr term) {
terms_.push_back(term);
ops_.push_back(op);
}
Expr ExpressionBalancer::balance() {
if (terms_.size() == 1) {
return terms_[0];
}
return balancedTree(0, ops_.size() - 1);
}
Expr ExpressionBalancer::balancedTree(int lo, int hi) {
int mid = (lo + hi + 1) / 2;
Expr left;
if (mid == lo) {
left = terms_[mid];
} else {
left = balancedTree(lo, mid - 1);
}
Expr right;
if (mid == hi) {
right = terms_[mid + 1];
} else {
right = balancedTree(mid + 1, hi);
}
return sf_->newGlobalCall(ops_[mid], function_, {left, right});
}
} // namespace parser
} // namespace expr
} // namespace api
} // namespace google