]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid quadratic complexity in IndexSet::add_ranges
authorMartin Kronbichler <martin.kronbichler@uni-a.de>
Fri, 30 Jun 2023 09:34:12 +0000 (11:34 +0200)
committerMartin Kronbichler <martin.kronbichler@uni-a.de>
Fri, 30 Jun 2023 09:34:12 +0000 (11:34 +0200)
source/base/index_set.cc

index f51d30ddfb626ff469841fb2608f209e6f727710..cf73cf524b3fa5d3a5c3869563c42c669d2bb8ee 100644 (file)
@@ -388,8 +388,15 @@ IndexSet::pop_front()
 void
 IndexSet::add_range_lower_bound(const Range &new_range)
 {
-  ranges.insert(Utilities::lower_bound(ranges.begin(), ranges.end(), new_range),
-                new_range);
+  // if the inserted range is already within the range we find by lower_bound,
+  // there is no need to do anything; we do not try to be clever here and
+  // leave all other work to compress().
+  const auto insert_position =
+    Utilities::lower_bound(ranges.begin(), ranges.end(), new_range);
+  if (insert_position == ranges.end() ||
+      insert_position->begin > new_range.begin ||
+      insert_position->end < new_range.end)
+    ranges.insert(insert_position, new_range);
 }
 
 

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.