@@ -143,8 +143,8 @@ extension UniqueElementsGraph where E == UnweightedEdge {
143
143
///
144
144
/// - Parameter recursion: A function that returns the neighbouring vertices for a given visited vertex.
145
145
/// - 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)
148
148
}
149
149
150
150
/// 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 {
154
154
/// - Parameter recursion: A function that returns the neighbouring elements for a given visited element.
155
155
/// - Parameter vertexFor: A function that returns the vertex that will be added to the graph for each visited element.
156
156
/// - 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 : [ ] )
159
159
160
160
let queue = Queue < QueueElement < T > > ( )
161
161
162
- vertices. append ( vertexFor ( initialElement) )
163
- edges. append ( [ E] ( ) )
162
+ g . vertices. append ( vertexFor ( initialElement) )
163
+ g . edges. append ( [ E] ( ) )
164
164
recursion ( initialElement) . forEach { v in
165
165
queue. push ( QueueElement ( v: v, previousIndex: 0 ) )
166
166
}
@@ -169,20 +169,22 @@ extension UniqueElementsGraph where E == UnweightedEdge {
169
169
let element = queue. pop ( )
170
170
let ( e, previousIndex) = ( element. v, element. previousIndex)
171
171
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] ( ) )
175
175
176
- let uIndex = vertices. count - 1
176
+ let uIndex = g . vertices. count - 1
177
177
178
178
recursion ( e) . forEach { v in
179
179
queue. push ( QueueElement ( v: v, previousIndex: uIndex) )
180
180
}
181
181
return uIndex
182
- } ( )
182
+ } ( )
183
183
184
- addEdge ( fromIndex: previousIndex, toIndex: uIndex, directed: true )
184
+ g . addEdge ( fromIndex: previousIndex, toIndex: uIndex, directed: true )
185
185
}
186
+
187
+ return g
186
188
}
187
189
}
188
190
0 commit comments