1
2#![doc(html_root_url = "https://docs.rs/petgraph/0.4/")]
19
20extern crate fixedbitset;
21#[cfg(feature = "graphmap")]
22extern crate ordermap;
23
24#[cfg(feature = "serde-1")]
25extern crate serde;
26#[cfg(feature = "serde-1")]
27#[macro_use]
28extern crate serde_derive;
29
30#[cfg(all(feature = "serde-1", test))]
31extern crate itertools;
32
33#[doc(no_inline)]
34pub use graph::Graph;
35
36pub use Direction::{Outgoing, Incoming};
37
38#[macro_use]
39mod macros;
40mod scored;
41
42#[macro_use]
44pub mod visit;
45#[macro_use]
46pub mod data;
47
48pub mod algo;
49#[cfg(feature = "generate")]
50pub mod generate;
51#[cfg(feature = "graphmap")]
52pub mod graphmap;
53mod graph_impl;
54pub mod dot;
55pub mod unionfind;
56mod dijkstra;
57mod astar;
58pub mod csr;
59mod iter_format;
60mod iter_utils;
61mod isomorphism;
62mod traits_graph;
63mod util;
64#[cfg(feature = "quickcheck")]
65mod quickcheck;
66#[cfg(feature = "serde-1")]
67mod serde_utils;
68
69pub mod prelude;
70
71pub mod graph {
73 pub use graph_impl::{
74 Edge,
75 EdgeIndex,
76 EdgeIndices,
77 EdgeReference,
78 EdgeReferences,
79 EdgeWeightsMut,
80 Edges,
81 Externals,
82 Frozen,
83 Graph,
84 Neighbors,
85 Node,
86 NodeIndex,
87 NodeIndices,
88 NodeWeightsMut,
89 NodeReferences,
90 WalkNeighbors,
91 GraphIndex,
92 IndexType,
93 edge_index,
94 node_index,
95 DefaultIx,
96 DiGraph,
97 UnGraph,
98 };
99}
100
101#[cfg(feature = "stable_graph")]
102pub use graph_impl::stable_graph;
103
104macro_rules! copyclone {
105 ($name:ident) => {
106 impl Clone for $name {
107 #[inline]
108 fn clone(&self) -> Self { *self }
109 }
110 }
111}
112
113#[derive(Copy, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
116#[repr(usize)]
117pub enum Direction {
118 Outgoing = 0,
120 Incoming = 1
122}
123
124copyclone!(Direction);
125
126impl Direction {
127 #[inline]
129 pub fn opposite(&self) -> Direction {
130 match *self {
131 Outgoing => Incoming,
132 Incoming => Outgoing,
133 }
134 }
135
136 #[inline]
138 pub fn index(&self) -> usize {
139 (*self as usize) & 0x1
140 }
141}
142
143#[doc(hidden)]
144pub use Direction as EdgeDirection;
145
146#[derive(Copy, Debug)]
148pub enum Directed { }
149copyclone!(Directed);
150
151#[derive(Copy, Debug)]
153pub enum Undirected { }
154copyclone!(Undirected);
155
156pub trait EdgeType {
158 fn is_directed() -> bool;
159}
160
161impl EdgeType for Directed {
162 #[inline]
163 fn is_directed() -> bool { true }
164}
165
166impl EdgeType for Undirected {
167 #[inline]
168 fn is_directed() -> bool { false }
169}
170
171
172pub trait IntoWeightedEdge<E> {
177 type NodeId;
178 fn into_weighted_edge(self) -> (Self::NodeId, Self::NodeId, E);
179}
180
181impl<Ix, E> IntoWeightedEdge<E> for (Ix, Ix)
182 where E: Default
183{
184 type NodeId = Ix;
185
186 fn into_weighted_edge(self) -> (Ix, Ix, E) {
187 let (s, t) = self;
188 (s, t, E::default())
189 }
190}
191
192impl<Ix, E> IntoWeightedEdge<E> for (Ix, Ix, E)
193{
194 type NodeId = Ix;
195 fn into_weighted_edge(self) -> (Ix, Ix, E) {
196 self
197 }
198}
199
200impl<'a, Ix, E> IntoWeightedEdge<E> for (Ix, Ix, &'a E)
201 where E: Clone
202{
203 type NodeId = Ix;
204 fn into_weighted_edge(self) -> (Ix, Ix, E) {
205 let (a, b, c) = self;
206 (a, b, c.clone())
207 }
208}
209
210impl<'a, Ix, E> IntoWeightedEdge<E> for &'a (Ix, Ix)
211 where Ix: Copy, E: Default
212{
213 type NodeId = Ix;
214 fn into_weighted_edge(self) -> (Ix, Ix, E) {
215 let (s, t) = *self;
216 (s, t, E::default())
217 }
218}
219
220impl<'a, Ix, E> IntoWeightedEdge<E> for &'a (Ix, Ix, E)
221 where Ix: Copy, E: Clone
222{
223 type NodeId = Ix;
224 fn into_weighted_edge(self) -> (Ix, Ix, E) {
225 self.clone()
226 }
227}