[PPL-devel] [GIT] ppl/ppl(master): When working with sizes, prefer vector::resize() to vector::erase().

Enea Zaffanella zaffanella at cs.unipr.it
Fri Feb 24 15:15:04 CET 2012


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

Author: Enea Zaffanella <zaffanella at cs.unipr.it>
Date:   Fri Feb 24 14:53:40 2012 +0100

When working with sizes, prefer vector::resize() to vector::erase().
Added helper function nth_iter().

---

 src/Bit_Matrix.cc           |    2 +-
 src/Bit_Matrix.inlines.hh   |    2 +-
 src/Box.templates.hh        |    2 +-
 src/DB_Matrix.templates.hh  |    2 +-
 src/Dense_Matrix.cc         |    2 +-
 src/Dense_Matrix.inlines.hh |    2 +-
 src/Grid_chdims.cc          |    4 ++--
 src/Linear_System.cc        |    4 ++--
 src/MIP_Problem.inlines.hh  |    4 ++--
 src/PIP_Problem.cc          |    2 +-
 src/PIP_Tree.cc             |    6 +++---
 src/globals.defs.hh         |    8 ++++++++
 src/globals.inlines.hh      |   14 ++++++++++++++
 13 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/src/Bit_Matrix.cc b/src/Bit_Matrix.cc
index 96243f7..2d0f107 100644
--- a/src/Bit_Matrix.cc
+++ b/src/Bit_Matrix.cc
@@ -137,7 +137,7 @@ PPL::Bit_Matrix::resize(dimension_type new_n_rows,
   }
   else if (new_n_rows < old_num_rows)
     // Drop some rows.
-    rows.erase(rows.begin() + new_n_rows, rows.end());
+    rows.resize(new_n_rows);
 
   PPL_ASSERT(OK());
 }
diff --git a/src/Bit_Matrix.inlines.hh b/src/Bit_Matrix.inlines.hh
index dfd2b03..39f614f 100644
--- a/src/Bit_Matrix.inlines.hh
+++ b/src/Bit_Matrix.inlines.hh
@@ -63,7 +63,7 @@ Bit_Matrix::remove_trailing_rows(const dimension_type n) {
   // than the actual number of the rows of the matrix.
   PPL_ASSERT(n <= rows.size());
   if (n != 0)
-    rows.erase(rows.end() - n, rows.end());
+    rows.resize(rows.size() - n);
   PPL_ASSERT(OK());
 }
 
diff --git a/src/Box.templates.hh b/src/Box.templates.hh
index 66357af..d67c7d8 100644
--- a/src/Box.templates.hh
+++ b/src/Box.templates.hh
@@ -2062,7 +2062,7 @@ Box<ITV>::remove_higher_space_dimensions(const dimension_type new_dim) {
     return;
   }
 
-  seq.erase(seq.begin() + new_dim, seq.end());
+  seq.resize(new_dim);
   PPL_ASSERT(OK());
 }
 
diff --git a/src/DB_Matrix.templates.hh b/src/DB_Matrix.templates.hh
index 374c9ad..e03974d 100644
--- a/src/DB_Matrix.templates.hh
+++ b/src/DB_Matrix.templates.hh
@@ -178,7 +178,7 @@ DB_Matrix<T>::resize_no_copy(const dimension_type new_n_rows) {
   }
   else if (new_n_rows < old_n_rows) {
     // Drop some rows.
-    rows.erase(rows.begin() + new_n_rows, rows.end());
+    rows.resize(new_n_rows);
     // Shrink the existing rows.
     for (dimension_type i = new_n_rows; i-- > 0; )
       rows[i].shrink(new_n_rows);
diff --git a/src/Dense_Matrix.cc b/src/Dense_Matrix.cc
index f7d132a..b702765 100644
--- a/src/Dense_Matrix.cc
+++ b/src/Dense_Matrix.cc
@@ -294,7 +294,7 @@ PPL::Dense_Matrix::resize_no_copy(const dimension_type new_n_rows,
   }
   else if (new_n_rows < old_n_rows) {
     // Drop some rows.
-    rows.erase(rows.begin() + new_n_rows, rows.end());
+    rows.resize(new_n_rows);
     old_n_rows = new_n_rows;
   }
   // Here we have the right number of rows.
diff --git a/src/Dense_Matrix.inlines.hh b/src/Dense_Matrix.inlines.hh
index c43ae82..0bac033 100644
--- a/src/Dense_Matrix.inlines.hh
+++ b/src/Dense_Matrix.inlines.hh
@@ -190,7 +190,7 @@ inline void
 Dense_Matrix::remove_trailing_rows(const dimension_type n) {
   PPL_ASSERT(n <= rows.size());
   if (n != 0)
-    rows.erase(rows.end() - n, rows.end());
+    rows.resize(rows.size() - n);
   PPL_ASSERT(OK());
 }
 
diff --git a/src/Grid_chdims.cc b/src/Grid_chdims.cc
index 2b7af92..e6731c7 100644
--- a/src/Grid_chdims.cc
+++ b/src/Grid_chdims.cc
@@ -351,7 +351,7 @@ PPL::Grid::remove_higher_space_dimensions(const dimension_type new_dimension) {
 	gen_sys.remove_trailing_rows(num_redundant);
 	gen_sys.unset_pending_rows();
       }
-      dim_kinds.erase(dim_kinds.begin() + new_dimension + 1, dim_kinds.end());
+      dim_kinds.resize(new_dimension + 1);
       // TODO: Consider if it is worth also preserving the congruences
       //       if they are also in minimal form.
     }
@@ -383,7 +383,7 @@ PPL::Grid::remove_higher_space_dimensions(const dimension_type new_dimension) {
       // form.
       con_sys.remove_trailing_rows(num_redundant);
     }
-    dim_kinds.erase(dim_kinds.begin() + new_dimension + 1, dim_kinds.end());
+    dim_kinds.resize(new_dimension + 1);
     clear_generators_up_to_date();
     // Replace gen_sys with an empty system of the right dimension.
     // Extra 2 columns for inhomogeneous term and modulus.
diff --git a/src/Linear_System.cc b/src/Linear_System.cc
index 506b507..f8a3e8c 100644
--- a/src/Linear_System.cc
+++ b/src/Linear_System.cc
@@ -337,8 +337,8 @@ PPL::Linear_System::sort_rows(const dimension_type first_row,
   PPL_ASSERT(first_row >= first_pending_row() || last_row <= first_pending_row());
 
   // First sort without removing duplicates.
-  std::vector<Dense_Row>::iterator first = rows.begin() + first_row;
-  std::vector<Dense_Row>::iterator last = rows.begin() + last_row;
+  std::vector<Dense_Row>::iterator first = nth_iter(rows, first_row);
+  std::vector<Dense_Row>::iterator last = nth_iter(rows, last_row);
   Implementation::swapping_sort(first, last, Row_Less_Than());
   // Second, move duplicates to the end.
   std::vector<Dense_Row>::iterator new_last
diff --git a/src/MIP_Problem.inlines.hh b/src/MIP_Problem.inlines.hh
index 1d6954f..df09249 100644
--- a/src/MIP_Problem.inlines.hh
+++ b/src/MIP_Problem.inlines.hh
@@ -111,7 +111,7 @@ MIP_Problem::~MIP_Problem() {
   // NOTE: do NOT delete inherited constraints; they are owned
   // (and will eventually be deleted) by ancestors.
   for (Constraint_Sequence::const_iterator
-         i = input_cs.begin() + inherited_constraints,
+         i = nth_iter(input_cs, inherited_constraints),
          i_end = input_cs.end(); i != i_end; ++i)
     delete *i;
 }
@@ -217,7 +217,7 @@ MIP_Problem::external_memory_in_bytes() const {
   // NOTE: disregard inherited constraints, as they are owned by ancestors.
   n += input_cs.capacity() * sizeof(Constraint*);
   for (Constraint_Sequence::const_iterator
-         i = input_cs.begin() + inherited_constraints,
+         i = nth_iter(input_cs, inherited_constraints),
          i_end = input_cs.end(); i != i_end; ++i)
     n += ((*i)->total_memory_in_bytes());
 
diff --git a/src/PIP_Problem.cc b/src/PIP_Problem.cc
index 682120b..9c82202 100644
--- a/src/PIP_Problem.cc
+++ b/src/PIP_Problem.cc
@@ -132,7 +132,7 @@ PPL::PIP_Problem::solve() const {
 
       // Go through all pending constraints.
       for (Constraint_Sequence::const_iterator
-             cs_i = input_cs.begin() + first_pending_constraint,
+             cs_i = nth_iter(input_cs, first_pending_constraint),
              cs_end = input_cs.end(); cs_i != cs_end; ++cs_i) {
         const Constraint& c = *cs_i;
         const dimension_type c_space_dim = c.space_dimension();
diff --git a/src/PIP_Tree.cc b/src/PIP_Tree.cc
index b7dbbb2..76b5a0d 100644
--- a/src/PIP_Tree.cc
+++ b/src/PIP_Tree.cc
@@ -2223,8 +2223,8 @@ PIP_Solution_Node::update_tableau(
           Need to insert the original variable id
           before the slack variable id's to respect variable ordering.
         */
-        basis.insert(basis.begin() + new_var_column, true);
-        mapping.insert(mapping.begin() + new_var_column, new_var_column);
+        basis.insert(nth_iter(basis, new_var_column), true);
+        mapping.insert(nth_iter(mapping, new_var_column), new_var_column);
         // Update variable id's of slack variables.
         for (dimension_type j = var_row.size(); j-- > 0; )
           if (var_row[j] >= new_var_column)
@@ -2252,7 +2252,7 @@ PIP_Solution_Node::update_tableau(
   Coefficient_traits::const_reference denom = tableau.denominator();
 
   for (Constraint_Sequence::const_iterator
-         c_iter = input_cs.begin() + first_pending_constraint,
+         c_iter = nth_iter(input_cs, first_pending_constraint),
          c_end = input_cs.end(); c_iter != c_end; ++c_iter) {
     const Constraint& constraint = *c_iter;
     // (Tentatively) Add new rows to s and t matrices.
diff --git a/src/globals.defs.hh b/src/globals.defs.hh
index 6c592ed..314194d 100644
--- a/src/globals.defs.hh
+++ b/src/globals.defs.hh
@@ -489,6 +489,14 @@ check_space_dimension_overflow(dimension_type dim,
                                const char* method,
                                const char* reason);
 
+template <typename RA_Container>
+typename RA_Container::iterator
+nth_iter(RA_Container& cont, dimension_type n);
+
+template <typename RA_Container>
+typename RA_Container::const_iterator
+nth_iter(const RA_Container& cont, dimension_type n);
+
 } // namespace Parma_Polyhedra_Library
 
 // By default, use sparse matrices both for MIP_Problem and PIP_Problem.
diff --git a/src/globals.inlines.hh b/src/globals.inlines.hh
index ce6c30d..09936ec 100644
--- a/src/globals.inlines.hh
+++ b/src/globals.inlines.hh
@@ -128,6 +128,20 @@ is_space(char c) {
   return isspace(c) != 0;
 }
 
+template <typename RA_Container>
+inline typename RA_Container::iterator
+nth_iter(RA_Container& cont, dimension_type n) {
+  typedef typename RA_Container::difference_type diff_t;
+  return cont.begin() + static_cast<diff_t>(n);
+}
+
+template <typename RA_Container>
+inline typename RA_Container::const_iterator
+nth_iter(const RA_Container& cont, dimension_type n) {
+  typedef typename RA_Container::difference_type diff_t;
+  return cont.begin() + static_cast<diff_t>(n);
+}
+
 } // namespace Parma_Polyhedra_Library
 
 #endif // !defined(PPL_globals_inlines_hh)




More information about the PPL-devel mailing list