//===----------------------------------------------------------------------===// // // 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, *) @_alwaysEmitIntoClient package func withTemporaryOutputSpan( of type: Element.Type, capacity: Int, _ body: (inout OutputSpan) throws(E) -> R ) throws(E) -> R { try _withUnsafeTemporaryAllocation( of: Element.self, capacity: capacity ) { buffer throws(E) in var span = unsafe OutputSpan(buffer: buffer, initializedCount: 0) return try body(&span) } } @available(SwiftStdlib 5.0, *) extension OutputSpan where Element: ~Copyable { /// Removes and returns the last element of this output span, if it exists. /// /// - Returns: The last element of the original span if it wasn't empty; /// otherwise, `nil`. /// /// - Complexity: O(1) @inlinable package mutating func _popLast() -> Element? { // FIXME: This needs to be in the stdlib. unsafe withUnsafeMutableBufferPointer { buffer, count in guard count > 0 else { return nil } count &-= 1 return unsafe buffer.moveElement(from: count) } } } @available(SwiftStdlib 5.0, *) extension OutputSpan where Element: ~Copyable { @inlinable @inline(__always) @_lifetime(self: copy self) @unsafe package mutating func _append( moving source: UnsafeMutableBufferPointer ) { // FIXME: This needs to be in the stdlib. guard source.count > 0 else { return } unsafe self.withUnsafeMutableBufferPointer { dst, dstCount in let dstEnd = dstCount + source.count precondition(dstEnd <= dst.count, "OutputSpan capacity overflow") unsafe dst ._extracting(uncheckedFrom: dstCount, to: dstEnd) .moveInitializeAll(fromContentsOf: source) dstCount &+= source.count } } @_lifetime(source: copy source) @inlinable @inline(__always) @_lifetime(self: copy self) package mutating func _append(moving source: inout OutputSpan) { // FIXME: This needs to be in the stdlib. unsafe source.withUnsafeMutableBufferPointer { src, srcCount in let items = unsafe src._extracting(uncheckedFrom: 0, to: srcCount) unsafe self._append(moving: items) srcCount = 0 } } } @available(SwiftStdlib 5.0, *) extension OutputSpan /*where Element: Copyable*/ { @inlinable @_lifetime(self: copy self) @unsafe package mutating func _append(copying source: UnsafeBufferPointer) { // FIXME: This needs to be in the stdlib. unsafe self.withUnsafeMutableBufferPointer { dst, dstCount in let dstEnd = dstCount + source.count precondition(dstEnd <= dst.count, "OutputSpan capacity overflow") unsafe dst ._extracting(uncheckedFrom: dstCount, to: dstEnd) .initializeAll(fromContentsOf: source) dstCount &+= source.count } } @inlinable @_lifetime(self: copy self) package mutating func _append(copying source: Span) { // FIXME: This needs to be in the stdlib. source.withUnsafeBufferPointer { buffer in unsafe self._append(copying: buffer) } } } @available(SwiftStdlib 5.0, *) extension OutputSpan where Element: ~Copyable { @inlinable @_lifetime(self: copy self) package mutating func _remove( from index: Int, where shouldBeRemoved: (borrowing Element) -> Bool ) { // FIXME: This needs to be in the stdlib. precondition(index >= 0 && index <= count, "Index out of bounds") unsafe self.withUnsafeMutableBufferPointer { buffer, count in var i = index var j = i // Invariant: i ..< j is uninitialized while j < count { if shouldBeRemoved(unsafe buffer[j]) { unsafe buffer.deinitializeElement(at: j) j &+= 1 } else { if i != j { unsafe buffer.initializeElement(at: i, to: buffer.moveElement(from: j)) } i &+= 1 j &+= 1 } } count = i } } } @available(SwiftStdlib 5.0, *) extension OutputSpan where Element: ~Copyable { @_alwaysEmitIntoClient @discardableResult @_lifetime(self: copy self) package mutating func _append( addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> R ) throws(E) -> R { // FIXME: This needs to be in the stdlib. // Note: This is the wrong shape for append, as it doesn't generalize // to piecewise contiguous containers and doesn't return indices. // However, it feels like we need to _also_ provide a simple append // implementation for flat containers, as the ability return things out of // the closure is quite important for usability. We can't name them both // `append(addingCount:initializingWith:)` though! precondition(newItemCount >= 0, "Cannot add a negative number of items") return unsafe try withUnsafeMutableBufferPointer { buf, c throws(E) in precondition(newItemCount <= buf.count - c, "OutputSpan capacity overflow") let dst = unsafe buf._extracting(unchecked: Range( uncheckedBounds: (c, c &+ newItemCount))) var span = unsafe OutputSpan(buffer: dst, initializedCount: 0) defer { c &+= unsafe span.finalize(for: dst) span = OutputSpan() } return try initializer(&span) } } }