diff --git a/Sources/DequeModule/Deque+Container.swift b/Sources/DequeModule/Deque+Container.swift index a1baa3a9..b523c973 100644 --- a/Sources/DequeModule/Deque+Container.swift +++ b/Sources/DequeModule/Deque+Container.swift @@ -21,7 +21,14 @@ extension Deque: RandomAccessContainer { @available(SwiftCompatibilitySpan 5.0, *) @lifetime(borrow self) - public func nextSpan(after index: inout Int, maximumCount: Int) -> Span { - _storage.value.nextSpan(after: &index, maximumCount: maximumCount) + public func nextSpan(after index: inout Int) -> Span { + _storage.value.nextSpan(after: &index) } + + @available(SwiftCompatibilitySpan 5.0, *) + @lifetime(borrow self) + public func previousSpan(before index: inout Int) -> Span { + _storage.value.previousSpan(before: &index) + } + } diff --git a/Sources/DequeModule/DynamicDeque.swift b/Sources/DequeModule/DynamicDeque.swift index 084e834f..b20719ae 100644 --- a/Sources/DequeModule/DynamicDeque.swift +++ b/Sources/DequeModule/DynamicDeque.swift @@ -35,8 +35,14 @@ extension DynamicDeque: @unchecked Sendable where Element: Sendable & ~Copyable extension DynamicDeque: RandomAccessContainer where Element: ~Copyable { @available(SwiftCompatibilitySpan 5.0, *) @lifetime(borrow self) - public func nextSpan(after index: inout Int, maximumCount: Int) -> Span { - _storage.nextSpan(after: &index, maximumCount: maximumCount) + public func nextSpan(after index: inout Int) -> Span { + _storage.nextSpan(after: &index) + } + + @available(SwiftCompatibilitySpan 5.0, *) + @lifetime(borrow self) + public func previousSpan(before index: inout Int) -> Span { + _storage.previousSpan(before: &index) } } diff --git a/Sources/DequeModule/RigidDeque.swift b/Sources/DequeModule/RigidDeque.swift index 9164d0ba..1c8a5d8b 100644 --- a/Sources/DequeModule/RigidDeque.swift +++ b/Sources/DequeModule/RigidDeque.swift @@ -82,17 +82,24 @@ extension RigidDeque where Element: ~Copyable { extension RigidDeque: RandomAccessContainer, MutableContainer where Element: ~Copyable { @inlinable @lifetime(borrow self) - public func nextSpan(after index: inout Int, maximumCount: Int) -> Span { - let slots = _handle.slotRange(following: &index, maximumCount: maximumCount) + public func nextSpan(after index: inout Int) -> Span { + let slots = _handle.slotRange(following: &index) + return _span(over: slots) + } + + @inlinable + @lifetime(borrow self) + public func previousSpan(before index: inout Int) -> Span { + let slots = _handle.slotRange(preceding: &index) return _span(over: slots) } @inlinable @lifetime(&self) public mutating func nextMutableSpan( - after index: inout Int, maximumCount: Int + after index: inout Int ) -> MutableSpan { - let slots = _handle.slotRange(following: &index, maximumCount: maximumCount) + let slots = _handle.slotRange(following: &index) return _mutableSpan(over: slots) } } diff --git a/Sources/DequeModule/_UnsafeDequeHandle.swift b/Sources/DequeModule/_UnsafeDequeHandle.swift index b6e2d2b1..a36939c1 100644 --- a/Sources/DequeModule/_UnsafeDequeHandle.swift +++ b/Sources/DequeModule/_UnsafeDequeHandle.swift @@ -386,24 +386,40 @@ extension _UnsafeDequeHandle where Element: ~Copyable { extension _UnsafeDequeHandle where Element: ~Copyable { @inlinable - internal func slotRange(following offset: inout Int, maximumCount: Int) -> Range { - precondition(offset >= 0 && offset <= count, "Invalid index") - precondition(maximumCount > 0, "maximumCount must be positive") + internal func slotRange(following offset: inout Int) -> Range { + precondition(offset >= 0 && offset <= count, "Index out of bounds") guard _buffer.baseAddress != nil else { return Range(uncheckedBounds: (Slot.zero, Slot.zero)) } let wrapOffset = Swift.min(capacity - startSlot.position, count) if offset < wrapOffset { - let endOffset = offset + Swift.min(wrapOffset - offset, maximumCount) - defer { offset += endOffset - offset } + defer { offset += wrapOffset - offset } return Range( - uncheckedBounds: (startSlot.advanced(by: offset), startSlot.advanced(by: endOffset))) + uncheckedBounds: (startSlot.advanced(by: offset), startSlot.advanced(by: wrapOffset))) } - let endOffset = offset + Swift.min(count - offset, maximumCount) let lowerSlot = Slot.zero.advanced(by: offset - wrapOffset) - let upperSlot = lowerSlot.advanced(by: endOffset - wrapOffset) - defer { offset += endOffset - offset } + let upperSlot = lowerSlot.advanced(by: count - wrapOffset) + defer { offset += count - offset } + return Range(uncheckedBounds: (lower: lowerSlot, upper: upperSlot)) + } + + @inlinable + internal func slotRange(preceding offset: inout Int) -> Range { + precondition(offset >= 0 && offset <= count, "Index out of bounds") + guard _buffer.baseAddress != nil else { + return Range(uncheckedBounds: (Slot.zero, Slot.zero)) + } + let wrapOffset = Swift.min(capacity - startSlot.position, count) + + if offset <= wrapOffset { + defer { offset = 0 } + return Range( + uncheckedBounds: (startSlot, startSlot.advanced(by: offset))) + } + let lowerSlot = Slot.zero + let upperSlot = lowerSlot.advanced(by: offset - wrapOffset) + defer { offset = wrapOffset } return Range(uncheckedBounds: (lower: lowerSlot, upper: upperSlot)) } } diff --git a/Sources/Future/Arrays/DynamicArray.swift b/Sources/Future/Arrays/DynamicArray.swift index ef8900aa..92f607d8 100644 --- a/Sources/Future/Arrays/DynamicArray.swift +++ b/Sources/Future/Arrays/DynamicArray.swift @@ -63,8 +63,13 @@ extension DynamicArray where Element: ~Copyable { @available(SwiftCompatibilitySpan 5.0, *) extension DynamicArray: RandomAccessContainer where Element: ~Copyable { @lifetime(borrow self) - public func nextSpan(after index: inout Int, maximumCount: Int) -> Span { - _storage.nextSpan(after: &index, maximumCount: maximumCount) + public func nextSpan(after index: inout Int) -> Span { + _storage.nextSpan(after: &index) + } + + @lifetime(borrow self) + public func previousSpan(before index: inout Int) -> Span { + _storage.previousSpan(before: &index) } } @@ -101,10 +106,8 @@ extension DynamicArray: MutableContainer where Element: ~Copyable { @available(SwiftCompatibilitySpan 5.0, *) @lifetime(&self) - public mutating func nextMutableSpan( - after index: inout Int, maximumCount: Int - ) -> MutableSpan { - _storage.nextMutableSpan(after: &index, maximumCount: maximumCount) + public mutating func nextMutableSpan(after index: inout Int) -> MutableSpan { + _storage.nextMutableSpan(after: &index) } } diff --git a/Sources/Future/Arrays/NewArray.swift b/Sources/Future/Arrays/NewArray.swift index 2e762aaa..cbce7f2d 100644 --- a/Sources/Future/Arrays/NewArray.swift +++ b/Sources/Future/Arrays/NewArray.swift @@ -65,10 +65,15 @@ extension NewArray: RandomAccessContainer, MutableContainer { public func borrowElement(at index: Int) -> Borrow { _storage.value.borrowElement(at: index) } - + @lifetime(borrow self) - public func nextSpan(after index: inout Int, maximumCount: Int) -> Span { - _storage.value.nextSpan(after: &index, maximumCount: maximumCount) + public func nextSpan(after index: inout Int) -> Span { + _storage.value.nextSpan(after: &index) + } + + @lifetime(borrow self) + public func previousSpan(before index: inout Int) -> Span { + return _storage.value.previousSpan(before: &index) } @lifetime(&self) @@ -78,11 +83,9 @@ extension NewArray: RandomAccessContainer, MutableContainer { } @lifetime(&self) - public mutating func nextMutableSpan( - after index: inout Int, maximumCount: Int - ) -> MutableSpan { + public mutating func nextMutableSpan(after index: inout Int) -> MutableSpan { _ensureUnique() - return _storage.value.nextMutableSpan(after: &index, maximumCount: maximumCount) + return _storage.value.nextMutableSpan(after: &index) } } diff --git a/Sources/Future/Arrays/RigidArray.swift b/Sources/Future/Arrays/RigidArray.swift index f84a3adb..71e1fc23 100644 --- a/Sources/Future/Arrays/RigidArray.swift +++ b/Sources/Future/Arrays/RigidArray.swift @@ -115,11 +115,17 @@ extension RigidArray where Element: ~Copyable { extension RigidArray where Element: ~Copyable { @inlinable - internal func _subrange(following index: inout Int, maximumCount: Int) -> Range { + internal func _contiguousSubrange(following index: inout Int) -> Range { precondition(index >= 0 && index <= _count, "Index out of bounds") - let end = index + Swift.min(_count - index, maximumCount) - defer { index = end } - return unsafe Range(uncheckedBounds: (index, end)) + defer { index = _count } + return unsafe Range(uncheckedBounds: (index, _count)) + } + + @inlinable + internal func _contiguousSubrange(preceding index: inout Int) -> Range { + precondition(index >= 0 && index <= _count, "Index out of bounds") + defer { index = 0 } + return unsafe Range(uncheckedBounds: (0, index)) } } @@ -129,8 +135,14 @@ extension RigidArray where Element: ~Copyable { extension RigidArray: RandomAccessContainer where Element: ~Copyable { @inlinable @lifetime(borrow self) - public func nextSpan(after index: inout Int, maximumCount: Int) -> Span { - _span(in: _subrange(following: &index, maximumCount: maximumCount)) + public func nextSpan(after index: inout Int) -> Span { + _span(in: _contiguousSubrange(following: &index)) + } + + @inlinable + @lifetime(borrow self) + public func previousSpan(before index: inout Int) -> Span { + _span(in: _contiguousSubrange(preceding: &index)) } } @@ -165,10 +177,8 @@ extension RigidArray where Element: ~Copyable { @available(SwiftCompatibilitySpan 5.0, *) extension RigidArray: MutableContainer where Element: ~Copyable { @lifetime(&self) - public mutating func nextMutableSpan( - after index: inout Int, maximumCount: Int - ) -> MutableSpan { - _mutableSpan(in: _subrange(following: &index, maximumCount: maximumCount)) + public mutating func nextMutableSpan(after index: inout Int) -> MutableSpan { + _mutableSpan(in: _contiguousSubrange(following: &index)) } } diff --git a/Sources/Future/Containers/BidirectionalContainer.swift b/Sources/Future/Containers/BidirectionalContainer.swift index 8b7db81f..6ac9873d 100644 --- a/Sources/Future/Containers/BidirectionalContainer.swift +++ b/Sources/Future/Containers/BidirectionalContainer.swift @@ -13,4 +13,21 @@ public protocol BidirectionalContainer: Container, ~Copyable, ~Escapable { func index(before i: Index) -> Index func formIndex(before i: inout Index) + + @lifetime(borrow self) + func previousSpan(before index: inout Index) -> Span +} + +@available(SwiftCompatibilitySpan 5.0, *) +extension BidirectionalContainer where Self: ~Copyable & ~Escapable { + @lifetime(borrow self) + func previousSpan(before index: inout Index, maximumCount: Int) -> Span { + var span = previousSpan(before: &index) + if span.count > maximumCount { + // Index remains within the same span, so offseting it is expected to be quick + index = self.index(index, offsetBy: span.count - maximumCount) + span = span._extracting(last: maximumCount) + } + return span + } } diff --git a/Sources/Future/Containers/Container.swift b/Sources/Future/Containers/Container.swift index 37bcb9cf..3e913cc9 100644 --- a/Sources/Future/Containers/Container.swift +++ b/Sources/Future/Containers/Container.swift @@ -30,19 +30,48 @@ public protocol Container: ~Copyable, ~Escapable { limitedBy limit: Index ) + // FIXME: Do we want these as standard requirements? + func index(alignedDown index: Index) -> Index + func index(alignedUp index: Index) -> Index + #if compiler(>=9999) // FIXME: We can't do this yet subscript(index: Index) -> Element { borrow } #else - @lifetime(copy self) + @lifetime(borrow self) func borrowElement(at index: Index) -> Borrow #endif - @lifetime(copy self) - func nextSpan(after index: inout Index, maximumCount: Int) -> Span + // See if index rounding results need to get returned somewhere + // maximumCount: see if it has any real reason to exist yet + @lifetime(borrow self) + func nextSpan(after index: inout Index) -> Span + + // Try a version where nextSpan takes an index range + + // See if it makes sense to have a ~Escapable ValidatedIndex type, as a sort of non-self-driving iterator substitute } @available(SwiftCompatibilitySpan 5.0, *) extension Container where Self: ~Copyable & ~Escapable { + @inlinable + public func index(alignedDown index: Index) -> Index { index } + + @inlinable + public func index(alignedUp index: Index) -> Index { index } + + @inlinable + @lifetime(borrow self) + public func nextSpan(after index: inout Index, maximumCount: Int) -> Span { + let original = index + var span = nextSpan(after: &index) + if span.count > maximumCount { + span = span._extracting(first: maximumCount) + // Index remains within the same span, so offseting it is expected to be quick + index = self.index(original, offsetBy: maximumCount) + } + return span + } + @inlinable public subscript(index: Index) -> Element { @lifetime(copy self) @@ -52,3 +81,41 @@ extension Container where Self: ~Copyable & ~Escapable { } } + +#if false // DEMO +@available(SwiftCompatibilitySpan 5.0, *) +extension Container where Self: ~Copyable & ~Escapable { + // This is just to demo the bulk iteration model + func forEachSpan(_ body: (Span) throws(E) -> Void) throws(E) { + var it = self.startIndex + while true { + let span = self.nextSpan(after: &it) + if span.isEmpty { break } + try body(span) + } + } + + // This is just to demo the bulk iteration model + func forEach(_ body: (borrowing Element) throws(E) -> Void) throws(E) { + var it = self.startIndex + while true { + let span = self.nextSpan(after: &it) + if span.isEmpty { break } + var i = 0 + while i < span.count { + unsafe try body(span[unchecked: i]) + i &+= 1 + } + } + } + + borrowing func borrowingMap( + _ transform: (borrowing Element) throws(E) -> U + ) throws(E) -> [U] { + var result: [U] = [] + result.reserveCapacity(count) + try self.forEach { value throws(E) in result.append(try transform(value)) } + return result + } +} +#endif diff --git a/Sources/Future/Containers/ContiguousContainer.swift b/Sources/Future/Containers/ContiguousContainer.swift index 3e9a92ef..853e7462 100644 --- a/Sources/Future/Containers/ContiguousContainer.swift +++ b/Sources/Future/Containers/ContiguousContainer.swift @@ -11,7 +11,7 @@ //===----------------------------------------------------------------------===// @available(SwiftCompatibilitySpan 5.0, *) -public protocol ContiguousContainer: ~Copyable, ~Escapable { +public protocol ContiguousContainer: /*RandomAccessContainer*/ ~Copyable, ~Escapable { associatedtype Element: ~Copyable/* & ~Escapable*/ var span: Span { @lifetime(copy self) get } diff --git a/Sources/Future/Containers/MutableContainer.swift b/Sources/Future/Containers/MutableContainer.swift index 6d69d453..d9c20d2e 100644 --- a/Sources/Future/Containers/MutableContainer.swift +++ b/Sources/Future/Containers/MutableContainer.swift @@ -19,11 +19,29 @@ public protocol MutableContainer: Container, ~Copyable, ~Escapable { #endif @lifetime(&self) - mutating func nextMutableSpan(after index: inout Index, maximumCount: Int) -> MutableSpan + mutating func nextMutableSpan(after index: inout Index) -> MutableSpan + + // FIXME: What about previousMutableSpan? } @available(SwiftCompatibilitySpan 5.0, *) extension MutableContainer where Self: ~Copyable & ~Escapable { + #if false // Hm... + @lifetime(&self) + mutating func nextMutableSpan( + after index: inout Index, maximumCount: Int + ) -> MutableSpan { + let original = index + var span = self.nextMutableSpan(after: &index) + if span.count > maximumCount { + span = span.extracting(first: maximumCount) + // Index remains within the same span, so offseting it is expected to be quick + index = self.index(original, offsetBy: maximumCount) + } + return span + } + #endif + @inlinable public subscript(index: Index) -> Element { @lifetime(borrow self) diff --git a/Sources/Future/Span/Span+Container.swift b/Sources/Future/Span/Span+Container.swift index fbe56dba..5f032241 100644 --- a/Sources/Future/Span/Span+Container.swift +++ b/Sources/Future/Span/Span+Container.swift @@ -25,11 +25,17 @@ extension Span: RandomAccessContainer where Element: ~Copyable { } @lifetime(copy self) - public func nextSpan(after index: inout Index, maximumCount: Int) -> Span { - precondition(index >= 0 && index <= count, "Invalid index") - let end = index + Swift.min(count - index, maximumCount) - defer { index = end } - return _extracting(unsafe Range(uncheckedBounds: (index, end))) + public func nextSpan(after index: inout Index) -> Span { + precondition(index >= 0 && index <= count, "Index out of bounds") + defer { index = count } + return _extracting(unsafe Range(uncheckedBounds: (index, count))) + } + + @lifetime(copy self) + public func previousSpan(before index: inout Int) -> Span { + precondition(index >= 0 && index <= count, "Index out of bounds") + defer { index = 0 } + return _extracting(unsafe Range(uncheckedBounds: (0, index))) } } diff --git a/Sources/Future/Tools/Shared.swift b/Sources/Future/Tools/Shared.swift index 1bb47b20..7eaaee1c 100644 --- a/Sources/Future/Tools/Shared.swift +++ b/Sources/Future/Tools/Shared.swift @@ -145,9 +145,16 @@ extension Shared where Storage: ~Copyable { } extension Shared where Storage: ~Copyable { - @available(SwiftStdlib 6.0, *) // for === @inlinable public func isIdentical(to other: Self) -> Bool { - unsafe self._box === other._box + if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) { + return unsafe self._box === other._box + } else { + // To call the standard `===`, we need to do `_SharedBox` -> AnyObject conversions + // that are only supported in the Swift 6+ runtime. + let a = unsafe Builtin.bridgeToRawPointer(self._box) + let b = unsafe Builtin.bridgeToRawPointer(other._box) + return Bool(Builtin.cmp_eq_RawPointer(a, b)) + } } } diff --git a/Tests/FutureTests/DynamicArrayTests.swift b/Tests/FutureTests/DynamicArrayTests.swift index 980545db..8a1ba8e7 100644 --- a/Tests/FutureTests/DynamicArrayTests.swift +++ b/Tests/FutureTests/DynamicArrayTests.swift @@ -146,14 +146,14 @@ class DynamicArrayTests: CollectionTestCase { var index = 0 do { - let span = array.nextSpan(after: &index, maximumCount: Int.max) + let span = array.nextSpan(after: &index) expectEqual(span.count, c) for i in 0 ..< span.count { expectEqual(span[i].value, 100 + i) } } do { - let span2 = array.nextSpan(after: &index, maximumCount: Int.max) + let span2 = array.nextSpan(after: &index) expectEqual(span2.count, 0) } } @@ -182,4 +182,11 @@ class DynamicArrayTests: CollectionTestCase { expectEqual(index, i) } } + + func iterationDemo() { + let c = 100_000 + let stride = 7 + let array = DynamicArray(count: c) { Counted($0) } + + } }