No public description PiperOrigin-RevId: 1015731434 Change-Id: Ief29671e1ede0c8ba31d40b145e47a68a4fdd9df
diff --git a/google3/third_party/libigl/include/igl/sort.cpp b/google3/third_party/libigl/include/igl/sort.cpp index bf3299a..66d4679 100644 --- a/google3/third_party/libigl/include/igl/sort.cpp +++ b/google3/third_party/libigl/include/igl/sort.cpp
@@ -129,21 +129,34 @@ // Sort the index map, using unsorted for comparison if(dim == 1) { - std::sort( - ix.data(), - ix.data()+ix.size(), - igl::IndexVectorLessThan<const typename DerivedX::ConstColXpr >(X.col(i))); + if(ascending) + { + std::stable_sort( + ix.data(), + ix.data()+ix.size(), + igl::IndexVectorLessThan<const typename DerivedX::ConstColXpr >(X.col(i))); + }else + { + std::stable_sort( + ix.data(), + ix.data()+ix.size(), + [&X,i](int a, int b) { return X(b,i) < X(a,i); }); + } }else { - std::sort( - ix.data(), - ix.data()+ix.size(), - igl::IndexVectorLessThan<const typename DerivedX::ConstRowXpr >(X.row(i))); - } - // if not ascending then reverse - if(!ascending) - { - std::reverse(ix.data(),ix.data()+ix.size()); + if(ascending) + { + std::stable_sort( + ix.data(), + ix.data()+ix.size(), + igl::IndexVectorLessThan<const typename DerivedX::ConstRowXpr >(X.row(i))); + }else + { + std::stable_sort( + ix.data(), + ix.data()+ix.size(), + [&X,i](int a, int b) { return X(i,b) < X(i,a); }); + } } for(int j = 0;j<num_inner;j++) { @@ -309,15 +322,18 @@ index_map[i] = i; } // Sort the index map, using unsorted for comparison -std::sort( - index_map.begin(), - index_map.end(), - igl::IndexLessThan<const std::vector<T>& >(unsorted)); - -// if not ascending then reverse -if(!ascending) +if(ascending) { - std::reverse(index_map.begin(),index_map.end()); + std::stable_sort( + index_map.begin(), + index_map.end(), + igl::IndexLessThan<const std::vector<T>& >(unsorted)); +}else +{ + std::stable_sort( + index_map.begin(), + index_map.end(), + [&unsorted](size_t a, size_t b) { return unsorted[b] < unsorted[a]; }); } // make space for output without clobbering sorted.resize(unsorted.size());
diff --git a/google3/third_party/libigl/include/igl/sortrows.cpp b/google3/third_party/libigl/include/igl/sortrows.cpp index 4455535..e451a0d 100644 --- a/google3/third_party/libigl/include/igl/sortrows.cpp +++ b/google3/third_party/libigl/include/igl/sortrows.cpp
@@ -6,6 +6,7 @@ // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include "sortrows.h" +#include <algorithm> #include "get_seconds.h" #include "SortableRow.h" @@ -78,7 +79,7 @@ } return false; }; - std::sort( + std::stable_sort( IX.data(), IX.data()+IX.size(), index_less_than @@ -91,7 +92,7 @@ } return false; }; - std::sort( + std::stable_sort( IX.data(), IX.data()+IX.size(), index_greater_than
diff --git a/google3/third_party/libigl/patches/18.sort_stable_sort.patch b/google3/third_party/libigl/patches/18.sort_stable_sort.patch new file mode 100644 index 0000000..759ffe9 --- /dev/null +++ b/google3/third_party/libigl/patches/18.sort_stable_sort.patch
@@ -0,0 +1,110 @@ +# Use std::stable_sort with strict ascending and descending comparators in +# igl::sort_new, igl::sort, and igl::sortrows so index sorting preserves +# deterministic tie-breaking order for duplicate elements and rows. +# Introduced in cl/1013391165. + +--- a/google3/third_party/libigl/include/igl/sort.cpp ++++ b/google3/third_party/libigl/include/igl/sort.cpp +@@ -129,21 +129,34 @@ IGL_INLINE void igl::sort_new( + // Sort the index map, using unsorted for comparison + if(dim == 1) + { +- std::sort( +- ix.data(), +- ix.data()+ix.size(), +- igl::IndexVectorLessThan<const typename DerivedX::ConstColXpr >(X.col(i))); ++ if(ascending) ++ { ++ std::stable_sort( ++ ix.data(), ++ ix.data()+ix.size(), ++ igl::IndexVectorLessThan<const typename DerivedX::ConstColXpr >(X.col(i))); ++ }else ++ { ++ std::stable_sort( ++ ix.data(), ++ ix.data()+ix.size(), ++ [&X,i](int a, int b) { return X(b,i) < X(a,i); }); ++ } + }else + { +- std::sort( +- ix.data(), +- ix.data()+ix.size(), +- igl::IndexVectorLessThan<const typename DerivedX::ConstRowXpr >(X.row(i))); +- } +- // if not ascending then reverse +- if(!ascending) +- { +- std::reverse(ix.data(),ix.data()+ix.size()); ++ if(ascending) ++ { ++ std::stable_sort( ++ ix.data(), ++ ix.data()+ix.size(), ++ igl::IndexVectorLessThan<const typename DerivedX::ConstRowXpr >(X.row(i))); ++ }else ++ { ++ std::stable_sort( ++ ix.data(), ++ ix.data()+ix.size(), ++ [&X,i](int a, int b) { return X(i,b) < X(i,a); }); ++ } + } + for(int j = 0;j<num_inner;j++) + { +@@ -309,15 +322,18 @@ for(size_t i=0;i<unsorted.size();i++) + index_map[i] = i; + } + // Sort the index map, using unsorted for comparison +-std::sort( +- index_map.begin(), +- index_map.end(), +- igl::IndexLessThan<const std::vector<T>& >(unsorted)); +- +-// if not ascending then reverse +-if(!ascending) ++if(ascending) + { +- std::reverse(index_map.begin(),index_map.end()); ++ std::stable_sort( ++ index_map.begin(), ++ index_map.end(), ++ igl::IndexLessThan<const std::vector<T>& >(unsorted)); ++}else ++{ ++ std::stable_sort( ++ index_map.begin(), ++ index_map.end(), ++ [&unsorted](size_t a, size_t b) { return unsorted[b] < unsorted[a]; }); + } + // make space for output without clobbering + sorted.resize(unsorted.size()); +--- a/google3/third_party/libigl/include/igl/sortrows.cpp ++++ b/google3/third_party/libigl/include/igl/sortrows.cpp +@@ -6,6 +6,7 @@ + // v. 2.0. If a copy of the MPL was not distributed with this file, You can + // obtain one at http://mozilla.org/MPL/2.0/. + #include "sortrows.h" ++#include <algorithm> + #include "get_seconds.h" + + #include "SortableRow.h" +@@ -78,7 +79,7 @@ IGL_INLINE void igl::sortrows( + } + return false; + }; +- std::sort( ++ std::stable_sort( + IX.data(), + IX.data()+IX.size(), + index_less_than +@@ -91,7 +92,7 @@ IGL_INLINE void igl::sortrows( + } + return false; + }; +- std::sort( ++ std::stable_sort( + IX.data(), + IX.data()+IX.size(), + index_greater_than
diff --git a/google3/third_party/libigl/patches/series b/google3/third_party/libigl/patches/series index af84f13..ef1f9e6 100644 --- a/google3/third_party/libigl/patches/series +++ b/google3/third_party/libigl/patches/series
@@ -9,4 +9,5 @@ 14.cl_313746457.patch 15.missing_includes.patch 16.missing_return.patch -17.uninitialized_variables.patch \ No newline at end of file +17.uninitialized_variables.patch +18.sort_stable_sort.patch \ No newline at end of file