//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // // SPDX-License-Identifier: Apache-2.0 WITH Swift-exception // //===----------------------------------------------------------------------===// @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { @available(*, deprecated, renamed: "nextSpan(after:)") @inlinable @_lifetime(borrow self) public func span(after index: inout Int) -> Span { self.nextSpan(after: &index) } @available(*, deprecated, renamed: "previousSpan(before:)") @inlinable @_lifetime(borrow self) public func span(before index: inout Int) -> Span { previousSpan(before: &index) } #if COLLECTIONS_SINGLE_MODULE // FIXME: Remove when references have been flushed @available(*, unavailable) @usableFromInline internal func _contiguousSubrange(following index: inout Int) -> Range { precondition(index >= 0 && index <= count, "Index out of bounds") defer { index = count } return unsafe Range(uncheckedBounds: (index, count)) } @available(*, unavailable) @usableFromInline internal func _contiguousSubrange(preceding index: inout Int) -> Range { precondition(index >= 0 && index <= count, "Index out of bounds") defer { index = 0 } return unsafe Range(uncheckedBounds: (0, index)) } #endif @available(*, deprecated, renamed: "nextMutableSpan(after:)") @_lifetime(&self) @inlinable public mutating func mutableSpan( after index: inout Int ) -> MutableSpan { nextMutableSpan(after: &index) } @available(*, deprecated, renamed: "previousMutableSpan(before:)") @_lifetime(&self) @inlinable public mutating func mutableSpan( before index: inout Int ) -> MutableSpan { previousMutableSpan(before: &index) } @available(*, deprecated, renamed: "nextSpan(after:maxCount:)") @inlinable @_lifetime(borrow self) public func nextSpan( after index: inout Int, maximumCount: Int ) -> Span { self.nextSpan( after: &index, maxCount: maximumCount, limitedBy: self.endIndex) } @available(*, deprecated, renamed: "nextMutableSpan(after:maxCount:)") @inlinable @_lifetime(&self) public mutating func nextMutableSpan( after index: inout Int, maximumCount: Int ) -> MutableSpan { self.nextMutableSpan( after: &index, maxCount: maximumCount, limitedBy: self.endIndex) } @available(*, deprecated, renamed: "previousSpan(before:maxCount:)") @inlinable @_lifetime(borrow self) public func previousSpan( before index: inout Int, maximumCount: Int ) -> Span { self.previousSpan(before: &index, maxCount: maximumCount) } /// 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: /// - count: The number of items to append to the array. /// - initializer: A callback that gets called at most 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`) @available(*, deprecated, renamed: "append(addingCount:initializingWith:)") @_alwaysEmitIntoClient @inline(__always) public mutating func append( count: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Result ) throws(E) -> Result { var result: Result? = nil try append(addingCount: count) { target throws(E) in result = try initializer(&target) } return result.take()! } /// 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 capacity of the array isn't sufficient to accommodate the new /// elements, then this method triggers a runtime error. /// /// - 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 exactly 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`) @available(*, deprecated, renamed: "insert(addingCount:at:initializingWith:)") @inlinable public mutating func insert( count: Int, at index: Int, initializingWith body: (inout OutputSpan) -> Result ) -> Result { var result: Result? = nil self.insert(addingCount: count, at: index) { target in result = body(&target) } return result! } /// 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`) @available(*, deprecated, renamed: "setCapacity(_:)") @inlinable public mutating func reallocate(capacity newCapacity: Int) { setCapacity(newCapacity) } } @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`) @available(*, deprecated, renamed: "clone()") @inlinable public func copy() -> Self { clone() } /// 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`) @available(*, deprecated, renamed: "clone(capacity:)") @inlinable public func copy(capacity: Int) -> Self { clone(capacity: capacity) } @available(*, deprecated, renamed: "replaceSubrange(_:addingCount:initializingWith:)") @inlinable public mutating func replace( removing subrange: Range, addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { try replaceSubrange( subrange, addingCount: newItemCount, initializingWith: initializer) } @available(*, deprecated, renamed: "replaceSubrange(_:moving:)") @_alwaysEmitIntoClient @unsafe public mutating func replace( removing subrange: Range, moving newElements: UnsafeMutableBufferPointer, ) { unsafe replaceSubrange(subrange, moving: newElements) } @available(*, deprecated, renamed: "replaceSubrange(_:moving:)") @_alwaysEmitIntoClient public mutating func replace( removing subrange: Range, moving items: inout OutputSpan ) { replaceSubrange(subrange, moving: &items) } @available(*, deprecated, renamed: "replaceSubrange(_:moving:)") @_alwaysEmitIntoClient public mutating func replace( removing subrange: Range, moving newElements: inout RigidArray, ) { replaceSubrange(subrange, moving: &newElements) } @available(*, deprecated, renamed: "replaceSubrange(_:consuming:)") @_alwaysEmitIntoClient public mutating func replace( removing subrange: Range, consuming newElements: consuming RigidArray, ) { replaceSubrange(subrange, moving: &newElements) } @available(*, deprecated, renamed: "replaceSubrange(_:copying:)") @inlinable @unsafe public mutating func replace( removing subrange: Range, copying newElements: UnsafeBufferPointer ) { unsafe replaceSubrange(subrange, copying: newElements) } @available(*, deprecated, renamed: "replaceSubrange(_:copying:)") @inlinable @unsafe public mutating func replace( removing subrange: Range, copying newElements: UnsafeMutableBufferPointer ) { unsafe replaceSubrange(subrange, copying: newElements) } @available(*, deprecated, renamed: "replaceSubrange(_:copying:)") @inlinable public mutating func replace( removing subrange: Range, copying newElements: Span ) { replaceSubrange(subrange, copying: newElements) } @available(*, deprecated, renamed: "replaceSubrange(_:copying:)") @inlinable @inline(__always) public mutating func replace( removing subrange: Range, copying newElements: __owned some Collection ) { replaceSubrange(subrange, copying: newElements) } }