Skip to main content

f1r3fly_models/rust/
par_map.rs

1// See models/src/main/scala/coop/rchain/models/ParMap.scala
2
3use crate::rhoapi::{Par, Var};
4
5use super::{sorted_par_map::SortedParMap, utils::union};
6
7#[derive(Clone, Debug)]
8pub struct ParMap {
9    pub ps: SortedParMap,
10    pub connective_used: bool,
11    pub locally_free: Vec<u8>,
12    pub remainder: Option<Var>,
13}
14
15impl ParMap {
16    pub fn new(
17        vec: Vec<(Par, Par)>,
18        connective_used: bool,
19        locally_free: Vec<u8>,
20        remainder: Option<Var>,
21    ) -> ParMap {
22        ParMap {
23            ps: SortedParMap::create_from_vec(vec),
24            connective_used,
25            locally_free,
26            remainder,
27        }
28    }
29
30    pub fn create_from_vec(vec: Vec<(Par, Par)>) -> Self {
31        ParMap::new(
32            vec.clone(),
33            ParMap::connective_used(&vec),
34            ParMap::update_locally_free(&vec),
35            None,
36        )
37    }
38
39    pub fn create_from_sorted_par_map(map: SortedParMap) -> Self {
40        ParMap::create_from_vec(map.sorted_list)
41    }
42
43    pub fn equals(&self, other: ParMap) -> bool {
44        self.ps.equals(other.ps)
45            && self.remainder == other.remainder
46            && self.connective_used == other.connective_used
47    }
48
49    fn connective_used(map: &Vec<(Par, Par)>) -> bool {
50        map.iter()
51            .any(|(k, v)| k.connective_used || v.connective_used)
52    }
53
54    fn update_locally_free(ps: &Vec<(Par, Par)>) -> Vec<u8> {
55        ps.into_iter().fold(Vec::new(), |acc, (key, value)| {
56            union(
57                acc,
58                union(key.locally_free.clone(), value.locally_free.clone()),
59            )
60        })
61    }
62}