@@ -682,7 +682,8 @@ function Base.iterate(forest::PartitionForestIterator, state)
682
682
# and `edge_set`.
683
683
deleteat! (level_sequence, subtree_root_index: subtree_last_index)
684
684
deleteat! (edge_set, subtree_root_index- 1 : subtree_last_index- 1 )
685
- edge_to_remove = findlast (== (false ), edge_set)
685
+
686
+ edge_to_remove = findprev (== (false ), edge_set, edge_to_remove - 1 )
686
687
if edge_to_remove === nothing
687
688
edge_to_remove = typemin (Int)
688
689
end
@@ -755,7 +756,7 @@ function partition_skeleton!(level_sequence, edge_set)
755
756
deleteat! (level_sequence, subtree_root_index)
756
757
deleteat! (edge_set, edge_to_contract)
757
758
758
- edge_to_contract = findlast (edge_set)
759
+ edge_to_contract = findprev (edge_set, edge_to_contract - 1 )
759
760
end
760
761
761
762
# The level sequence `level_sequence` will not automatically be a canonical
@@ -788,14 +789,27 @@ function all_partitions(t::RootedTree)
788
789
skeletons = [partition_skeleton (t, edge_set)]
789
790
790
791
for edge_set_value in 1 : (2 ^ length (edge_set) - 1 )
791
- digits ! (edge_set, edge_set_value, base = 2 )
792
+ binary_digits ! (edge_set, edge_set_value)
792
793
push! (forests, partition_forest (t, edge_set))
793
794
push! (skeletons, partition_skeleton (t, edge_set))
794
795
end
795
796
796
797
return (; forests, skeletons)
797
798
end
798
799
800
+ # A helper function to comute the binary representation of an integer `n` as
801
+ # a vector of `Bool`s. This is a more efficient version of
802
+ # binary_digits!(digits, n) = digits!(digits, n, base=2)
803
+ function binary_digits! (digits:: Vector{Bool} , n:: Int )
804
+ bit = 1
805
+ for i in eachindex (digits)
806
+ digits[i] = n & bit > 0
807
+ bit = bit << 1
808
+ end
809
+ digits
810
+ end
811
+
812
+
799
813
800
814
"""
801
815
PartitionIterator(t::RootedTree)
@@ -855,7 +869,7 @@ function Base.iterate(partitions::PartitionIterator, edge_set_value)
855
869
edge_set = partitions. edge_set
856
870
edge_set_tmp = partitions. edge_set_tmp
857
871
858
- digits ! (edge_set, edge_set_value, base = 2 )
872
+ binary_digits ! (edge_set, edge_set_value)
859
873
860
874
# Compute the partition skeleton.
861
875
# The following is a more efficient version of
@@ -918,7 +932,7 @@ function all_splittings(t::RootedTree)
918
932
subtrees = Vector {RootedTree{T, Vector{T}}} () # ordered subtrees
919
933
920
934
for node_set_value in 0 : (2 ^ order (t) - 1 )
921
- digits ! (node_set, node_set_value, base = 2 )
935
+ binary_digits ! (node_set, node_set_value)
922
936
923
937
# Check that if a node is removed then all of its descendants are removed
924
938
subtree_root_index = 1
@@ -1000,7 +1014,7 @@ function Base.iterate(splittings::SplittingIterator, node_set_value)
1000
1014
forest = Vector {RootedTree{T, Vector{T}}} ()
1001
1015
1002
1016
while node_set_value <= splittings. max_node_set_value
1003
- digits ! (node_set, node_set_value, base = 2 )
1017
+ binary_digits ! (node_set, node_set_value)
1004
1018
1005
1019
# Check that if a node is removed then all of its descendants are removed
1006
1020
subtree_root_index = 1
0 commit comments