From 95395ad9d2b99374b24bacb2baf55a07f0ad3153 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 20 Jul 2026 11:51:43 -0700 Subject: [PATCH] Add BidirectionalContainer.spanBoundary(before:maxDistance:limitedBy:) and make it the core primitive --- .../RigidArray/RigidArray+Container.swift | 17 ++- .../UniqueArray/UniqueArray+Container.swift | 17 ++- .../Conformances/InputSpan+Container.swift | 15 +- .../Conformances/MutableSpan+Container.swift | 13 +- .../Conformances/OutputSpan+Container.swift | 15 +- .../Conformances/Span+Container.swift | 17 ++- .../Container/BidirectionalContainer.swift | 141 +++++++++++------- .../RigidDeque/RigidDeque+Container.swift | 17 ++- .../UniqueDeque/UniqueDeque+Container.swift | 12 +- Sources/DequeModule/_UnsafeDequeHandle.swift | 26 ++++ 10 files changed, 214 insertions(+), 76 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index 5e4cf41d..89c8b468 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -463,18 +463,25 @@ extension RigidArray where Element: ~Copyable { } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { + public func spanBoundary(before index: Index) -> (index: Index, distance: Int) { precondition(index >= 0 && index <= count, "Index out of bounds") - precondition(maxDistance > 0, "maxDistance must be positive") - guard index > 0 else { return nil } - return Swift.max(0, index &- maxDistance) + return (0, index) } - @inlinable + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + self.span.spanBoundary( + before: index, maxDistance: maxDistance, limitedBy: limit) + } + + @_alwaysEmitIntoClient @_lifetime(borrow self) public func previousSpan( before index: inout Int, maxCount: Int ) -> Span { + // FIXME: Remove this in favor of the BidirectionalContainer algorithm. _checkValidIndex(index) precondition(maxCount > 0, "maxCount must be positive") let start = index diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index eb041b82..86f4e4c3 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -409,15 +409,26 @@ extension UniqueArray where Element: ~Copyable { } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { - _storage.spanBoundary(before: index, maxDistance: maxDistance) + public func spanBoundary( + before index: Index + ) -> (index: Index, distance: Int) { + _storage.spanBoundary(before: index) } - @inlinable + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + _storage.spanBoundary( + before: index, maxDistance: maxDistance, limitedBy: limit) + } + + @_alwaysEmitIntoClient @_lifetime(borrow self) public func previousSpan( before index: inout Int, maxCount: Int ) -> Span { + // FIXME: Remove this in favor of the BidirectionalContainer algorithm. _storage.previousSpan(before: &index, maxCount: maxCount) } } diff --git a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift index c6e88a04..75453beb 100644 --- a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift @@ -72,11 +72,20 @@ where Element: ~Copyable } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { + public func spanBoundary(before index: Index) -> (index: Index, distance: Int) { precondition(index >= 0 && index <= count, "Index out of bounds") + return (0, index) + } + + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + precondition(index >= 0 && index <= count, "Index out of bounds") + precondition(limit >= 0 && limit <= count, "Index out of bounds") precondition(maxDistance > 0, "maxDistance must be positive") - guard index > 0 else { return nil } - return Swift.max(0, index &- maxDistance) + let p = index._clampedDown(towards: 0, maxDistance: maxDistance, limitedBy: limit) + return (p, index &- p) } } #endif diff --git a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift index 25760b26..950f6731 100644 --- a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift @@ -71,8 +71,17 @@ extension MutableSpan: RandomAccessContainer where Element: ~Copyable { } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { - self.span.spanBoundary(before: index, maxDistance: maxDistance) + public func spanBoundary( + before index: Index + ) -> (index: Index, distance: Int) { + self.span.spanBoundary(before: index) + } + + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + self.span.spanBoundary(before: index, maxDistance: maxDistance, limitedBy: limit) } } diff --git a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift index e95408f3..5ac50db2 100644 --- a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift @@ -70,11 +70,20 @@ where Element: ~Copyable } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { + public func spanBoundary(before index: Index) -> (index: Index, distance: Int) { precondition(index >= 0 && index <= count, "Index out of bounds") + return (0, index) + } + + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + precondition(index >= 0 && index <= count, "Index out of bounds") + precondition(limit >= 0 && limit <= count, "Index out of bounds") precondition(maxDistance > 0, "maxDistance must be positive") - guard index > 0 else { return nil } - return Swift.max(0, index &- maxDistance) + let p = index._clampedDown(towards: 0, maxDistance: maxDistance, limitedBy: limit) + return (p, index &- p) } } #endif diff --git a/Sources/ContainersPreview/Conformances/Span+Container.swift b/Sources/ContainersPreview/Conformances/Span+Container.swift index c20d33cc..dcb2a835 100644 --- a/Sources/ContainersPreview/Conformances/Span+Container.swift +++ b/Sources/ContainersPreview/Conformances/Span+Container.swift @@ -92,11 +92,22 @@ extension Span where Element: ~Copyable { } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { + public func spanBoundary( + before index: Index + ) -> (index: Index, distance: Int) { precondition(index >= 0 && index <= count, "Index out of bounds") + return (0, index) + } + + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + precondition(index >= 0 && index <= count, "Index out of bounds") + precondition(limit >= 0 && limit <= count, "Index out of bounds") precondition(maxDistance > 0, "maxDistance must be positive") - guard index > 0 else { return nil } - return Swift.max(0, index &- maxDistance) + let p = index._clampedDown(towards: 0, maxDistance: maxDistance, limitedBy: limit) + return (p, index &- p) } } diff --git a/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift b/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift index e59e52de..591e99cc 100644 --- a/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift @@ -48,7 +48,11 @@ where Element: ~Copyable, Index: Comparable func formIndex(before i: inout Index) - func spanBoundary(before index: Index, maxDistance: Int) -> Index? + func spanBoundary(before index: Index) -> (index: Index, distance: Int) + + func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) override func index(after index: Index) -> Index @@ -67,45 +71,54 @@ where Element: ~Copyable, Index: Comparable extension BidirectionalContainer where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_alwaysEmitIntoClient - public func spanBoundary(before index: Index) -> Index? { - self.spanBoundary(before: index, maxDistance: Int.max) + @_transparent + public func spanBoundary( + before index: Index + ) -> (index: Index, distance: Int) { + self.spanBoundary( + before: index, + maxDistance: Int.max, + limitedBy: self.endIndex) } @_alwaysEmitIntoClient - @_lifetime(borrow self) - public func previousSpan(before index: inout Index) -> Span { - previousSpan(before: &index, maxCount: Int.max) + @_transparent + public func spanBoundary( + before index: Index, + maxDistance: Int + ) -> (index: Index, distance: Int) { + self.spanBoundary( + before: index, + maxDistance: maxDistance, + limitedBy: self.endIndex) } @_alwaysEmitIntoClient + @_transparent @_lifetime(borrow self) public func previousSpan( - before index: inout Index, maxCount: Int + before index: inout Index, + maxCount: Int = Int.max ) -> Span { - guard let i = spanBoundary(before: index, maxDistance: maxCount) else { - return .init() - } - var j = i - let span = nextSpan(after: &j, maxCount: maxCount) - precondition(j == index, "Invalid BidirectionalContainer") - index = i - return span + self.previousSpan( + before: &index, + maxCount: maxCount, + limitedBy: self.endIndex) } @_alwaysEmitIntoClient @_lifetime(borrow self) public func previousSpan( before index: inout Index, + maxCount: Int = Int.max, limitedBy limit: Index ) -> Span { - var j = index - let span = self.previousSpan(before: &j) - if limit <= index, limit > j { - let d = self.distance(from: limit, to: index) - index = limit - return span.extracting(last: d) - } - index = j + let (i, d) = spanBoundary(before: index, maxDistance: maxCount, limitedBy: limit) + if d == 0 { return .init() } + var j = i + let span = nextSpan(after: &j, limitedBy: index) + precondition(j == index && span.count <= maxCount, "Invalid BidirectionalContainer") + index = i return span } } @@ -113,6 +126,11 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { @available(SwiftStdlib 6.4, *) extension BidirectionalContainer where Self: ~Copyable & ~Escapable, Element: ~Copyable { + @_alwaysEmitIntoClient + public func index(before i: Index) -> Index { + self.spanBoundary(before: i, maxDistance: 1).index + } + @_alwaysEmitIntoClient public func formIndex(before i: inout Index) { i = self.index(before: i) @@ -122,16 +140,20 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { public func index(_ index: Index, offsetBy n: Int) -> Index { var index = index var n = n + let end = self.endIndex if n >= 0 { while n > 0 { - self.formIndex(after: &index) - n &-= 1 - } - } else { - while n < 0 { - self.formIndex(before: &index) - n &+= 1 + let c = self.nextSpan(after: &index, maxCount: n, limitedBy: end).count + precondition(c > 0, "Cannot advance index beyond the end of the container") + n &-= c } + return index + } + n = -n + while n > 0 { + let r = self.spanBoundary(before: index, maxDistance: n, limitedBy: end) + index = r.index + n &-= r.distance } return index } @@ -143,35 +165,22 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { var n = n if n >= 0 { while n > 0 { - var j = index - let c = self.nextSpan(after: &j, limitedBy: limit).count - if c == 0 { - // We hit the limit (or the end) - break - } - if c > n { - index = self.index(index, offsetBy: n) - n = 0 - break - } - index = j + let c = self.nextSpan(after: &index, maxCount: n, limitedBy: limit).count + precondition(c <= n, "Invalid container") + guard c > 0 else { break } n &-= c } return } - // Note: Don't negate `n` -- it may be `Int.min`. - while n < 0 { - let c = self.previousSpan(before: &index, limitedBy: limit).count - if c == 0 { + n = -n + while n > 0 { + let r = self.spanBoundary(before: index, maxDistance: n, limitedBy: limit) + if r.distance == 0 { // We hit the limit (or the start) return } - n &+= c - if n > 0 { - index = self.index(index, offsetBy: n) - n = 0 - return - } + n &+= r.distance + index = r.index } } @@ -189,6 +198,34 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { // Note: `distance(from:to:)` comes from `Container where Index: Comparable`. } +extension Strideable { + @_alwaysEmitIntoClient + package func _clampedUp( + towards boundary: Self, maxDistance: Stride, limitedBy limit: Self + ) -> Self { + assert(self <= boundary) + assert(maxDistance >= 0) + let limit = (limit >= self ? Swift.min(limit, boundary) : boundary) + if limit.distance(to: self) <= maxDistance { + return limit + } + return self.advanced(by: maxDistance) + } + + @_alwaysEmitIntoClient + package func _clampedDown( + towards boundary: Self, maxDistance: Stride, limitedBy limit: Self + ) -> Self { + assert(self >= boundary) + assert(maxDistance >= 0) + let limit = (limit <= self ? Swift.max(limit, boundary) : boundary) + if self.distance(to: limit) <= maxDistance { + return limit + } + return self.advanced(by: -maxDistance) + } +} + // FIXME: Add ambiguity resolvers against BidirectionCollection algorithms. #endif diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift index 6b513d6d..72a26205 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift @@ -195,16 +195,27 @@ extension RigidDeque where Element: ~Copyable { } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { + public func spanBoundary(before index: Index) -> (index: Index, distance: Int) { precondition(index >= 0 && index <= count, "Index out of bounds") + let r = self._handle.spanBoundary(before: index) + return (r.offset, r.distance) + } + + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + precondition(index >= 0 && index <= count, "Index out of bounds") + precondition(limit >= 0 && limit <= count, "Index out of bounds") precondition(maxDistance > 0, "maxDistance must be positive") - guard index > 0 else { return nil } - return Swift.max(0, index &- maxDistance) + let r = self._handle.spanBoundary(before: index, maxDistance: maxDistance, limitedBy: limit) + return (r.offset, r.distance) } @_alwaysEmitIntoClient @_lifetime(borrow self) public func previousSpan(before index: inout Int, maxCount: Int) -> Span { + // FIXME: Remove this in favor of the BidirectionalContainer algorithm. _checkValidIndex(index) precondition(maxCount > 0, "maxCount must be positive") let segment = self._handle diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift index 373a9ace..42b06bde 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift @@ -121,13 +121,21 @@ extension UniqueDeque where Element: ~Copyable { } @_alwaysEmitIntoClient - public func spanBoundary(before index: Index, maxDistance: Int) -> Index? { - _storage.spanBoundary(before: index, maxDistance: maxDistance) + public func spanBoundary(before index: Index) -> (index: Index, distance: Int) { + _storage.spanBoundary(before: index) + } + + @_alwaysEmitIntoClient + public func spanBoundary( + before index: Index, maxDistance: Int, limitedBy limit: Index + ) -> (index: Index, distance: Int) { + _storage.spanBoundary(before: index, maxDistance: maxDistance, limitedBy: limit) } @_alwaysEmitIntoClient @_lifetime(borrow self) public func previousSpan(before index: inout Int, maxCount: Int) -> Span { + // FIXME: Remove this in favor of the BidirectionalContainer algorithm. _storage.previousSpan(before: &index, maxCount: maxCount) } } diff --git a/Sources/DequeModule/_UnsafeDequeHandle.swift b/Sources/DequeModule/_UnsafeDequeHandle.swift index 3281eb17..f686da1c 100644 --- a/Sources/DequeModule/_UnsafeDequeHandle.swift +++ b/Sources/DequeModule/_UnsafeDequeHandle.swift @@ -369,6 +369,32 @@ extension _UnsafeDequeHandle where Element: ~Copyable { count: position - capacity) } + @_alwaysEmitIntoClient + internal func spanBoundary( + before startOffset: Int + ) -> (offset: Int, distance: Int) { + assert(startOffset >= 0 && startOffset <= count) + let wrapOffset = capacity &- startSlot.position + if startOffset <= wrapOffset { + return (0, startOffset) + } + return (wrapOffset, startOffset &- wrapOffset) + } + + @_alwaysEmitIntoClient + internal func spanBoundary( + before startOffset: Int, maxDistance: Int, limitedBy limitOffset: Int + ) -> (offset: Int, distance: Int) { + assert(startOffset >= 0 && startOffset <= count) + assert(limitOffset >= 0 && limitOffset <= count) + let wrapOffset = capacity &- startSlot.position + let p = startOffset._clampedDown( + towards: startOffset <= wrapOffset ? 0 : wrapOffset, + maxDistance: maxDistance, + limitedBy: limitOffset) + return (p, p &- startOffset) + } + @_alwaysEmitIntoClient internal func segments() -> _UnsafeDequeSegments { guard _buffer.baseAddress != nil else {