import ComplexModule import RealModule let bufferAlignment = 32 @frozen public struct Buffer: ~Copyable { public let buffer: UnsafeMutableBufferPointer var count: Int { buffer.count } var baseAddress: UnsafeMutablePointer { buffer.baseAddress! } public init(capacity: Int) { buffer = UnsafeMutableRawBufferPointer.allocate( byteCount: MemoryLayout.stride * capacity, alignment: bufferAlignment ).bindMemory(to: T.self) } deinit { buffer.deallocate() } @inlinable public func withUnsafeMutableBufferPointer(_ body: (UnsafeMutableBufferPointer) throws -> R) rethrows -> R { try body(buffer) } @inlinable public func withUnsafeBufferPointer(_ body: (UnsafeBufferPointer) throws -> R) rethrows -> R { try body(UnsafeBufferPointer(buffer)) } @inlinable public func withUnsafeMutableBytes(_ body: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R { try body(UnsafeMutableRawBufferPointer(buffer)) } @inlinable public func map(_ transform: (T) throws -> U) rethrows -> [U] { try buffer.map(transform) } @inlinable public func mapInPlace(_ body: (Int, inout T) throws -> Void) rethrows { for i in 0 ..< buffer.count { try body(i, &buffer[i]) } } } public protocol ComplexType { associatedtype RealType: Real var real: RealType { get set } var imaginary: RealType { get set } } extension Complex: ComplexType {} public extension Buffer where T: ComplexType { @inlinable func mapInPlaceSwapLast(_ body: (Int, inout T) throws -> Void) rethrows { for i in 0 ..< buffer.count { try body(i, &buffer[i]) } buffer[0].imaginary = buffer[buffer.count - 1].real } }