crdt: Relax bounds and take references
The getter-style functions don't need owned keys. Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
b015b29f34
commit
2230c8ddc4
|
|
@ -10,15 +10,15 @@ pub struct LWWMap<K, V, C> {
|
||||||
inner: BTreeMap<K, LWWReg<Option<V>, C>>,
|
inner: BTreeMap<K, LWWReg<Option<V>, C>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Ord, V: Semilattice + PartialOrd + Eq, C: PartialOrd + Ord> LWWMap<K, V, C> {
|
impl<K: Ord, V: Semilattice, C: PartialOrd + Ord> LWWMap<K, V, C> {
|
||||||
pub fn singleton(key: K, value: V, clock: C) -> Self {
|
pub fn singleton(key: K, value: V, clock: C) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: BTreeMap::from_iter([(key, LWWReg::new(Some(value), clock))]),
|
inner: BTreeMap::from_iter([(key, LWWReg::new(Some(value), clock))]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, key: K) -> Option<&V> {
|
pub fn get(&self, key: &K) -> Option<&V> {
|
||||||
let Some(value) = self.inner.get(&key) else {
|
let Some(value) = self.inner.get(key) else {
|
||||||
// If the element was never added, return nothing.
|
// If the element was never added, return nothing.
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
@ -47,8 +47,8 @@ impl<K: Ord, V: Semilattice + PartialOrd + Eq, C: PartialOrd + Ord> LWWMap<K, V,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn contains_key(&self, key: K) -> bool {
|
pub fn contains_key(&self, key: &K) -> bool {
|
||||||
let Some(value) = self.inner.get(&key) else {
|
let Some(value) = self.inner.get(key) else {
|
||||||
// If the element was never added, return false.
|
// If the element was never added, return false.
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
@ -144,9 +144,9 @@ mod tests {
|
||||||
map.insert('b', Max::from(2), 0);
|
map.insert('b', Max::from(2), 0);
|
||||||
map.insert('c', Max::from(3), 0);
|
map.insert('c', Max::from(3), 0);
|
||||||
|
|
||||||
assert_eq!(map.get('a'), Some(&Max::from(1)));
|
assert_eq!(map.get(&'a'), Some(&Max::from(1)));
|
||||||
assert_eq!(map.get('b'), Some(&Max::from(2)));
|
assert_eq!(map.get(&'b'), Some(&Max::from(2)));
|
||||||
assert_eq!(map.get('?'), None);
|
assert_eq!(map.get(&'?'), None);
|
||||||
|
|
||||||
let values = map.iter().collect::<Vec<(&char, &Max<u8>)>>();
|
let values = map.iter().collect::<Vec<(&char, &Max<u8>)>>();
|
||||||
assert!(values.contains(&(&'a', &Max::from(1))));
|
assert!(values.contains(&(&'a', &Max::from(1))));
|
||||||
|
|
@ -160,17 +160,17 @@ mod tests {
|
||||||
let mut map = LWWMap::default();
|
let mut map = LWWMap::default();
|
||||||
|
|
||||||
map.insert('a', Max::from("alice"), 1);
|
map.insert('a', Max::from("alice"), 1);
|
||||||
assert!(map.contains_key('a'));
|
assert!(map.contains_key(&'a'));
|
||||||
|
|
||||||
map.remove('a', 0);
|
map.remove('a', 0);
|
||||||
assert!(map.contains_key('a'));
|
assert!(map.contains_key(&'a'));
|
||||||
|
|
||||||
map.remove('a', 1);
|
map.remove('a', 1);
|
||||||
assert!(map.contains_key('a')); // Add takes precedence over remove.
|
assert!(map.contains_key(&'a')); // Add takes precedence over remove.
|
||||||
assert!(map.iter().any(|(c, _)| *c == 'a'));
|
assert!(map.iter().any(|(c, _)| *c == 'a'));
|
||||||
|
|
||||||
map.remove('a', 2);
|
map.remove('a', 2);
|
||||||
assert!(!map.contains_key('a'));
|
assert!(!map.contains_key(&'a'));
|
||||||
assert!(!map.iter().any(|(c, _)| *c == 'a'));
|
assert!(!map.iter().any(|(c, _)| *c == 'a'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,15 +191,15 @@ mod tests {
|
||||||
let mut map = LWWMap::default();
|
let mut map = LWWMap::default();
|
||||||
|
|
||||||
map.insert('a', Max::from("alice"), 1);
|
map.insert('a', Max::from("alice"), 1);
|
||||||
assert_eq!(map.get('a'), Some(&Max::from("alice")));
|
assert_eq!(map.get(&'a'), Some(&Max::from("alice")));
|
||||||
|
|
||||||
map.remove('a', 2);
|
map.remove('a', 2);
|
||||||
assert!(!map.contains_key('a'));
|
assert!(!map.contains_key(&'a'));
|
||||||
|
|
||||||
map.insert('a', Max::from("alice"), 1);
|
map.insert('a', Max::from("alice"), 1);
|
||||||
assert!(!map.contains_key('a'));
|
assert!(!map.contains_key(&'a'));
|
||||||
|
|
||||||
map.insert('a', Max::from("amy"), 2);
|
map.insert('a', Max::from("amy"), 2);
|
||||||
assert_eq!(map.get('a'), Some(&Max::from("amy")));
|
assert_eq!(map.get(&'a'), Some(&Max::from("amy")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ pub struct LWWReg<T, C = clock::Lamport> {
|
||||||
value: T,
|
value: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: PartialOrd + Semilattice, C: PartialOrd> LWWReg<T, C> {
|
impl<T: Semilattice, C: PartialOrd> LWWReg<T, C> {
|
||||||
pub fn new(value: T, clock: C) -> Self {
|
pub fn new(value: T, clock: C) -> Self {
|
||||||
Self {
|
Self {
|
||||||
clock: Max::from(clock),
|
clock: Max::from(clock),
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ impl<T: Ord, C: Ord> LWWSet<T, C> {
|
||||||
self.inner.remove(value, clock);
|
self.inner.remove(value, clock);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn contains(&self, value: T) -> bool {
|
pub fn contains(&self, value: &T) -> bool {
|
||||||
self.inner.contains_key(value)
|
self.inner.contains_key(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -101,9 +101,9 @@ mod tests {
|
||||||
set.insert('b', 0);
|
set.insert('b', 0);
|
||||||
set.insert('c', 0);
|
set.insert('c', 0);
|
||||||
|
|
||||||
assert!(set.contains('a'));
|
assert!(set.contains(&'a'));
|
||||||
assert!(set.contains('b'));
|
assert!(set.contains(&'b'));
|
||||||
assert!(!set.contains('?'));
|
assert!(!set.contains(&'?'));
|
||||||
|
|
||||||
let values = set.iter().cloned().collect::<Vec<_>>();
|
let values = set.iter().cloned().collect::<Vec<_>>();
|
||||||
assert!(values.contains(&'a'));
|
assert!(values.contains(&'a'));
|
||||||
|
|
@ -117,17 +117,17 @@ mod tests {
|
||||||
let mut set = LWWSet::default();
|
let mut set = LWWSet::default();
|
||||||
|
|
||||||
set.insert('a', 1);
|
set.insert('a', 1);
|
||||||
assert!(set.contains('a'));
|
assert!(set.contains(&'a'));
|
||||||
|
|
||||||
set.remove('a', 0);
|
set.remove('a', 0);
|
||||||
assert!(set.contains('a'));
|
assert!(set.contains(&'a'));
|
||||||
|
|
||||||
set.remove('a', 1);
|
set.remove('a', 1);
|
||||||
assert!(set.contains('a')); // Add takes precedence over remove.
|
assert!(set.contains(&'a')); // Add takes precedence over remove.
|
||||||
assert!(set.iter().any(|c| *c == 'a'));
|
assert!(set.iter().any(|c| *c == 'a'));
|
||||||
|
|
||||||
set.remove('a', 2);
|
set.remove('a', 2);
|
||||||
assert!(!set.contains('a'));
|
assert!(!set.contains(&'a'));
|
||||||
assert!(!set.iter().any(|c| *c == 'a'));
|
assert!(!set.iter().any(|c| *c == 'a'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,15 +136,15 @@ mod tests {
|
||||||
let mut set = LWWSet::default();
|
let mut set = LWWSet::default();
|
||||||
|
|
||||||
set.insert('a', 1);
|
set.insert('a', 1);
|
||||||
assert!(set.contains('a'));
|
assert!(set.contains(&'a'));
|
||||||
|
|
||||||
set.remove('a', 2);
|
set.remove('a', 2);
|
||||||
assert!(!set.contains('a'));
|
assert!(!set.contains(&'a'));
|
||||||
|
|
||||||
set.insert('a', 1);
|
set.insert('a', 1);
|
||||||
assert!(!set.contains('a'));
|
assert!(!set.contains(&'a'));
|
||||||
|
|
||||||
set.insert('a', 2);
|
set.insert('a', 2);
|
||||||
assert!(set.contains('a'));
|
assert!(set.contains(&'a'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue