Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated with XCTest Swift3 syntax for shortest path
XCTest Success
  • Loading branch information
gonini committed Jan 3, 2017
commit 6eed97951a180899d8887c739bdb493e554e99da
4 changes: 2 additions & 2 deletions Shortest Path (Unweighted)/ShortestPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ func breadthFirstSearchShortestPath(graph: Graph, source: Node) -> Graph {
let shortestPathGraph = graph.duplicate()

var queue = Queue<Node>()
let sourceInShortestPathsGraph = shortestPathGraph.findNodeWithLabel(source.label)
queue.enqueue(sourceInShortestPathsGraph)
let sourceInShortestPathsGraph = shortestPathGraph.findNodeWithLabel(label: source.label)
queue.enqueue(element: sourceInShortestPathsGraph)
sourceInShortestPathsGraph.distance = 0

while let current = queue.dequeue() {
Expand Down
34 changes: 17 additions & 17 deletions Shortest Path (Unweighted)/Tests/Graph.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// MARK: - Edge

public class Edge: Equatable {
public var neighbor: Node
open class Edge: Equatable {
open var neighbor: Node

public init(neighbor: Node) {
self.neighbor = neighbor
Expand All @@ -15,31 +15,31 @@ public func == (lhs: Edge, rhs: Edge) -> Bool {

// MARK: - Node

public class Node: CustomStringConvertible, Equatable {
public var neighbors: [Edge]
open class Node: CustomStringConvertible, Equatable {
open var neighbors: [Edge]

public private(set) var label: String
public var distance: Int?
public var visited: Bool
open fileprivate(set) var label: String
open var distance: Int?
open var visited: Bool

public init(label: String) {
self.label = label
neighbors = []
visited = false
}

public var description: String {
open var description: String {
if let distance = distance {
return "Node(label: \(label), distance: \(distance))"
}
return "Node(label: \(label), distance: infinity)"
}

public var hasDistance: Bool {
open var hasDistance: Bool {
return distance != nil
}

public func remove(edge: Edge) {
open func remove(_ edge: Edge) {
neighbors.remove(at: neighbors.index { $0 === edge }!)
}
}
Expand All @@ -50,25 +50,25 @@ public func == (lhs: Node, rhs: Node) -> Bool {

// MARK: - Graph

public class Graph: CustomStringConvertible, Equatable {
public private(set) var nodes: [Node]
open class Graph: CustomStringConvertible, Equatable {
open fileprivate(set) var nodes: [Node]

public init() {
self.nodes = []
}

public func addNode(label: String) -> Node {
open func addNode(label: String) -> Node {
let node = Node(label: label)
nodes.append(node)
return node
}

public func addEdge(_ source: Node, neighbor: Node) {
open func addEdge(_ source: Node, neighbor: Node) {
let edge = Edge(neighbor: neighbor)
source.neighbors.append(edge)
}

public var description: String {
open var description: String {
var description = ""

for node in nodes {
Expand All @@ -79,11 +79,11 @@ public class Graph: CustomStringConvertible, Equatable {
return description
}

public func findNodeWithLabel(label: String) -> Node {
open func findNodeWithLabel(label: String) -> Node {
return nodes.filter { $0.label == label }.first!
}

public func duplicate() -> Graph {
open func duplicate() -> Graph {
let duplicated = Graph()

for node in nodes {
Expand Down
2 changes: 1 addition & 1 deletion Shortest Path (Unweighted)/Tests/Queue.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public struct Queue<T> {
private var array: [T]
fileprivate var array: [T]

public init() {
array = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
TargetAttributes = {
7B2BBC7F1C779D720067B71D = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -230,6 +231,7 @@
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -242,6 +244,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down