diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index 4eb17ccf..dc72b4df 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -40,12 +40,10 @@ extension RigidArray: Container where Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator( - from start: Index + from start: Index, to end: Index ) -> BorrowingIterator_ { - let span = self.span - let it = span.makeBorrowingIterator(from: start) - // FIXME: `it` is borrowing `span`, not self - return _overrideLifetime(it, borrowing: self) + // FIXME: `makeBorrowingIterator` would be borrowing the temporary `span`, not self + self.span._makeBorrowingIterator(from: start, to: end) } @_alwaysEmitIntoClient diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index 255e3e3b..53116a7c 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -40,9 +40,9 @@ extension UniqueArray: Container where Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator( - from start: Index + from start: Index, to end: Index ) -> BorrowingIterator_ { - _storage.makeBorrowingIterator(from: start) + _storage.makeBorrowingIterator(from: start, to: end) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift index a65d2637..6a88e605 100644 --- a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift @@ -23,10 +23,11 @@ where Element: ~Copyable @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator( - from start: Index + from start: Index, + to end: Index ) -> BorrowingIterator_ { let span = self.span - let it = span.makeBorrowingIterator(from: start) + let it = span.makeBorrowingIterator(from: start, to: end) // FIXME: `it` is borrowing `span`, not self return _overrideLifetime(it, borrowing: self) } diff --git a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift index 9f61e033..ed5625ff 100644 --- a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift @@ -21,16 +21,10 @@ extension MutableSpan: RandomAccessContainer where Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator( - from start: Int + from start: Int, to end: Int ) -> BorrowingIterator_ { - var it = Span.BorrowingIterator_(self.span) - var remainder = start - while remainder > 0 { - let d = it.skip_(by: remainder) - precondition(d > 0) - remainder &-= d - } - return it + // FIXME: `makeBorrowingIterator` would be borrowing the temporary `span`, not self + self.span._makeBorrowingIterator(from: start, to: end) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift index a73894e7..f048680a 100644 --- a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift @@ -23,12 +23,10 @@ where Element: ~Copyable @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator( - from start: Index + from start: Index, to end: Index ) -> BorrowingIterator_ { - let span = self.span - let it = span.makeBorrowingIterator(from: start) - // FIXME: `it` is borrowing `span`, not self - return _overrideLifetime(it, borrowing: self) + // FIXME: `makeBorrowingIterator` would be borrowing the temporary `span`, not self + self.span._makeBorrowingIterator(from: start, to: end) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Conformances/Span+Container.swift b/Sources/ContainersPreview/Conformances/Span+Container.swift index b6f872db..95fac5e1 100644 --- a/Sources/ContainersPreview/Conformances/Span+Container.swift +++ b/Sources/ContainersPreview/Conformances/Span+Container.swift @@ -14,19 +14,18 @@ #if compiler(>=6.4) && UnstableContainersPreview @available(SwiftStdlib 6.4, *) extension Span: RandomAccessContainer where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(copy self) + package func _makeBorrowingIterator(from start: Int, to end: Int) -> BorrowingIterator_ { + // Note: This is declared `copy self` so that types can forward to it without having to override lifetimes. + // FIXME: SpanIterator needs to have a public "slicing" initializer + BorrowingIterator_(self, from: start, to: end) + } + @_alwaysEmitIntoClient @_lifetime(borrow self) - public func makeBorrowingIterator(from start: Int) -> BorrowingIterator_ { - precondition(start >= 0 && start <= self.count, "Index out of bounds") - // FIXME: SpanIterator needs to have a direct initializer that takes `start` - var it = BorrowingIterator_(self) - var remainder = start - while remainder > 0 { - let d = it.skip_(by: remainder) - precondition(d > 0) - remainder &-= d - } - return it + public func makeBorrowingIterator(from start: Int, to end: Int) -> BorrowingIterator_ { + _makeBorrowingIterator(from: start, to: end) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Conformances/Span+Iterable.swift b/Sources/ContainersPreview/Conformances/Span+Iterable.swift index 256c41ae..f841bc26 100644 --- a/Sources/ContainersPreview/Conformances/Span+Iterable.swift +++ b/Sources/ContainersPreview/Conformances/Span+Iterable.swift @@ -51,6 +51,16 @@ extension Span where Element: ~Copyable { _count = elements.count } + @_lifetime(copy elements) + public init(_ elements: Span, from start: Int, to end: Int) { + precondition(start >= 0 && start <= elements.count, "Index out of bounds") + precondition(end >= 0 && end <= elements.count, "Index out of bounds") + precondition(start <= end, "start index must be less than or equal to end index") + _span = elements + _start = start + _count = end &- start + } + @_alwaysEmitIntoClient @_lifetime(&self) @_lifetime(self: copy self) diff --git a/Sources/ContainersPreview/Protocols/Container/Container.swift b/Sources/ContainersPreview/Protocols/Container/Container.swift index 36213044..51c8e534 100644 --- a/Sources/ContainersPreview/Protocols/Container/Container.swift +++ b/Sources/ContainersPreview/Protocols/Container/Container.swift @@ -22,7 +22,7 @@ public protocol Container: Iterable_, ~Copyable, ~Escapable where Element: ~Copyable, Element == Element_, Failure_ == Never { - associatedtype Element: ~Copyable + override associatedtype Element: ~Copyable /// Indices are expected to implement `==` and `hash(into:)` with /// constant complexity. @@ -45,7 +45,7 @@ public protocol Container: /// - Complexity: Recommended to be O(1); conforming types must clearly /// document deviations from this expectation. @_lifetime(borrow self) - func makeBorrowingIterator(from start: Index) -> BorrowingIterator_ + func makeBorrowingIterator(from start: Index, to end: Index) -> BorrowingIterator_ /// - Complexity: Recommended to be O(1); conforming types must clearly /// document deviations from this expectation. @@ -188,6 +188,18 @@ public protocol Container: limitedBy limit: Index ) -> Span + /// Accesses the element at the specified position. + /// + /// You can subscript a collection with any valid index other than the + /// collection’s end index. The end index refers to the position one past the + /// last element of a collection, so it doesn’t correspond with an element. + /// + /// - Parameter position: The position of the element to access. + /// `position` must be a valid index of the container that is not equal + /// to the `endIndex` property. + /// - Complexity: O(1). This is a hard requirement. + subscript(index: Index) -> Element { borrow } + /// Returns the position immediately after the given index. /// /// - Parameter index: A valid index of the container. `i` must be less @@ -259,18 +271,6 @@ public protocol Container: /// expectation. func distance(from start: Index, to end: Index) -> Int - /// Accesses the element at the specified position. - /// - /// You can subscript a collection with any valid index other than the - /// collection’s end index. The end index refers to the position one past the - /// last element of a collection, so it doesn’t correspond with an element. - /// - /// - Parameter position: The position of the element to access. - /// `position` must be a valid index of the container that is not equal - /// to the `endIndex` property. - /// - Complexity: O(1). This is a hard requirement. - subscript(index: Index) -> Element { borrow } - func _customIndexOfEquatableElement(_ element: borrowing Element) -> Index?? func _customLastIndexOfEquatableElement(_ element: borrowing Element) -> Index?? } @@ -280,6 +280,20 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_alwaysEmitIntoClient @_transparent public var underestimatedCount_: Int { count } + + @_alwaysEmitIntoClient + @_transparent + @_lifetime(borrow self) + public func makeBorrowingIterator_() -> BorrowingIterator_ { + self.makeBorrowingIterator(from: self.startIndex, to: self.endIndex) + } + + @_alwaysEmitIntoClient + @_transparent + @_lifetime(borrow self) + public func makeBorrowingIterator_(from start: Index) -> BorrowingIterator_ { + self.makeBorrowingIterator(from: start, to: self.endIndex) + } } @available(SwiftStdlib 6.4, *) @@ -398,18 +412,44 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable { extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_alwaysEmitIntoClient public func distance(from start: Index, to end: Index) -> Int { - // This variant allows bulk iteration, but as we can't decide if - // start <= end, we have to measure distances from both ends. +#if true + // This variant allows start to follow end, but as indices aren't + // comparable, we have to measure distances from both ends. var d1 = 0 var d2 = 0 - var i = start - var j = end - while true { - d1 += self.nextSpan(after: &i, limitedBy: end).count - if i == end { return d1 } - d2 += self.nextSpan(after: &j, limitedBy: start).count - if j == start { return d2 } + var i1 = start + var i2 = end + var forward = true + var backward = true + while forward || backward { + if forward { + let c = self.nextSpan(after: &i1, limitedBy: end).count + d1 += c + if i1 == end { return d1 } + if c == 0 { forward = false } + } + if backward { + let c = self.nextSpan(after: &i2, limitedBy: start).count + d2 -= c + if i2 == start { return d2 } + if c == 0 { backward = false } + } } + fatalError("Invalid Container") +#else + // Worse variant: this requires start <= end, but it has no way to quickly + // validate it. Furthermore, the restriction conflicts with Collection's + // more flexible implementation. + var i = start + var d = 0 + while true { + let c = self.nextSpan(after: &i, limitedBy: j).count + d += c + if i == end { break } + precondition(c > 0, "Invalid Container or 'start' does not precede 'end'") + } + return d +#endif } } diff --git a/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift b/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift index 3e3f5795..ca71c43f 100644 --- a/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift +++ b/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift @@ -23,13 +23,16 @@ where @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator_() -> BorrowingIterator_ { - ContainerIterator(_borrowing: self, from: self.startIndex) + ContainerIterator( + _borrowing: self, + from: self.startIndex, + to: self.endIndex) } @_alwaysEmitIntoClient @_lifetime(borrow self) - public func makeBorrowingIterator(from start: Index) -> BorrowingIterator_ { - ContainerIterator(_borrowing: self, from: start) + public func makeBorrowingIterator(from start: Index, to end: Index) -> BorrowingIterator_ { + ContainerIterator(_borrowing: self, from: start, to: end) } @_alwaysEmitIntoClient @@ -49,14 +52,18 @@ where Base.Element: ~Copyable @_alwaysEmitIntoClient package let _base: Ref // FIXME: This doesn't support nonescapable Bases + @_alwaysEmitIntoClient + package let _end: Base.Index + @_alwaysEmitIntoClient package var _position: Base.Index @_alwaysEmitIntoClient @_lifetime(borrow base) - package init(_borrowing base: borrowing Base, from position: Base.Index) { + package init(_borrowing base: borrowing Base, from start: Base.Index, to end: Base.Index) { self._base = Ref(base) - self._position = position + self._end = end + self._position = start } } @@ -72,7 +79,11 @@ where @_unsafeNonescapableResult // FIXME: we cannot convert from a borrow to an inout dependence?! @_lifetime(&self) public mutating func nextSpan_(maxCount: Int) -> Span { - _base.value.nextSpan(after: &self._position, maxCount: maxCount) + _base.value + .nextSpan( + after: &self._position, + maxCount: maxCount, + limitedBy: self._end) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift index aa844857..9c200bf4 100644 --- a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift @@ -27,7 +27,6 @@ where /// - Complexity: O(1) subscript(index: Index) -> Element { borrow mutate } - @_alwaysEmitIntoClient @_lifetime(&self) mutating func nextMutableSpan(after index: inout Index) -> MutableSpan diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift index c0f5750b..3cf99daa 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift @@ -53,16 +53,20 @@ extension RigidDeque where Element: ~Copyable { @_lifetime(borrow _deque) internal init( _deque: borrowing RigidDeque, - from start: Int + from start: Int, + to end: Int ) { precondition(start >= 0 && start <= _deque.count, "Index out of bounds") - self.init(_deque: _deque) - var remainder = start - while remainder > 0 { - let d = self.skip_(by: remainder) - precondition(d > 0) - remainder &-= d - } + precondition(end >= 0 && end <= _deque.count, "Index out of bounds") + precondition(start <= end, "The start must not be greater than the end") + let segments = _deque._handle.segments(forOffsets: start ..< end) + self._currentSegment = _overrideLifetime( + Span(_unsafeElements: segments.first), + borrowing: _deque) + self._nextSegment = _overrideLifetime( + Span( + _unsafeElements: segments.second ?? UnsafeBufferPointer._empty), + borrowing: _deque) self._position = start } @@ -94,9 +98,9 @@ extension RigidDeque: Container where Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator( - from start: Index + from start: Index, to end: Index ) -> BorrowingIterator_ { - BorrowingIterator(_deque: self, from: start) + BorrowingIterator(_deque: self, from: start, to: end) } @_alwaysEmitIntoClient diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift index 8ee86f4f..dfa485a2 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift @@ -38,9 +38,9 @@ extension UniqueDeque: Container where Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(borrow self) public func makeBorrowingIterator( - from start: Index + from start: Index, to end: Index ) -> BorrowingIterator_ { - _storage.makeBorrowingIterator(from: start) + _storage.makeBorrowingIterator(from: start, to: end) } @_alwaysEmitIntoClient diff --git a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift index 19e6cbfb..243d7db2 100644 --- a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift +++ b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift @@ -60,7 +60,7 @@ package struct _StaccatoParameters { fromOffset startOffset: Int, maxCount: Int ) -> Int { - precondition(maxCount > 0) + precondition(maxCount >= 0) precondition(startOffset >= 0 && startOffset <= _count) var cycleOffset = startOffset % _modulus var i = 0 @@ -82,21 +82,26 @@ public struct _StaccatoBorrowingIterator: BorrowingIteratorP internal let _contents: Span internal let _params: _StaccatoParameters internal var _offset: Int + internal let _end: Int @_lifetime(copy contents) internal init( contents: Span, params: _StaccatoParameters, - start: _StaccatoIndex = .init(_offset: 0) + start: _StaccatoIndex = .init(_offset: 0), + end: _StaccatoIndex? = nil ) { precondition(start._offset >= 0 && start._offset <= params._count) self._contents = contents self._params = params self._offset = start._offset + self._end = end?._offset ?? params._count } - @_lifetime(&self) // FIXME: Should be `@_lifetime(copy self)` + @_lifetime(&self) public mutating func nextSpan_(maxCount: Int) -> Span { + precondition(maxCount > 0) + let maxCount = Swift.min(maxCount, _end - _offset) let endOffset = _params.endOffset(fromOffset: _offset, maxCount: maxCount) let startOffset = _offset _offset = endOffset @@ -147,8 +152,8 @@ extension StaccatoContainer: Container where Element: ~Copyable { public var count: Int { _params._count } @_lifetime(borrow self) - public func makeBorrowingIterator(from start: Index) -> BorrowingIterator_ { - BorrowingIterator_(contents: _contents.span, params: _params, start: start) + public func makeBorrowingIterator(from start: Index, to end: Index) -> BorrowingIterator_ { + BorrowingIterator_(contents: _contents.span, params: _params, start: start, end: end) } public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index {