graph_types/entries/
mod.rs

1use crate::{errors::GraphError, Query};
2use std::ops::{Deref, DerefMut};
3
4pub mod query;
5
6/// # Arguments
7///
8/// * `index`:
9///
10/// returns: Option<Cow<Self::Node>>
11///
12/// # Examples
13///
14/// ```
15/// use graph_theory::GraphEngine;
16/// ```
17#[repr(u8)]
18#[derive(Copy, Clone, Debug)]
19pub enum GraphEntry {
20    /// # Arguments
21    ///
22    /// * `index`:
23    ///
24    /// returns: Option<Cow<Self::Node>>
25    ///
26    /// # Examples
27    ///
28    /// ```
29    /// use graph_theory::GraphEngine;
30    /// ```
31    Node = 0,
32    /// # Arguments
33    ///
34    /// * `index`:
35    /// raphEngine
36    /// returns: Option<Cow<Self::Node>>
37    ///
38    /// # Examples
39    ///
40    /// ```
41    /// use graph_theory::GraphEngine;
42    /// ```
43    Edge = 1,
44}
45
46/// Mark the graph as directed or undirected.
47///
48/// Generally speaking, a directed graph engine can insert undirected edges,
49/// but a undirected graph engine cannot insert directed edges.
50///
51/// # Examples
52///
53/// ```
54/// use graph_theory::{graph_engines::CycleGraph, GraphEngine, GraphKind};
55/// assert_eq!(CycleGraph::one_way(5).graph_kind(), GraphKind::Directed);
56/// assert_eq!(CycleGraph::two_way(5).graph_kind(), GraphKind::Undirected);
57/// ```
58#[repr(u8)]
59#[derive(Copy, Clone, Debug, Eq, PartialEq)]
60pub enum GraphKind {
61    /// # Arguments
62    ///
63    /// * `index`:
64    ///
65    /// returns: Option<Cow<Self::Node>>
66    ///
67    /// # Examples
68    ///
69    /// ```
70    /// use graph_theory::GraphEngine;
71    /// ```
72    Directed = 0,
73    /// # Arguments
74    ///
75    /// * `index`:
76    ///
77    /// returns: Option<Cow<Self::Node>>
78    ///
79    /// # Examples
80    ///
81    /// ```
82    /// use graph_theory::GraphEngine;
83    /// ```
84    Undirected = 1,
85}
86
87impl GraphKind {
88    pub const fn is_one_way(&self) -> bool {
89        matches!(self, GraphKind::Directed)
90    }
91    pub const fn is_two_way(&self) -> bool {
92        matches!(self, GraphKind::Undirected)
93    }
94}
95
96pub trait EntryEngine<V> {
97    type EntryRef<'i>: Deref<Target = V>
98    where
99        Self: 'i;
100    type EntryMut<'i>: DerefMut<Target = V>
101    where
102        Self: 'i;
103
104    /// Get the reference of data by given query from the storage.
105    ///
106    /// # Examples
107    ///
108    /// ```
109    /// use graph_theory::GraphEngine;
110    /// ```
111    fn get_entry<'i, Q: Into<Query>>(&'i self, query: Q) -> V;
112    /// Get the reference of data by given query from the storage.
113    ///
114    /// # Examples
115    ///
116    /// ```
117    /// use graph_theory::GraphEngine;
118    /// ```
119    fn try_entry<'i, Q: Into<Query>>(&'i self, query: Q) -> Result<Self::EntryRef<'i>, GraphError>;
120    /// Get the reference of data by given query from the storage.
121    ///
122    /// # Examples
123    ///
124    /// ```
125    /// use graph_theory::GraphEngine;
126    /// ```
127    fn mut_entry<'i, Q: Into<Query>>(&'i mut self, query: Q) -> Result<Self::EntryMut<'i>, GraphError>;
128    /// Get the reference of data by given query from the storage.
129    ///
130    /// # Examples
131    ///
132    /// ```
133    /// use graph_theory::GraphEngine;
134    /// ```
135    fn set_entry<'i, Q: Into<Query>>(&'i mut self, query: Q, entry: V) -> Result<(), GraphError> {
136        let mut entry_ref = self.mut_entry(query)?;
137        *entry_ref.deref_mut() = entry;
138        Ok(())
139    }
140    fn get_node_data<'i>(&'i self, node: usize) -> V {
141        self.get_entry(Query::NodeID(node))
142    }
143    fn try_node_data<'i>(&'i self, node: usize) -> Option<Self::EntryRef<'i>> {
144        self.try_entry(Query::NodeID(node)).ok()
145    }
146    fn mut_node_data<'i>(&'i mut self, node: usize) -> Option<Self::EntryMut<'i>> {
147        self.mut_entry(Query::NodeID(node)).ok()
148    }
149    fn set_node_data<'i>(&'i mut self, node: usize, data: V) {
150        self.set_entry(Query::NodeID(node), data).ok();
151    }
152    fn get_edge_data<'i>(&'i self, edge: usize) -> V {
153        self.get_entry(Query::EdgeID(edge))
154    }
155    fn try_edge_data<'i>(&'i self, edge: usize) -> Option<Self::EntryRef<'i>> {
156        self.try_entry(Query::EdgeID(edge)).ok()
157    }
158    fn mut_edge_data<'i>(&'i mut self, edge: usize) -> Option<Self::EntryMut<'i>> {
159        self.mut_entry(Query::EdgeID(edge)).ok()
160    }
161    fn set_edge_data<'i>(&'i mut self, edge: usize, data: V) {
162        self.set_entry(Query::EdgeID(edge), data).ok();
163    }
164}