Pass ExpressionRef by const reference instead of by value
Currently there is a bug in the copy constructor of ExpressionRef.
Fixing this bug, will create new expressions each time a copy is
executed. To reduce the amount of copy expression we pass
ExpressionRef by const reference now.
Change-Id: I0f81fb3d08aa4c66b9191f3138650f17023af67e
diff --git a/include/ceres/internal/expression_ref.h b/include/ceres/internal/expression_ref.h
index 570a8d8..5f293b0 100644
--- a/include/ceres/internal/expression_ref.h
+++ b/include/ceres/internal/expression_ref.h
@@ -88,10 +88,10 @@
ExpressionRef& operator=(const ExpressionRef& other);
// Compound operators
- ExpressionRef& operator+=(ExpressionRef x);
- ExpressionRef& operator-=(ExpressionRef x);
- ExpressionRef& operator*=(ExpressionRef x);
- ExpressionRef& operator/=(ExpressionRef x);
+ ExpressionRef& operator+=(const ExpressionRef& x);
+ ExpressionRef& operator-=(const ExpressionRef& x);
+ ExpressionRef& operator*=(const ExpressionRef& x);
+ ExpressionRef& operator/=(const ExpressionRef& x);
bool IsInitialized() const { return id != kInvalidExpressionId; }
@@ -102,23 +102,23 @@
};
// Arithmetic Operators
-ExpressionRef operator-(ExpressionRef x);
-ExpressionRef operator+(ExpressionRef x);
-ExpressionRef operator+(ExpressionRef x, ExpressionRef y);
-ExpressionRef operator-(ExpressionRef x, ExpressionRef y);
-ExpressionRef operator*(ExpressionRef x, ExpressionRef y);
-ExpressionRef operator/(ExpressionRef x, ExpressionRef y);
+ExpressionRef operator-(const ExpressionRef& x);
+ExpressionRef operator+(const ExpressionRef& x);
+ExpressionRef operator+(const ExpressionRef& x, const ExpressionRef& y);
+ExpressionRef operator-(const ExpressionRef& x, const ExpressionRef& y);
+ExpressionRef operator*(const ExpressionRef& x, const ExpressionRef& y);
+ExpressionRef operator/(const ExpressionRef& x, const ExpressionRef& y);
// Functions
#define CERES_DEFINE_UNARY_FUNCTION_CALL(name) \
- inline ExpressionRef name(ExpressionRef x) { \
+ inline ExpressionRef name(const ExpressionRef& x) { \
return ExpressionRef::Create( \
Expression::CreateFunctionCall(#name, {x.id})); \
}
-#define CERES_DEFINE_BINARY_FUNCTION_CALL(name) \
- inline ExpressionRef name(ExpressionRef x, ExpressionRef y) { \
- return ExpressionRef::Create( \
- Expression::CreateFunctionCall(#name, {x.id, y.id})); \
+#define CERES_DEFINE_BINARY_FUNCTION_CALL(name) \
+ inline ExpressionRef name(const ExpressionRef& x, const ExpressionRef& y) { \
+ return ExpressionRef::Create( \
+ Expression::CreateFunctionCall(#name, {x.id, y.id})); \
}
CERES_DEFINE_UNARY_FUNCTION_CALL(abs);
CERES_DEFINE_UNARY_FUNCTION_CALL(acos);
@@ -161,27 +161,33 @@
// ...
struct ComparisonExpressionRef {
ExpressionId id;
- explicit ComparisonExpressionRef(ExpressionRef ref) : id(ref.id) {}
+ explicit ComparisonExpressionRef(const ExpressionRef& ref) : id(ref.id) {}
};
-ExpressionRef Ternary(ComparisonExpressionRef c,
- ExpressionRef a,
- ExpressionRef b);
+ExpressionRef Ternary(const ComparisonExpressionRef& c,
+ const ExpressionRef& x,
+ const ExpressionRef& y);
// Comparison operators
-ComparisonExpressionRef operator<(ExpressionRef a, ExpressionRef b);
-ComparisonExpressionRef operator<=(ExpressionRef a, ExpressionRef b);
-ComparisonExpressionRef operator>(ExpressionRef a, ExpressionRef b);
-ComparisonExpressionRef operator>=(ExpressionRef a, ExpressionRef b);
-ComparisonExpressionRef operator==(ExpressionRef a, ExpressionRef b);
-ComparisonExpressionRef operator!=(ExpressionRef a, ExpressionRef b);
+ComparisonExpressionRef operator<(const ExpressionRef& x,
+ const ExpressionRef& y);
+ComparisonExpressionRef operator<=(const ExpressionRef& x,
+ const ExpressionRef& y);
+ComparisonExpressionRef operator>(const ExpressionRef& x,
+ const ExpressionRef& y);
+ComparisonExpressionRef operator>=(const ExpressionRef& x,
+ const ExpressionRef& y);
+ComparisonExpressionRef operator==(const ExpressionRef& x,
+ const ExpressionRef& y);
+ComparisonExpressionRef operator!=(const ExpressionRef& x,
+ const ExpressionRef& y);
// Logical Operators
-ComparisonExpressionRef operator&&(ComparisonExpressionRef a,
- ComparisonExpressionRef b);
-ComparisonExpressionRef operator||(ComparisonExpressionRef a,
- ComparisonExpressionRef b);
-ComparisonExpressionRef operator!(ComparisonExpressionRef a);
+ComparisonExpressionRef operator&&(const ComparisonExpressionRef& x,
+ const ComparisonExpressionRef& y);
+ComparisonExpressionRef operator||(const ComparisonExpressionRef& x,
+ const ComparisonExpressionRef& y);
+ComparisonExpressionRef operator!(const ComparisonExpressionRef& x);
// This struct is used to mark numbers which are constant over
// multiple invocations but can differ between instances.
@@ -234,7 +240,8 @@
inline ExpressionRef MakeParameter(const std::string& name) {
return ExpressionRef::Create(Expression::CreateInputAssignment(name));
}
-inline ExpressionRef MakeOutput(ExpressionRef v, const std::string& name) {
+inline ExpressionRef MakeOutput(const ExpressionRef& v,
+ const std::string& name) {
return ExpressionRef::Create(Expression::CreateOutputAssignment(v.id, name));
}
diff --git a/internal/ceres/expression_ref.cc b/internal/ceres/expression_ref.cc
index 8f7300c..23af0d1 100644
--- a/internal/ceres/expression_ref.cc
+++ b/internal/ceres/expression_ref.cc
@@ -63,73 +63,74 @@
}
// Compound operators
-ExpressionRef& ExpressionRef::operator+=(ExpressionRef y) {
- *this = *this + y;
+ExpressionRef& ExpressionRef::operator+=(const ExpressionRef& x) {
+ *this = *this + x;
return *this;
}
-ExpressionRef& ExpressionRef::operator-=(ExpressionRef y) {
- *this = *this - y;
+ExpressionRef& ExpressionRef::operator-=(const ExpressionRef& x) {
+ *this = *this - x;
return *this;
}
-ExpressionRef& ExpressionRef::operator*=(ExpressionRef y) {
- *this = *this * y;
+ExpressionRef& ExpressionRef::operator*=(const ExpressionRef& x) {
+ *this = *this * x;
return *this;
}
-ExpressionRef& ExpressionRef::operator/=(ExpressionRef y) {
- *this = *this / y;
+ExpressionRef& ExpressionRef::operator/=(const ExpressionRef& x) {
+ *this = *this / x;
return *this;
}
// Arith. Operators
-ExpressionRef operator-(ExpressionRef x) {
+ExpressionRef operator-(const ExpressionRef& x) {
return ExpressionRef::Create(Expression::CreateUnaryArithmetic("-", x.id));
}
-ExpressionRef operator+(ExpressionRef x) {
+ExpressionRef operator+(const ExpressionRef& x) {
return ExpressionRef::Create(Expression::CreateUnaryArithmetic("+", x.id));
}
-ExpressionRef operator+(ExpressionRef x, ExpressionRef y) {
+ExpressionRef operator+(const ExpressionRef& x, const ExpressionRef& y) {
return ExpressionRef::Create(
Expression::CreateBinaryArithmetic("+", x.id, y.id));
}
-ExpressionRef operator-(ExpressionRef x, ExpressionRef y) {
+ExpressionRef operator-(const ExpressionRef& x, const ExpressionRef& y) {
return ExpressionRef::Create(
Expression::CreateBinaryArithmetic("-", x.id, y.id));
}
-ExpressionRef operator/(ExpressionRef x, ExpressionRef y) {
+ExpressionRef operator/(const ExpressionRef& x, const ExpressionRef& y) {
return ExpressionRef::Create(
Expression::CreateBinaryArithmetic("/", x.id, y.id));
}
-ExpressionRef operator*(ExpressionRef x, ExpressionRef y) {
+ExpressionRef operator*(const ExpressionRef& x, const ExpressionRef& y) {
return ExpressionRef::Create(
Expression::CreateBinaryArithmetic("*", x.id, y.id));
}
-ExpressionRef Ternary(ComparisonExpressionRef c,
- ExpressionRef a,
- ExpressionRef b) {
+ExpressionRef Ternary(const ComparisonExpressionRef& c,
+ const ExpressionRef& x,
+ const ExpressionRef& y) {
return ExpressionRef::Create(
- Expression::CreateFunctionCall("Ternary", {c.id, a.id, b.id}));
+ Expression::CreateFunctionCall("Ternary", {c.id, x.id, y.id}));
}
-#define CERES_DEFINE_EXPRESSION_COMPARISON_OPERATOR(op) \
- ComparisonExpressionRef operator op(ExpressionRef a, ExpressionRef b) { \
- return ComparisonExpressionRef(ExpressionRef::Create( \
- Expression::CreateBinaryCompare(#op, a.id, b.id))); \
+#define CERES_DEFINE_EXPRESSION_COMPARISON_OPERATOR(op) \
+ ComparisonExpressionRef operator op(const ExpressionRef& x, \
+ const ExpressionRef& y) { \
+ return ComparisonExpressionRef(ExpressionRef::Create( \
+ Expression::CreateBinaryCompare(#op, x.id, y.id))); \
}
-#define CERES_DEFINE_EXPRESSION_LOGICAL_OPERATOR(op) \
- ComparisonExpressionRef operator op(ComparisonExpressionRef a, \
- ComparisonExpressionRef b) { \
- return ComparisonExpressionRef(ExpressionRef::Create( \
- Expression::CreateBinaryCompare(#op, a.id, b.id))); \
+#define CERES_DEFINE_EXPRESSION_LOGICAL_OPERATOR(op) \
+ ComparisonExpressionRef operator op(const ComparisonExpressionRef& x, \
+ const ComparisonExpressionRef& y) { \
+ return ComparisonExpressionRef(ExpressionRef::Create( \
+ Expression::CreateBinaryCompare(#op, x.id, y.id))); \
}
CERES_DEFINE_EXPRESSION_COMPARISON_OPERATOR(<)
@@ -143,9 +144,9 @@
#undef CERES_DEFINE_EXPRESSION_COMPARISON_OPERATOR
#undef CERES_DEFINE_EXPRESSION_LOGICAL_OPERATOR
-ComparisonExpressionRef operator!(ComparisonExpressionRef a) {
+ComparisonExpressionRef operator!(const ComparisonExpressionRef& x) {
return ComparisonExpressionRef(
- ExpressionRef::Create(Expression::CreateLogicalNegation(a.id)));
+ ExpressionRef::Create(Expression::CreateLogicalNegation(x.id)));
}
} // namespace internal