]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid quadratic complexity in IndexSet::subtract_set 13066/head
authorMartin Kronbichler <martin.kronbichler@it.uu.se>
Mon, 13 Dec 2021 13:58:55 +0000 (14:58 +0100)
committerMartin Kronbichler <martin.kronbichler@it.uu.se>
Mon, 13 Dec 2021 13:58:55 +0000 (14:58 +0100)
doc/news/changes/minor/20211213MartinKronbichler [new file with mode: 0644]
source/base/index_set.cc

diff --git a/doc/news/changes/minor/20211213MartinKronbichler b/doc/news/changes/minor/20211213MartinKronbichler
new file mode 100644 (file)
index 0000000..d141a40
--- /dev/null
@@ -0,0 +1,4 @@
+Fixed: IndexSet::subtract_set would previously run in quadratic complexity in
+the number of ranges. This is now fixed.
+<br>
+(Martin Kronbichler, 2021/12/13)
index 1383dd9495744ad1ecc5d4b133a35b42e5298e4e..774ddfacb6e452ad9ce6a556e879b19c8249c57c 100644 (file)
@@ -270,7 +270,7 @@ IndexSet::subtract_set(const IndexSet &other)
   is_compressed = false;
 
 
-  // we save new ranges to be added to our IndexSet in an temporary vector and
+  // we save all new ranges to our IndexSet in an temporary vector and
   // add all of them in one go at the end.
   std::vector<Range> new_ranges;
 
@@ -282,6 +282,7 @@ IndexSet::subtract_set(const IndexSet &other)
       // advance own iterator until we get an overlap
       if (own_it->end <= other_it->begin)
         {
+          new_ranges.push_back(*own_it);
           ++own_it;
           continue;
         }
@@ -314,15 +315,11 @@ IndexSet::subtract_set(const IndexSet &other)
       // next.
     }
 
-  // Now delete all empty ranges we might
-  // have created.
-  for (std::vector<Range>::iterator it = ranges.begin(); it != ranges.end();)
-    {
-      if (it->begin >= it->end)
-        it = ranges.erase(it);
-      else
-        ++it;
-    }
+  // make sure to take over the remaining ranges
+  for (; own_it != ranges.end(); ++own_it)
+    new_ranges.push_back(*own_it);
+
+  ranges.clear();
 
   // done, now add the temporary ranges
   const std::vector<Range>::iterator end = new_ranges.end();

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.