Align Heap._UnsafeHandle min/maxValue tie-breaking with Swift.min/max#455
Merged
lorentey merged 4 commits intoapple:mainfrom May 16, 2025
Merged
Align Heap._UnsafeHandle min/maxValue tie-breaking with Swift.min/max#455lorentey merged 4 commits intoapple:mainfrom
lorentey merged 4 commits intoapple:mainfrom
Conversation
lorentey
reviewed
May 2, 2025
Member
There was a problem hiding this comment.
Thanks -- this is definitely a good idea.
The goal of this is to guarantee that Heap behaves in a specific way for equal-but-distinguishable elements. This new guarantee must come with tests that verify that the implementation behaves as expected; otherwise the bug we want to fix could be accidentally revived in future package releases.
| @inlinable @inline(__always) | ||
| internal func minValue(_ a: _HeapNode, _ b: _HeapNode) -> _HeapNode { | ||
| self[a] < self[b] ? a : b | ||
| self[a] <= self[b] ? a : b |
Member
There was a problem hiding this comment.
I think it would make sense to add a comment explaining that the result emulates the stdlib. The implementation should also match precisely what the stdlib does, to emphasize this even further.
Suggested change
| self[a] <= self[b] ? a : b | |
| // The expression used here matches the implementation of the | |
| // standard `Swift.min(_:_:)` function. This attempts to | |
| // preserve any pre-existing order in case `T` has identity. | |
| // `(min(x, y), max(x, y))` should return `(x, y)` in case `x == y`. | |
| self[b] < self[a] ? b : a |
| @inlinable @inline(__always) | ||
| internal func maxValue(_ a: _HeapNode, _ b: _HeapNode) -> _HeapNode { | ||
| self[a] < self[b] ? b : a | ||
| self[a] > self[b] ? a : b |
Member
There was a problem hiding this comment.
Suggested change
| self[a] > self[b] ? a : b | |
| // In case `a` and `b` match, we need to pick `b`. See `minValue(_:_:)`. | |
| self[b] >= self[a] ? b : a |
Contributor
Author
|
Sounds good, thanks for the revision. Will go ahead and also add those tests. |
…xpectations The existing expressions had the same effect, but they used different operators (`<=` instead of `<` and `>` instead of `>=`). The distinction is usually irrelevant, but there is no reason not to just follow what the stdlib is doing exactly.
Member
|
@swift-ci test |
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.
Heap._UnsafeHandle has minValue and maxValue. Original functionality was
minValue,if a == b, return b. FormaxValue,if a == b, return a. In both cases, this is the opposite tiebreaker choice fromSwift.minandSwift.max.Heap.replaceMaxusedHeap._UnsafeHandle.maxValuewhileHeap.maxusedSwift.max, and due to different tiebreaker choices, led to inconsistency as seen in #439 .Therefore I go ahead and update
Heap._UnsafeHandle.maxValue/minValueto be consistent withSwift.max/min.Fixes #439
Checklist