Skip to content

Commit

Permalink
Fix Xcode 9 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-isaza committed Sep 21, 2017
1 parent 90f2eb5 commit 0545952
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
3.2
16 changes: 10 additions & 6 deletions Source/MatrixArithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ public func inv<M: QuadraticType>(_ x: M) -> Matrix<Double> where M.Element == D
var error: __CLPK_integer = 0
var nc = __CLPK_integer(x.columns)

withPointer(&results) { pointer in
dgetrf_(&nc, &nc, pointer, &nc, &ipiv, &error)
dgetri_(&nc, pointer, &nc, &ipiv, &work, &lwork, &error)
withUnsafeMutablePointersTo(&nc, &lwork, &error) { nc, lwork, error in
withPointer(&results) { pointer in
dgetrf_(nc, nc, pointer, nc, &ipiv, error)
dgetri_(nc, pointer, nc, &ipiv, &work, lwork, error)
}
}

assert(error == 0, "Matrix not invertible")
Expand Down Expand Up @@ -197,9 +199,11 @@ public func inv<M: QuadraticType>(_ x: M) -> Matrix<Float> where M.Element == Fl
var error: __CLPK_integer = 0
var nc = __CLPK_integer(x.columns)

withPointer(&results) { pointer in
sgetrf_(&nc, &nc, pointer, &nc, &ipiv, &error)
sgetri_(&nc, pointer, &nc, &ipiv, &work, &lwork, &error)
withUnsafeMutablePointersTo(&nc, &lwork, &error) { nc, lwork, error in
withPointer(&results) { pointer in
sgetrf_(nc, nc, pointer, nc, &ipiv, error)
sgetri_(nc, pointer, nc, &ipiv, &work, lwork, error)
}
}

assert(error == 0, "Matrix not invertible")
Expand Down
51 changes: 51 additions & 0 deletions Source/PointerUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ public func withPointer<T: MutableTensorType, R>(_ t: inout T, body: (UnsafeMuta

// MARK: Two parameters

/// Invokes the given closure with pointers to the given arguments (2 parameter version).
///
/// - See: `withUnsafePointer(to:body:)`
@discardableResult
public func withUnsafePointersTo<A, B, Result>(_ a: inout A, _ b: inout B, body: (UnsafePointer<A>, UnsafePointer<B>) throws -> Result) rethrows -> Result {
return try withUnsafePointer(to: &a) { (a: UnsafePointer<A>) throws -> Result in
return try withUnsafePointer(to: &b) { (b: UnsafePointer<B>) throws -> Result in
return try body(a, b)
}
}
}

/// Invokes the given closure with mutable pointers to the given arguments (2 parameter version).
///
/// - See: `withUnsafeMutablePointer(to:body:)`
@discardableResult
public func withUnsafeMutablePointersTo<A, B, Result>(_ a: inout A, _ b: inout B, body: (UnsafeMutablePointer<A>, UnsafeMutablePointer<B>) throws -> Result) rethrows -> Result {
return try withUnsafeMutablePointer(to: &a) { (a: UnsafeMutablePointer<A>) throws -> Result in
return try withUnsafeMutablePointer(to: &b) { (b: UnsafeMutablePointer<B>) throws -> Result in
return try body(a, b)
}
}
}
/// Call `body(p1, p2)` with the pointers for the two types
public func withPointers<T1: TensorType, T2: TensorType, R>(_ t1: T1, _ t2: T2, body: (UnsafePointer<T1.Element>, UnsafePointer<T2.Element>) throws -> R) rethrows -> R where T1.Element == T2.Element {
return try t1.withUnsafeBufferPointer { p1 in
Expand Down Expand Up @@ -82,3 +105,31 @@ public func withPointers<T1: TensorType, T2: TensorType, T3: MutableTensorType,
}
}
}

/// Invokes the given closure with pointers to the given arguments (3 parameter version).
///
/// - See: `withUnsafePointer(to:body:)`
@discardableResult
public func withUnsafePointersTo<A, B, C, Result>(_ a: inout A, _ b: inout B, _ c: inout C, body: (UnsafePointer<A>, UnsafePointer<B>, UnsafePointer<C>) throws -> Result) rethrows -> Result {
return try withUnsafePointer(to: &a) { (a: UnsafePointer<A>) throws -> Result in
return try withUnsafePointer(to: &b) { (b: UnsafePointer<B>) throws -> Result in
return try withUnsafePointer(to: &c) { (c: UnsafePointer<C>) throws -> Result in
return try body(a, b, c)
}
}
}
}

/// Invokes the given closure with mutable pointers to the given arguments (3 parameter version).
///
/// - See: `withUnsafeMutablePointer(to:body:)`
@discardableResult
public func withUnsafeMutablePointersTo<A, B, C, Result>(_ a: inout A, _ b: inout B, _ c: inout C, body: (UnsafeMutablePointer<A>, UnsafeMutablePointer<B>, UnsafeMutablePointer<C>) throws -> Result) rethrows -> Result {
return try withUnsafeMutablePointer(to: &a) { (a: UnsafeMutablePointer<A>) throws -> Result in
return try withUnsafeMutablePointer(to: &b) { (b: UnsafeMutablePointer<B>) throws -> Result in
return try withUnsafeMutablePointer(to: &c) { (c: UnsafeMutablePointer<C>) throws -> Result in
return try body(a, b, c)
}
}
}
}

0 comments on commit 0545952

Please sign in to comment.