mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
[BasicContainers] RigidArray, UniqueArray: Add formIndex(before:)
This commit is contained in:
5 files changed
+262
-225
No files matched your search
@@ -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<E: Error>(
|
||||
addingCapacity uninitializedCount: Int,
|
||||
initializingWith body: (inout OutputSpan<Element>) 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<E: Error, Result: ~Copyable>(
|
||||
count: Int,
|
||||
initializingWith body: (inout OutputSpan<Element>) 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()!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Int> { unsafe Range(uncheckedBounds: (0, count)) }
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
@inlinable @inline(__always)
|
||||
internal func _ptr(to index: Int) -> UnsafePointer<Element> {
|
||||
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<Element> {
|
||||
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
|
||||
|
||||
@@ -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<Int> { unsafe Range(uncheckedBounds: (0, count)) }
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
@inlinable @inline(__always)
|
||||
internal func _ptr(to index: Int) -> UnsafePointer<Element> {
|
||||
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<Element> {
|
||||
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
|
||||
|
||||
@@ -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<Element>.BorrowingIterator
|
||||
extension UniqueArray: BorrowingSequence where Element: ~Copyable {
|
||||
public typealias BorrowingIterator = RigidArray<Element>.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<Int> { _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
|
||||
|
||||
@@ -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<Element>.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<Int> { _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
|
||||
|
||||
Reference in new issue
Block a user