@@ -85,6 +85,46 @@ def sort_classes(self) -> None:
8585 """
8686 Sort the class info collection in order of dependence.
8787 """
88+ cache = dict ()
89+
90+ def compare (a : CppClassInfo , b : CppClassInfo ) -> int :
91+ """
92+ Compare two class info objects for dependence order.
93+
94+ Parameters
95+ ----------
96+ a : CppClassInfo
97+ b : CppClassInfo
98+
99+ Returns
100+ -------
101+ int
102+ -1 if a comes before b (b depends on a)
103+ 0 if there is no dependence
104+ 1 if a comes after b (a depends on b)
105+ """
106+ order = cache .get ((a , b ), None )
107+ if order is not None :
108+ return order
109+
110+ a_req_b = a .requires (b )
111+ b_req_a = b .requires (a )
112+ if a .is_child_of (b ) or (a_req_b and not b_req_a ):
113+ # a comes after b (ignore cyclic dependencies)
114+ cache [(a , b )] = 1
115+ cache [(b , a )] = - 1
116+ return 1
117+ elif b .is_child_of (a ) or (b_req_a and not a_req_b ):
118+ # a comes before b (ignore cyclic dependencies)
119+ cache [(a , b )] = - 1
120+ cache [(b , a )] = 1
121+ return - 1
122+
123+ # Order doesn't matter
124+ cache [(a , b )] = 0
125+ cache [(b , a )] = 0
126+ return 0
127+
88128 self .class_info_collection .sort (key = lambda x : x .name )
89129
90130 i = 0
@@ -96,13 +136,11 @@ def sort_classes(self) -> None:
96136
97137 for j in range (i + 1 , n ):
98138 cls_j = self .class_info_collection [j ]
99- i_req_j = cls_i .requires (cls_j )
100- j_req_i = cls_j .requires (cls_i )
101- if cls_i .is_child_of (cls_j ) or (i_req_j and not j_req_i ):
102- # Position cls_i after all classes it depends on,
103- # ignoring forward declaration cycles
139+ order = compare (cls_i , cls_j )
140+ if order == 1 :
141+ # Position cls_i after all classes it depends on
104142 ii = j
105- elif cls_j . is_child_of ( cls_i ) or ( j_req_i and not i_req_j ) :
143+ elif order == - 1 :
106144 # Collect positions of cls_i's dependents
107145 j_pos .append (j )
108146
0 commit comments