@@ -21,6 +21,7 @@ use crate::{
2121/// The rule body is divided into multiple independent factors.
2222/// Those can either be [BodyAtom]s
2323/// or [Operation]s assigning constants to a variable.
24+ #[ derive( Debug ) ]
2425enum Factor {
2526 /// Factor consisting of body atoms
2627 Atoms ( Vec < BodyAtom > ) ,
@@ -107,7 +108,7 @@ impl GeneratorJoinCartesian {
107108 /// atoms: [a(x, y), b(y, z), c(z), r(w), t(w)]
108109 /// operations: [q = 3, s = 7]
109110 /// result: [[a(x, y), b(y, z), c(z)], [r(w), t(w)], [q = 3, s = 7]]
110- fn partition_atoms ( atoms : & [ BodyAtom ] , operations : & [ Operation ] ) -> Vec < Factor > {
111+ fn partition_atoms ( atoms : & [ BodyAtom ] , operations : & mut Vec < Operation > ) -> Vec < Factor > {
111112 #[ derive( Clone ) ]
112113 struct Partition {
113114 pub variables : HashSet < Variable > ,
@@ -207,26 +208,26 @@ impl GeneratorJoinCartesian {
207208 . flat_map ( |atom| atom. terms ( ) )
208209 . collect :: < HashSet < _ > > ( ) ;
209210
210- let mut constant_operations = HashSet :: < usize > :: default ( ) ;
211+ let mut constant_operations = Vec :: < Operation > :: default ( ) ;
211212 let mut constant_operations_len = constant_operations. len ( ) ;
212213 let mut constant_operations_variables = HashSet :: < Variable > :: default ( ) ;
213214
214215 loop {
215- for ( index, operation) in operations. iter ( ) . enumerate ( ) {
216- if constant_operations. contains ( & index) {
217- continue ;
218- }
219-
216+ operations. retain ( |operation| {
220217 if let Some ( ( left, right) ) = operation. variable_assignment ( )
221218 && !body_variables. contains ( left)
222219 && right
223220 . variables ( )
224221 . all ( |variable| constant_operations_variables. contains ( variable) )
225222 {
226- constant_operations. insert ( index ) ;
223+ constant_operations. push ( operation . clone ( ) ) ;
227224 constant_operations_variables. insert ( left. clone ( ) ) ;
225+
226+ return false ;
228227 }
229- }
228+
229+ true
230+ } ) ;
230231
231232 if constant_operations_len == constant_operations. len ( ) {
232233 break ;
@@ -235,11 +236,6 @@ impl GeneratorJoinCartesian {
235236 constant_operations_len = constant_operations. len ( ) ;
236237 }
237238
238- let constant_operations = constant_operations
239- . iter ( )
240- . map ( |& index| operations[ index] . clone ( ) )
241- . collect :: < Vec < _ > > ( ) ;
242-
243239 if !constant_operations. is_empty ( ) {
244240 result. push ( Factor :: Operations ( constant_operations) ) ;
245241 }
0 commit comments