@@ -632,37 +632,28 @@ Base.length(forest::PartitionForestIterator) = count(==(false), forest.edge_set)
632
632
Base. eltype (:: Type{PartitionForestIterator{T, V, Tree}} ) where {T, V, Tree} = Tree
633
633
634
634
function Base. iterate (forest:: PartitionForestIterator )
635
- edge_to_remove = findlast (== (false ), forest. edge_set)
636
- # If `edge_to_remove` can only be inferred as `Union{Nothing, Int}`, the
637
- # return value of `iterate` can only be inferred as a double union, which
638
- # is too much for the compiler right now and introduces type instabilities.
639
- # Thus, we set `edge_to_remove` to a sensible value indicating that there
640
- # is no edge to remove.
641
- if edge_to_remove === nothing
642
- edge_to_remove = typemin (Int)
643
- end
644
- finished = false
645
- iterate (forest, (edge_to_remove, finished))
635
+ iterate (forest, lastindex (forest. edge_set))
646
636
end
647
637
648
- function Base. iterate (forest:: PartitionForestIterator , state )
638
+ function Base. iterate (forest:: PartitionForestIterator , search_start )
649
639
t = forest. t
650
640
edge_set = forest. edge_set
651
641
level_sequence = forest. level_sequence
652
- edge_to_remove, finished = state
653
642
654
- # We have already returned the final tree.
655
- if finished
643
+ # We use `search_start = typemin(Int)` to indicate that we have already
644
+ # returned the final tree in the previous call.
645
+ if search_start == typemin (Int)
656
646
return nothing
657
647
end
658
648
649
+ edge_to_remove = findprev (== (false ), edge_set, search_start)
650
+
659
651
# There are no further edges to remove and we can return the final tree.
660
- if edge_to_remove == typemin (Int)
652
+ if edge_to_remove === nothing
661
653
resize! (t. level_sequence, length (level_sequence))
662
654
copy! (t. level_sequence, level_sequence)
663
655
canonical_representation! (t)
664
- finished = true
665
- return (t, (edge_to_remove, finished))
656
+ return (t, typemin (Int))
666
657
end
667
658
668
659
# On to the next subtree
@@ -683,12 +674,7 @@ function Base.iterate(forest::PartitionForestIterator, state)
683
674
deleteat! (level_sequence, subtree_root_index: subtree_last_index)
684
675
deleteat! (edge_set, subtree_root_index- 1 : subtree_last_index- 1 )
685
676
686
- edge_to_remove = findprev (== (false ), edge_set, edge_to_remove - 1 )
687
- if edge_to_remove === nothing
688
- edge_to_remove = typemin (Int)
689
- end
690
- finished = false
691
- return (t, (edge_to_remove, finished))
677
+ return (t, edge_to_remove - 1 )
692
678
end
693
679
694
680
# necessary for simple and convenient use since the iterates may be modified
0 commit comments