From 5edd1be23ce06d55da78f2aa3a56efe7eeaba071 Mon Sep 17 00:00:00 2001 From: Dakshin Devanand Date: Sat, 8 Mar 2025 10:08:39 -0600 Subject: [PATCH 1/4] Make Heap._UnsafeHandle min/maxValue tie-breaker consistent with Swift.min/max --- Sources/HeapModule/Heap+UnsafeHandle.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/HeapModule/Heap+UnsafeHandle.swift b/Sources/HeapModule/Heap+UnsafeHandle.swift index b7d8ceeb..c348357b 100644 --- a/Sources/HeapModule/Heap+UnsafeHandle.swift +++ b/Sources/HeapModule/Heap+UnsafeHandle.swift @@ -81,12 +81,12 @@ extension Heap._UnsafeHandle { @inlinable @inline(__always) internal func minValue(_ a: _HeapNode, _ b: _HeapNode) -> _HeapNode { - self[a] < self[b] ? a : b + self[a] <= self[b] ? a : b } @inlinable @inline(__always) internal func maxValue(_ a: _HeapNode, _ b: _HeapNode) -> _HeapNode { - self[a] < self[b] ? b : a + self[a] > self[b] ? a : b } } From d93a6ab8a198f43ef13c69752ea33df3a01a3d73 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 15 May 2025 17:30:25 -0700 Subject: [PATCH 2/4] =?UTF-8?q?[Heap]=20Follow=20the=20spelling=20of=20std?= =?UTF-8?q?lib=E2=80=99s=20min/max;=20add=20note=20explaining=20expectatio?= =?UTF-8?q?ns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Sources/HeapModule/Heap+UnsafeHandle.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sources/HeapModule/Heap+UnsafeHandle.swift b/Sources/HeapModule/Heap+UnsafeHandle.swift index c348357b..774ea170 100644 --- a/Sources/HeapModule/Heap+UnsafeHandle.swift +++ b/Sources/HeapModule/Heap+UnsafeHandle.swift @@ -81,12 +81,17 @@ extension Heap._UnsafeHandle { @inlinable @inline(__always) internal func minValue(_ a: _HeapNode, _ b: _HeapNode) -> _HeapNode { - 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] ? a : b + // In case `a` and `b` match, we need to pick `b`. See `minValue(_:_:)`. + self[b] >= self[a] ? b : a } } From 4e48f1d0c61a9c787fa2655f2442e7f6018724b0 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 15 May 2025 17:32:15 -0700 Subject: [PATCH 3/4] [Heap] Add quick tests for consistent tie breaks --- Tests/HeapTests/HeapTests.swift | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/Tests/HeapTests/HeapTests.swift b/Tests/HeapTests/HeapTests.swift index 07cc17c4..040d5541 100644 --- a/Tests/HeapTests/HeapTests.swift +++ b/Tests/HeapTests/HeapTests.swift @@ -587,4 +587,58 @@ final class HeapTests: CollectionTestCase { } } } + + struct Distinguishable: Comparable, CustomStringConvertible { + var value: Int + var id: Int + + static func ==(left: Self, right: Self) -> Bool { + left.value == right.value + } + static func <(left: Self, right: Self) -> Bool { + left.value < right.value + } + var description: String { "\(value)/\(id)" } + } + + func test_tieBreaks_min() { + var heap: Heap = [ + Distinguishable(value: 1, id: 1), + Distinguishable(value: 1, id: 2), + Distinguishable(value: 1, id: 3), + Distinguishable(value: 1, id: 4), + Distinguishable(value: 1, id: 5), + ] + while !heap.isEmpty { + let oldID = heap.min!.id + let newID = 10 * oldID + let old = heap.replaceMin(with: Distinguishable(value: 1, id: newID)) + expectEqual(old.id, oldID) + expectEqual(heap.min?.id, 10 * oldID) + expectNotNil(heap.removeMin()) { min in + expectEqual(min.id, newID) + } + } + } + + func test_tieBreaks_max() { + var heap: Heap = [ + Distinguishable(value: 1, id: 1), + Distinguishable(value: 1, id: 2), + Distinguishable(value: 1, id: 3), + Distinguishable(value: 1, id: 4), + Distinguishable(value: 1, id: 5), + ] + while !heap.isEmpty { + let oldID = heap.max!.id + let newID = 10 * oldID + print(heap.unordered) + let old = heap.replaceMax(with: Distinguishable(value: 1, id: newID)) + expectEqual(old.id, oldID) + expectEqual(heap.max?.id, 10 * oldID) + expectNotNil(heap.removeMax()) { max in + expectEqual(max.id, newID) + } + } + } } From 225159599d37106d63795d2df1bcbe0ccf7b3479 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 15 May 2025 17:32:48 -0700 Subject: [PATCH 4/4] [Heap] Fix remaining tie break inconsistency in popMax --- Sources/HeapModule/Heap.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/HeapModule/Heap.swift b/Sources/HeapModule/Heap.swift index 7c60a674..d533ead6 100644 --- a/Sources/HeapModule/Heap.swift +++ b/Sources/HeapModule/Heap.swift @@ -212,7 +212,7 @@ extension Heap { handle.swapAt(.leftMax, with: &removed) } } else { - let maxNode = handle.maxValue(.rightMax, .leftMax) + let maxNode = handle.maxValue(.leftMax, .rightMax) handle.swapAt(maxNode, with: &removed) handle.trickleDownMax(maxNode) }