This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol JSONParser { | |
static func obtainModel(from json: JSON) -> Self? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Some model | |
struct MyModel { | |
let name: String | |
let number: Int | |
let date: NSDate | |
} | |
// Conforming to the new protocol | |
extension MyModel: JSONParser { | |
static func obtainModel(from json: JSON) -> MyModel? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let json = JSON(data: dataFromServer) | |
if let model = MyModel.obtainModel(from: json) { | |
// success | |
} else { | |
// failure | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Node { | |
let name: String | |
var visited: Bool = false | |
var connections: [Connection] = [] | |
init(name: String) { | |
self.name = name | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Connection { | |
let to: Node | |
let weight: Int | |
init(to toNode: Node, weight: Int) { | |
assert(weight >= 0, "connection weight has to be equal or greater than zero") | |
self.to = toNode | |
self.weight = weight | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Path { | |
let cumulativeWeight: Int | |
let lastNode: Node | |
let previousPath: Path? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init(toNode: Node, viaConnection: Connection?=nil, viaPath: Path?=nil) { | |
if let | |
previousPath = viaPath, | |
lastConnection = viaConnection { | |
self.cumulativeWeight = lastConnection.weight + previousPath.cumulativeWeight | |
} else { | |
self.cumulativeWeight = 0 | |
} | |
self.lastNode = toNode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ==(x: Path, y: Path) -> Bool { | |
return x.cumulativeWeight == y.cumulativeWeight | |
} | |
func <(x: Path, y: Path) -> Bool { | |
return x.cumulativeWeight < y.cumulativeWeight | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Path: Comparable { | |
let cumulativeWeight: Int | |
let lastNode: Node | |
let previousPath: Path? | |
init(toNode: Node, viaConnection: Connection?=nil, viaPath: Path?=nil) { | |
if let | |
previousPath = viaPath, | |
lastConnection = viaConnection { | |
self.cumulativeWeight = lastConnection.weight + previousPath.cumulativeWeight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal func shortestPath(source: Vertex, destination: Vertex) -> Path? { | |
var frontier: [Path] = [] { | |
didSet { | |
frontier.sortInPlace() | |
} | |
} | |
var finalPaths: [Path] = [] { | |
didSet { | |
finalPaths.sortInPlace() |
OlderNewer