Skip to content

Sort with exact comparisons rather than within tolerance - #152

Merged
fontanf merged 2 commits into
mainfrom
stable-sort
Oct 31, 2026
Merged

fontanf merged 2 commits into
mainfrom
stable-sort

Conversation

@fontanf

@fontanf fontanf commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

8 sorts compared the lengths along an element within the tolerance (strictly_lesser, directly or through Shape::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, and std::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 use operator< on the lengths; the sorts at lines 504, 630, 689 and 993 use the new Shape::is_exactly_closer_to_path_start;
  • src/rasterization.cpp:111 and src/shape.cpp:1746 use Shape::is_exactly_closer_to_path_start.

Shape::is_strictly_closer_to_path_start is 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

Tests

All 714 shape tests pass.

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.
@fontanf fontanf changed the title Sort with std::stable_sort and exact comparisons Sort with exact comparisons rather than within tolerance Oct 31, 2026
@fontanf
fontanf merged commit d2dc186 into main Oct 31, 2026
3 checks passed
@fontanf
fontanf deleted the stable-sort branch October 31, 2026 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant