modify Buffer mapInPlace interface

This commit is contained in:
2024-10-31 14:41:00 -05:00
parent 795003432e
commit 437f984b11
2 changed files with 13 additions and 15 deletions

View File

@@ -45,12 +45,12 @@ public struct Buffer<T>: ~Copyable {
} }
/// Calls the given closure on each element in the buffer for mutation in place. /// 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 /// - Parameter body: A closure that accepts a zero-based enumeration index.
/// buffer as a parameter. `body` may throw and the error will be propagated to the /// and must return a new value for the element at that index.
/// caller. Any return value of the closure is discarded. /// `body` may throw and the error will be propagated to the caller.
@inlinable public func enumerateInPlace(_ body: (Int, inout T) throws -> Void) rethrows { @inlinable public func mapInPlace(_ body: (Int) throws -> T) rethrows {
for i in 0 ..< buffer.count { 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 {} extension Complex: ComplexType {}
public extension Buffer where T: 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. /// Nyquist replacement at the end.
/// ///
/// When operating on Complex->Real transforms PFFFT internally uses a slightly more compact /// 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 /// 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 real part of the 0th element as expected, but places the Nyquist `(n/2)` component
/// in the imaginary part of the 0th element. /// 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 /// 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)`. /// 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 /// - Parameter body: A closure that accepts a zero-based enumeration index.
/// buffer as a parameter. `body` may throw and the error will be propagated to the /// and must return a new value for the element at that index.
/// caller. Any return value of the closure is discarded. /// `body` may throw and the error will be propagated to the caller.
@inlinable func enumerateInPlaceSwapLast(_ body: (Int, inout T) throws -> Void) rethrows { @inlinable func mapInPlaceSwapLast(_ body: (Int) throws -> T) rethrows {
for i in 0 ..< buffer.count { for i in 0 ..< buffer.count {
try body(i, &buffer[i]) try buffer[i] = body(i)
} }
buffer[0].imaginary = buffer[buffer.count - 1].real buffer[0].imaginary = buffer[buffer.count - 1].real
} }

View File

@@ -8,9 +8,7 @@ final class FFTTests: XCTestCase {
let signal = fft.makeSignalBuffer() let signal = fft.makeSignalBuffer()
let spectrum = fft.makeSpectrumBuffer() let spectrum = fft.makeSpectrumBuffer()
signal.enumerateInPlace { i, v in signal.mapInPlace { Complex(Float($0) + 1.0, Float($0) - 2.0) }
v = Complex(Float(i) + 1.0, Float(i) - 2.0)
}
fft.forward(signal: signal, spectrum: spectrum) fft.forward(signal: signal, spectrum: spectrum)