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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
use crate::{Edge, EdgeInsertID, EdgeQuery, EdgesVisitor, GraphError, GraphKind, NodesVisitor, Query};
use std::{
future::Future,
mem::size_of,
ops::{Deref, DerefMut},
pin::Pin,
};
pub mod weighted;
/// Represent a graph storage, with a set of nodes and edges.
///
/// # Examples
///
/// ```
/// use graph_theory::GraphEngine;
/// ```
#[allow(unused_variables)]
pub trait GraphEngine
where
Self: Sized,
{
type NodeIter: Iterator<Item = usize>;
/// Check the graph kind, it can be directed or undirected.
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::CompleteGraph, GraphEngine};
/// assert_eq!(CompleteGraph::one_way(5).has_node(5), true);
/// assert_eq!(CompleteGraph::one_way(5).has_node(6), false);
/// ```
fn graph_kind(&self) -> GraphKind;
/// Check if the node exists, return the node id if exists.
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::CompleteGraph, GraphEngine};
/// assert_eq!(CompleteGraph::one_way(5).has_node(5), true);
/// assert_eq!(CompleteGraph::one_way(5).has_node(6), false);
/// ```
fn has_node(&self, node_id: usize) -> Option<usize>;
/// Check if the node exists, return the node id if exists.
///
/// Return [None] if the node does not exist.
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::CompleteGraph, GraphEngine};
/// assert_eq!(CompleteGraph::one_way(5).count_nodes(), 5);
/// ```
fn get_node_degree(&self, node_id: usize) -> Result<usize, GraphError> {
todo!()
}
/// Count the number of nodes in the graph.
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::CompleteGraph, GraphEngine};
/// assert_eq!(CompleteGraph::one_way(5).count_nodes(), 5);
/// ```
fn count_nodes(&self) -> usize;
/// Traverse all nodes in the graph.
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::CompleteGraph, GraphEngine};
/// let mut graph = CompleteGraph::one_way(5);
/// assert_eq!(graph.traverse_nodes().count(), 20)
/// ```
fn traverse_nodes(&self) -> NodesVisitor<Self>;
/// Check if the edge exists, return the node id if exists.
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::CompleteGraph, GraphEngine};
/// assert_eq!(CompleteGraph::one_way(5).has_node(5), true);
/// assert_eq!(CompleteGraph::one_way(5).has_node(6), false);
/// ```
fn has_edge<E: Into<EdgeQuery>>(&self, edge: E) -> Option<usize>;
/// Get the edges of the graph.
///
///
/// ```
/// use graph_theory::{graph_engines::CompleteGraph, GraphEngine};
/// let mut graph = CompleteGraph::one_way(5);
/// assert_eq!(graph.traverse_nodes().count(), 20)
/// ```
fn traverse_edges(&self) -> EdgesVisitor<Self>;
/// Count the number of edges in the graph.
///
/// # Examples
///
/// ```
/// # use graph_theory::{GraphEngine};
/// # use graph_theory::graph_engines::{CycleGraph, StarGraph, CompleteGraph};
/// assert_eq!(CycleGraph::one_way(5).count_edges(), 5);
/// assert_eq!(CycleGraph::two_way(5).count_edges(), 10);
/// assert_eq!(StarGraph::one_way(5).count_edges(), 5);
/// assert_eq!(StarGraph::two_way(5).count_edges(), 10);
/// assert_eq!(CompleteGraph::one_way(5).count_edges(), 5);
/// assert_eq!(CompleteGraph::one_way(5).count_edges(), 10);
/// ```
fn count_edges(&self) -> usize;
/// Query the total space occupied by the structure, return 0 if failed to query
///
/// Note that this volume contains garbage data, call [GraphEngine::shrink] at the right time to perform garbage collection.
fn size_hint(&self) -> usize {
size_of::<Self>()
}
}
/// Mark a graph engine that can add and delete edges or points
pub trait MutableGraph: GraphEngine {
/// Insert a node without any neighbors (edges).
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::AdjacencyNodeList, GraphEngine};
/// let mut graph = AdjacencyNodeList::default();
/// assert_eq!(graph.count_nodes(), 0);
/// graph.insert_node(5);
/// assert_eq!(graph.count_nodes(), 1);
/// ```
fn insert_node(&mut self, node_id: usize) -> usize;
/// Remove the given node.
///
/// # Undefined Behavior
///
/// - If the node has any edges, the behavior is undefined.
///
/// It is recommended to remove all edges before removing the node, see [`GraphEngine::remove_node_with_edges`].
///
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::AdjacencyNodeList, GraphEngine};
/// let mut graph = AdjacencyNodeList::default();
/// assert_eq!(graph.count_nodes(), 0);
/// graph.insert_node(5);
/// assert_eq!(graph.count_nodes(), 1);
/// ```
fn remove_node(&mut self, node_id: usize) {
self.remove_node_with_edges(node_id)
}
/// Remove the given node and all edges connected to it.
///
/// # Examples
///
/// ```
/// use graph_theory::{graph_engines::AdjacencyNodeList, GraphEngine};
/// let mut graph = AdjacencyNodeList::default();
/// assert_eq!(graph.count_nodes(), 0);
/// graph.insert_node(5);
/// assert_eq!(graph.count_nodes(), 1);
/// ```
fn remove_node_with_edges(&mut self, node_id: usize);
/// Insert a edge between two nodes.
///
/// # Undefined Behaviors
///
/// - If the nodes does not exist, the behavior is undefined.
///
/// It is recommended to check the existence of the nodes before inserting the edge, see [`GraphEngine::insert_edge_with_nodes`].
///
/// - Insert undirected edge to directed graph.
///
/// Two edges will be inserted, but only return last edge's id.
///
/// # Panics
///
/// - No such ability
///
/// Not all graph engine supports insert edge.
///
/// - Insert disconnected edge
///
/// Meaningless, don't do that.
///
/// # Examples
///
/// ```
/// use graph_theory::GraphEngine;
/// ```
fn insert_edge<E: Edge>(&mut self, edge: E) -> EdgeInsertID {
self.insert_edge_with_nodes(edge)
}
/// Insert edge to graph, if the nodes does not exist, also insert them.
///
/// # Panics
///
/// - No such ability
///
/// Not all graph engine supports insert edge.
///
/// # Examples
///
/// ```
/// use graph_theory::GraphEngine;
/// ```
fn insert_edge_with_nodes<E: Edge>(&mut self, edge: E) -> EdgeInsertID;
/// Remove edge by given edge-id or start and end node-id.
///
/// # Panics
///
/// - No such ability
///
/// Not all graph engine supports insert edge.
///
/// # Examples
///
/// ```
/// use graph_theory::GraphEngine;
/// ```
fn remove_edge<E>(&mut self, edge: E)
where
E: Into<EdgeQuery>;
/// Remove invalid edges and nodes to improve the efficiency of subsequent queries.
fn shrink(&mut self) {}
}