@@ -227,11 +227,7 @@ Apply a function to each node in a tree.
227
227
"""
228
228
function foreach (f:: Function , tree:: AbstractNode ; break_sharing:: Val = Val (false ))
229
229
tree_mapreduce (
230
- t -> (@inline (f (t)); nothing ),
231
- Returns (nothing ),
232
- tree,
233
- Nothing;
234
- break_sharing,
230
+ t -> (@inline (f (t)); nothing ), Returns (nothing ), tree, Nothing; break_sharing
235
231
)
236
232
return nothing
237
233
end
@@ -245,8 +241,11 @@ specifying the `result_type` of `map_fnc` so the resultant array can
245
241
be preallocated.
246
242
"""
247
243
function filter_map (
248
- filter_fnc:: F , map_fnc:: G , tree:: AbstractNode , result_type:: Type{GT} ;
249
- break_sharing:: Val = Val (false )
244
+ filter_fnc:: F ,
245
+ map_fnc:: G ,
246
+ tree:: AbstractNode ,
247
+ result_type:: Type{GT} ;
248
+ break_sharing:: Val = Val (false ),
250
249
) where {F<: Function ,G<: Function ,GT}
251
250
stack = Array {GT} (undef, count (filter_fnc, tree; init= 0 , break_sharing))
252
251
filter_map! (filter_fnc, map_fnc, stack, tree; break_sharing)
259
258
Equivalent to `filter_map`, but stores the results in a preallocated array.
260
259
"""
261
260
function filter_map! (
262
- filter_fnc:: Function , map_fnc:: Function , destination:: Vector{GT} , tree:: AbstractNode ;
263
- break_sharing:: Val = Val (false )
261
+ filter_fnc:: Function ,
262
+ map_fnc:: Function ,
263
+ destination:: Vector{GT} ,
264
+ tree:: AbstractNode ;
265
+ break_sharing:: Val = Val (false ),
264
266
) where {GT}
265
267
pointer = Ref (0 )
266
268
foreach (tree; break_sharing) do t
@@ -291,15 +293,19 @@ end
291
293
Map a function over a tree and return a flat array of the results in depth-first order.
292
294
Pre-specifying the `result_type` of the function can be used to avoid extra allocations.
293
295
"""
294
- function map (f:: F , tree:: AbstractNode , result_type:: Type{RT} = Nothing; break_sharing:: Val = Val (false )) where {F<: Function ,RT}
296
+ function map (
297
+ f:: F , tree:: AbstractNode , result_type:: Type{RT} = Nothing; break_sharing:: Val = Val (false )
298
+ ) where {F<: Function ,RT}
295
299
if RT == Nothing
296
300
return f .(collect (tree; break_sharing))
297
301
else
298
302
return filter_map (Returns (true ), f, tree, result_type; break_sharing)
299
303
end
300
304
end
301
305
302
- function count (f:: F , tree:: AbstractNode ; init= 0 , break_sharing:: Val = Val (false )) where {F<: Function }
306
+ function count (
307
+ f:: F , tree:: AbstractNode ; init= 0 , break_sharing:: Val = Val (false )
308
+ ) where {F<: Function }
303
309
return tree_mapreduce (
304
310
t -> @inline (f (t)) ? 1 : 0 ,
305
311
+ ,
@@ -348,11 +354,15 @@ function mapreduce(
348
354
if preserve_sharing (typeof (tree))
349
355
@assert typeof (return_type) != = Undefined " Must specify `return_type` as a keyword argument to `mapreduce` if `preserve_sharing` is true."
350
356
end
351
- return tree_mapreduce (f, (n... ) -> reduce (op, n), tree, return_type; f_on_shared, break_sharing)
357
+ return tree_mapreduce (
358
+ f, (n... ) -> reduce (op, n), tree, return_type; f_on_shared, break_sharing
359
+ )
352
360
end
353
361
354
362
isempty (:: AbstractNode ) = false
355
- iterate (root:: AbstractNode ) = (root, collect (root; break_sharing= Val (true ))[(begin + 1 ): end ])
363
+ function iterate (root:: AbstractNode )
364
+ return (root, collect (root; break_sharing= Val (true ))[(begin + 1 ): end ])
365
+ end
356
366
iterate (:: AbstractNode , stack) = isempty (stack) ? nothing : (popfirst! (stack), stack)
357
367
in (item, tree:: AbstractNode ) = any (t -> t == item, tree)
358
368
function length (tree:: AbstractNode ; break_sharing:: Val = Val (false ))
0 commit comments