Skip to content

Commit

Permalink
Merge pull request #70 from ferranpujolcamins/swift5-fix
Browse files Browse the repository at this point in the history
Swift5 fix
  • Loading branch information
davecom committed Apr 12, 2019
2 parents 9e8b018 + 113b588 commit 5911a06
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 175 deletions.
25 changes: 6 additions & 19 deletions .travis.yml
Expand Up @@ -5,39 +5,26 @@ env:
matrix:
include:
- os: osx
osx_image: xcode10
osx_image: xcode10.2
language: swift
install:
- ./BuildScripts/osx_install.sh
- ./BuildScripts/osx_coverage_install.sh
script: ./BuildScripts/osx_script.sh
# Coverage delivery to codeclimate setup
# codeclimate only shows us results of one run, so we just run it once on last xcode release.
before_script: ./BuildScripts/osx_coverage_before.sh
before_script:
- ./BuildScripts/osx_check_test_generated.sh
- ./BuildScripts/osx_coverage_before.sh
after_script: ./BuildScripts/osx_coverage_after.sh

#- os: osx
# osx_image: xcode9.3
# language: swift
# install: ./BuildScripts/osx_install.sh
# script: ./BuildScripts/osx_script.sh

- os: linux
language: generic
# The only purpose of this var is that it's dispalyed in travis-ci.com so we can identify this job
env: SWIFT=4.2
env: SWIFT=5.0
install:
- ./BuildScripts/linux_install.sh "https://swift.org/builds/swift-4.2-release/ubuntu1404/swift-4.2-RELEASE/swift-4.2-RELEASE-ubuntu14.04.tar.gz"
- ./BuildScripts/linux_install.sh "https://swift.org/builds/swift-5.0-release/ubuntu1404/swift-5.0-RELEASE/swift-5.0-RELEASE-ubuntu14.04.tar.gz"
# According to Travis docs, PATH modification must be done here.
# https://docs.travis-ci.com/user/installing-dependencies/#Installing-Projects-from-Source
- PATH=$PATH:$PWD/swift/usr/bin/
script: ./BuildScripts/linux_script.sh
before_script: ./BuildScripts/linux_check_test_generated.sh

#- os: linux
# language: generic
# env: SWIFT=4.0
# install:
# - ./BuildScripts/linux_install.sh "https://swift.org/builds/swift-4.0-release/ubuntu1404/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu14.04.tar.gz"
# - PATH=$PATH:$PWD/swift/usr/bin/
# script: ./BuildScripts/linux_script.sh
3 changes: 0 additions & 3 deletions BuildScripts/linux_check_test_generated.sh

This file was deleted.

1 change: 1 addition & 0 deletions BuildScripts/linux_install.sh
@@ -1,4 +1,5 @@
#!/bin/bash
set -ev
SWIFT_URL=$1

wget ${SWIFT_URL} -O /tmp/swift.tar.gz
Expand Down
1 change: 1 addition & 0 deletions BuildScripts/linux_script.sh
@@ -1,3 +1,4 @@
#!/bin/bash
set -ev
swift test -c debug --filter "SwiftGraphTests"
# swift test -c release -Xswiftc -enable-testing --filter "SwiftGraphPerformanceTests"
7 changes: 7 additions & 0 deletions BuildScripts/osx_check_test_generated.sh
@@ -0,0 +1,7 @@
#!/bin/bash
set -ev

# Assert that the linux test files are generated.
# We need to make this test here since the command only works on osx.
swift test --generate-linuxmain
git diff --exit-code
2 changes: 1 addition & 1 deletion BuildScripts/osx_coverage_install.sh
@@ -1,3 +1,3 @@
#!/bin/bash
set -v
gem install slather --no-ri --no-rdoc
gem install slather
2 changes: 1 addition & 1 deletion BuildScripts/osx_install.sh
@@ -1,3 +1,3 @@
#!/bin/bash
set -v
gem install xcpretty --no-ri --no-rdoc
gem install xcpretty
2 changes: 1 addition & 1 deletion Package.swift
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:5.0

import PackageDescription

Expand Down
19 changes: 10 additions & 9 deletions Sources/SwiftGraph/Union.swift
Expand Up @@ -29,37 +29,38 @@ public extension UniqueElementsGraph where E == UnweightedEdge {
///
/// - Parameters:
/// - graphs: Array of graphs to build the union from.
convenience init(unionOf graphs: [UniqueElementsGraph]) {
self.init()
static func unionOf(_ graphs: [UniqueElementsGraph]) -> UniqueElementsGraph{
let union = UniqueElementsGraph()

guard let firstGraph = graphs.first else { return }
guard let firstGraph = graphs.first else { return union }
let others = graphs.dropFirst()

// We know vertices in lhs are unique, so we call Graph.addVertex to avoid the uniqueness check of UniqueElementsGraph.addVertex.
for vertex in firstGraph.vertices {
_ = addVertex(vertex)
_ = union.addVertex(vertex)
}

// When vertices are removed from Graph, edges might mutate,
// so we need to add new copies of them for the result graph.
for edge in firstGraph.edges.joined() {
addEdge(edge, directed: true)
union.addEdge(edge, directed: true)
}

for g in others {
// Vertices in rhs might be equal to some vertex in lhs, so we need to add them
// with self.addVertex to guarantee uniqueness.
for vertex in g.vertices {
_ = addVertex(vertex)
_ = union.addVertex(vertex)
}

for edge in g.edges.joined() {
addEdge(edge)
union.addEdge(from: g[edge.u], to: g[edge.v], directed: true)
}
}
return union
}

convenience init(unionOf graphs: UniqueElementsGraph...) {
self.init(unionOf: graphs)
static func unionOf(_ graphs: UniqueElementsGraph...) -> UniqueElementsGraph{
return unionOf(graphs)
}
}
66 changes: 47 additions & 19 deletions Sources/SwiftGraph/UniqueElementsGraph.swift
Expand Up @@ -87,15 +87,16 @@ extension UniqueElementsGraph where E == UnweightedEdge {
/// - directed: If false, undirected edges are created.
/// If true, edges are directed from vertex i to vertex i+1 in path.
/// Default is false.
public convenience init(withPath path: [V], directed: Bool = false) {
self.init(vertices: path)
public static func withPath(_ path: [V], directed: Bool = false) -> UniqueElementsGraph {
let g = UniqueElementsGraph(vertices: path)

guard path.count >= 2 else {
return
return g
}

let indices: [Int] = path.map({ vertices.firstIndex(of: $0)! })
addEdgesForPath(withIndices: indices, directed: directed)
let indices = path.map({ g.indexOfVertex($0)! })
g.addEdgesForPath(withIndices: indices, directed: directed)
return g
}

/// Initialize an UniqueElementsGraph consisting of cycle.
Expand All @@ -114,31 +115,58 @@ extension UniqueElementsGraph where E == UnweightedEdge {
/// - directed: If false, undirected edges are created.
/// If true, edges are directed from vertex i to vertex i+1 in cycle.
/// Default is false.
public convenience init(withCycle cycle: [V], directed: Bool = false) {
self.init(vertices: cycle)
public static func withCycle(_ cycle: [V], directed: Bool = false) -> UniqueElementsGraph {
let g = UniqueElementsGraph(vertices: cycle)

guard cycle.count >= 2 else {
if let v = cycle.first {
let index = addVertex(v)
let ue = UnweightedEdge(u: index, v: index, directed: directed)
addEdge(ue)
let index = g.addVertex(v)
g.addEdge(fromIndex: index, toIndex: index)
}
return
return g
}

let indices: [Int] = cycle.map({ vertex -> Int in

let index: Int = vertices.firstIndex(of: vertex)!
return index
})
addEdgesForPath(withIndices: indices, directed: directed)
let ue = UnweightedEdge(u: indices.last!, v: indices.first!, directed: directed)
addEdge(ue)
let indices = cycle.map({ g.indexOfVertex($0)! })
g.addEdgesForPath(withIndices: indices, directed: directed)
g.addEdge(fromIndex: indices.last!, toIndex: indices.first!, directed: directed)
return g
}

}

extension UniqueElementsGraph where V: Hashable, E == UnweightedEdge {
public static func withPath(_ path: [V], directed: Bool = false) -> UniqueElementsGraph {
let g = UniqueElementsGraph()

guard path.count >= 2 else {
if let v = path.first {
_ = g.addVertex(v)
}
return g
}

let indices = g.indicesForPath(path)
g.addEdgesForPath(withIndices: indices, directed: directed)
return g
}


public static func withCycle(_ cycle: [V], directed: Bool = false) -> UniqueElementsGraph {
let g = UniqueElementsGraph()

guard cycle.count >= 2 else {
if let v = cycle.first {
let index = g.addVertex(v)
g.addEdge(fromIndex: index, toIndex: index)
}
return g
}

let indices = g.indicesForPath(cycle)
g.addEdgesForPath(withIndices: indices, directed: directed)
g.addEdge(fromIndex: indices.last!, toIndex: indices.first!, directed: directed)
return g
}

private func indicesForPath(_ path: [V]) -> [Int] {
var indices: [Int] = []
Expand Down
20 changes: 11 additions & 9 deletions Sources/SwiftGraph/UnweightedGraph.swift
Expand Up @@ -46,16 +46,17 @@ extension Graph where E == UnweightedEdge {
/// - directed: If false, undirected edges are created.
/// If true, edges are directed from vertex i to vertex i+1 in path.
/// Default is false.
public init(withPath path: [V], directed: Bool = false) {
self.init(vertices: path)
public static func withPath(_ path: [V], directed: Bool = false) -> Self {
let g = Self(vertices: path)

guard path.count >= 2 else {
return
return g
}

for i in 0..<path.count - 1 {
self.addEdge(fromIndex: i, toIndex: i+1, directed: directed)
g.addEdge(fromIndex: i, toIndex: i+1, directed: directed)
}
return g
}

/// Initialize an UnweightedGraph consisting of cycle.
Expand All @@ -64,8 +65,8 @@ extension Graph where E == UnweightedEdge {
/// each pair of consecutive vertices in cycle,
/// plus an edge between the last and the first vertices.
///
/// If path is an empty array, the resulting graph is the empty graph.
/// If path is an array with a single vertex, the resulting graph has the vertex
/// If cycle is an empty array, the resulting graph is the empty graph.
/// If cycle is an array with a single vertex, the resulting graph has the vertex
/// and a single edge to itself if directed is true.
/// If directed is false the resulting graph has the vertex and two edges to itself.
///
Expand All @@ -74,11 +75,12 @@ extension Graph where E == UnweightedEdge {
/// - directed: If false, undirected edges are created.
/// If true, edges are directed from vertex i to vertex i+1 in cycle.
/// Default is false.
public init(withCycle cycle: [V], directed: Bool = false) {
self.init(withPath: cycle, directed: directed)
public static func withCycle(_ cycle: [V], directed: Bool = false) -> Self {
let g = Self.withPath(cycle, directed: directed)
if cycle.count > 0 {
self.addEdge(fromIndex: cycle.count-1, toIndex: 0, directed: directed)
g.addEdge(fromIndex: cycle.count-1, toIndex: 0, directed: directed)
}
return g
}

/// This is a convenience method that adds an unweighted edge.
Expand Down
21 changes: 11 additions & 10 deletions SwiftGraph.xcodeproj/project.pbxproj
Expand Up @@ -444,28 +444,26 @@
TargetAttributes = {
55E620011A194C80000A5F7B = {
CreatedOnToolsVersion = 6.1;
LastSwiftMigration = 1020;
LastSwiftMigration = 1000;
};
7985B8FA1E5A4FB800C100E7 = {
CreatedOnToolsVersion = 8.2.1;
LastSwiftMigration = 1020;
LastSwiftMigration = 1000;
ProvisioningStyle = Automatic;
};
7985B9021E5A4FB900C100E7 = {
CreatedOnToolsVersion = 8.2.1;
LastSwiftMigration = 1020;
LastSwiftMigration = 1000;
ProvisioningStyle = Automatic;
};
B5EACB0A2172315E00E527BD = {
LastSwiftMigration = 1020;
};
};
};
buildConfigurationList = 55E61FFD1A194C7F000A5F7B /* Build configuration list for PBXProject "SwiftGraph" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = en;
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -619,6 +617,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_CODE_COVERAGE = YES;
Expand Down Expand Up @@ -666,7 +665,7 @@
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
Expand All @@ -676,6 +675,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_CODE_COVERAGE = YES;
Expand Down Expand Up @@ -715,7 +715,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
Expand Down Expand Up @@ -886,6 +886,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_CODE_COVERAGE = YES;
Expand Down Expand Up @@ -931,7 +932,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
ONLY_ACTIVE_ARCH = YES;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
Expand Down

0 comments on commit 5911a06

Please sign in to comment.