Skip to content

Commit cab9a34

Browse files
committed
address review comments
1 parent 7645d98 commit cab9a34

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/utils.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ where
2020
}
2121

2222
/// Union two BTreeMaps, call f to resolve conflicts if duplicate keys are encountered.
23-
pub fn union_b_tree_maps_with<K: Clone + Ord, V: Clone, F: Fn(&V, &V) -> V>(
23+
pub fn union_btree_maps_with<K: Clone + Ord, V: Clone, F: Fn(V, V) -> V>(
2424
f: F,
25-
l: &BTreeMap<K, V>,
26-
r: &BTreeMap<K, V>,
25+
l: BTreeMap<K, V>,
26+
r: BTreeMap<K, V>,
2727
) -> BTreeMap<K, V> {
2828
r.into_iter().fold(l.clone(), |mut acc, (k, vr)| {
29-
acc.entry(k.clone())
30-
.and_modify(|vl| *vl = f(&vl, vr))
31-
.or_insert(vr.clone());
32-
29+
let v = if let Some((_, vl)) = acc.remove_entry(&k) {
30+
f(vl, vr)
31+
} else {
32+
vr
33+
};
34+
acc.insert(k, v);
3335
acc
3436
})
3537
}

src/v1/value/value_utils.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{singleton, union_b_tree_maps_with};
1+
use crate::utils::{singleton, union_btree_maps_with};
22
use num_bigint::BigInt;
33
use num_traits::Zero;
44
use std::{
@@ -134,10 +134,10 @@ impl Add<&Value> for &Value {
134134
type Output = Value;
135135

136136
fn add(self, rhs: &Value) -> Self::Output {
137-
Value(union_b_tree_maps_with(
138-
|lhs, rhs| union_b_tree_maps_with(|lhs, rhs| lhs + rhs, lhs, rhs),
139-
&self.0,
140-
&rhs.0,
137+
Value(union_btree_maps_with(
138+
|lhs, rhs| union_btree_maps_with(|lhs, rhs| lhs + rhs, lhs.clone(), rhs.clone()),
139+
self.0.clone(),
140+
rhs.0.clone(),
141141
))
142142
}
143143
}

0 commit comments

Comments
 (0)