Skip to content

Commit

Permalink
Fix warnings for Xcode 10.2.1 and Swift 5. (alejandro-isaza#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradhowes authored and alejandro-isaza committed May 14, 2019
1 parent 66c6625 commit 932b897
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode9.3
osx_image: xcode10.2

script:
- set -o pipefail
Expand Down
20 changes: 17 additions & 3 deletions Sources/Upsurge/2DTensorSlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ open class TwoDimensionalTensorSlice<Element: Value>: MutableQuadraticType, Equa
assert(span.dimensions.reduce(0) { $1 > 1 ? $0 + 1 : $0 } <= 2)
assert(span.dimensions.last! >= 1)

let rowIndex: Int = span.dimensions.index { $0 > 1 } ??
(span.dimensions.count - 2)

#if swift(>=5.0)
let rowIndex: Int = span.dimensions.firstIndex { $0 > 1 } ?? (span.dimensions.count - 2)
#else
let rowIndex: Int = span.dimensions.index { $0 > 1 } ?? (span.dimensions.count - 2)
#endif
rows = span.dimensions[rowIndex]
columns = span.dimensions.last!

Expand Down Expand Up @@ -133,15 +135,27 @@ open class TwoDimensionalTensorSlice<Element: Value>: MutableQuadraticType, Equa
}

open var isContiguous: Bool {
#if swift(>=5.0)
let onesCount: Int = (dimensions.firstIndex { $0 != 1 }) ?? rank
#else
let onesCount: Int = (dimensions.index { $0 != 1 }) ?? rank
#endif

let diff = (0..<rank).map({ dimensions[$0] - base.dimensions[$0] }).reversed()
let fullCount: Int
#if swift(>=5.0)
if let index = (diff.firstIndex { $0 != 0 }), index.base < count {
fullCount = rank - index.base
} else {
fullCount = rank
}
#else
if let index = (diff.index { $0 != 0 }), index.base < count {
fullCount = rank - index.base
} else {
fullCount = rank
}
#endif

return rank - fullCount - onesCount <= 1
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/Upsurge/Complex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public struct Complex<Element: Real>: Value {
return Complex(real: x.magnitude, imag: 0.0)
}

public var hashValue: Int {
return real.hashValue ^ imag.hashValue
public func hash(into: inout Hasher) {
into.combine(real)
into.combine(imag)
}

public var description: String {
Expand Down
12 changes: 6 additions & 6 deletions Sources/Upsurge/LinearType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ public protocol LinearType: TensorType, CustomStringConvertible, CustomDebugStri
}

public extension LinearType {
public var dimensions: [Int] {
var dimensions: [Int] {
return [count]
}

public func index(after i: Int) -> Int {
func index(after i: Int) -> Int {
return i + 1
}

public func index(before i: Int) -> Int {
func index(before i: Int) -> Int {
return i - 1
}

public func formIndex(after i: inout Int) {
func formIndex(after i: inout Int) {
i += 1
}

public var description: String {
var description: String {
return "[\(map { "\($0)" }.joined(separator: ", "))]"
}

public var debugDescription: String {
var debugDescription: String {
return description
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Upsurge/QuadraticType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public protocol QuadraticType: TensorType {

public extension QuadraticType {
/// The number of valid element in the memory block, taking into account the step size.
public var count: Int {
var count: Int {
return rows * columns
}

public var dimensions: [Int] {
var dimensions: [Int] {
if arrangement == .rowMajor {
return [rows, columns]
} else {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Upsurge/TensorSlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ open class TensorSlice<Element: Value>: MutableTensorType, Equatable {
}

open var isContiguous: Bool {
let onesCount: Int = (dimensions.index { $0 != 1 }) ?? rank
let onesCount: Int = (dimensions.firstIndex { $0 != 1 }) ?? rank
let diff = (0..<rank).map { dimensions[$0] - base.dimensions[$0] }.reversed()
let fullCount: Int
if let index = (diff.index { $0 != 0 }), index.base < count {
if let index = (diff.firstIndex { $0 != 0 }), index.base < count {
fullCount = rank - index.base
} else {
fullCount = rank
Expand Down
8 changes: 4 additions & 4 deletions Sources/Upsurge/TensorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ public protocol TensorType {

public extension TensorType {
/// The size of each dimension
public var dimensions: [Int] {
var dimensions: [Int] {
return span.dimensions
}

/// The number of dimensions
public var rank: Int {
var rank: Int {
return span.rank
}

/// Convert a high-dimensional index into an integer index for a LinearType
public func linearIndex(_ indices: [Int]) -> Int {
func linearIndex(_ indices: [Int]) -> Int {
precondition(indexIsValid(indices))
var index = indices[0]
for (i, dim) in span.dimensions[1..<rank].enumerated() {
Expand All @@ -58,7 +58,7 @@ public extension TensorType {
}

/// Check that an index falls within the span
public func indexIsValid(_ indices: [Int]) -> Bool {
func indexIsValid(_ indices: [Int]) -> Bool {
return indices.count == rank && indices.enumerated().all { (i, index) in
self.span[i].contains(index)
}
Expand Down
Loading

0 comments on commit 932b897

Please sign in to comment.