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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
pub mod adjacencylist;
pub mod dense_adjacency;
pub mod disk_adjacency;
pub mod linear;
pub mod prepost;
pub mod registry;
pub mod union;
use crate::malloc_size_of::MallocSizeOf;
use crate::{
annostorage::AnnotationStorage,
errors::Result,
types::{AnnoKey, Annotation, Edge, NodeID},
};
use serde::{Deserialize, Serialize};
use std::{self, path::Path};
#[derive(Serialize, Deserialize, Clone, MallocSizeOf)]
pub struct GraphStatistic {
pub cyclic: bool,
pub rooted_tree: bool,
pub nodes: usize,
pub avg_fan_out: f64,
pub fan_out_99_percentile: usize,
pub inverse_fan_out_99_percentile: usize,
pub max_fan_out: usize,
pub max_depth: usize,
pub dfs_visit_ratio: f64,
}
impl std::fmt::Display for GraphStatistic {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"nodes={}, avg_fan_out={:.2}, max_fan_out={}, max_depth={}",
self.nodes, self.avg_fan_out, self.max_fan_out, self.max_depth
)?;
if self.cyclic {
write!(f, ", cyclic")?;
}
if self.rooted_tree {
write!(f, ", tree")?;
}
Ok(())
}
}
pub trait EdgeContainer: Sync + Send + MallocSizeOf {
fn get_outgoing_edges<'a>(&'a self, node: NodeID) -> Box<dyn Iterator<Item = NodeID> + 'a>;
fn has_outgoing_edges(&self, node: NodeID) -> bool {
self.get_outgoing_edges(node).next().is_some()
}
fn get_ingoing_edges<'a>(&'a self, node: NodeID) -> Box<dyn Iterator<Item = NodeID> + 'a>;
fn get_statistics(&self) -> Option<&GraphStatistic> {
None
}
fn source_nodes<'a>(&'a self) -> Box<dyn Iterator<Item = NodeID> + 'a>;
}
pub trait GraphStorage: EdgeContainer {
fn find_connected<'a>(
&'a self,
node: NodeID,
min_distance: usize,
max_distance: std::ops::Bound<usize>,
) -> Box<dyn Iterator<Item = NodeID> + 'a>;
fn find_connected_inverse<'a>(
&'a self,
node: NodeID,
min_distance: usize,
max_distance: std::ops::Bound<usize>,
) -> Box<dyn Iterator<Item = NodeID> + 'a>;
fn distance(&self, source: NodeID, target: NodeID) -> Option<usize>;
fn is_connected(
&self,
source: NodeID,
target: NodeID,
min_distance: usize,
max_distance: std::ops::Bound<usize>,
) -> bool;
fn get_anno_storage(&self) -> &dyn AnnotationStorage<Edge>;
fn copy(
&mut self,
node_annos: &dyn AnnotationStorage<NodeID>,
orig: &dyn GraphStorage,
) -> Result<()>;
fn as_edgecontainer(&self) -> &dyn EdgeContainer;
fn as_writeable(&mut self) -> Option<&mut dyn WriteableGraphStorage> {
None
}
fn inverse_has_same_cost(&self) -> bool {
false
}
fn serialization_id(&self) -> String;
fn load_from(location: &Path) -> Result<Self>
where
Self: std::marker::Sized;
fn save_to(&self, location: &Path) -> Result<()>;
}
pub fn default_serialize_gs<GS>(gs: &GS, location: &Path) -> Result<()>
where
GS: Serialize,
{
let data_path = location.join("component.bin");
let f_data = std::fs::File::create(&data_path)?;
let mut writer = std::io::BufWriter::new(f_data);
bincode::serialize_into(&mut writer, gs)?;
Ok(())
}
pub fn default_deserialize_gs<GS>(location: &Path) -> Result<GS>
where
for<'de> GS: std::marker::Sized + Deserialize<'de>,
{
let data_path = location.join("component.bin");
let f_data = std::fs::File::open(data_path)?;
let input = std::io::BufReader::new(f_data);
let result = bincode::deserialize_from(input)?;
Ok(result)
}
pub trait WriteableGraphStorage: GraphStorage {
fn add_edge(&mut self, edge: Edge) -> Result<()>;
fn add_edge_annotation(&mut self, edge: Edge, anno: Annotation) -> Result<()>;
fn delete_edge(&mut self, edge: &Edge) -> Result<()>;
fn delete_edge_annotation(&mut self, edge: &Edge, anno_key: &AnnoKey) -> Result<()>;
fn delete_node(&mut self, node: NodeID) -> Result<()>;
fn calculate_statistics(&mut self);
fn clear(&mut self) -> Result<()>;
}