]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Prefer std::vector to std::list. 3149/head
authorDavid Wells <wellsd2@rpi.edu>
Thu, 22 Sep 2016 13:51:08 +0000 (09:51 -0400)
committerDavid Wells <wellsd2@rpi.edu>
Thu, 22 Sep 2016 13:51:08 +0000 (09:51 -0400)
The top comment implies that a previous version of this algorithm saved
iterators into the temporary collection of new ranges (and hence this
required std::list); however, since the current version stores no such
iterators (it just uses push_back) we can make things a bit faster by
using std::vector instead of std::list.

source/base/index_set.cc

index bff0e5349f8d1db6d1442e262d404b3dae405eb2..6126766db58fd0ea8ccfeb519a1639d69944ed8d 100644 (file)
@@ -16,7 +16,8 @@
 #include <deal.II/base/memory_consumption.h>
 #include <deal.II/base/mpi.h>
 #include <deal.II/base/index_set.h>
-#include <list>
+
+#include <vector>
 
 #ifdef DEAL_II_WITH_TRILINOS
 #  ifdef DEAL_II_WITH_MPI
@@ -274,10 +275,9 @@ IndexSet::subtract_set (const IndexSet &other)
   is_compressed = false;
 
 
-  // we save new ranges to be added to our IndexSet in an temporary list and
-  // add all of them in one go at the end. This is necessary because a growing
-  // ranges vector invalidates iterators.
-  std::list<Range> temp_list;
+  // we save new ranges to be added to our IndexSet in an temporary vector and
+  // add all of them in one go at the end.
+  std::vector<Range> new_ranges;
 
   std::vector<Range>::iterator own_it = ranges.begin();
   std::vector<Range>::iterator other_it = other.ranges.begin();
@@ -303,7 +303,7 @@ IndexSet::subtract_set (const IndexSet &other)
         {
           Range r(own_it->begin, other_it->begin);
           r.nth_index_in_set = 0; //fix warning of unused variable
-          temp_list.push_back(r);
+          new_ranges.push_back(r);
         }
       // change own_it to the sub range behind other_it. Do not delete own_it
       // in any case. As removal would invalidate iterators, we just shrink
@@ -331,9 +331,9 @@ IndexSet::subtract_set (const IndexSet &other)
     }
 
   // done, now add the temporary ranges
-  for (std::list<Range>::iterator it = temp_list.begin();
-       it != temp_list.end();
-       ++it)
+  const std::vector<Range>::iterator end = new_ranges.end();
+  for (std::vector<Range>::iterator it = new_ranges.begin();
+       it != end; ++it)
     add_range(it->begin, it->end);
 
   compress();

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.