eta_graph/
graph.rs

1use crate::edge_storage::{EdgeStorage};
2use crate::handles::types::{VHandle, Ci};
3use crate::traits::{EdgeManipulate, StoreVertex};
4use crate::vertex_storage::VertexStorage;
5use crate::views::tree::Tree;
6
7#[derive(Debug)]
8pub enum Error {
9    NoHandle,
10}
11pub struct Graph<VertexType, VertexStorageType, EdgeStorageType>
12where
13    VertexStorageType: StoreVertex<VertexType=VertexType>,
14    EdgeStorageType: EdgeManipulate,
15{
16    pub vertices: VertexStorageType,
17    pub edge_storage: EdgeStorageType,
18}
19
20impl<VertexType, VertexStorageType, EdgeStorageType> Clone for Graph<VertexType, VertexStorageType, EdgeStorageType>
21where
22    EdgeStorageType: EdgeManipulate,
23    VertexType: Clone,
24    VertexStorageType: StoreVertex<VertexType=VertexType> + Clone {
25    #[inline(always)]
26    fn clone(&self) -> Self {
27        Graph{
28            vertices: self.vertices.clone(),
29            edge_storage: self.edge_storage.clone(),
30        }
31    }
32    #[inline(always)]
33    fn clone_from(&mut self, source: &Self) {
34        self.vertices.clone_from(&source.vertices);
35        self.edge_storage.clone_from(&source.edge_storage);
36    }
37}
38
39impl<VertexType> Default for Graph<VertexType, VertexStorage<VertexType>, EdgeStorage> {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl<VertexType> Graph<VertexType, VertexStorage<VertexType>, EdgeStorage>
46{
47    pub fn new_large() -> Self {
48        Graph{
49            edge_storage: EdgeStorage::new_large(),
50            vertices: VertexStorage::new(),
51        }
52    }
53    pub fn with_reserve(reserve: Ci) -> Self {
54        Graph{
55            edge_storage: EdgeStorage::with_reserve(reserve),
56            vertices: VertexStorage::new(),
57        }
58    }
59    pub fn new() -> Self {
60        Graph{
61            edge_storage: EdgeStorage::new(),
62            vertices: VertexStorage::new(),
63        }
64    }
65
66
67}
68
69impl<VertexType, VertexStorageType, EdgeStorageType> Graph<VertexType, VertexStorageType, EdgeStorageType>
70where
71    EdgeStorageType: EdgeManipulate,
72    VertexStorageType: StoreVertex<VertexType=VertexType>{
73    #[inline(always)]
74    pub fn tree_view(&mut self) -> Tree<VertexType, VertexStorageType, EdgeStorageType> {
75        return Tree::new(&mut self.edge_storage, &mut self.vertices);
76    }
77
78    pub fn create_and_connect(&mut self, from: VHandle, val: VertexType, edge_count: Ci) -> VHandle {
79        let new_vertex = self.create(val, edge_count);
80        self.edge_storage.connect(from, new_vertex);
81        new_vertex
82    }
83    #[inline(always)]
84    pub fn create_and_connect_0(&mut self, from: VHandle, val: VertexType) -> VHandle {
85        self.create_and_connect(from, val, 0)
86    }
87
88    pub fn create(&mut self, val: VertexType, edge_count: Ci) -> VHandle {
89        self.vertices.push(val);
90        self.edge_storage.create_vertex_entry(edge_count)
91    }
92    #[inline(always)]
93    pub fn create_leaf(&mut self, val: VertexType) -> VHandle {
94        self.create(val, 0)
95    }
96}