Repository navigation
Sort with exact comparisons rather than within tolerance - #152
Merged
Merged
Conversation
std::sort doesn't specify the relative order of elements which compare equal, and each standard library orders them its own way: libstdc++, for example, keeps them in input order for 16 elements or fewer and not beyond. So the same input could give different results with libstdc++, libc++ and MSVC, or even with another libstdc++ version. All the calls to std::sort are replaced by std::stable_sort, which keeps elements which compare equal in input order. Besides, 8 sorts compared the lengths along an element within the tolerance, with strictly_lesser, directly or through Shape::is_strictly_closer_to_path_start. Equivalence within tolerance is not transitive: with a = 0, b = 0.7e-6 and c = 1.4e-6, a and b are equivalent, b and c are equivalent, but a is lesser than c. Such a comparison is not a strict weak ordering, and std::sort's behavior is then undefined; the libstdc++ implementation may even read out of the range. These sorts now compare exactly, with the new Shape::is_exactly_closer_to_path_start or with operator<. The points within tolerance of each other are still adjacent once sorted, and the code which goes through them still compares them within tolerance.
std::stable_sort may be slower than std::sort. Only the sorts which compared within tolerance are changed, to compare exactly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
8 sorts compared the lengths along an element within the tolerance (
strictly_lesser, directly or throughShape::is_strictly_closer_to_path_start). Equivalence within tolerance isn't transitive (a = 0, b = 0.7e-6, c = 1.4e-6: a ~ b, b ~ c, but a < c), so these comparisons aren't strict weak orderings, andstd::sort's behavior is undefined (the libstdc++ implementation may even read out of the range).Changes
These sorts now compare exactly:
src/shapes_intersections.cpp: the sorts at lines 322 and 369 useoperator<on the lengths; the sorts at lines 504, 630, 689 and 993 use the newShape::is_exactly_closer_to_path_start;src/rasterization.cpp:111andsrc/shape.cpp:1746useShape::is_exactly_closer_to_path_start.Shape::is_strictly_closer_to_path_startis unchanged, since it is also used outside of sorts, where the tolerance is intended. The points within tolerance of each other are still adjacent once sorted, and the code which goes through them still compares them within tolerance.Not changed
std::sortis kept: it is not replaced bystd::stable_sort. So the relative order of elements which compare equal still depends on the standard library (see rectangle: tree_search_maximal_spaces orders insertions of equal guide by std::sort, so results depend on the standard library packingsolver#758).std::minmax_elementcalls insrc/shapes_intersections.cpp(lines 310 and 616) still compare within the tolerance. They don't read out of the range, but which point of a cluster within tolerance they return is not well defined either.Tests
All 714 shape tests pass.