zawgl_core/matcher/vf2/
base_state.rs

1// MIT License
2// Copyright (c) 2022 Alexandre RICCIARDI
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use std::collections::HashMap;
22
23pub struct BaseState<NID0, NID1> where NID0: std::hash::Hash + Eq + Copy, NID1: std::hash::Hash + Eq + Copy {
24    pub term_in_count: usize,
25    pub term_out_count: usize,
26    pub term_both_count: usize,
27    pub core_count: usize,
28    pub core_map: HashMap<NID0, NID1>,
29    pub in_map: HashMap<NID0, usize>,
30    pub out_map: HashMap<NID0, usize>,
31}
32
33impl <NID0, NID1> BaseState<NID0, NID1> where NID0: std::hash::Hash + Eq + Copy, NID1: std::hash::Hash + Eq + Copy {
34    
35    pub fn new() -> Self {
36        BaseState {
37            term_in_count: 0,
38            term_out_count: 0,
39            term_both_count: 0,
40            core_count: 0,
41            core_map: HashMap::new(),
42            in_map: HashMap::new(),
43            out_map: HashMap::new(),
44        }
45    }
46
47    pub fn term_in(&self) -> bool {
48        self.core_count < self.term_in_count
49    }
50
51    pub fn term_in_vertex(&self, v0: &NID0) -> bool {
52        let has_in_count = self.in_map.contains_key(v0);
53        has_in_count && !self.core_map.contains_key(v0)
54    }
55
56    pub fn term_out(&self) -> bool {
57        self.core_count < self.term_out_count
58    }
59
60    pub fn term_out_vertex(&self, v0: &NID0) -> bool {
61        let has_out_count = self.out_map.contains_key(v0);
62        has_out_count && !self.core_map.contains_key(v0)
63    }
64
65    
66    pub fn term_both(&self) -> bool {
67        self.core_count < self.term_both_count
68    }
69    
70    pub fn term_both_vertex(&self, v0: &NID0) -> bool {
71        let has_in_count = self.in_map.contains_key(v0); 
72        let has_out_count = self.out_map.contains_key(v0);
73        has_in_count && has_out_count && !self.core_map.contains_key(v0)
74    }
75
76    pub fn in_core(&self, v0: &NID0) -> bool
77    {
78        self.core_map.contains_key(v0)
79    }
80
81    pub fn count(&self) -> usize {
82        self.core_count
83    }
84    pub fn core(&self, v0: &NID0) -> Option<&NID1> {
85        self.core_map.get(v0)
86    }
87    pub fn get_map(&self) ->  &HashMap<NID0, NID1> {
88        &self.core_map
89    }
90
91
92    pub fn in_depth(&self, v0: &NID0) -> usize {
93        if let Some(count) = self.in_map.get(v0) {
94            *count
95        } else {
96            0
97        }
98    }
99
100    pub fn out_depth(&self, v0: &NID0) -> usize {
101        if let Some(count) = self.out_map.get(v0) {
102            *count
103        } else {
104            0
105        }
106    }
107
108    pub fn term_set(&self) -> (usize, usize, usize) {
109        (self.term_in_count, self.term_out_count, self.term_both_count)
110    }
111}