From 437f984b112fecbd4303d21f7c934f2085b6d9af Mon Sep 17 00:00:00 2001 From: "John K. Luebs" Date: Thu, 31 Oct 2024 14:41:00 -0500 Subject: [PATCH] modify Buffer mapInPlace interface --- Sources/PFFFT/Buffer.swift | 24 ++++++++++++------------ Tests/PFFFTTests/PFFFTTests.swift | 4 +--- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Sources/PFFFT/Buffer.swift b/Sources/PFFFT/Buffer.swift index 3c7be98..75a8eac 100644 --- a/Sources/PFFFT/Buffer.swift +++ b/Sources/PFFFT/Buffer.swift @@ -45,12 +45,12 @@ public struct Buffer: ~Copyable { } /// Calls the given closure on each element in the buffer for mutation in place. - /// - Parameter body: A closure that accepts a zero-based index and mutable element of the - /// buffer as a parameter. `body` may throw and the error will be propagated to the - /// caller. Any return value of the closure is discarded. - @inlinable public func enumerateInPlace(_ body: (Int, inout T) throws -> Void) rethrows { + /// - Parameter body: A closure that accepts a zero-based enumeration index. + /// and must return a new value for the element at that index. + /// `body` may throw and the error will be propagated to the caller. + @inlinable public func mapInPlace(_ body: (Int) throws -> T) rethrows { for i in 0 ..< buffer.count { - try body(i, &buffer[i]) + try buffer[i] = body(i) } } } @@ -64,7 +64,7 @@ public protocol ComplexType { extension Complex: ComplexType {} public extension Buffer where T: ComplexType { - /// Calls the given closure on each element in the buffer for mutation in place with + /// Calls the given closure on each space in the buffer for mutation in place with /// Nyquist replacement at the end. /// /// When operating on Complex->Real transforms PFFFT internally uses a slightly more compact @@ -72,15 +72,15 @@ public extension Buffer where T: ComplexType { /// spectral components are always real, PFFFT places the DC (0) component /// in the real part of the 0th element as expected, but places the Nyquist `(n/2)` component /// in the imaginary part of the 0th element. - /// This enumerator works like `enumerateInPlace` but at the end places the real part of the + /// This enumerator works like `mapInPlace` but at the end places the real part of the /// n/2 component into the imaginary part of the 0th element. In normal use it is expected /// that a spectral buffer of 1 extra element is created suct that `count == (n/2 + 1)`. - /// - Parameter body: A closure that accepts a zero-based index and mutable element of the - /// buffer as a parameter. `body` may throw and the error will be propagated to the - /// caller. Any return value of the closure is discarded. - @inlinable func enumerateInPlaceSwapLast(_ body: (Int, inout T) throws -> Void) rethrows { + /// - Parameter body: A closure that accepts a zero-based enumeration index. + /// and must return a new value for the element at that index. + /// `body` may throw and the error will be propagated to the caller. + @inlinable func mapInPlaceSwapLast(_ body: (Int) throws -> T) rethrows { for i in 0 ..< buffer.count { - try body(i, &buffer[i]) + try buffer[i] = body(i) } buffer[0].imaginary = buffer[buffer.count - 1].real } diff --git a/Tests/PFFFTTests/PFFFTTests.swift b/Tests/PFFFTTests/PFFFTTests.swift index 9c37cfc..17fef5b 100644 --- a/Tests/PFFFTTests/PFFFTTests.swift +++ b/Tests/PFFFTTests/PFFFTTests.swift @@ -8,9 +8,7 @@ final class FFTTests: XCTestCase { let signal = fft.makeSignalBuffer() let spectrum = fft.makeSpectrumBuffer() - signal.enumerateInPlace { i, v in - v = Complex(Float(i) + 1.0, Float(i) - 2.0) - } + signal.mapInPlace { Complex(Float($0) + 1.0, Float($0) - 2.0) } fft.forward(signal: signal, spectrum: spectrum)