diff --git a/Sources/BasicContainers/BasicContainers.docc/Extensions/RigidArray.md b/Sources/BasicContainers/BasicContainers.docc/Extensions/RigidArray.md index 0cdc5a79..cfb311f6 100644 --- a/Sources/BasicContainers/BasicContainers.docc/Extensions/RigidArray.md +++ b/Sources/BasicContainers/BasicContainers.docc/Extensions/RigidArray.md @@ -1,4 +1,98 @@ -# ``ContainersPreview/Box`` +# ``BasicContainers/RigidArray`` ## Topics +### Creating a Rigid Array + +- ``init()`` +- ``init(capacity:)`` +- ``init(capacity:initializingWith:)`` +- ``init(repeating:count:)`` +- ``init(consuming:)`` +- ``init(capacity:copying:)-(_,Sequence)`` +- ``init(capacity:copying:)-(_,Collection)`` + +### Inspecting a Rigid Array + +- ``isEmpty`` +- ``isFull`` +- ``count`` +- ``capacity`` +- ``freeCapacity`` + +### Indices + +- ``Index`` +- ``startIndex`` +- ``endIndex`` +- ``indices`` + +### Accessing Elements + +- ``subscript(position:)`` +- ``swapAt(_:_:)`` +- ``edit(_:)`` + +### Memory Management + +- ``reallocate(capacity:)`` +- ``reserveCapacity(_:)`` +- ``copy()`` +- ``copy(capacity:)`` + +### Spans + +- ``span`` +- ``span(after:)`` +- ``span(before:)`` +- ``mutableSpan`` +- ``mutableSpan(after:)`` +- ``mutableSpan(before:)`` + +### Appending Items + +- ``append(_:)`` +- ``pushLast(_:)`` +- ``append(count:initializingWith:)`` +- ``append(moving:)-(UnsafeMutableBufferPointer)`` +- ``append(moving:)-(OutputSpan)`` +- ``append(moving:)-(RigidArray)`` +- ``append(copying:)-(Sequence)`` +- ``append(copying:)-(Span)`` +- ``append(copying:)-(UnsafeBufferPointer)`` +- ``append(copying:)-(UnsafeMutableBufferPointer)`` + +### Inserting Items + +- ``insert(_:at:)`` +- ``insert(count:at:initializingWith:)`` +- ``insert(moving:at:)-(OutputSpan,_)`` +- ``insert(moving:at:)-(RigidArray,_)`` +- ``insert(moving:at:)-(UnsafeMutableBufferPointer,_)`` +- ``insert(consuming:at:)`` +- ``insert(copying:at:)-(Collection,_)`` +- ``insert(copying:at:)-(Span,_)`` +- ``insert(copying:at:)-(UnsafeBufferPointer,_)`` +- ``insert(copying:at:)-(UnsafeMutableBufferPointer,_)`` + +### Replacing Items + +- ``replaceSubrange(_:newCount:initializingWith:)`` +- ``replaceSubrange(_:moving:)-(_,OutputSpan)`` +- ``replaceSubrange(_:moving:)-(_,RigidArray))`` +- ``replaceSubrange(_:moving:)-(_,UnsafeMutableBufferPointer)`` +- ``replaceSubrange(_:consuming:)`` +- ``replaceSubrange(_:copying:)-(_,Collection)`` +- ``replaceSubrange(_:copying:)-(_,Span)`` +- ``replaceSubrange(_:copying:)-(_,UnsafeBufferPointer)`` +- ``replaceSubrange(_:copying:)-(_,UnsafeMutableBufferPointer)`` + +### Removing Items + +- ``removeAll()`` +- ``removeLast()`` +- ``popLast()`` +- ``removeLast(_:)`` +- ``remove(at:)`` +- ``removeSubrange(_:)-(Range)`` +- ``removeSubrange(_:)-(RangeExpression)`` diff --git a/Sources/BasicContainers/BasicContainers.docc/Extensions/UniqueArray.md b/Sources/BasicContainers/BasicContainers.docc/Extensions/UniqueArray.md index 173096e8..612486c8 100644 --- a/Sources/BasicContainers/BasicContainers.docc/Extensions/UniqueArray.md +++ b/Sources/BasicContainers/BasicContainers.docc/Extensions/UniqueArray.md @@ -8,6 +8,7 @@ - ``init(capacity:)`` - ``init(capacity:initializingWith:)`` - ``init(repeating:count:)`` +- ``init(consuming:)`` - ``init(capacity:copying:)-(_,Sequence)`` - ``init(capacity:copying:)-(_,Collection)`` @@ -21,6 +22,7 @@ ### Indices +- ``Index`` - ``startIndex`` - ``endIndex`` - ``indices`` diff --git a/Sources/BasicContainers/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray+Append.swift index 34d3f214..f1c57ee4 100644 --- a/Sources/BasicContainers/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray+Append.swift @@ -178,9 +178,9 @@ extension RigidArray where Element: ~Copyable { } } -#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW /// Appends the elements of a given container to the end of this array by /// consuming the source container. /// @@ -200,8 +200,8 @@ extension RigidArray where Element: ~Copyable { var items = items self.append(moving: &items) } -} #endif +} @available(SwiftStdlib 5.0, *) extension RigidArray { diff --git a/Sources/BasicContainers/RigidArray+Initializers.swift b/Sources/BasicContainers/RigidArray+Initializers.swift index 2f2c7f80..d9d8fc72 100644 --- a/Sources/BasicContainers/RigidArray+Initializers.swift +++ b/Sources/BasicContainers/RigidArray+Initializers.swift @@ -23,7 +23,7 @@ extension RigidArray where Element: ~Copyable { /// /// - Parameters: /// - capacity: The storage capacity of the new array. - /// - body: A callback that gets called precisely once to directly + /// - body: A callback that gets called precisely once to directly /// populate newly reserved storage within the array. The function /// is allowed to add fewer than `capacity` items. The array is /// initialized with however many items the callback adds to the @@ -47,6 +47,8 @@ extension RigidArray /*where Element: Copyable*/ { /// - repeatedValue: The element to repeat. /// - count: The number of times to repeat the value passed in the /// `repeating` parameter. `count` must be zero or greater. + /// + /// - Complexity: O(`count`) public init(repeating repeatedValue: Element, count: Int) { self.init(capacity: count) unsafe _freeSpace.initialize(repeating: repeatedValue) @@ -54,6 +56,18 @@ extension RigidArray /*where Element: Copyable*/ { } } +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { + /// Creates a new rigid array taking over the storage of the specified + /// unique array instance, consuming it in the process. + /// + /// - Complexity: O(1) + @inlinable + public init(consuming array: consuming UniqueArray) { + self = array._storage + } +} + @available(SwiftStdlib 5.0, *) extension RigidArray /*where Element: Copyable*/ { /// Creates a new array with the specified capacity, holding a copy diff --git a/Sources/BasicContainers/RigidArray+Insertions.swift b/Sources/BasicContainers/RigidArray+Insertions.swift index 56953f1c..669a4fc1 100644 --- a/Sources/BasicContainers/RigidArray+Insertions.swift +++ b/Sources/BasicContainers/RigidArray+Insertions.swift @@ -62,7 +62,7 @@ extension RigidArray where Element: ~Copyable { /// If the capacity of the array isn't sufficient to accommodate the new /// elements, then this method triggers a runtime error. /// - /// - Parameters + /// - Parameters: /// - count: The number of items to insert into the array. /// - index: The position at which to insert the new items. /// `index` must be a valid index in the array. @@ -221,6 +221,7 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW /// Inserts the elements of a given array into the given position in this /// array by consuming the source container. /// @@ -242,9 +243,11 @@ extension RigidArray where Element: ~Copyable { consuming items: consuming RigidArray, at index: Int ) { + // FIXME: Remove this in favor of a generic algorithm over consumable containers var items = items self.insert(moving: &items, at: index) } +#endif } @available(SwiftStdlib 5.0, *) diff --git a/Sources/BasicContainers/RigidArray+Removals.swift b/Sources/BasicContainers/RigidArray+Removals.swift index 55303c1c..a1149b90 100644 --- a/Sources/BasicContainers/RigidArray+Removals.swift +++ b/Sources/BasicContainers/RigidArray+Removals.swift @@ -119,6 +119,22 @@ extension RigidArray where Element: ~Copyable { } } +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { + /// Removes and returns the last element of the array, if there is one. + /// + /// - Returns: The last element of the array if the array is not empty; + /// otherwise, `nil`. + /// + /// - Complexity: O(1) + @_alwaysEmitIntoClient + public mutating func popLast() -> Element? { + // FIXME: Remove this in favor of a standard algorithm. + if isEmpty { return nil } + return removeLast() + } +} + #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { @@ -126,8 +142,7 @@ extension RigidArray where Element: ~Copyable { /// allow them to be consumed in place. /// The input span extends the exclusive mutating access initiated by this /// operation to cover the span's lifetime. Once the span is destroyed, - /// the array becomes empty, but it still holds its original storage capacity, - /// and it will be ready to use it to hold new items. + /// the array becomes empty, but it preserves its original storage capacity. /// /// - Complexity: O(*n*), where *n* is the original count of the array. @_lifetime(&self) @@ -198,20 +213,4 @@ extension RigidArray where Element: ~Copyable { } #endif -@available(SwiftStdlib 5.0, *) -extension RigidArray where Element: ~Copyable { - /// Removes and returns the last element of the array, if there is one. - /// - /// - Returns: The last element of the array if the array is not empty; - /// otherwise, `nil`. - /// - /// - Complexity: O(1) - @_alwaysEmitIntoClient - public mutating func popLast() -> Element? { - // FIXME: Remove this in favor of a standard algorithm. - if isEmpty { return nil } - return removeLast() - } -} - #endif diff --git a/Sources/BasicContainers/RigidArray+Replacements.swift b/Sources/BasicContainers/RigidArray+Replacements.swift index faa89fc9..654cca7d 100644 --- a/Sources/BasicContainers/RigidArray+Replacements.swift +++ b/Sources/BasicContainers/RigidArray+Replacements.swift @@ -231,6 +231,7 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW /// Replaces the specified range of elements by moving the elements of a /// given array into their place, consuming it in the process. /// @@ -262,8 +263,10 @@ extension RigidArray where Element: ~Copyable { _ subrange: Range, consuming newElements: consuming RigidArray, ) { + // FIXME: Remove this in favor of a generic algorithm over consumable containers replaceSubrange(subrange, moving: &newElements) } +#endif } @available(SwiftStdlib 5.0, *) diff --git a/Sources/BasicContainers/RigidArray.swift b/Sources/BasicContainers/RigidArray.swift index c3c1b749..6d78ed8c 100644 --- a/Sources/BasicContainers/RigidArray.swift +++ b/Sources/BasicContainers/RigidArray.swift @@ -104,6 +104,8 @@ public struct RigidArray: ~Copyable { } /// Initializes a new rigid array with zero capacity and no elements. + /// + /// - Complexity: O(1) @inlinable public init() { unsafe _storage = .init(start: nil, count: 0) @@ -119,16 +121,28 @@ extension RigidArray: @unchecked Sendable where Element: Sendable & ~Copyable {} @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + /// The maximum number of elements this rigid array can hold. + /// + /// - Complexity: O(1) @inlinable @_transparent public var capacity: Int { _assumeNonNegative(unsafe _storage.count) } + /// The number of additional elements that can be added to this array without + /// exceeding its storage capacity. + /// + /// - Complexity: O(1) @inlinable @_transparent public var freeCapacity: Int { _assumeNonNegative(capacity &- count) } + /// A Boolean value indicating whether this rigid array is fully populated. + /// If this property returns true, then the array's storage is at capacity, + /// and it cannot accommodate any additional elements. + /// + /// - Complexity: O(1) @inlinable @inline(__always) public var isFull: Bool { freeCapacity == 0 } @@ -151,6 +165,9 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + /// A span over the elements of this array, providing direct read-only access. + /// + /// - Complexity: O(1) public var span: Span { @_lifetime(borrow self) @inlinable @@ -160,6 +177,10 @@ extension RigidArray where Element: ~Copyable { } } + /// A mutable span over the elements of this array, providing direct + /// mutating access. + /// + /// - Complexity: O(1) public var mutableSpan: MutableSpan { @_lifetime(&self) @inlinable @@ -248,10 +269,16 @@ extension RigidArray where Element: ~Copyable { @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 } @@ -272,16 +299,31 @@ extension RigidArray: Container where Element: ~Copyable { @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 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)) } @@ -304,11 +346,20 @@ extension RigidArray where Element: ~Copyable { 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) } @@ -317,6 +368,15 @@ extension RigidArray where Element: ~Copyable { @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( @@ -328,12 +388,26 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + /// Return a borrowing span over the maximal storage chunk following the + /// specified position in the array. The span provides direct read-only access + /// to all array elements in the range `index ..< count`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @inlinable @_lifetime(borrow self) public func span(after index: inout Int) -> Span { _span(in: _contiguousSubrange(following: &index)) } + /// Return a borrowing span over the maximal storage chunk preceding the + /// specified position in the array. The span provides direct read-only access + /// to all array elements in the range `0 ..< index`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @inlinable @_lifetime(borrow self) public func span(before index: inout Int) -> Span { @@ -343,6 +417,13 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + /// Return a mutable span over the maximal storage chunk following the + /// specified position in the array. The span provides direct mutating access + /// to all array elements in the range `index ..< count`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @_lifetime(&self) public mutating func mutableSpan( after index: inout Int @@ -350,6 +431,13 @@ extension RigidArray where Element: ~Copyable { _mutableSpan(in: _contiguousSubrange(following: &index)) } + /// Return a mutable span over the maximal storage chunk preceding the specified + /// position in the array. The span provides direct mutating access to all + /// array elements in the range `0 ..< index`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @_lifetime(&self) public mutating func mutableSpan( before index: inout Int @@ -362,6 +450,17 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + /// Grow or shrink the capacity of a rigid array instance without discarding + /// its contents. + /// + /// This operation replaces the array's storage buffer with a newly allocated + /// buffer of the specified capacity, moving all existing elements + /// to its new storage. The old storage is then deallocated. + /// + /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be + /// greater than or equal to the current count. + /// + /// - Complexity: O(`count`) @inlinable public mutating func reallocate(capacity newCapacity: Int) { precondition(newCapacity >= count, "RigidArray capacity overflow") @@ -374,6 +473,14 @@ extension RigidArray where Element: ~Copyable { unsafe _storage = newStorage } + /// Ensure that the array has capacity to store the specified number of + /// elements, by growing its storage buffer if necessary. + /// + /// If `capacity < n`, then this operation reallocates the rigid array's + /// storage to grow it; on return, the array's capacity becomes `n`. + /// Otherwise the array is left as is. + /// + /// - Complexity: O(`count`) @inlinable public mutating func reserveCapacity(_ n: Int) { guard capacity < n else { return } @@ -385,11 +492,22 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray { + /// Copy the contents of this array into a newly allocated rigid array + /// instance with just enough capacity to hold all its elements. + /// + /// - Complexity: O(`count`) @inlinable public func copy() -> Self { - copy(capacity: capacity) + copy(capacity: count) } + /// Copy the contents of this array into a newly allocated rigid array + /// instance with the specified capacity. + /// + /// - Parameter capacity: The desired capacity of the resulting rigid array. + /// `capacity` must be greater than or equal to `count`. + /// + /// - Complexity: O(`count`) @inlinable public func copy(capacity: Int) -> Self { precondition(capacity >= count, "RigidArray capacity overflow") diff --git a/Sources/BasicContainers/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray+Append.swift index 36d96574..7cb3c56d 100644 --- a/Sources/BasicContainers/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray+Append.swift @@ -21,11 +21,12 @@ extension UniqueArray where Element: ~Copyable { /// Adds an element to the end of the array. /// /// If the array does not have sufficient capacity to hold any more elements, - /// then this reallocates the array's storage to grow its capacity. + /// then this reallocates the array's storage to grow its capacity, using a + /// geometric growth rate. /// /// - Parameter item: The element to append to the collection. /// - /// - Complexity: O(1) when amortized over many invocations on the same array + /// - Complexity: O(1) as amortized over many invocations on the same array. @inlinable public mutating func append(_ item: consuming Element) { _ensureFreeCapacity(1) @@ -35,6 +36,22 @@ extension UniqueArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension UniqueArray 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 hold the requested + /// number of new elements, then this reallocates the array's storage to + /// grow its capacity, using a geometric growth rate. + /// + /// - Parameters + /// - count: 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 `count` 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(`count`) @_alwaysEmitIntoClient public mutating func append( count: Int, @@ -51,14 +68,14 @@ extension UniqueArray where Element: ~Copyable { /// buffer uninitialized. /// /// If the array does not have sufficient capacity to hold all items in the - /// buffer, then this reallocates the array's storage to grow its capacity. + /// buffer, then this reallocates the array's storage to grow its capacity, + /// using a geometric growth rate. /// /// - Parameters /// - items: A fully initialized buffer whose contents to move into /// the array. /// - /// - Complexity: O(`items.count`) when amortized over many invocations on - /// the same array + /// - Complexity: O(`count` + `items.count`) @_alwaysEmitIntoClient public mutating func append( moving items: UnsafeMutableBufferPointer @@ -68,6 +85,17 @@ extension UniqueArray where Element: ~Copyable { } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Moves the elements of a input span to the end of this array, leaving the + /// span empty. + /// + /// If the array does not have sufficient capacity to hold all new items, + /// then this reallocates the array's storage to grow its capacity, + /// using a geometric growth rate. + /// + /// - Parameters + /// - items: An input span whose contents need to be appended to this array. + /// + /// - Complexity: O(`items.count`) @_alwaysEmitIntoClient public mutating func append( moving items: inout InputSpan @@ -77,6 +105,17 @@ extension UniqueArray where Element: ~Copyable { } #endif + /// Moves the elements of a output span to the end of this array, leaving the + /// span empty. + /// + /// If the array does not have sufficient capacity to hold all new items, + /// then this reallocates the array's storage to grow its capacity, + /// using a geometric growth rate. + /// + /// - Parameters + /// - items: An output span whose contents need to be appended to this array. + /// + /// - Complexity: O(`items.count`) @_alwaysEmitIntoClient public mutating func append( moving items: inout OutputSpan @@ -91,7 +130,7 @@ extension UniqueArray where Element: ~Copyable { /// /// If the target array does not have sufficient capacity to hold all items /// in the source array, then this automatically grows the target array's - /// capacity. + /// capacity, using a geometric growth rate. /// /// - Parameters /// - items: An array whose items to move to the end of this array. @@ -108,14 +147,15 @@ extension UniqueArray where Element: ~Copyable { } } -#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW /// Appends the elements of a given container to the end of this array by /// consuming the source container. /// /// If the target array does not have sufficient capacity to hold all items - /// in the source array, then this triggers a runtime error. + /// in the source array, then this automatically grows the target array's + /// capacity, using a geometric growth rate. /// /// - Parameters /// - items: An array whose items to move to the end of this array. @@ -129,15 +169,16 @@ extension UniqueArray where Element: ~Copyable { var items = items self.append(moving: &items) } -} #endif +} @available(SwiftStdlib 5.0, *) extension UniqueArray { /// Copies the elements of a buffer to the end of this array. /// - /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// If the array does not have sufficient capacity to hold all items + /// in the source buffer, then this automatically grows the array's + /// capacity, using a geometric growth rate. /// /// - Parameters /// - newElements: A fully initialized buffer whose contents to copy into @@ -156,7 +197,8 @@ extension UniqueArray { /// Copies the elements of a buffer to the end of this array. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using + /// a geometric growth rate. /// /// - Parameters /// - newElements: A fully initialized buffer whose contents to copy into @@ -174,7 +216,8 @@ extension UniqueArray { /// Copies the elements of a span to the end of this array. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: A span whose contents to copy into the array. @@ -191,7 +234,8 @@ extension UniqueArray { /// Copies the elements of a container to the end of this array. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using + /// a geometric growth rate. /// /// - Parameters /// - newElements: A container whose contents to copy into the array. @@ -214,8 +258,10 @@ extension UniqueArray { /// Copies the elements of a sequence to the end of this array. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. This - /// reallocation can happen multiple times. + /// then this reallocates the array's storage to extend its capacity, using + /// a geometric growth rate. If the input sequence does not provide a correct + /// estimate of its count, then the array's storage may need to be resized + /// more than once. /// /// - Parameters /// - newElements: The new elements to copy into the array. @@ -243,7 +289,8 @@ extension UniqueArray { /// Copies the elements of a container to the end of this array. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: A container whose contents to copy into the array. @@ -259,7 +306,6 @@ extension UniqueArray { _storage._append(copyingContainer: newElements) } #endif - } #endif diff --git a/Sources/BasicContainers/UniqueArray+Initializers.swift b/Sources/BasicContainers/UniqueArray+Initializers.swift index 5a2dfe3f..f0110a85 100644 --- a/Sources/BasicContainers/UniqueArray+Initializers.swift +++ b/Sources/BasicContainers/UniqueArray+Initializers.swift @@ -18,6 +18,16 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Creates a new array with the specified capacity, directly initializing + /// its storage using an output span. + /// + /// - Parameters: + /// - capacity: The storage capacity of the new array. + /// - body: A callback that gets called precisely once to directly + /// populate newly reserved storage within the array. The function + /// is allowed to add fewer than `capacity` items. The array is + /// initialized with however many items the callback adds to the + /// output span before it returns (or before it throws an error). @inlinable public init( capacity: Int, @@ -37,6 +47,8 @@ extension UniqueArray /*where Element: Copyable*/ { /// - repeatedValue: The element to repeat. /// - count: The number of times to repeat the value passed in the /// `repeating` parameter. `count` must be zero or greater. + /// + /// - Complexity: O(`count`) public init(repeating repeatedValue: Element, count: Int) { self.init(consuming: RigidArray(repeating: repeatedValue, count: count)) } @@ -44,6 +56,10 @@ extension UniqueArray /*where Element: Copyable*/ { @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Creates a new unique array taking over the storage of the specified + /// rigid array instance, consuming it in the process. + /// + /// - Complexity: O(1) @inlinable public init(consuming storage: consuming RigidArray) { self._storage = storage @@ -53,6 +69,14 @@ extension UniqueArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension UniqueArray /*where Element: Copyable*/ { #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Creates a new array with the specified initial capacity, holding a copy + /// of the contents of a given container. + /// + /// - Parameters: + /// - capacity: The storage capacity of the new array, or nil to allocate + /// just enough capacity to store the contents. + /// - contents: The container whose contents to copy into the new array. + /// The container must not contain more than `capacity` elements. @_alwaysEmitIntoClient @inline(__always) public init & ~Copyable & ~Escapable>( @@ -64,6 +88,13 @@ extension UniqueArray /*where Element: Copyable*/ { } #endif + /// Creates a new array with the specified initial capacity, holding a copy + /// of the contents of a given sequence. + /// + /// - Parameters: + /// - capacity: The storage capacity of the new array, or nil to allocate + /// just enough capacity to store the contents. + /// - contents: The sequence whose contents to copy into the new array. @_alwaysEmitIntoClient @inline(__always) public init( @@ -75,6 +106,13 @@ extension UniqueArray /*where Element: Copyable*/ { } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Creates a new array with the specified initial capacity, holding a copy + /// of the contents of a given container. + /// + /// - Parameters: + /// - capacity: The storage capacity of the new array, or nil to allocate + /// just enough capacity to store the contents. + /// - contents: The container whose contents to copy into the new array. @_alwaysEmitIntoClient @inline(__always) public init & Sequence>( diff --git a/Sources/BasicContainers/UniqueArray+Insertions.swift b/Sources/BasicContainers/UniqueArray+Insertions.swift index 62d4f20e..4e9fbf27 100644 --- a/Sources/BasicContainers/UniqueArray+Insertions.swift +++ b/Sources/BasicContainers/UniqueArray+Insertions.swift @@ -21,7 +21,8 @@ extension UniqueArray where Element: ~Copyable { /// Inserts a new element into the array at the specified position. /// /// If the array does not have sufficient capacity to hold any more elements, - /// then this reallocates storage to extend its capacity. + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. /// /// The new element is inserted before the element currently at the specified /// index. If you pass the array's `endIndex` as the `index` parameter, then @@ -46,6 +47,27 @@ extension UniqueArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Inserts a given number of new items into this array at the specified + /// position, using a callback to directly initialize array storage by + /// populating an output span. + /// + /// All existing elements at or following the specified position are moved to + /// make room for the new items. + /// + /// If the array does not have sufficient capacity to hold the new elements, + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. + /// + /// - Parameters: + /// - count: The number of items to insert into the array. + /// - index: The position at which to insert the new items. + /// `index` must be a valid index in the array. + /// - body: A callback that gets called precisely once to directly + /// populate newly reserved storage within the array. The function + /// is called with an empty output span of capacity matching the + /// supplied count, and it must fully populate it before returning. + /// + /// - Complexity: O(`self.count` + `count`) @inlinable public mutating func insert( count: Int, @@ -65,7 +87,8 @@ extension UniqueArray where Element: ~Copyable { /// uninitialized. /// /// If the array does not have sufficient capacity to hold all elements, - /// then this reallocates storage to extend its capacity. + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. /// /// - Parameters /// - items: A fully initialized buffer whose contents to move into @@ -83,6 +106,23 @@ extension UniqueArray where Element: ~Copyable { } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Moves the elements of an input span into this array, + /// starting at the specified position, and leaving the span empty. + /// + /// All existing elements at or following the specified position are moved to + /// make room for the new items. + /// + /// If the array does not have sufficient capacity to hold the new elements, + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. + /// + /// - Parameters + /// - items: An input span whose contents to move into + /// the array. + /// - index: The position at which to insert the new items. + /// `index` must be a valid index in the array. + /// + /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient public mutating func insert( moving items: inout InputSpan, @@ -93,6 +133,23 @@ extension UniqueArray where Element: ~Copyable { } #endif + /// Moves the elements of an output span into this array, + /// starting at the specified position, and leaving the span empty. + /// + /// All existing elements at or following the specified position are moved to + /// make room for the new items. + /// + /// If the array does not have sufficient capacity to hold the new elements, + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. + /// + /// - Parameters + /// - items: An output span whose contents to move into + /// the array. + /// - index: The position at which to insert the new items. + /// `index` must be a valid index in the array. + /// + /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient public mutating func insert( moving items: inout OutputSpan, @@ -108,7 +165,8 @@ extension UniqueArray where Element: ~Copyable { /// storage capacity. /// /// If the array does not have sufficient capacity to hold all elements, - /// then this reallocates storage to extend its capacity. + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. /// /// - Parameters /// - items: An array whose contents to move into `self`. @@ -127,11 +185,13 @@ extension UniqueArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW /// Inserts the elements of a given array into the given position in this /// array by consuming the source container. /// /// If the array does not have sufficient capacity to hold all elements, - /// then this reallocates storage to extend its capacity. + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. /// /// - Parameters /// - items: A fully initialized buffer whose contents to move into @@ -143,10 +203,12 @@ extension UniqueArray where Element: ~Copyable { consuming items: consuming RigidArray, at index: Int ) { + // FIXME: Remove this in favor of a generic algorithm over consumable containers // FIXME: Avoid moving the subsequent elements twice. _ensureFreeCapacity(items.count) _storage.insert(consuming: items, at: index) } +#endif } @available(SwiftStdlib 5.0, *) @@ -162,7 +224,8 @@ extension UniqueArray { /// make room for the new item. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: The new elements to insert into the array. The buffer @@ -191,7 +254,8 @@ extension UniqueArray { /// make room for the new item. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: The new elements to insert into the array. The buffer @@ -218,7 +282,8 @@ extension UniqueArray { /// make room for the new item. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: The new elements to insert into the array. @@ -247,7 +312,8 @@ extension UniqueArray { /// make room for the new item. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: The new elements to insert into the array. @@ -281,7 +347,8 @@ extension UniqueArray { /// to make room for the new item. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: The new elements to insert into the array. @@ -312,7 +379,8 @@ extension UniqueArray { /// make room for the new item. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// - Parameters /// - newElements: The new elements to insert into the array. diff --git a/Sources/BasicContainers/UniqueArray+Removals.swift b/Sources/BasicContainers/UniqueArray+Removals.swift index e84e9a8f..376cf430 100644 --- a/Sources/BasicContainers/UniqueArray+Removals.swift +++ b/Sources/BasicContainers/UniqueArray+Removals.swift @@ -124,18 +124,46 @@ extension UniqueArray where Element: ~Copyable { #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Remove all items currently in this array, returning an input span to + /// allow them to be consumed in place. + /// The input span extends the exclusive mutating access initiated by this + /// operation to cover the span's lifetime. Once the span is destroyed, + /// the array becomes empty, but it preserves its original storage. + /// + /// - Complexity: O(*n*), where *n* is the original count of the array. @_lifetime(&self) @_alwaysEmitIntoClient public mutating func consumeAll() -> InputSpan { _storage.consumeAll() } + /// Remove the specified number of items from the end of this array, + /// returning an input span to allow them to be consumed in place. + /// The input span extends the exclusive mutating access initiated by this + /// operation to cover the span's lifetime. + /// + /// - Parameter n: The number of items to consume from the end of the array. + /// `n` must be greater than or equal to zero and must not exceed + /// the count of the array. + /// + /// - Complexity: O(`n`) @_lifetime(&self) @_alwaysEmitIntoClient public mutating func consumeLast(_ count: Int) -> InputSpan { _storage.consumeLast(count) } + /// Remove the specified subrange of items from this array, + /// passing an input span to the given function to consume them in place. + /// + /// - Parameter bounds: The subrange of items to consume from this array. + /// - Parameter body: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is not required to consume all items in the span; + /// however, the span's remaining items will still be removed from + /// the array. + /// + /// - Complexity: O(`self.count`) @_alwaysEmitIntoClient public mutating func consumeSubrange( _ bounds: Range, diff --git a/Sources/BasicContainers/UniqueArray+Replacements.swift b/Sources/BasicContainers/UniqueArray+Replacements.swift index e4f72815..b52b020d 100644 --- a/Sources/BasicContainers/UniqueArray+Replacements.swift +++ b/Sources/BasicContainers/UniqueArray+Replacements.swift @@ -18,6 +18,37 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Replaces the specified range of elements by a given count of new items, + /// using a callback to directly initialize array storage by populating + /// an output span. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting room for the new elements starting at the + /// same location. The number of new elements need not match the number + /// of elements being removed. + /// + /// If the array does not have sufficient capacity to perform the replacement, + /// then this reallocates storage to extend its capacity, using a geometric + /// growth rate. + /// + /// If you pass a zero-length range as the `subrange` parameter, then + /// this method is equivalent to calling + /// `insert(count: newCount, initializingWith: body)`. + /// + /// Likewise, if you pass a zero for `newCount`, then this method + /// removes the elements in the given subrange without any replacement. + /// Calling `removeSubrange(subrange)` is preferred in this case. + /// + /// - Parameters + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - newCount: the number of items to replace the old subrange. + /// - body: A callback that gets called precisely once to directly + /// populate newly reserved storage within the array. The function + /// is called with an empty output span of capacity `newCount`, + /// and it must fully populate it before returning. + /// + /// - Complexity: O(`self.count` + `newCount`) @inlinable public mutating func replaceSubrange( _ subrange: Range, @@ -45,8 +76,9 @@ extension UniqueArray where Element: ~Copyable { /// location. The number of new elements need not match the number of elements /// being removed. /// - /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// If the array does not have sufficient capacity to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling @@ -75,6 +107,33 @@ extension UniqueArray where Element: ~Copyable { } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Replaces the specified range of elements by moving the contents of an + /// input span into their place. On return, the span is left empty. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting the new elements starting at the same + /// location. The number of new elements need not match the number of elements + /// being removed. + /// + /// If the array does not have sufficient capacity to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// + /// If you pass a zero-length range as the `subrange` parameter, this method + /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling + /// the `insert(moving:at:)` method instead is preferred in this case. + /// + /// Likewise, if you pass a zero-length buffer as the `newElements` + /// parameter, this method removes the elements in the given subrange + /// without replacement. Calling the `removeSubrange(_:)` method instead is + /// preferred in this case. + /// + /// - Parameters + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - items: An input span whose contents are to be moved into the array. + /// + /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient public mutating func replaceSubrange( _ subrange: Range, @@ -86,6 +145,33 @@ extension UniqueArray where Element: ~Copyable { } #endif + /// Replaces the specified range of elements by moving the contents of an + /// output span into their place. On return, the span is left empty. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting the new elements starting at the same + /// location. The number of new elements need not match the number of elements + /// being removed. + /// + /// If the array does not have sufficient capacity to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// + /// If you pass a zero-length range as the `subrange` parameter, this method + /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling + /// the `insert(moving:at:)` method instead is preferred in this case. + /// + /// Likewise, if you pass a zero-length buffer as the `newElements` + /// parameter, this method removes the elements in the given subrange + /// without replacement. Calling the `removeSubrange(_:)` method instead is + /// preferred in this case. + /// + /// - Parameters + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - items: An output span whose contents are to be moved into the array. + /// + /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient public mutating func replaceSubrange( _ subrange: Range, @@ -107,7 +193,8 @@ extension UniqueArray where Element: ~Copyable { /// being removed. /// /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling @@ -145,8 +232,9 @@ extension UniqueArray where Element: ~Copyable { /// location. The number of new elements need not match the number of elements /// being removed. /// - /// If the array does not have sufficient capacity to hold enough elements, - /// then this reallocates the array's storage to extend its capacity. + /// If the array does not have sufficient capacity to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling @@ -182,6 +270,10 @@ extension UniqueArray { /// The number of new elements need not match the number of elements being /// removed. /// + /// If the capacity of the array isn't sufficient to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling /// the `insert(copying:at:)` method instead is preferred in this case. @@ -217,6 +309,10 @@ extension UniqueArray { /// The number of new elements need not match the number of elements being /// removed. /// + /// If the capacity of the array isn't sufficient to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling /// the `insert(copying:at:)` method instead is preferred in this case. @@ -251,6 +347,10 @@ extension UniqueArray { /// The number of new elements need not match the number of elements being /// removed. /// + /// If the capacity of the array isn't sufficient to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling /// the `insert(copying:at:)` method instead is preferred in this case. @@ -286,6 +386,10 @@ extension UniqueArray { /// The number of new elements need not match the number of elements being /// removed. /// + /// If the capacity of the array isn't sufficient to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling /// the `insert(copying:at:)` method instead is preferred in this case. @@ -326,6 +430,10 @@ extension UniqueArray { /// The number of new elements need not match the number of elements being /// removed. /// + /// If the capacity of the array isn't sufficient to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling /// the `insert(copying:at:)` method instead is preferred in this case. @@ -364,6 +472,10 @@ extension UniqueArray { /// The number of new elements need not match the number of elements being /// removed. /// + /// If the capacity of the array isn't sufficient to perform the replacement, + /// then this reallocates the array's storage to extend its capacity, using a + /// geometric growth rate. + /// /// If you pass a zero-length range as the `subrange` parameter, this method /// inserts the elements of `newElements` at `subrange.lowerBound`. Calling /// the `insert(copying:at:)` method instead is preferred in this case. diff --git a/Sources/BasicContainers/UniqueArray.swift b/Sources/BasicContainers/UniqueArray.swift index 7711122c..8bf93ab1 100644 --- a/Sources/BasicContainers/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray.swift @@ -63,11 +63,13 @@ public struct UniqueArray: ~Copyable { @usableFromInline internal var _storage: RigidArray + /// Initializes a new unique array with the specified capacity and no elements. @inlinable public init(capacity: Int) { _storage = .init(capacity: capacity) } + /// Initializes a new unique array with no elements. @inlinable public init() { _storage = .init(capacity: 0) @@ -81,10 +83,16 @@ extension UniqueArray: Sendable where Element: Sendable & ~Copyable {} @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// The maximum number of elements this array can hold without reallocating + /// its storage. @inlinable @inline(__always) public var capacity: Int { _assumeNonNegative(_storage.capacity) } + /// The number of additional elements that can be added to this array without + /// reallocating its storage. + /// + /// - Complexity: O(1) @inlinable @inline(__always) public var freeCapacity: Int { @@ -96,6 +104,9 @@ extension UniqueArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// A span over the elements of this array, providing direct read-only access. + /// + /// - Complexity: O(1) public var span: Span { @_lifetime(borrow self) @inlinable @@ -104,6 +115,10 @@ extension UniqueArray where Element: ~Copyable { } } + /// A mutable span over the elements of this array, providing direct + /// mutating access. + /// + /// - Complexity: O(1) @available(SwiftStdlib 5.0, *) public var mutableSpan: MutableSpan { @_lifetime(&self) @@ -145,10 +160,16 @@ extension UniqueArray where Element: ~Copyable { @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 } @@ -169,25 +190,49 @@ extension UniqueArray: Container where Element: ~Copyable { @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) } @@ -196,6 +241,15 @@ extension UniqueArray where Element: ~Copyable { @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) @@ -204,12 +258,26 @@ extension UniqueArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Return a borrowing span over the maximal storage chunk following the + /// specified position in the array. The span provides direct read-only access + /// to all array elements in the range `index ..< count`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @inlinable @_lifetime(borrow self) public func span(after index: inout Int) -> Span { _storage.span(after: &index) } + /// Return a borrowing span over the maximal storage chunk preceding the + /// specified position in the array. The span provides direct read-only access + /// to all array elements in the range `0 ..< index`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @inlinable @_lifetime(borrow self) public func span(before index: inout Int) -> Span { @@ -219,6 +287,13 @@ extension UniqueArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Return a mutable span over the maximal storage chunk following the + /// specified position in the array. The span provides direct mutating access + /// to all array elements in the range `index ..< count`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @_lifetime(&self) public mutating func mutableSpan( after index: inout Int @@ -226,6 +301,13 @@ extension UniqueArray where Element: ~Copyable { _storage.mutableSpan(after: &index) } + /// Return a mutable span over the maximal storage chunk preceding the specified + /// position in the array. The span provides direct mutating access to all + /// array elements in the range `0 ..< index`. + /// + /// - Parameter index: A valid index in the array, including the end index. + /// + /// - Complexity: O(1) @_lifetime(&self) public mutating func mutableSpan( before index: inout Int @@ -247,11 +329,30 @@ internal func _growDynamicArrayCapacity(_ capacity: Int) -> Int { @available(SwiftStdlib 5.0, *) extension UniqueArray where Element: ~Copyable { + /// Grow or shrink the capacity of a unique array instance without discarding + /// its contents. + /// + /// This operation replaces the array's storage buffer with a newly allocated + /// buffer of the specified capacity, moving all existing elements + /// to its new storage. The old storage is then deallocated. + /// + /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be + /// greater than or equal to the current count. + /// + /// - Complexity: O(`count`) @inlinable @inline(never) public mutating func reallocate(capacity: Int) { _storage.reallocate(capacity: capacity) } + /// Ensure that the array has capacity to store the specified number of + /// elements, by growing its storage buffer if necessary. + /// + /// If `capacity < n`, then this operation reallocates the unique array's + /// storage to grow it; on return, the array's capacity becomes `n`. + /// Otherwise the array is left as is. + /// + /// - Complexity: O(`count`) @inlinable @inline(never) public mutating func reserveCapacity(_ n: Int) { _storage.reserveCapacity(n) @@ -279,4 +380,34 @@ extension UniqueArray where Element: ~Copyable { } } +//MARK: - Copying helpers + +@available(SwiftStdlib 5.0, *) +extension UniqueArray { + /// Copy the contents of this array into a newly allocated unique array + /// instance with just enough capacity to hold all its elements. + /// + /// - Complexity: O(`count`) + @inlinable + @inline(__always) + public func copy() -> Self { + UniqueArray(consuming: _storage.copy()) + } + + /// Copy the contents of this array into a newly allocated unique array + /// instance with the specified capacity. + /// + /// - Parameter capacity: The desired capacity of the resulting unique array. + /// `capacity` must be greater than or equal to `count`. + /// + /// - Complexity: O(`count`) + @inlinable + @inline(__always) + public func copy(capacity: Int) -> Self { + UniqueArray(consuming: _storage.copy(capacity: capacity)) + } +} + + + #endif