Skip to main content

f1r3fly_models/rust/
sorted_par_hash_set.rs

1// See models/src/main/scala/coop/rchain/models/SortedParHashSet.scala
2
3use std::collections::HashSet;
4
5use crate::rhoapi::Par;
6
7use super::rholang::sorter::{
8    ordering::Ordering, par_sort_matcher::ParSortMatcher, sortable::Sortable,
9};
10
11// Enforce ordering and uniqueness.
12// - uniqueness is handled by using HashSet.
13// - ordering comes from sorting the elements prior to serializing.
14#[derive(Clone)]
15pub struct SortedParHashSet {
16    pub ps: HashSet<Par>,
17    pub sorted_pars: Vec<Par>,
18    pub sorted_ps: HashSet<Par>,
19}
20
21impl SortedParHashSet {
22    pub fn create_from_vec(vec: Vec<Par>) -> Self {
23        let set: HashSet<Par> = vec.clone().into_iter().collect();
24        let sorted_pars = Ordering::sort_pars(&set.clone().into_iter().collect());
25        let sorted_ps: HashSet<Par> = sorted_pars.clone().into_iter().collect();
26
27        // println!("\nsorted_ps in SortedParHashSet: {:?}", sorted_ps);
28
29        SortedParHashSet {
30            ps: set,
31            sorted_pars,
32            sorted_ps,
33        }
34    }
35
36    pub fn create_from_set(set: HashSet<Par>) -> Self {
37        let vec = set.into_iter().collect();
38        SortedParHashSet::create_from_vec(vec)
39    }
40
41    pub fn create_from_empty() -> Self {
42        SortedParHashSet::create_from_set(HashSet::new())
43    }
44
45    // alias for '+'
46    pub fn insert(&mut self, elem: Par) -> SortedParHashSet {
47        self.ps.insert(Self::sort(&elem));
48        Self::create_from_set(self.ps.clone())
49    }
50
51    // alias for '-'
52    pub fn remove(&mut self, elem: Par) -> SortedParHashSet {
53        self.ps.remove(&Self::sort(&elem));
54        Self::create_from_set(self.ps.clone())
55    }
56
57    pub fn contains(&self, elem: Par) -> bool {
58        self.sorted_ps.contains(&Self::sort(&elem))
59    }
60
61    pub fn union(&self, that: HashSet<Par>) -> SortedParHashSet {
62        SortedParHashSet::create_from_set(
63            self.sorted_ps
64                .union(&that.iter().map(Self::sort).collect())
65                .cloned()
66                .collect(),
67        )
68    }
69
70    pub fn equals(&self, that: SortedParHashSet) -> bool {
71        self.sorted_pars == that.sorted_pars
72    }
73
74    pub fn length(&self) -> usize {
75        self.sorted_ps.len()
76    }
77
78    pub fn sort(par: &Par) -> Par {
79        ParSortMatcher::sort_match(&par).term.clone()
80    }
81}
82
83impl PartialEq for SortedParHashSet {
84    fn eq(&self, other: &Self) -> bool {
85        self.sorted_pars == other.sorted_pars
86    }
87}
88
89use std::fmt;
90
91impl fmt::Debug for SortedParHashSet {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        f.debug_struct("SortedParHashSet")
94            .field("ps", &self.ps)
95            .field("sorted_pars", &self.sorted_pars)
96            .field("sorted_ps", &self.sorted_ps)
97            .finish()
98    }
99}