Skip to content

Commit 04b735d

Browse files
committed
Merge branch 'cbehan-patch7'
2 parents ba747e0 + e765eda commit 04b735d

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

core/algorithms/sort_product.cc

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

tests/implicit.cdb

100755100644
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,16 @@ def test16():
209209
print("Test 16 passed")
210210

211211
test16()
212-
212+
213+
def test17():
214+
__cdbkernel__=create_scope()
215+
{A,B}::ImplicitIndex.
216+
tr{#}::Trace.
217+
ex:=tr( A B B A );
218+
sort_product(_)
219+
tst:= tr( A A B B ) - @(ex);
220+
assert(tst==0)
221+
print("Test 17 passed")
222+
223+
test17()
224+

0 commit comments

Comments
 (0)