|
3 | 3 | public typealias vDSP_Length = UInt |
4 | 4 | /// An integer value that represents the differences between indices of elements, including the lengths of strides. |
5 | 5 | public typealias vDSP_Stride = Int |
| 6 | + |
| 7 | +/// A type that represents an immutable buffer. |
| 8 | +/// |
| 9 | +/// If you implement your own type that conforms to ``AccelerateBuffer`` and uses the default implementation of ``withUnsafeBufferPointer(_:)``, |
| 10 | +/// your type needs to return a nonnil result from `withContiguousStorageIfAvailable(_:)`. |
| 11 | +// https://developer.apple.com/documentation/accelerate/acceleratebuffer |
| 12 | +public protocol AccelerateBuffer<Element> { |
| 13 | + /// The buffer’s element type. |
| 14 | + associatedtype Element |
| 15 | + /// The number of elements in the buffer. |
| 16 | + var count: Int { get } |
| 17 | + /// Calls a closure with a pointer to the object’s contiguous storage. |
| 18 | + /// - Parameter body: A closure that receives an UnsafeBufferPointer to the sequence’s contiguous storage. |
| 19 | + func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Self.Element>) throws -> R) rethrows -> R |
| 20 | +} |
| 21 | + |
| 22 | +// https://developer.apple.com/documentation/accelerate/acceleratebuffer/3240656-withunsafebufferpointer |
| 23 | +extension AccelerateBuffer where Self: Collection { |
| 24 | + public func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Self.Element>) throws -> R) rethrows -> R { |
| 25 | + try self.withContiguousStorageIfAvailable(body)! |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +extension Slice: AccelerateBuffer where Base: AccelerateBuffer {} |
| 30 | + |
| 31 | +/// A type that represents a mutable buffer. |
| 32 | +/// |
| 33 | +/// If you implement your own type that conforms to `AccelerateMutableBuffer` and uses the default implementation of ``withUnsafeMutableBufferPointer(_:)``, |
| 34 | +/// your type needs to return a nonnil result from `withContiguousMutableStorageIfAvailable(_:)``. |
| 35 | +// https://developer.apple.com/documentation/accelerate/acceleratemutablebuffer |
| 36 | +public protocol AccelerateMutableBuffer<Element>: AccelerateBuffer { |
| 37 | + /// Calls the given closure with a pointer to the object’s mutable contiguous storage. |
| 38 | + /// - Parameter body: A closure that receives an UnsafeMutableBufferPointer to the sequence’s contiguous storage. |
| 39 | + mutating func withUnsafeMutableBufferPointer<R>(_ body: (inout UnsafeMutableBufferPointer<Self.Element>) throws -> R) rethrows -> R |
| 40 | +} |
| 41 | + |
| 42 | +// https://developer.apple.com/documentation/accelerate/acceleratemutablebuffer/3240659-withunsafemutablebufferpointer |
| 43 | +extension AccelerateMutableBuffer where Self: MutableCollection { |
| 44 | + public mutating func withUnsafeMutableBufferPointer<R>(_ body: (inout UnsafeMutableBufferPointer<Self.Element>) throws -> R) rethrows |
| 45 | + -> R |
| 46 | + { |
| 47 | + try self.withContiguousMutableStorageIfAvailable(body)! |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +extension Array: AccelerateMutableBuffer {} |
| 52 | +extension ArraySlice: AccelerateMutableBuffer {} |
| 53 | +extension ContiguousArray: AccelerateMutableBuffer {} |
| 54 | +extension Slice: AccelerateMutableBuffer where Base: AccelerateMutableBuffer & MutableCollection {} |
| 55 | +extension UnsafeMutableBufferPointer: AccelerateMutableBuffer {} |
6 | 56 | #endif |
0 commit comments