From d93a6ab8a198f43ef13c69752ea33df3a01a3d73 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 15 May 2025 17:30:25 -0700 Subject: [PATCH] =?UTF-8?q?[Heap]=20Follow=20the=20spelling=20of=20stdlib?= =?UTF-8?q?=E2=80=99s=20min/max;=20add=20note=20explaining=20expectations?= 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 } }