]> https://gitweb.dealii.org/ - dealii.git/commitdiff
AffineConstraints::close(): Avoid sorting of sorted list 16132/head
authorMartin Kronbichler <martin.kronbichler@uni-a.de>
Fri, 13 Oct 2023 18:05:05 +0000 (20:05 +0200)
committerMartin Kronbichler <martin.kronbichler@uni-a.de>
Fri, 13 Oct 2023 21:27:59 +0000 (23:27 +0200)
include/deal.II/lac/affine_constraints.templates.h

index eb2029bd430af55bfaec5853f0d28ef038d64540..3b4c4cf3a6875574fd2874041d0b5a361dad102c 100644 (file)
@@ -954,25 +954,33 @@ AffineConstraints<number>::close()
       for (ConstraintLine &line : boost::iterator_range<
              typename std::vector<ConstraintLine>::iterator>(begin, end))
         {
-          std::sort(line.entries.begin(),
-                    line.entries.end(),
-                    [](const std::pair<unsigned int, number> &a,
-                       const std::pair<unsigned int, number> &b) -> bool {
-                      // Let's use lexicographic ordering with std::abs for
-                      // number type (it might be complex valued).
-                      return (a.first < b.first) ||
-                             (a.first == b.first &&
-                              std::abs(a.second) < std::abs(b.second));
-                    });
-
-          // loop over the now sorted list and see whether any of the entries
-          // references the same dofs more than once in order to find how many
-          // non-duplicate entries we have. This lets us allocate the correct
-          // amount of memory for the constraint entries.
-          size_type duplicates = 0;
-          for (size_type i = 1; i < line.entries.size(); ++i)
-            if (line.entries[i].first == line.entries[i - 1].first)
-              ++duplicates;
+          unsigned int duplicates                   = 0;
+          bool         is_sorted_without_duplicates = true;
+          for (unsigned int i = 1; i < line.entries.size(); ++i)
+            if (!(line.entries[i - 1].first < line.entries[i].first))
+              {
+                is_sorted_without_duplicates = false;
+                break;
+              }
+          if (is_sorted_without_duplicates == false)
+            {
+              std::sort(line.entries.begin(),
+                        line.entries.end(),
+                        [](const std::pair<unsigned int, number> &a,
+                           const std::pair<unsigned int, number> &b) -> bool {
+                          // Just look at the index, ignore the value.
+                          return a.first < b.first;
+                        });
+
+              // loop over the now sorted list and see whether any of the
+              // entries references the same dofs more than once in order to
+              // find how many non-duplicate entries we have. This lets us
+              // allocate the correct amount of memory for the constraint
+              // entries.
+              for (size_type i = 1; i < line.entries.size(); ++i)
+                if (line.entries[i].first == line.entries[i - 1].first)
+                  ++duplicates;
+            }
 
           if (duplicates > 0 || (line.entries.size() < line.entries.capacity()))
             {

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.