@@ -115,17 +115,47 @@ Algorithm::result_t sort_product::apply(iterator& st)
115115 }
116116 }
117117 if (found) {
118+ // Construct all cyclic orderings
119+ std::vector<std::vector<Ex::sibling_iterator>> candidates;
118120 one=tr.begin (st);
119- two=one;
120- ++two;
121- while (two!=tr.end (st)) {
122- compare.clear ();
123- auto es=compare.equal_subtree (one, two);
124- if (compare.should_swap (one, es)) one=two;
121+ while (one!=tr.end (st)) {
122+ two=one;
125123 ++two;
124+ if (two==tr.end (st)) two=tr.begin (st);
125+ std::vector<Ex::sibling_iterator> candidate (1 , one);
126+ while (two!=one) {
127+ candidate.push_back (two);
128+ ++two;
129+ if (two==tr.end (st)) two=tr.begin (st);
130+ }
131+ candidates.push_back (candidate);
132+ ++one;
133+ }
134+ // Narrow them down by comparing first digit, then second digit, ...
135+ unsigned int digit=1 ;
136+ while (candidates.size ()>1 && digit<=num) {
137+ std::vector<std::vector<Ex::sibling_iterator>>::iterator candidate=candidates.begin ();
138+ one=candidate->at (digit-1 );
139+ ++candidate;
140+ while (candidate!=candidates.end ()) {
141+ two=candidate->at (digit-1 );
142+ compare.clear ();
143+ auto es=compare.equal_subtree (one, two);
144+ if (es==Ex_comparator::match_t ::no_match_greater || es==Ex_comparator::match_t ::match_index_greater) {
145+ --candidate;
146+ candidate=candidates.erase (candidate);
147+ one=candidate->at (digit-1 );
148+ ++candidate;
149+ }
150+ else if (es==Ex_comparator::match_t ::no_match_less || es==Ex_comparator::match_t ::match_index_less) {
151+ candidate=candidates.erase (candidate);
152+ }
153+ else ++candidate;
154+ }
155+ ++digit;
126156 }
127- // We have found the element that should go at the front of the trace
128- Ex::sibling_iterator front=one ;
157+ // Use the first ordering but keep track of signs this time
158+ Ex::sibling_iterator front=candidates. at ( 0 ). at ( 0 ) ;
129159 if (front!=tr.begin (st)) {
130160 while (tr.begin (st)!=front) {
131161 one=tr.begin (st);
0 commit comments