From 99da8a72e061fbe01f7feb5a776aaeb30f81f43a Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 3 Feb 2026 15:10:11 -0800 Subject: [PATCH] [BasicContainers] RigidArray, UniqueArray: Add formIndex(before:) --- .../RigidArray/RigidArray+Append.swift | 46 +++++-- .../RigidArray/RigidArray+Container.swift | 120 ++++++++++++++++++ .../RigidArray/RigidArray.swift | 107 ---------------- .../UniqueArray/UniqueArray+Container.swift | 112 +++++++++++++++- .../UniqueArray/UniqueArray.swift | 102 --------------- 5 files changed, 262 insertions(+), 225 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index b051dabe..57e64641 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -54,6 +54,37 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + /// Append a given number of items to the end of this array by populating + /// an output span. + /// + /// If the array does not have sufficient capacity to store the new items in + /// the buffer, then this triggers a runtime error. + /// + /// - Parameters + /// - uninitializedCount: The number of items to append to the array. + /// - body: A callback that gets called precisely once to directly + /// populate newly reserved storage within the array. The function + /// is allowed to initialize fewer than `uninitializedCount` items. + /// The array is appended however many items the callback adds to + /// the output span before it returns (or before it throws an error). + /// + /// - Complexity: O(`uninitializedCount`) + @_alwaysEmitIntoClient + public mutating func append( + addingCapacity uninitializedCount: Int, + initializingWith body: (inout OutputSpan) throws(E) -> Void + ) throws(E) { + precondition(uninitializedCount >= 0, "Negative count") + precondition(freeCapacity >= uninitializedCount, "RigidArray capacity overflow") + let buffer = _freeSpace._extracting(first: uninitializedCount) + var span = OutputSpan(buffer: buffer, initializedCount: 0) + defer { + _count &+= span.finalize(for: buffer) + span = OutputSpan() + } + return try body(&span) + } + /// Append a given number of items to the end of this array by populating /// an output span. /// @@ -69,21 +100,18 @@ extension RigidArray where Element: ~Copyable { /// before it returns (or before it throws an error). /// /// - Complexity: O(`count`) + @available(*, deprecated, renamed: "append(addingCapacity:initializingWith:)") @_alwaysEmitIntoClient + @inline(__always) public mutating func append( count: Int, initializingWith body: (inout OutputSpan) throws(E) -> Result ) throws(E) -> Result { - // FIXME: This is extremely similar to `edit()`, except it provides a narrower span. - precondition(count >= 0, "Negative count") - precondition(freeCapacity >= count, "RigidArray capacity overflow") - let buffer = _freeSpace._extracting(first: count) - var span = OutputSpan(buffer: buffer, initializedCount: 0) - defer { - _count &+= span.finalize(for: buffer) - span = OutputSpan() + var result: Result? = nil + try append(addingCapacity: count) { target throws(E) in + result = try body(&target) } - return try body(&span) + return result.take()! } } diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index d50057cf..efbc5246 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -40,6 +40,112 @@ extension RigidArray: Container where Element: ~Copyable { } #endif +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { + /// A Boolean value indicating whether this array contains no elements. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var isEmpty: Bool { count == 0 } + + /// The number of elements in this array. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var count: Int { _count } +} + +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { + /// A type that represents a position in the array: an integer offset from the + /// start. + /// + /// Valid indices consist of the position of every element and a "past the + /// end” position that’s not valid for use as a subscript argument. + public typealias Index = Int + + /// The position of the first element in a nonempty array. This is always zero. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var startIndex: Int { 0 } + + /// The array’s "past the end” position—that is, the position one greater than + /// the last valid subscript argument. This is always equal to the array's + /// count. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var endIndex: Int { count } + + /// The range of indices that are valid for subscripting the array. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var indices: Range { unsafe Range(uncheckedBounds: (0, count)) } +} + +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { + @inlinable @inline(__always) + internal func _ptr(to index: Int) -> UnsafePointer { + precondition(index >= 0 && index < _count, "Index out of bounds") + let p = _storage.baseAddress.unsafelyUnwrapped.advanced(by: index) + return UnsafePointer(p) + } + + @inlinable @inline(__always) + internal mutating func _mutablePtr( + to index: Int + ) -> UnsafeMutablePointer { + precondition(index >= 0 && index < _count, "Index out of bounds") + return _storage.baseAddress.unsafelyUnwrapped.advanced(by: index) + } + + /// Accesses the element at the specified position. + /// + /// - Parameter position: The position of the element to access. + /// The position must be a valid index of the array that is not equal + /// to the `endIndex` property. + /// + /// - Complexity: O(1) + @inlinable + public subscript(position: Int) -> Element { + @inline(__always) + unsafeAddress { + _ptr(to: position) + } + @inline(__always) + unsafeMutableAddress { + _mutablePtr(to: position) + } + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { + /// Exchanges the values at the specified indices of the array. + /// + /// Both parameters must be valid indices of the array and not equal to + /// endIndex. Passing the same index as both `i` and `j` has no effect. + /// + /// - Parameter i: The index of the first value to swap. + /// - Parameter j: The index of the second valud to swap. + /// + /// - Complexity: O(1) + @inlinable + public mutating func swapAt(_ i: Int, _ j: Int) { + precondition( + i >= 0 && i < _count && j >= 0 && j < _count, + "Index out of bounds") + unsafe _items.swapAt(i, j) + } +} @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { /// Returns the position immediately after the given index. @@ -86,6 +192,20 @@ extension RigidArray where Element: ~Copyable { @inline(__always) public func formIndex(after index: inout Int) { index += 1 } + /// Replaces the given index with its predecessor. + /// + /// - Note: To improve performance, this method does not validate that the + /// given index is valid before decrementing it. Index validation is + /// deferred until the resulting index is used to access an element. + /// This optimization may be removed in future versions; do not rely on it. + /// + /// - Parameter index: A valid index of the array. `i` must be greater than + /// `startIndex`. + /// - Complexity: O(1) + @_alwaysEmitIntoClient + @inline(__always) + public func formIndex(before index: inout Int) { index -= 1 } + /// Returns an index that is the specified distance from the given index. /// /// The value passed as `n` must not offset `index` beyond the bounds of the diff --git a/Sources/BasicContainers/RigidArray/RigidArray.swift b/Sources/BasicContainers/RigidArray/RigidArray.swift index ccc8be1d..2c5d2b67 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray.swift @@ -253,113 +253,6 @@ extension RigidArray where Element: ~Copyable { //MARK: - Container primitives -@available(SwiftStdlib 5.0, *) -extension RigidArray where Element: ~Copyable { - /// A Boolean value indicating whether this array contains no elements. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var isEmpty: Bool { count == 0 } - - /// The number of elements in this array. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var count: Int { _count } -} - -@available(SwiftStdlib 5.0, *) -extension RigidArray where Element: ~Copyable { - /// A type that represents a position in the array: an integer offset from the - /// start. - /// - /// Valid indices consist of the position of every element and a "past the - /// end” position that’s not valid for use as a subscript argument. - public typealias Index = Int - - /// The position of the first element in a nonempty array. This is always zero. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var startIndex: Int { 0 } - - /// The array’s "past the end” position—that is, the position one greater than - /// the last valid subscript argument. This is always equal to the array's - /// count. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var endIndex: Int { count } - - /// The range of indices that are valid for subscripting the array. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var indices: Range { unsafe Range(uncheckedBounds: (0, count)) } -} - -@available(SwiftStdlib 5.0, *) -extension RigidArray where Element: ~Copyable { - @inlinable @inline(__always) - internal func _ptr(to index: Int) -> UnsafePointer { - precondition(index >= 0 && index < _count, "Index out of bounds") - let p = _storage.baseAddress.unsafelyUnwrapped.advanced(by: index) - return UnsafePointer(p) - } - - @inlinable @inline(__always) - internal mutating func _mutablePtr( - to index: Int - ) -> UnsafeMutablePointer { - precondition(index >= 0 && index < _count, "Index out of bounds") - return _storage.baseAddress.unsafelyUnwrapped.advanced(by: index) - } - - /// Accesses the element at the specified position. - /// - /// - Parameter position: The position of the element to access. - /// The position must be a valid index of the array that is not equal - /// to the `endIndex` property. - /// - /// - Complexity: O(1) - @inlinable - public subscript(position: Int) -> Element { - @inline(__always) - unsafeAddress { - _ptr(to: position) - } - @inline(__always) - unsafeMutableAddress { - _mutablePtr(to: position) - } - } -} - -@available(SwiftStdlib 5.0, *) -extension RigidArray where Element: ~Copyable { - /// Exchanges the values at the specified indices of the array. - /// - /// Both parameters must be valid indices of the array and not equal to - /// endIndex. Passing the same index as both `i` and `j` has no effect. - /// - /// - Parameter i: The index of the first value to swap. - /// - Parameter j: The index of the second valud to swap. - /// - /// - Complexity: O(1) - @inlinable - public mutating func swapAt(_ i: Int, _ j: Int) { - precondition( - i >= 0 && i < _count && j >= 0 && j < _count, - "Index out of bounds") - unsafe _items.swapAt(i, j) - } -} - @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { /// Return a borrowing span over the maximal storage chunk following the diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index 7208516d..99464203 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -18,15 +18,13 @@ import ContainersPreview #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @available(SwiftStdlib 5.0, *) -extension RigidArray: BorrowingSequence where Element: ~Copyable { - public typealias BorrowingIterator = Span.BorrowingIterator +extension UniqueArray: BorrowingSequence where Element: ~Copyable { + public typealias BorrowingIterator = RigidArray.BorrowingIterator - @_alwaysEmitIntoClient - @inline(__always) - public var estimatedCount: EstimatedCount { - self._storage.estimatedCount + public var estimatedCount: ContainersPreview.EstimatedCount { + .exactly(count) } - + @_alwaysEmitIntoClient @inline(__always) public func makeBorrowingIterator() -> BorrowingIterator { @@ -41,6 +39,92 @@ extension UniqueArray: Container where Element: ~Copyable { } #endif +@available(SwiftStdlib 5.0, *) +extension UniqueArray where Element: ~Copyable { + /// A Boolean value indicating whether this array contains no elements. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var isEmpty: Bool { _storage.isEmpty } + + /// The number of elements in this array. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var count: Int { _storage.count } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueArray where Element: ~Copyable { + /// A type that represents a position in the array: an integer offset from the + /// start. + /// + /// Valid indices consist of the position of every element and a "past the + /// end” position that’s not valid for use as a subscript argument. + public typealias Index = Int + + /// The position of the first element in a nonempty array. This is always zero. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var startIndex: Int { _storage.startIndex } + + /// The array’s "past the end” position—that is, the position one greater than + /// the last valid subscript argument. This is always equal to array's count. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var endIndex: Int { _storage.count } + + /// The range of indices that are valid for subscripting the array. + /// + /// - Complexity: O(1) + @inlinable + @inline(__always) + public var indices: Range { _storage.indices } + + /// Accesses the element at the specified position. + /// + /// - Parameter position: The position of the element to access. + /// The position must be a valid index of the array that is not equal + /// to the `endIndex` property. + /// + /// - Complexity: O(1) + @inlinable + public subscript(position: Int) -> Element { + @inline(__always) + unsafeAddress { + _storage._ptr(to: position) + } + @inline(__always) + unsafeMutableAddress { + _storage._mutablePtr(to: position) + } + } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueArray where Element: ~Copyable { + /// Exchanges the values at the specified indices of the array. + /// + /// Both parameters must be valid indices of the array and not equal to + /// endIndex. Passing the same index as both `i` and `j` has no effect. + /// + /// - Parameter i: The index of the first value to swap. + /// - Parameter j: The index of the second valud to swap. + /// + /// - Complexity: O(1) + @inlinable + public mutating func swapAt(_ i: Int, _ j: Int) { + _storage.swapAt(i, j) + } +} + + @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { /// Returns the position immediately after the given index. @@ -87,6 +171,20 @@ extension UniqueArray where Element: ~Copyable { @inline(__always) public func formIndex(after index: inout Int) { index += 1 } + /// Replaces the given index with its predecessor. + /// + /// - Note: To improve performance, this method does not validate that the + /// given index is valid before decrementing it. Index validation is + /// deferred until the resulting index is used to access an element. + /// This optimization may be removed in future versions; do not rely on it. + /// + /// - Parameter index: A valid index of the array. `i` must be greater than + /// `startIndex`. + /// - Complexity: O(1) + @_alwaysEmitIntoClient + @inline(__always) + public func formIndex(before index: inout Int) { index -= 1 } + /// Returns an index that is the specified distance from the given index. /// /// The value passed as `n` must not offset `index` beyond the bounds of the diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray.swift b/Sources/BasicContainers/UniqueArray/UniqueArray.swift index c1957e51..081e07c3 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray.swift @@ -153,108 +153,6 @@ extension UniqueArray where Element: ~Copyable { //MARK: - Container primitives -@available(SwiftStdlib 5.0, *) -extension UniqueArray where Element: ~Copyable { - /// A Boolean value indicating whether this array contains no elements. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var isEmpty: Bool { _storage.isEmpty } - - /// The number of elements in this array. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var count: Int { _storage.count } -} - -#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW -@available(SwiftStdlib 5.0, *) -extension UniqueArray: BorrowingSequence where Element: ~Copyable { - public typealias BorrowingIterator = RigidArray.BorrowingIterator - - public var estimatedCount: ContainersPreview.EstimatedCount { - .exactly(count) - } - - @_alwaysEmitIntoClient - @inline(__always) - public func makeBorrowingIterator() -> BorrowingIterator { - self._storage.makeBorrowingIterator() - } -} -#endif - -@available(SwiftStdlib 5.0, *) -extension UniqueArray where Element: ~Copyable { - /// A type that represents a position in the array: an integer offset from the - /// start. - /// - /// Valid indices consist of the position of every element and a "past the - /// end” position that’s not valid for use as a subscript argument. - public typealias Index = Int - - /// The position of the first element in a nonempty array. This is always zero. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var startIndex: Int { _storage.startIndex } - - /// The array’s "past the end” position—that is, the position one greater than - /// the last valid subscript argument. This is always equal to array's count. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var endIndex: Int { _storage.count } - - /// The range of indices that are valid for subscripting the array. - /// - /// - Complexity: O(1) - @inlinable - @inline(__always) - public var indices: Range { _storage.indices } - - /// Accesses the element at the specified position. - /// - /// - Parameter position: The position of the element to access. - /// The position must be a valid index of the array that is not equal - /// to the `endIndex` property. - /// - /// - Complexity: O(1) - @inlinable - public subscript(position: Int) -> Element { - @inline(__always) - unsafeAddress { - _storage._ptr(to: position) - } - @inline(__always) - unsafeMutableAddress { - _storage._mutablePtr(to: position) - } - } -} - -@available(SwiftStdlib 5.0, *) -extension UniqueArray where Element: ~Copyable { - /// Exchanges the values at the specified indices of the array. - /// - /// Both parameters must be valid indices of the array and not equal to - /// endIndex. Passing the same index as both `i` and `j` has no effect. - /// - /// - Parameter i: The index of the first value to swap. - /// - Parameter j: The index of the second valud to swap. - /// - /// - Complexity: O(1) - @inlinable - public mutating func swapAt(_ i: Int, _ j: Int) { - _storage.swapAt(i, j) - } -} - @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { /// Return a borrowing span over the maximal storage chunk following the