gpt4 book ai didi

具有复杂类型的 Swift CustomStringConvertible

转载 作者:行者123 更新时间:2023-11-28 05:46:44 28 4
gpt4 key购买 nike

我希望 CustomStringConvertible 为我提供节点的描述,特别是它包含的边。

作为背景,我一直在研究图论并创建了一个节点:

class Node : CustomStringConvertible {
// unique identifier required for each node
var identifier : Int
var distance : Int = Int.max
var edges = [Edge]()
var visited = false

var description: String {
return "identifier: " + identifier.description + ", Edges: " + ( "edgesString" )
}

init(visited: Bool, identifier: Int, edges: [Edge]) {
self.visited = visited
self.identifier = identifier
self.edges = edges
}

static func == (lhs: Node, rhs: Node) -> Bool {
return lhs.identifier == rhs.identifier
}
}

有边

class Edge {
var from: Node // does not actually need to be stored!
var to: Node
var weight: Int
var description : String {
return "from: " + from.description + ", to: " + to.description + ", weight: " + weight.description
}
init(to: Node, from: Node, weight: Int) {
self.to = to
self.weight = weight
self.from = from
}
}

我可以很容易地打印出每个节点的每条边

testGraph.nodes.forEach { $0.edges.forEach{ print ($0.description)}}

但是我无法通过节点的描述来实现 in。

我尝试为每个语句写出与我的等效的内容

var description: String {
var edgesString = String()
edges.forEach{ edgesString.append($0.description)}
return "identifier: " + identifier.description + ", Edges: " + ( edgesString )
}

但在这种情况下,执行会给出 EXC_BAD_ACCESS,实际上我无法完成任何代码,也无法提供节点的描述和其中包含的边。

如何完成节点的描述字符串,然后继续描述边?

最佳答案

您的description 代码似乎会导致Node.descriptionEdge.description 无限循环。

Node 为每条边调用 Edge.descriptionEdge 为它的 from 和到节点。如果图有循环连接,而不是星形连接,则会导致无限循环。

一个简单的方法是Edge.description只显示Node.identifier而不是详细的描述。

class Edge {
var from: Node // does not actually need to be stored!
var to: Node
var weight: Int
var description : String {
return "{ Edge, from: \(from.identifier), to: \(to.identifier), weight: \(weight) }"
}
init(to: Node, from: Node, weight: Int) {
self.to = to
self.weight = weight
self.from = from
}
}
class Node : CustomStringConvertible {
// unique identifier required for each node
var identifier : Int
var distance : Int = Int.max
var edges = [Edge]()
var visited = false

var description: String {
let edgesString = edges.map { $0.description }.joined(separator: ", ")
return "{ Node, identifier: \(identifier), Edges: [\(edgesString)] }"
}

init(visited: Bool, identifier: Int, edges: [Edge]) {
self.visited = visited
self.identifier = identifier
self.edges = edges
}

static func == (lhs: Node, rhs: Node) -> Bool {
return lhs.identifier == rhs.identifier
}
}

let rootNode = Node(visited: false, identifier: 10, edges: [])
var edges: [Edge] = []
for i in 0..<3 {
let node = Node(visited: false, identifier: i, edges: [])
let edge = Edge(to: node, from: rootNode, weight: i)
edges.append(edge)
}
rootNode.edges = edges

print(rootNode)
// { Node, identifier: 10, Edges: [{ Edge, from: 10, to: 0, weight: 0 }, { Edge, from: 10, to: 1, weight: 1 }, { Edge, from: 10, to: 2, weight: 2 }] }

如果你想搜索和打印所有节点,你最好再做一个函数来做。

它应该记住你已经访问过哪个节点(或者如果可以的话,使用visited成员),尽量不要访问那些访问过的节点。

关于具有复杂类型的 Swift CustomStringConvertible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54394405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com