-
Notifications
You must be signed in to change notification settings - Fork 0
/
aworset_opt.rs
76 lines (66 loc) · 2.02 KB
/
aworset_opt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use core::hash::Hash;
use std::{collections::{HashSet}, fmt::Display, mem::size_of};
use std::fmt::Debug;
use crate::{NodeId, DotContext};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AworsetOpt<E>
where
E: Eq + Display + Clone + Hash + Debug,
{
pub id: NodeId,
pub set: HashSet<(NodeId, E, i64)>,
pub cc: DotContext<NodeId> // Equivalent to the cc in aworset.rs
}
impl<E> AworsetOpt<E>
where
E: Eq + Display + Clone + Hash + Debug,
{
pub fn new(id: NodeId) -> Self {
Self {
id,
set: HashSet::new(),
cc: DotContext::new()
}
}
pub fn get_bytes_size(&self) -> usize {
let mut total_size = 0;
total_size += self.id.get_bytes_size();
total_size += self.cc.get_bytes_size();
for (nodeid, _, _) in self.set.iter(){
total_size += nodeid.get_bytes_size();
total_size += size_of::<E>();
total_size += size_of::<i64>();
}
total_size
}
pub fn elements(&self) -> HashSet<E>{
let mut res: HashSet<E> = HashSet::new();
for (_, e, _) in self.set.iter(){
res.insert(e.clone());
}
res
}
pub fn add(&mut self, element: E) {
let dot = self.cc.makedot(&self.id);
self.set.insert((dot.0, element, dot.1));
}
pub fn rm(&mut self, element: E) {
self.set= self.set.drain().filter(|(_, e, _) | {
if element == *e { return false; }
true
}).collect();
}
pub fn join(&mut self, other: &Self){
// Intersentions and elements not known by other.
self.set = self.set.drain().filter(|v|
other.set.contains(v) || !other.cc.dotin(&(v.0.clone(), v.2)))
.collect();
// Elements known by other, but not by self.
for entry in other.set.iter() {
if !self.cc.dotin(&(entry.0.clone(), entry.2)) {
self.set.insert(entry.clone());
}
}
self.cc.join(&other.cc);
}
}