Skip to content

Commit

Permalink
Switch to binary search for large additions
Browse files Browse the repository at this point in the history
  • Loading branch information
benruijl committed Oct 11, 2024
1 parent 2439ef5 commit 783e567
Showing 1 changed file with 58 additions and 20 deletions.
78 changes: 58 additions & 20 deletions src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,21 +1420,53 @@ impl<'a> AtomView<'a> {
return;
}

let mut found = false;
for x in a1.iter() {
// TODO: find the position of rhs in self with a binary search
if found {
a.extend(x);
continue;
}

match x.cmp_terms(&rhs) {
Ordering::Less => {
if a1.get_nargs() < 50 {
let mut found = false;
for x in a1.iter() {
if found {
a.extend(x);
continue;
}

match x.cmp_terms(&rhs) {
Ordering::Less => {
a.extend(x);
}
Ordering::Equal => {
found = true;
b.set_from_view(&x);
if b.merge_terms(rhs, &mut helper) {
if let AtomView::Num(n) = a.as_view() {
if !n.is_zero() {
a.extend(b.as_view());
}
} else {
a.extend(b.as_view());
}
} else {
unreachable!("Equal terms do not merge");
}
}
Ordering::Greater => {
found = true;
a.extend(rhs);
a.extend(x);
}
}
Ordering::Equal => {
found = true;
b.set_from_view(&x);
}

if !found {
a.extend(rhs);
}
} else {
let v: Vec<_> = a1.iter().collect();
match v.binary_search_by(|a| a.cmp_terms(&rhs)) {
Ok(p) => {
for x in v.iter().take(p) {
a.extend(*x);
}

b.set_from_view(&v[p]);
if b.merge_terms(rhs, &mut helper) {
if let AtomView::Num(n) = a.as_view() {
if !n.is_zero() {
Expand All @@ -1446,19 +1478,25 @@ impl<'a> AtomView<'a> {
} else {
unreachable!("Equal terms do not merge");
}

for x in v.iter().skip(p + 1) {
a.extend(*x);
}
}
Ordering::Greater => {
found = true;
Err(p) => {
for x in v.iter().take(p) {
a.extend(*x);
}

a.extend(rhs);
a.extend(x);

for x in v.iter().skip(p) {
a.extend(*x);
}
}
}
}

if !found {
a.extend(rhs);
}

a.set_normalized(true);
} else if let AtomView::Add(_) = rhs {
rhs.add_normalized(*self, ws, out);
Expand Down

0 comments on commit 783e567

Please sign in to comment.