Skip to content

Commit 31e4665

Browse files
committed
docs && simplify union_b_tree_maps_with
1 parent 23db7ef commit 31e4665

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/utils.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ where
1919
empty::<T>().collect()
2020
}
2121

22-
pub fn union_b_tree_maps_with<const N: usize, K: Clone + Ord, V: Clone, F: Fn(&V, &V) -> V>(
22+
/// 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>(
2324
f: F,
24-
maps: [&BTreeMap<K, V>; N],
25+
l: &BTreeMap<K, V>,
26+
r: &BTreeMap<K, V>,
2527
) -> BTreeMap<K, V> {
26-
maps.into_iter().fold(BTreeMap::new(), |acc, m| {
27-
m.into_iter().fold(acc, |mut acc, (k, v)| {
28-
acc.entry(k.clone())
29-
.and_modify(|va: &mut V| *va = f(va, v))
30-
.or_insert(v.clone());
28+
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());
3132

32-
acc
33-
})
33+
acc
3434
})
3535
}

src/v1/value/value_utils.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,33 @@ use std::{
99
use super::{CurrencySymbol, TokenName, Value};
1010

1111
impl Value {
12+
/// Create a Value containing only ada tokens, given the quantity in lovelace.
1213
pub fn ada_value(amount: &BigInt) -> Self {
1314
Self::token_value(&CurrencySymbol::Ada, &TokenName::ada(), amount)
1415
}
1516

17+
/// Create a Value containing only the given quantity of the given token.
1618
pub fn token_value(cs: &CurrencySymbol, tn: &TokenName, amount: &BigInt) -> Self {
1719
Value(singleton((
1820
cs.clone(),
1921
singleton((tn.clone(), amount.clone())),
2022
)))
2123
}
2224

25+
/// Lookup the quantity of the given token.
2326
pub fn get_token_amount(&self, cs: &CurrencySymbol, tn: &TokenName) -> BigInt {
2427
self.0
2528
.get(cs)
2629
.and_then(|tn_map| tn_map.get(&tn))
2730
.map_or(BigInt::zero(), Clone::clone)
2831
}
2932

33+
/// Lookup the quantity of ada(unit: lovelace).
3034
pub fn get_ada_amount(&self) -> BigInt {
3135
self.get_token_amount(&CurrencySymbol::Ada, &TokenName::ada())
3236
}
3337

38+
/// Insert a new token into the value, or replace the existing quantity.
3439
pub fn insert_token(&self, cs: &CurrencySymbol, tn: &TokenName, a: &BigInt) -> Self {
3540
let mut result_map = self.0.clone();
3641

@@ -49,21 +54,29 @@ impl Value {
4954
Self(result_map)
5055
}
5156

57+
/// Return true if the value don't have any entries.
5258
pub fn is_empty(&self) -> bool {
5359
self.0.is_empty()
5460
}
5561

62+
/// Remove all tokens whose quantity is zero.
5663
pub fn normalize(&self) -> Self {
5764
self.filter_map_amount(|_, _, a| a.is_zero().not().then(|| a.clone()))
5865
}
5966

67+
/// Apply a function to each token of the value, and use its result as the new amount.
6068
pub fn map_amount<F>(&self, mut f: F) -> Self
6169
where
6270
F: FnMut(&CurrencySymbol, &TokenName, &BigInt) -> BigInt,
6371
{
6472
self.filter_map_amount(|cs, tn, a| Some(f(cs, tn, a)))
6573
}
6674

75+
/// Apply a function to each token of the value. If the result is None, the token entry will be
76+
/// removed.
77+
///
78+
/// Note that if the name-quantity map of any given currency symbols is empty, the currency entry
79+
/// will be removed from the top-level map entirely.
6780
pub fn filter_map_amount<F>(&self, mut f: F) -> Self
6881
where
6982
F: FnMut(&CurrencySymbol, &TokenName, &BigInt) -> Option<BigInt>,
@@ -113,8 +126,9 @@ impl Add<&Value> for &Value {
113126

114127
fn add(self, rhs: &Value) -> Self::Output {
115128
Value(union_b_tree_maps_with(
116-
|lhs, rhs| union_b_tree_maps_with(|lhs, rhs| lhs + rhs, [lhs, rhs]),
117-
[&self.0, &rhs.0],
129+
|lhs, rhs| union_b_tree_maps_with(|lhs, rhs| lhs + rhs, lhs, rhs),
130+
&self.0,
131+
&rhs.0,
118132
))
119133
}
120134
}
@@ -128,8 +142,9 @@ impl Sub<&Value> for &Value {
128142

129143
fn sub(self, rhs: &Value) -> Self::Output {
130144
Value(union_b_tree_maps_with(
131-
|lhs, rhs| union_b_tree_maps_with(|lhs, rhs| lhs - rhs, [lhs, rhs]),
132-
[&self.0, &rhs.0],
145+
|lhs, rhs| union_b_tree_maps_with(|lhs, rhs| lhs - rhs, lhs, rhs),
146+
&self.0,
147+
&rhs.0,
133148
))
134149
}
135150
}

0 commit comments

Comments
 (0)