Skip to content

Commit 9024eb8

Browse files
Convert inits to static functions
1 parent 91f03f4 commit 9024eb8

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Sources/SwiftGraph/UniqueElementsGraph.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ extension UniqueElementsGraph where E == UnweightedEdge {
143143
///
144144
/// - Parameter recursion: A function that returns the neighbouring vertices for a given visited vertex.
145145
/// - Parameter initialVertex: The first vertex to which the recursion function is applied.
146-
public convenience init(fromRecursion recursion: (V) -> [V], startingWith initialVertex: V) {
147-
self.init(fromRecursion: recursion, selectingVertex: { $0 }, startingWith: initialVertex)
146+
public static func fromRecursion(_ recursion: (V) -> [V], startingWith initialVertex: V) -> UniqueElementsGraph {
147+
return self.fromRecursion(recursion, selectingVertex: { $0 }, startingWith: initialVertex)
148148
}
149149

150150
/// Construct a UniqueElementsGraph by repeatedly applying a recursion function to some elements and adding the corresponding vertex to the graph.
@@ -154,13 +154,13 @@ extension UniqueElementsGraph where E == UnweightedEdge {
154154
/// - Parameter recursion: A function that returns the neighbouring elements for a given visited element.
155155
/// - Parameter vertexFor: A function that returns the vertex that will be added to the graph for each visited element.
156156
/// - Parameter initialElement: The first element to which the recursion function is applied.
157-
public convenience init<T>(fromRecursion recursion: (T) -> [T], selectingVertex vertexFor: (T) -> V, startingWith initialElement: T) {
158-
self.init()
157+
public static func fromRecursion<T>(_ recursion: (T) -> [T], selectingVertex vertexFor: (T) -> V, startingWith initialElement: T) -> UniqueElementsGraph {
158+
let g = UniqueElementsGraph(vertices: [])
159159

160160
let queue = Queue<QueueElement<T>>()
161161

162-
vertices.append(vertexFor(initialElement))
163-
edges.append([E]())
162+
g.vertices.append(vertexFor(initialElement))
163+
g.edges.append([E]())
164164
recursion(initialElement).forEach { v in
165165
queue.push(QueueElement(v: v, previousIndex: 0))
166166
}
@@ -169,20 +169,22 @@ extension UniqueElementsGraph where E == UnweightedEdge {
169169
let element = queue.pop()
170170
let (e, previousIndex) = (element.v, element.previousIndex)
171171
let u = vertexFor(e)
172-
let uIndex = indexOfVertex(u) ?? {
173-
vertices.append(u)
174-
edges.append([E]())
172+
let uIndex = g.indexOfVertex(u) ?? {
173+
g.vertices.append(u)
174+
g.edges.append([E]())
175175

176-
let uIndex = vertices.count - 1
176+
let uIndex = g.vertices.count - 1
177177

178178
recursion(e).forEach { v in
179179
queue.push(QueueElement(v: v, previousIndex: uIndex))
180180
}
181181
return uIndex
182-
}()
182+
}()
183183

184-
addEdge(fromIndex: previousIndex, toIndex: uIndex, directed: true)
184+
g.addEdge(fromIndex: previousIndex, toIndex: uIndex, directed: true)
185185
}
186+
187+
return g
186188
}
187189
}
188190

Tests/SwiftGraphTests/UniqueElementsGraph/UniqueElementsGraphInitTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class UnweightedUniqueElementsGraphInitTests: XCTestCase {
228228
return structure[i] ?? []
229229
}
230230

231-
let g = UniqueElementsGraph(fromRecursion: next, startingWith: 0)
231+
let g = UniqueElementsGraph.fromRecursion(next, startingWith: 0)
232232
XCTAssertTrue(g.edgeExists(from: 0, to: 1))
233233
XCTAssertTrue(g.edgeExists(from: 0, to: 2))
234234
XCTAssertTrue(g.edgeExists(from: 0, to: 3))
@@ -280,7 +280,7 @@ class UnweightedUniqueElementsGraphInitTests: XCTestCase {
280280
)
281281
)
282282

283-
let g = UniqueElementsGraph(fromRecursion: next, selectingVertex: select, startingWith: tree)
283+
let g = UniqueElementsGraph.fromRecursion(next, selectingVertex: select, startingWith: tree)
284284
XCTAssertTrue(g.edgeExists(from: 0, to: 1))
285285
XCTAssertTrue(g.edgeExists(from: 0, to: 6))
286286

0 commit comments

Comments
 (0)