[PPL-devel] [GIT] ppl/ppl(master): Three FIXMEs related to Linear_Expression resolved.

Enea Zaffanella zaffanella at cs.unipr.it
Sun Mar 22 17:52:59 CET 2009


Module: ppl/ppl
Branch: master
Commit: 1491112a597743c630269e2c67f84eafc6b31928
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=1491112a597743c630269e2c67f84eafc6b31928

Author: Enea Zaffanella <zaffanella at cs.unipr.it>
Date:   Sun Mar 22 17:47:33 2009 +0100

Three FIXMEs related to Linear_Expression resolved.
The three operators are now friend of Linear_Expression: hence they can use
the sizing constructors and limit the use of short-lived temporaries.

---

 src/Linear_Expression.cc         |   25 ++++++++++++++++++++++---
 src/Linear_Expression.defs.hh    |    6 ++++++
 src/Linear_Expression.inlines.hh |   29 ++++++++++++++++++-----------
 3 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/src/Linear_Expression.cc b/src/Linear_Expression.cc
index aabaad5..a694a15 100644
--- a/src/Linear_Expression.cc
+++ b/src/Linear_Expression.cc
@@ -180,13 +180,30 @@ PPL::operator-(const Linear_Expression& e1, const Linear_Expression& e2) {
 
 /*! \relates Parma_Polyhedra_Library::Linear_Expression */
 PPL::Linear_Expression
+PPL::operator-(const Variable v, const Linear_Expression& e) {
+  const dimension_type v_space_dim = v.space_dimension();
+  if (v_space_dim > Linear_Expression::max_space_dimension())
+    throw std::length_error("Linear_Expression "
+                            "PPL::operator-(v, e):\n"
+                            "v exceeds the maximum allowed "
+                            "space dimension.");
+  const dimension_type e_space_dim = e.space_dimension();
+  const dimension_type space_dim = std::max(v_space_dim, e_space_dim);
+  Linear_Expression result(space_dim+1, false);
+  ++result[v_space_dim];
+  for (dimension_type i = e_space_dim+1; i-- > 0; )
+    result[i] -= e[i];
+  return result;
+}
+
+/*! \relates Parma_Polyhedra_Library::Linear_Expression */
+PPL::Linear_Expression
 PPL::operator-(Coefficient_traits::const_reference n,
 	       const Linear_Expression& e) {
   Linear_Expression r(e);
   for (dimension_type i = e.size(); i-- > 0; )
     neg_assign(r[i]);
   r[0] += n;
-
   return r;
 }
 
@@ -222,7 +239,8 @@ PPL::Linear_Expression&
 PPL::operator+=(Linear_Expression& e, const Variable v) {
   const dimension_type v_space_dim = v.space_dimension();
   if (v_space_dim > Linear_Expression::max_space_dimension())
-    throw std::length_error("PPL::operator+=(e, v):\n"
+    throw std::length_error("Linear_Expression& "
+                            "PPL::operator+=(e, v):\n"
 			    "v exceeds the maximum allowed space dimension.");
   const dimension_type e_size = e.size();
   if (e_size <= v_space_dim) {
@@ -255,7 +273,8 @@ PPL::Linear_Expression&
 PPL::operator-=(Linear_Expression& e, const Variable v) {
   const dimension_type v_space_dim = v.space_dimension();
   if (v_space_dim > Linear_Expression::max_space_dimension())
-    throw std::length_error("PPL::operator-=(e, v):\n"
+    throw std::length_error("Linear_Expression& "
+                            "PPL::operator-=(e, v):\n"
 			    "v exceeds the maximum allowed space dimension.");
   const dimension_type e_size = e.size();
   if (e_size <= v_space_dim) {
diff --git a/src/Linear_Expression.defs.hh b/src/Linear_Expression.defs.hh
index db4af74..6b1adc1 100644
--- a/src/Linear_Expression.defs.hh
+++ b/src/Linear_Expression.defs.hh
@@ -409,6 +409,8 @@ private:
   friend Linear_Expression
   operator+(const Linear_Expression& e, Coefficient_traits::const_reference n);
   friend Linear_Expression
+  operator+(Variable v, const Linear_Expression& e);
+  friend Linear_Expression
   operator+(Variable v, Variable w);
 
   friend Linear_Expression
@@ -422,6 +424,10 @@ private:
   operator-(Coefficient_traits::const_reference n, const Linear_Expression& e);
   friend Linear_Expression
   operator-(const Linear_Expression& e, Coefficient_traits::const_reference n);
+  friend Linear_Expression
+  operator-(Variable v, const Linear_Expression& e);
+  friend Linear_Expression
+  operator-(const Linear_Expression& e, Variable v);
 
   friend Linear_Expression
   operator*(Coefficient_traits::const_reference n, const Linear_Expression& e);
diff --git a/src/Linear_Expression.inlines.hh b/src/Linear_Expression.inlines.hh
index a5da9d2..1af543a 100644
--- a/src/Linear_Expression.inlines.hh
+++ b/src/Linear_Expression.inlines.hh
@@ -144,8 +144,15 @@ operator+(const Linear_Expression& e, Coefficient_traits::const_reference n) {
 /*! \relates Linear_Expression */
 inline Linear_Expression
 operator+(const Variable v, const Linear_Expression& e) {
-  // FIXME(0.10.1): provide a better implementation.
-  return e + Linear_Expression(v);
+  const dimension_type v_space_dim = v.space_dimension();
+  if (v_space_dim > Linear_Expression::max_space_dimension())
+    throw std::length_error("PPL::operator+(v, e):\n"
+                            "v exceeds the maximum allowed "
+                            "space dimension.");
+  const dimension_type space_dim = std::max(v_space_dim, e.space_dimension());
+  Linear_Expression result(e, space_dim+1);
+  ++result[v_space_dim];
+  return result;
 }
 
 /*! \relates Linear_Expression */
@@ -168,16 +175,16 @@ operator-(const Variable v, const Variable w) {
 
 /*! \relates Linear_Expression */
 inline Linear_Expression
-operator-(const Variable v, const Linear_Expression& e) {
-  // FIXME(0.10.1): provide a better implementation.
-  return Linear_Expression(v) - e;
-}
-
-/*! \relates Linear_Expression */
-inline Linear_Expression
 operator-(const Linear_Expression& e, const Variable v) {
-  // FIXME(0.10.1): provide a better implementation.
-  return e - Linear_Expression(v);
+  const dimension_type v_space_dim = v.space_dimension();
+  if (v_space_dim > Linear_Expression::max_space_dimension())
+    throw std::length_error("PPL::operator-(e, v):\n"
+                            "v exceeds the maximum allowed "
+                            "space dimension.");
+  const dimension_type space_dim = std::max(v_space_dim, e.space_dimension());
+  Linear_Expression result(e, space_dim+1);
+  --result[v_space_dim];
+  return result;
 }
 
 /*! \relates Linear_Expression */




More information about the PPL-devel mailing list